๐ŸŽ„โ›„โ„๏ธ๐ŸŽ It's beginning to look a lot like ADABOX-mas! The next ADABOX from Adafruit ships soon! Get yours now, or give as a gift! ๐ŸŽ„โ›„โ„๏ธ๐ŸŽ
0

Feed Reading and Monitoring looping at every 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.

Feed Reading and Monitoring looping at every run

by JoseeMallah on Thu Oct 31, 2019 6:41 am

Hello everyone,
I have a problem using Adafruit IO with NodeMCU. I have a feed which value should be read and tested and an email/notification should be sent accordingly once per day. But it seems to me that it is called back at every call of io.run().
Can anyone help me solve this issue? I can post parts of the code if needed. I am using Arduino IDE.
Thank you

JoseeMallah
 
Posts: 7
Joined: Thu Oct 31, 2019 6:34 am

Re: Feed Reading and Monitoring looping at every run

by brubell on Thu Oct 31, 2019 11:24 am

But it seems to me that it is called back at every call of io.run().

I'm not sure what you mean by this. Could you elaborate, specifically - what should be happening and what's not happening?

io.run() only maintains an active connection with the Adafruit IO MQTT server.

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

Re: Feed Reading and Monitoring looping at every run

by JoseeMallah on Thu Oct 31, 2019 1:10 pm

Here is my code. Please I have a project and I need your help everyone! :)
Is there a boolean method to check if there is new data on a feed?

Code: Select all | TOGGLE FULL SIZE
int n = 0;

int i = 0;

String w = " ";
int lastn = 0;

/************************ Adafruit IO Configuration *******************************/

// visit io.adafruit.com if you need to create an account,
// or if you need your Adafruit IO key.
#define IO_USERNAME    "JoseeMallah"
#define IO_KEY         "SNIP"

/******************************* WIFI Configuration **************************************/

#define WIFI_SSID       "Nada"
#define WIFI_PASS       "SNIP"

#include "AdafruitIO_WiFi.h"
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);

/************************ Main Program Starts Here *******************************/
#include <ESP8266WiFi.h>
#include <AdafruitIO.h>
#include <Adafruit_MQTT.h>
#include <ArduinoHttpClient.h>

AdafruitIO_Feed *Door = io.feed("Door");
AdafruitIO_Feed *Temperature = io.feed("Temperature"); // set up the 'Temperature' feed
AdafruitIO_Feed *Weather = io.feed("Weather"); // set up the 'Weather' feed
AdafruitIO_Feed *ClothesTC = io.feed("ClothesTC"); // set up the 'ClothesTC' feed
AdafruitIO_Feed *ClothesC = io.feed("ClothesC"); // set up the 'ClothesC' feed
AdafruitIO_Feed *ClothesAC = io.feed("ClothesAC"); // set up the 'ClothesAC' feed
AdafruitIO_Feed *ClothesAH = io.feed("ClothesAH"); // set up the 'ClothesAH' feed
AdafruitIO_Feed *ClothesH = io.feed("ClothesH"); // set up the 'ClothesH' feed
AdafruitIO_Feed *ClothesTH = io.feed("ClothesTH"); // set up the 'ClothesAH' feed
AdafruitIO_Feed *WearToday = io.feed("WearToday"); // set up the 'WearToday' feed

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  //pins
  pinMode (D0, INPUT);
  pinMode (D1, OUTPUT);
  pinMode (D2, OUTPUT);
  pinMode (D5, OUTPUT);
  pinMode (D6, OUTPUT);
  pinMode (D7, OUTPUT);
  pinMode (D8, OUTPUT);

  // set up a message handler for the feeds.
  // the handleMessage functions (defined below)
  // will be called whenever a message is
  // received from adafruit io.
  Door->onMessage(handleMessage1);
  Temperature->onMessage(handleMessage2);
  Weather->onMessage(handleMessage);
  ClothesTC->onMessage(handleMessageTC);
  ClothesC->onMessage(handleMessageC);
  ClothesAC->onMessage(handleMessageAC);
  ClothesAH->onMessage(handleMessageAH);
  ClothesH->onMessage(handleMessageH);
  ClothesTH->onMessage(handleMessageTH);
  WearToday->onMessage(handleMessageWT);

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

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

  Weather->get();

}

void loop()
{
  // put your main code here, to run repeatedly:
  //io.run();
  temperature(0, 0, 0, 0);
}

