How to return value using the switch in arduino iot dashboard

Hello guys, I have some doubts about working with the dashboard in Arduino IoT Cloud. I would like to add a Switch button in the dashboard like the figure below. But I wanted the switch to return a value to a variable in my main function.
But, once we added the switch, Arduino IoT will auto-generate the code as below:

void onLEDChange()  {
  //type your code here

As you can see the onLEDChange() is in void type. That is mean it cannot return a value in the main loop function. Can I actually change the type of function? For example from "void" to "int"? For your information, the function is already predefined in the thingProperties's file like below.

void onLEDChange();

CloudLight lED;
CloudElectricPotential gSR_Sensor;

void initProperties(){

  ArduinoCloud.addProperty(lED, READWRITE, ON_CHANGE, onLEDChange);
  ArduinoCloud.addProperty(gSR_Sensor, READ, 1 * SECONDS, NULL);


WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);


This function is a callback (an event that is called when an external library want to tell you that something happened), your code must not call this function, so expecting a return value is pointless.

In fact, if it returned something, the return value would be used by the code who called the callback, never by your own code

To understand, here is an example... Imagine a library had code like this:

bool userCallbackResult = onTryConnect(); // call the callback and store its return value

if ( userCallbackResult == true ) // user decided to return true
else // user decided to return false

Then in your code you could do something like this :

bool onTryConnect()
  // block connections between 6h and 12h
  if ( hour >= 6 && hour < 12 )
    return false;

  return true;

Hi Guix. I was using an Arduino Uno to send sensor data to NodeMCU. And the NodeMCU is to receive the data from the Arduino Uno and then update it to the IoT platform. So here at the dashboard, I added a switch, and I expect to use this switch to return a value of 1 or 0 to my NodeMCU. Then, NodeMCU will send the "0" and "1" to the Arduino Uno. So If the Arduino Uno receives a "1" it will turn on the LED connected to it. If it received a "0" it will turn off the LED. I cannot connect the LED directly to my NodeMCU, because it outputs only 3.3V. So, I wanted to Arduino Uno to receive the "1" and "0" from iot cloud through NodeMCU to power up my led. (LED in this case is a RGB strip that operate at 5V). Is there any suggestion from you I can do this?

The value of the switch is in the variable lED, it can be read and written like a boolean (true or false)

Test with something like this:

// add in setup() : pinMode( LED_BUILTIN, OUTPUT );

void onLEDChange()
  digitalWrite( LED_BUILTIN, lED == true ? HIGH : LOW );

Ok, but I think this code only allows me to switch on the led directly in my nodemcu. My intention is to switch on the led that is connected to Arduino Uno.

Is there any way I can use the switch to turn on my led in the Arduino Uno but not the NodeMcu? (Just to recap, my Arduino Uno is connected to node MCU for serial communication purpose. )

@guix Something like this.

What have you tried? Because it's not hard, use Serial.write on one side, on the other side... Like any other Serial communications.

It could be very simplistic or more robust... I suggest that you read this Serial Input Basics - updated

I started to get some idea now. I will try it out and update it here in the future. Thanks @guix :grin: :grin:

I have try it. And it works!!!! The arduino uno receive "1" when switch is on and "0" when the switch is off. I can't thank you enough :star_struck: :star_struck:

Good :slight_smile:

1 Like

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