0

io.run() error handling?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

io.run() error handling?

by daveNC on Sat Apr 20, 2019 11:45 am

I am testing this at two different sites with two units running the same code as listed below. I have io.run() at the top of my loop. It sometimes gets stuck when the WiFi connection is lost, as tested by rebooting or unplugging the WiFi router. I turned Debug on in Adafruit_MQTT.h and the debug loops with:

14:59:36.452 -> Connection failed!
14:59:41.526 ->
14:59:41.526 -> Adafruit IO connected.
14:59:41.526 -> before io.run call: Adafruit IO connected.
14:59:41.526 -> Connecting to: io.adafruit.com
14:59:41.526 -> Connect result: 0
14:59:41.526 -> Connecting to: io.adafruit.com
14:59:41.526 -> Connect result: 0
14:59:41.526 -> Connecting to: io.adafruit.com
14:59:41.526 -> Connect result: 0

Sometimes it will eventually connect. However, in two cases it has looped like that for over a week so far.

I'm thinking of adding more Prints in Adafruit_MQTT.h to better determine where it is looping.

Any advice?

Thanks

Here is the code. Execution sometimes never returns from io.run() call once WiFi is lost. I would like to survive a restart of the connection in this case. My WiFi situation is sporadically poor at the production site.

Code: Select all | TOGGLE FULL SIZE
#include "config.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DATA_PIN 27

int dr_counter;

// create DHT11 instance
DHT_Unified dht(DATA_PIN, DHT11);

AdafruitIO_Feed *temperature = io.feed("H_temperature");
AdafruitIO_Feed *humidity = io.feed("H_humidity");

void setup() {

  Serial.begin(115200);

  while(! Serial);

  // initialize dht22
  dht.begin();

  Serial.print("Connecting to Adafruit IO");
  io.connect();

  dr_counter = 0;

  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
   
    delay(500);

    //dr_counter++;
   
   // if(dr_counter > 10) {
   //   Serial.println("Executing the esp_restart");
   //   esp_restart();
   // }
   
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

}

void loop() {

  Serial.print("before io.run call:  ");
  Serial.println(io.statusText());

  io.run();

  Serial.print("after io.run call:  ");
  Serial.println(io.statusText());

  sensors_event_t event;
  dht.temperature().getEvent(&event);

  float celsius = event.temperature;
  float fahrenheit = (celsius * 1.8) + 32;

  Serial.print("celsius: ");
  Serial.print(celsius);
  Serial.println("C");

  Serial.print("fahrenheit: ");
  Serial.print(fahrenheit);
  Serial.println("F");

  // save fahrenheit (or celsius) to Adafruit IO
  temperature->save(fahrenheit);

  dht.humidity().getEvent(&event);

  Serial.print("humidity: ");
  Serial.print(event.relative_humidity);
  Serial.println("%");

  // save humidity to Adafruit IO
  humidity->save(event.relative_humidity);

  // wait at least 5 seconds (5000 milliseconds == 5 seconds, 900000 == 15 minutes)
  //delay(900000);
  delay(5000);

  Serial.println();
  Serial.println(io.statusText());
 
}
Last edited by adafruit_support_mike on Sun Apr 21, 2019 3:19 am, edited 1 time in total.
Reason: added CODE tags

daveNC
 
Posts: 40
Joined: Thu Nov 09, 2017 2:32 pm

Re: io.run() error handling?

by adafruit_support_mike on Sun Apr 21, 2019 3:26 am

Try adding some code to check the connection before calling any function that needs a working connection to succeed.

TCP creates reliable transport from an unreliable network layer through sheer persistence. In theory, it will keep trying to send messages forever, only stopping when it gets an 'okay. I got that part' response from the recipient.

Any code that needs to operate for a long time should have error-checking to check and re-establish the connection. Wireless connections are much more vulnerable to noise and interference than wired connections. The question isn't, "will the link fail?" so much as "when will the link fail?"

adafruit_support_mike
 
Posts: 57856
Joined: Thu Feb 11, 2010 2:51 pm

Re: io.run() error handling?

by daveNC on Sun Apr 21, 2019 6:23 am

