How do I use LED variable from iot dashboard to show relays statuses

Hi,

I have been reading a lot and trying out but it seems I do not get a simple Led to turn on when a relay is ON. Basically I have the code to operate relay1 and relay2 in a sequence so by pressing a button relay1 is ON and upon set time it goes OFF and relay 2 goes ON.
What I would like is to have LED1 and LED2 in the dashboard so I can see which relays is actually ON.
the relays work as set it is just I do not get the LEDs implemented. :frowning:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/c69200b7-9d1f-4419-83b2-a3a5a7964d8a 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool button;
  bool relay1;
  bool relay2;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"



#define relay1 1
#define relay2 2


String relay1State;

void setup() {

  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
   


  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
}



void loop() {

  ArduinoCloud.update();
    // Your code here 

}


/*
  Since Button is READ_WRITE variable, onButtonChange() is
  executed every time a new value is received from IoT Cloud.
*/
unsigned long startTime = millis();
unsigned long lastButtonPress = 0;
void onButtonChange()  {
  if(millis() - lastButtonPress > 20000) {
    lastButtonPress = millis();
    digitalWrite(relay1, HIGH); // turn on relay1
    Serial.println("relay1 is ON");
    startTime = millis();
    while(millis()-startTime<5000){}
    digitalWrite(relay1, LOW); // turn off relay1
    startTime = millis();
    while(millis()-startTime<1000){}
    digitalWrite(relay2, HIGH); // turn on relay2
    startTime = millis();
    while(millis()-startTime<10000){}
    digitalWrite(relay2, LOW); // turn off relay2
  }
}


/*
  Since Relay1 is READ_WRITE variable, onRelay1Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay1Change()  {
// Add your code here 

}

/*
  Since Relay2 is READ_WRITE variable, onRelay2Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay2Change()  {
  // Add your code here to act upon Relay2 change

}

/*
  Since Relay1Status is READ_WRITE variable, onRelay1StatusChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay1StatusChange()  {
  // Add your code here to act upon Relay1Status change
}

/*
  Since Relay2Status is READ_WRITE variable, onRelay2StatusChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay2StatusChange()  {
  // Add your code here to act upon Relay2Status change
}


1 Like

not clear what "dashboard" is, nor what thingproperties does/supports. is it primarily some sort of wireless connection.

the code looks like a skeleton. i don't see where the buttons are read or where the onRelay Change functions are called or configured as some callback to some thingproperty background processing (i.e. ArduinoCloud.update())

1 Like

The dashboard is a part of arduino iot Cloud from where I press the button to start the code and the Leds are supposed to show which relays is ON. see attached screen grab.

So in the thing properties I got:


// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)

void onButtonChange();
void onRelay1Change();
void onRelay2Change();

bool button;
bool relay1;
bool relay2;

void initProperties(){

  ArduinoCloud.addProperty(button, READWRITE, ON_CHANGE, onButtonChange, 1);
  ArduinoCloud.addProperty(relay1, READWRITE, ON_CHANGE, onRelay1Change);
  ArduinoCloud.addProperty(relay2, READWRITE, ON_CHANGE, onRelay2Change);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

where is this? on your PC?

yes, it is a screengrab.

more info on cloud app:

Hi,

I have a code that sets high or low relay1 and relay2 by pressing a button in the IoT cloud Dashboard but I can not make the led1 and led2 indicate whether the relays are HIGH or LOW. I have been working only on this the past two weeks.

All what is stated in the LED Widget is the following:

The LED widget is a virtual LED that can signal the status of something. Can either be set to ON or OFF.
Can be linked with a boolean variable.
An example of how it is used in a sketch:

ledVariable = true;
//or
ledVariable = false;

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/c69200b7-9d1f-4419-83b2-a3a5a7964d8a 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool button;
  bool relay1;
  bool relay2;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"


#define relay1 1
#define relay2 2


void setup() {

  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
   




  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
}




/*
  Since Button is READ_WRITE variable, onButtonChange() is
  executed every time a new value is received from IoT Cloud.
*/
enum State {
  IDLE,
  RELAY1_ON,
  RELAY1_OFF,
  RELAY2_ON,
  RELAY2_OFF
};

State currentState = IDLE;
unsigned long stateStartTime;

void onButtonChange() {
  if (currentState == IDLE) {
    currentState = RELAY1_ON;
    stateStartTime = millis();
    digitalWrite(relay1, HIGH);
    Serial.println("relay1 is ON");
  }
}


void loop() {

  ArduinoCloud.update();
    // Your code here 
  switch (currentState) {
    case RELAY1_ON:
      if (millis() - stateStartTime >= 15000) {
        currentState = RELAY1_OFF;
        stateStartTime = millis();
        digitalWrite(relay1, LOW);
      }
      break;

    case RELAY1_OFF:
      if (millis() - stateStartTime >= 1000) {
        currentState = RELAY2_ON;
        stateStartTime = millis();
        digitalWrite(relay2, HIGH);
        Serial.println("relay2 is ON");
      }
      break;

    case RELAY2_ON:
      if (millis() - stateStartTime >= 15000) {
        currentState = RELAY2_OFF;
        stateStartTime = millis();
        digitalWrite(relay2, LOW);
      }
      break;

    case RELAY2_OFF:
      currentState = IDLE;
      break;

    default:
      currentState = IDLE;
      break;
  }
}




void onRelay1Change()  {
// Add your code here 
}

/*
  Since Relay2 is READ_WRITE variable, onRelay2Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay2Change()  {
  // Add your code here to act upon Relay2 change
}

/*
  Since Relay1Status is READ_WRITE variable, onRelay1StatusChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay1StatusChange()  {
  // Add your code here to act upon Relay1Status change
}

/*
  Since Relay2Status is READ_WRITE variable, onRelay2StatusChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay2StatusChange()  {
  // Add your code here to act upon Relay2Status change
}

AND this is what I have in thingProperties.h

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)

void onButtonChange();
void onRelay1Change();
void onRelay2Change();

bool button;
bool relay1;
bool relay2;

void initProperties(){

  ArduinoCloud.addProperty(button, READWRITE, ON_CHANGE, onButtonChange, 1);
  ArduinoCloud.addProperty(relay1, READWRITE, ON_CHANGE, onRelay1Change);
  ArduinoCloud.addProperty(relay2, READWRITE, ON_CHANGE, onRelay2Change);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

I have merged your topics due to them having too much overlap on the same subject matter @loosepipe.

In the future, please only create one topic for each distinct subject matter and be careful not to cause them to converge into parallel discussions.

The reason is that generating multiple threads on the same subject matter can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Thanks in advance for your cooperation.

1 Like

thanks.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.