Multiples feed values to use inside arduino loop() function

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
igorrocha
 
Posts: 2
Joined: Mon Jul 05, 2021 5:41 pm

Multiples feed values to use inside arduino loop() function

Post by igorrocha »

Hello everybody.

I'm trying to get multiple feed values inside the Arduino loop function like it's shown below by calling the SerialPrint (defined at the bottom of the code), which contains some 'Serial.print()' commands.

But I still not getting in the Serial Monitor the values of all feeds, only the 'relay' feed is being updated after the Setup function:
⸮⸮......
Adafruit IO connected.
Updating relay state..
Setup completed!
Received Command <- ON
Activating Relay

11511
Relay Status: ON
Schedule hour: AM
Top Shelf Time: seconds
Middle Shelf Time: seconds
Bottom Shelf Time: seconds
Somebody can help me?

Code: Select all

// LIBRARIES
#include "config.h"

// DEFINITIONS
#define UpdatingTime 10000
#define TurnON   LOW
#define TurnOFF HIGH

AdafruitIO_Feed *relay          = io.feed(       "relay");
AdafruitIO_Feed *timer          = io.feed(       "timer");
AdafruitIO_Feed *ScheduleTime   = io.feed("ScheduleTime");
AdafruitIO_Feed *TopShelf       = io.feed(    "TopShelf");
AdafruitIO_Feed *MiddleShelf    = io.feed( "MiddleShelf");
AdafruitIO_Feed *BottomShelf    = io.feed( "BottomShelf");

// FUNCTIONS DECLARATION
void MQTT_setup();
void returnRelay         (AdafruitIO_Data *data);
void returnScheduleTime  (AdafruitIO_Data *data);
void returnTopShelf      (AdafruitIO_Data *data);
void returnMiddleShelf   (AdafruitIO_Data *data);
void returnBottomShelf   (AdafruitIO_Data *data);

// VARIABLES DECLARATION
unsigned long TimeController = 0;
String Relay_State;
String Schedule_Time;
String Top_Shelf;
String Middle_Shelf;
String Bottom_Shelf;

void setup() {
  Serial.begin(9600);
  while (! Serial);

  Serial.println("Connecting with Adafruit IO...");
  io.connect();

  relay->onMessage(returnRelay);
  ScheduleTime->onMessage(returnScheduleTime);
  TopShelf->onMessage(returnTopShelf);
  MiddleShelf->onMessage(returnMiddleShelf);
  BottomShelf->onMessage(returnBottomShelf);

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

  Serial.println();
  Serial.println(io.statusText());

  Serial.println("Updating relay state..");
  relay->get();
  ScheduleTime->get();
  TopShelf->get();
  MiddleShelf->get();
  BottomShelf->get();
  
  Serial.println("Setup completed!");
}

void loop() {
  io.run();

  if (millis() > TimeController + UpdatingTime) {
    TimeController = millis();
    timer->save(TimeController);
    Serial.println(TimeController);
    SerialPrint();
  }
}

void returnRelay(AdafruitIO_Data *data) {
  Serial.print("Received Command <- ");
  Serial.println(data->value());

  Relay_State = data->value();

  if (Relay_State == "ON") {
    Serial.println("Activating Relay");
  } else if (Relay_State == "OFF") {
    Serial.println("Deactivating Relay");
  }
  Serial.println(" ");
}

void returnScheduleTime(AdafruitIO_Data *data) {
  Serial.print("Received Command <- ");
  Serial.println(data->value());
  Schedule_Time = data->value();
  Serial.print("Schedule hour: "); Serial.print(Schedule_Time); Serial.println(" AM"); Serial.println(" ");
}

void returnTopShelf(AdafruitIO_Data *data) {
  Serial.print("Received Command <- ");
  Serial.println(data->value());
  Top_Shelf = data->value();
  Serial.print("Top Shelf Time: "); Serial.print(Top_Shelf); Serial.println(" seconds"); Serial.println(" ");
}

void returnMiddleShelf(AdafruitIO_Data *data) {
  Serial.print("Received Command <- ");
  Serial.println(data->value());
  Middle_Shelf = data->value();
  Serial.print("Middle Shelf Time: "); Serial.print(Middle_Shelf); Serial.println(" seconds"); Serial.println(" ");
}

void returnBottomShelf(AdafruitIO_Data *data) {
  Serial.print("Received Command <- ");
  Serial.println(data->value());
  Bottom_Shelf = data->value();
  Serial.print("Bottom Shelf Time: "); Serial.print(Bottom_Shelf); Serial.println(" seconds"); Serial.println(" ");
}

void SerialPrint(){
    Serial.print("Relay Status: "); Serial.println(Relay_State);
    Serial.print("Schedule hour: "); Serial.print(Schedule_Time); Serial.println(" AM");
    Serial.print("Top Shelf Time: "); Serial.print(Top_Shelf); Serial.println(" seconds");
    Serial.print("Middle Shelf Time: "); Serial.print(Middle_Shelf); Serial.println(" seconds");
    Serial.print("Bottom Shelf Time: "); Serial.print(Bottom_Shelf); Serial.println(" seconds");
    Serial.println(" ");
} 

User avatar
brubell
Learn User Page
 
Posts: 2017
Joined: Fri Jul 17, 2015 10:33 pm

Re: Multiples feed values to use inside arduino loop() funct

Post by brubell »

The code looks OK to me.. Could you try adding a `delay(500)` between the get() calls in your setup function?

User avatar
igorrocha
 
Posts: 2
Joined: Mon Jul 05, 2021 5:41 pm

Re: Multiples feed values to use inside arduino loop() funct

Post by igorrocha »

brubell wrote:The code looks OK to me.. Could you try adding a `delay(500)` between the get() calls in your setup function?
Yeah.. I tried to do that but it still doesn't work.. Really don't know what's going on..

Code: Select all

  relay->get(); delay(500);
  ScheduleTime->get(); delay(500);
  TopShelf->get(); delay(500);
  MiddleShelf->get(); delay(500);
  BottomShelf->get(); delay(500);
I either tried to comment all the `relay' code lines to see there's a problem with the interaction with adafruit server (hoping that the next feed `ScheduleTime' could be updated instead of the `relay' one), but no feed was updated.

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”