void temperature(int i, int j, int k, int t)
{
  io.run();
  digitalWrite(D6, LOW);
  digitalWrite(D7, LOW);
  digitalWrite(D8, LOW);
  int val = analogRead(A0);
  float mv = ( val / 1024.0) * 5000;
  float cel = mv / 10;
  minute[i] = cel;
  delay(1000);
 
............. etc.............

    if (t == 0)
    {
      Serial.print("sending data -> ");
      Serial.println(w);
      WearToday->save(w);
      digitalWrite(D6, HIGH);
      digitalWrite(D7, HIGH);
      digitalWrite(D8, HIGH);
    }
    if (t == 86399)
    {
      t = 0;
    }
    else
    {
      t++;
    }
    temperature(i, j, k, t);
}

..........etc............

void handleMessage(AdafruitIO_Data *data)
{
  String Weather = data->toString();
   
    Serial.print("received <- "); [b][u]// this part keeps looping every around 50 seconds and I do not know why[/u][/b]
    Serial.println("High/Low: ");
    Serial.println(Weather);
    digitalWrite(D6, HIGH);
    digitalWrite(D7, LOW);
    digitalWrite(D8, HIGH);
    delay(500);
    digitalWrite(D6, LOW);
    digitalWrite(D7, LOW);
    digitalWrite(D8, LOW);
    Serial.println("Done");
    int high = Weather.toInt();
    Serial.println(high);
    ...........
 }


And apparently this part is not saving value to arrays:

Code: Select all | TOGGLE FULL SIZE
void handleMessageAC(AdafruitIO_Data *data)
{
  String ClothesAC = data->toString();

    Serial.print("received <- ");
    Serial.println(ClothesAC);
    nbac++;
    AC[nbac] = ClothesAC;
}

JoseeMallah
 
Posts: 7
Joined: Thu Oct 31, 2019 6:34 am

Re: Feed Reading and Monitoring looping at every run

by brubell on Thu Oct 31, 2019 3:16 pm

Is there a boolean method to check if there is new data on a feed?



Yes, when you set up a message handler in your code (i.e: https://github.com/adafruit/Adafruit_IO ... ub.ino#L58), the handleMessage function (https://github.com/adafruit/Adafruit_IO ... ub.ino#L99) will be called whenever a message from the feed its linked to receives data.

In your handleMessage function, it's odd that it's getting called..
Code: Select all | TOGGLE FULL SIZE
void handleMessage(AdafruitIO_Data *data)
{
  String Weather = data->toString();
   
    Serial.print("received <- "); [b][u]// this part keeps looping every around 50 seconds and I do not know why[/u][/b]
    Serial.println("High/Low: ");
    Serial.println(Weather);
    digitalWrite(D6, HIGH);
    digitalWrite(D7, LOW);
    digitalWrite(D8, HIGH);
    delay(500);



Your Weather feed seems to be attached to this function(Weather->onMessage(handleMessage);). Is there anywhere in your code where the weather feed is written to? On the Adafruit IO Website, is the weather feed updated every ~50 seconds? What data is it getting? Is updating with data sent to the feed by the sketch, or garbage data?

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

Re: Feed Reading and Monitoring looping at every run

by JoseeMallah on Thu Oct 31, 2019 3:30 pm

I get values once per day from ifttt where I made an applet which gets the daily high/low temperatures from Weather Underground at 7:00 am so values are added once daily mainly. And values seem to be right. So the handleMessage should be run once daily so at reset first. I was thinking of something that tests if there are new values to run the method. I was trying to use global variables but I still couldn't get it working. Any idea?

JoseeMallah
 
Posts: 7
Joined: Thu Oct 31, 2019 6:34 am

Re: Feed Reading and Monitoring looping at every run

by JoseeMallah on Thu Oct 31, 2019 4:46 pm

If you @brubell say that the code is right, I notice that the NodeMCU is restarting for a reason which I do not know. The code is working good but due to the reset data from the feed is loaded again. Using the serial monitor, every 1 minute now more than 50s maybe, I see something not very understandable saying boot and reset maybe and shpwing Connecting to Adarfruit IO again. So I mainly need help please to know/fix what is/may be causing this. I am using the A0 pin to read temperature, D0 to digitalRead pin 2 on Arduino UNO and D6, D7 and D8 to control a 4 pin RGB LED (other pins are available in pinMode but not really used now) and I established a common ground for the 2 boards.

JoseeMallah
 
Posts: 7
Joined: Thu Oct 31, 2019 6:34 am

Re: Feed Reading and Monitoring looping at every run

by JoseeMallah on Sun Nov 03, 2019 12:48 pm

This topic is now closed.

JoseeMallah
 
Posts: 7
Joined: Thu Oct 31, 2019 6:34 am

Please be positive and constructive with your questions and comments.