Skip to content

Commit afc88f2

Browse files
committed
- Improve HelloMesh.ino.
1 parent 78812a7 commit afc88f2

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

libraries/ESP8266WiFiMesh/examples/HelloMesh/HelloMesh.ino

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ bool useLED = false; // Change this to true if you wish the onboard LED to mark
5050
bool meshMessageHandler(String &message, FloodingMesh &meshInstance) {
5151
int32_t delimiterIndex = message.indexOf(meshInstance.broadcastMetadataDelimiter());
5252
if (delimiterIndex == 0) {
53-
Serial.print("Message received from STA " + meshInstance.getEspnowMeshBackend().getSenderMac() + ": ");
54-
Serial.println(message.substring(1, 101));
53+
Serial.print("Message received from STA MAC " + meshInstance.getEspnowMeshBackend().getSenderMac() + ": ");
54+
Serial.println(message.substring(2, 102));
5555

56-
String potentialMac = message.substring(1, 13);
57-
58-
if (potentialMac > theOneMac) {
59-
if (theOne) {
60-
if (useLED) {
61-
digitalWrite(LED_BUILTIN, HIGH); // Turn LED off (LED is active low)
62-
}
56+
String potentialMac = message.substring(2, 14);
6357

58+
if (potentialMac >= theOneMac) {
59+
if (potentialMac > theOneMac) {
6460
theOne = false;
61+
theOneMac = potentialMac;
6562
}
6663

67-
theOneMac = potentialMac;
64+
if (useLED && !theOne) {
65+
bool ledState = message.charAt(1) == '1';
66+
digitalWrite(LED_BUILTIN, ledState); // Turn LED on/off (LED is active low)
67+
}
6868

6969
return true;
7070
} else {
@@ -142,6 +142,7 @@ void setup() {
142142

143143
int32_t timeOfLastProclamation = -10000;
144144
void loop() {
145+
static bool ledState = 1;
145146
static uint32_t benchmarkCount = 0;
146147
static uint32_t loopStart = millis();
147148

@@ -160,7 +161,10 @@ void loop() {
160161
if (theOne) {
161162
if (millis() - timeOfLastProclamation > 10000) {
162163
uint32_t startTime = millis();
163-
floodingMesh.broadcast(String(floodingMesh.broadcastMetadataDelimiter()) + theOneMac + " is The One.");
164+
ledState = ledState ^ bool(benchmarkCount); // Make other nodes' LEDs alternate between on and off once benchmarking begins.
165+
166+
// Note: The maximum length of an unencrypted broadcast message is given by floodingMesh.maxUnencryptedMessageSize(). It is around 670 bytes by default.
167+
floodingMesh.broadcast(String(floodingMesh.broadcastMetadataDelimiter()) + String(ledState) + theOneMac + " is The One.");
164168
Serial.println("Proclamation broadcast done in " + String(millis() - startTime) + " ms.");
165169

166170
timeOfLastProclamation = millis();

0 commit comments

Comments
 (0)