Long delays to handleMessage

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.
Locked
User avatar
jerryn
 
Posts: 1869
Joined: Sat Sep 14, 2013 9:05 am

Long delays to handleMessage

Post by jerryn »

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

// 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());

}

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: Long delays to handleMessage

Post by jwcooper »

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

User avatar
jerryn
 
Posts: 1869
Joined: Sat Sep 14, 2013 9:05 am

Re: Long delays to handleMessage

Post by jerryn »

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...

User avatar
jerryn
 
Posts: 1869
Joined: Sat Sep 14, 2013 9:05 am

Re: Long delays to handleMessage

Post by jerryn »

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...

User avatar
jerryn
 
Posts: 1869
Joined: Sat Sep 14, 2013 9:05 am

Re: Long delays to handleMessage

Post by jerryn »

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...

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

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.

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

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.

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

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?

User avatar
abachman
 
Posts: 352
Joined: Mon Feb 01, 2010 12:48 pm

Re: Long delays to handleMessage

Post by abachman »

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

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

Thanks Adam, I'll try these things.

john

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

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

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

Hi abachman,
I did as you ,suggest and watched the feeds on a monitor. The toggles show up and correctly. i also tried the simple io toggle experiment which turns a switch on and off using something like 10.0.0.205/gpio/1. It works perfectly.

the code attaached reads two sensors, hunidity and temperature, and sends them to io.ada as feeds. This works perfectly although i had to add a five second delay to keep the datastream below thw maximum allowed for free service.

so we know the toggle sets a feed to either 0 or 1 and that similar code which doesn't have the data stream and the 5 second delays works perfectly. the code below lets the toggle thourgh once in a while - no pattern. when it does let a toggle thourgh I can see it on the serial monitor.

to speculate a little, I suspect the toggle signal is getting to my device, but there is a problem with the device not 'seeing' it. what else can i ttry?
Attachments
adafruitio_temp_humidity-2 .txt
io.ada tmep/humidity/ reporter with toggle for relay to turn on and off a heater.
(3.13 KiB) Downloaded 148 times

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

Since I don't really care about collecting data, but more reliably turning the heate on and off, I should forget about the data stream and try having it print present-temp and present-humiditry to feed whe I toggle the heater. I'm going to see if that works.

User avatar
jferguson
 
Posts: 37
Joined: Mon Aug 20, 2012 2:29 pm

Re: Long delays to handleMessage

Post by jferguson »

SOLVED: Problem was incoming toggle blocked by delay() functions. I replaced them with millis() functions and increased data reporting interval to assure it would fall below the 30 item/minute threshold and Voila!

I suppose if I'd been a bit smarter when I suspected a delay() problem and searched this site for millis() comments I would have figured this out sooner, but alas...

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”