Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Different Placement for io.run()
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Different Placement for io.run()

by 1CM69 on Fri Aug 09, 2019 2:45 pm

I thought I would share this little piece of info as it may help someone else.

I am using ESP-Now on my home IoT and there are a number of sensors (Masters) dotted around the house and they send their data back periodically via the wireless ESP-Now system to a receiving station (Slave).

The advantage is that the Masters do not need to connect to WiFi, thus cutting down on power usage, especially if running on a battery, the data transfer is very fast so they are only at max power for a fraction of a second before going back to DeepSleep and the cycle starts over again.

Because of how the coding for the Master & Slave needs to be constructed, it is very problematic to place the
Code: Select all | TOGGLE FULL SIZE
io.run();
function call at the top of the main loop.

It took me a long time of trial & error but I finally got a working sketch, here is a small section

Code: Select all | TOGGLE FULL SIZE
void loop() {
 
  if (millis()-heartBeat > 30000) {
    Serial.println("Waiting for ESP-NOW messages...");
    heartBeat = millis();
  }

  if (haveReading) {
    haveReading = false;
    ThingSpeak.begin(client);
    wifiConnect();
    sendToThingSpeak();
    sendToAdafruit();
    delay(200);
    ESP.restart(); // <----- Reboots to re-enable ESP-NOW
  }
}

void sendToThingSpeak() {
  ThingSpeak.writeField(myChannelNumber, 1, String(sensorData.temp), myWriteAPIKey);
}

void sendToAdafruit() {
  io.run();
  BedroomTempFeed->save(String(sensorData.temp));
}


you can see that I found placing the
Code: Select all | TOGGLE FULL SIZE
io.run();
function call within another function that in turn is called from an 'if' statement inside the main loop, works without issue.

regards..,

Kirk

1CM69
 
Posts: 4
Joined: Mon Jul 22, 2019 9:10 am

Please be positive and constructive with your questions and comments.