Thanks, I put a connection check just before the call to io.run() and I reboot if not connected. That does seem to work.

One question, I can't locate the code that is run by that io.run() call. I searched all the libraries and don't see and IO object. Where is it located, please?

daveNC
 
Posts: 40
Joined: Thu Nov 09, 2017 2:32 pm

Re: io.run() error handling?

by adafruit_support_mike on Mon Apr 22, 2019 12:47 am

The variable 'io' is created in config.h and depends on the kind of device you're using to make the connection.

All of the device specific library modules inherit the function .run() from the AdafruitIO base class, and the function itself is defined here:

https://github.com/adafruit/Adafruit_IO ... #L168-L183

adafruit_support_mike
 
Posts: 57856
Joined: Thu Feb 11, 2010 2:51 pm

Re: io.run() error handling?

by agostinone on Thu May 09, 2019 9:13 pm

adafruit_support_mike wrote:Try adding some code to check the connection before calling any function that needs a working connection to succeed.

TCP creates reliable transport from an unreliable network layer through sheer persistence. In theory, it will keep trying to send messages forever, only stopping when it gets an 'okay. I got that part' response from the recipient.

Any code that needs to operate for a long time should have error-checking to check and re-establish the connection. Wireless connections are much more vulnerable to noise and interference than wired connections. The question isn't, "will the link fail?" so much as "when will the link fail?"



Could you share an example of what error- checking might look like?

I just finished my first Arduino project and I used a Feather M0 WiFi to monitor temp and humidity in our greenhouse. It runs from 6 hours to 72 hours before hanging. I'm pretty sure it's the wifi connection to our greenhouse where this resides. Like the OP, I have "io.run()" at the beginning of the loop that samples data and uploads it (I n my case, to feeds on io.adafruit.com that are used with IFTTT).

Thanks

agostinone
 
Posts: 2
Joined: Sun Apr 14, 2019 10:45 pm

Re: io.run() error handling?

by brubell on Fri May 10, 2019 9:52 am

agostinone wrote:
adafruit_support_mike wrote:Try adding some code to check the connection before calling any function that needs a working connection to succeed.

TCP creates reliable transport from an unreliable network layer through sheer persistence. In theory, it will keep trying to send messages forever, only stopping when it gets an 'okay. I got that part' response from the recipient.

Any code that needs to operate for a long time should have error-checking to check and re-establish the connection. Wireless connections are much more vulnerable to noise and interference than wired connections. The question isn't, "will the link fail?" so much as "when will the link fail?"



Could you share an example of what error- checking might look like?

I just finished my first Arduino project and I used a Feather M0 WiFi to monitor temp and humidity in our greenhouse. It runs from 6 hours to 72 hours before hanging. I'm pretty sure it's the wifi connection to our greenhouse where this resides. Like the OP, I have "io.run()" at the beginning of the loop that samples data and uploads it (I n my case, to feeds on io.adafruit.com that are used with IFTTT).

Thanks


An example of this, within Adafruit IO Arduino would be polling (and/or printing) the io.status() function prior to the io.run().

brubell
 
Posts: 430
Joined: Fri Jul 17, 2015 10:33 pm

Re: io.run() error handling?

by agostinone on Sun May 12, 2019 12:04 am

I appreciate the help--obviously I'm new at this.

I tried to put in a little loop that I thought would poll the WiFi and if it was not present it would continue to poll it until it came back. Unfortunately the Feather M0 just locks-up (with the little yellow light on)--same as before when it locked-up on the io.run().

I tested this by running my project for a few sampling periods and then unplugged the WiFi and tracked the serial prints. Once the WiFi is gone it goes into the loop and hangs--not looping over and over and printing out the status. It just prints the status in the loop once.



Here is the part I added before the io.run()

Code: Select all | TOGGLE FULL SIZE
void loop() {

  io.status();
  Serial.println(io.statusText());
  if (io.status() != AIO_CONNECTED)
  {
    Serial.println("no wifi");
    io.status();
    Serial.print("searchng");
    delay(500);
  }
 
  io.run();

agostinone
 
Posts: 2
Joined: Sun Apr 14, 2019 10:45 pm

Please be positive and constructive with your questions and comments.