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.
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
{
allowConnection();
}
else // user decided to return false
{
blockConnection();
}
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?
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. )