0

Long delays to handleMessage
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Long delays to handleMessage

by jerryn on Mon Feb 20, 2017 5:39 pm

I am trying a modified version of the adafruitio_15_temp_humidity example. My addition is to add a feed to to toggle an led via a button on the dashboard following the example in adafruitio_07_digital_out.

The DHT sensor updates appear as expected every 5 minutes and if I toggle the LED Via my dashboard, the feed value updates immediately but there can be very long (several minutes) delays before the LED actually turns on/off on the board. It appears that the delay is in adafruitio call generating the message to my feed. How long should it take?

I am using a feather Huzzah ESP8266 and a DHT11 sensor. The LED is on pin 16, The DHT11 is on Pin2

Am I doing something wrong in my code?


Code: Select all | TOGGLE FULL SIZE
// Adafruit IO Temperature & Humidity Example
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-temperature-and-humidity
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016-2017 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"

/************************ Example Starts Here *******************************/
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

// pin connected to DH22 data line
#define DATA_PIN 2
long previousMillis = 0;        // will store last time DTH was updated
 
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 300000;           // interval at which to blink (milliseconds)
 
// create DHT11 instance
DHT_Unified dht(DATA_PIN, DHT11);

// set up the 'temperature' and 'humidity' feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
// digital pin 16
#define LED_PIN 16

// set up the 'digital' feed
AdafruitIO_Feed *digital = io.feed("digital");

void setup() {

  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  //while(! Serial);

  // initialize dht11
  dht.begin();
  // set led pin as a digital output
  pinMode(LED_PIN, OUTPUT);
 
  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

// set up a message handler for the 'digital' feed.
  // the handleMessage function (defined below)
  // will be called whenever a message is
  // received from adafruit io.
  digital->onMessage(handleMessage);

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

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

}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   
    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);
  }
}

// this function is called whenever an 'digital' feed message
// is received from Adafruit IO. it was attached to
// the 'digital' feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {

  Serial.print("received <- ");

  if(data->toPinLevel() == HIGH)
    Serial.println("HIGH");
  else
    Serial.println("LOW");

  // write the current state to the led
  digitalWrite(LED_PIN, data->toPinLevel());

}

jerryn
 
Posts: 489
Joined: Sat Sep 14, 2013 9:05 am

Re: Long delays to handleMessage

by jwcooper on Mon Feb 20, 2017 6:28 pm

Are you still seeing delays? We just upgraded another server that was likely causing delays.

jwcooper
 
Posts: 646
Joined: Tue May 01, 2012 9:08 pm

Re: Long delays to handleMessage

by jerryn on Mon Feb 20, 2017 6:33 pm

Just ran a test - The LED turned on immediately but when I sent an off request a few seconds later, it has remained on - the feed says the setting is 0 (off) but the board has not yet received the message. Been a few minutes already - waiting...

jerryn
 
Posts: 489
Joined: Sat Sep 14, 2013 9:05 am

Re: Long delays to handleMessage

by jerryn on Mon Feb 20, 2017 6:42 pm

hmm - after waiting almost 10 minutes, I gave up and toggled it again - this time it went off when request. Perhaps I am just missing the messages. I suppose I could query the feed periodically...

jerryn
 
Posts: 489
Joined: Sat Sep 14, 2013 9:05 am

Re: Long delays to handleMessage

by jerryn on Mon Feb 20, 2017 6:44 pm

odd - I just didd several toggles a few seconds apart and they all worked fine. It seems like it is just problem for the first action after a long delay... or something like that Or you just fixed it ;-)

edited -
it seems to be working fine now - Thanks!

P.S - A few hours later and it continues to work! Whatever you did seems to have cleared up the problem. I was fighting with this all afternoon...

jerryn
 
Posts: 489
Joined: Sat Sep 14, 2013 9:05 am

Re: Long delays to handleMessage

by jferguson on Thu Jan 04, 2018 11:45 pm

This is astonishly similar to my code and my problem. I set up a toggle on the dashboard to turn a heater on or off, chose 1 for "on" text and 0 for "off" text and used a diferent sensor module but essentially the loop feeds temp and humidity output to dashboard at 5 second intervals. I have gauges and they work fine changing at the stipulated five second intervals.

But alas, the on/off toggle may or may not have any effect on the heater switch. If you'd like, I could post my code.

While we're thinking about this, 'TEXT" wasn't the best choice of words for values for the toggle. I spent a couple of hours fooling around with this before I realzed that if used "1" and "0" for the names of the geen and brown sides of the switch it could then work some of the time.

jferguson
 
Posts: 5
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

by jferguson on Fri Jan 05, 2018 11:09 am

maybe an "aha" revelation. I had a second dashboard instance using the same toggle. Even though I did not have it active, mayne there is some latent connection which confused the Hurrah setup. It would be nice to know if this is a reasonable possiiblity.

jferguson
 
Posts: 5
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

by jferguson on Sat Jan 06, 2018 12:27 pm

Nope. killing the other dashboard using the same toggel feed, didn't cure the problem. If I hit the toggle often enough eventually it connects.
Where should I read to see how to do binary "condition" lights on the dahsboard which would show what the state of a digital pin is?

jferguson
 
Posts: 5
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

by abachman on Mon Jan 08, 2018 2:13 pm

Hi jferguson,


A couple other troubleshooting steps you could try are adding a stream block to your dashboard that reads the feed that the toggle is writing to. You should see the messages pop up in the stream pretty much immediately after triggering the toggle. That send -> receive loop is actually going all the way to IO and back out via the MQTT API, so if it's working on the dashboard, your device (all other things being equal and hardware, software, and local network conditions functioning perfectly) should be getting the messages.

Second is to keep https://io.adafruit.com/monitor logged in and open while you're using the dashboard. That interface should show every piece of data and all the error messages your account is processing. It should also help you see the toggle in action and can relieve you from cluttering your dashboard.

Third, are you able to duplicate the "lost data" error with a normal push button block? If you set the "Press Value" field and leave "Release Value" blank, it'll send only when pressed. If your device is not sensitive to repeated "ON" messages but seems to sometimes be missing data that was properly sent (i.e., is visible via troubleshooting methods 1 & 2), that may work better than a toggle which sends ON and OFF alternately to a device that seems to respond randomly.


- adam

abachman
 
Posts: 131
Joined: Mon Feb 01, 2010 12:48 pm

Re: Long delays to handleMessage

by jferguson on Mon Jan 08, 2018 4:21 pm

Thanks Adam, I'll try these things.

john

jferguson
 
Posts: 5
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

by jferguson on Tue Jan 09, 2018 9:23 am

I ran into a problem like this some years ago with using "delay" as a timer in the loop. I'll research this, but I think the essence of the thing was that 'delay' interfered with interrupts. So very likely, the problem is in the sekth NOT in io.adafruit.com

jferguson
 
Posts: 5
Joined: Mon Aug 20, 2012 2:29 pm

Please be positive and constructive with your questions and comments.