Can esp32 close a switch in the iot remote app

Hello, I am a bit new in esp32 and iot games but I have successfully build a switch in the iot remote that opens a relay for my espresso machine. But the thing I’m trying to understand is if it is possible to put a real push button to the Esp32 that would close the switch online in the iot app to close the relay? Because I’m always closing the switch with my cellphone and it’s quite annoying, this would make the work easier if I could just press a button on my espresso machine. Thanks

Hi @5020junior.

Yes. As long as the Arduino Cloud IoT Variable you linked to the switch Dashboard Widget has the "Read & Write" Variable permission (instead of the "Read Only" permission), then if you change the value of the Variable in your Thing sketch program, the state of the switch Widget will change accordingly in the app.

If you did configure the Variable to have the "Read Only" permission, you can change its configuration via the "Setup" tab for the Thing on the Arduino Cloud website.

If this is not clear, I recommend creating a simple test Thing sketch that only changes the value of the variable periodically. Something like this:

void loop() {
  ArduinoCloud.update();
  someVariable = !someVariable;
  delay(2000);  
}
1 Like

ok, I have tried to change many variables and values in the sketch but I can't find how to turn the dashboard switch with the esp32 without my cellphone. In this sketch, the name of the cloud variable is Machine and the declaration is a boolean machine (no capital M) but when I change the boolean to false with the push button connected to one of the esp32 pin, it doesn't close the switch in the dashboard. There is probably something wrong with my code but I can't figure out what it is

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
  https://create.arduino.cc/cloud/things/4b092b59-ed79-49b7-ac6f-0762739566e6 

  Arduino IoT Cloud Variables description

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

  bool machine;

  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"
const int relay1 = 4;
const int relay2 = 5;
const int led = 2;
const int buttonpin = 22;
int buttonstate = 0;
int Machine = false;
void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(buttonpin, INPUT);
  digitalWrite (relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(led, LOW);
  // 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();
  buttonstate = digitalRead(buttonpin);
   if (machine == false)
  {
    digitalWrite (relay1, LOW);
    digitalWrite (relay2, LOW);
    digitalWrite (led,LOW);
  }
   else
  {
    digitalWrite (relay1, HIGH);
    digitalWrite (relay2, HIGH);
    digitalWrite (led, HIGH);
  }
if(buttonstate == HIGH){
  digitalWrite(machine, true);
  digitalWrite(led, HIGH);
}

}
  // Your code here 
/*
  Since Machine is READ_WRITE variable, onMachineChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMachineChange()  {

  // Add your code here to act upon Machine change
}

I know in the part where I put digitalWrite(machine, true): it should be false but I have tried with false and it doesn't change something

last thing, when I tried it this way, the led did light up but the switch on the dashboard didn't

Did you try my suggestion?:


You never do that in the code you shared.

This is the way to set the value of the variable to false:

machine = false;

There is no such code in your sketch.

Maybe you thought that this code did it?:

The digitalWrite function sets the state of the pin on your Arduino board. The digitalWrite function does not change the value of a variable!

This code seems nonsensical. The first parameter of the digitalWrite function is the Arduino pin number. The second parameter of the function is the pin state. So what this code will do is:

  • If the value of the Machine Cloud Variable, as set by the dashboard Widget is "true", it will set pin 1 (the value of the true Boolean literal is 1) on the board to HIGH (the value of the HIGH macro of the ESP32 core is 1).
  • If the value of the Machine Cloud Variable, as set by the dashboard Widget is "false", it will set pin 0 (the value of the false Boolean literal is 1) on the board to LOW (the value of the LOW macro of the ESP32 core is 0).

This indicates that you don't have an understanding of the absolute basics of Arduino. It would save you a lot of time and headaches in the end if you took the time to learn the basics before starting on complex projects like this IoT Thing project. Most of us here started by blinking an LED (no Cloud IoT involved). That taught us how to use digitalWrite.

OOhhhhhhhhh, thanks I didn't know that I couldn't change the value of my variable by putting true/false. Now it's kinda working, I just need to fix the noise in my circuit because the relays are doing a party

You are welcome. I'm glad if I was able to be of assistance.

The hypothesis that the problem is caused by the electrical conditions in the circuit is certainly reasonable. However, you should keep in mind that an unexpected "relay party" could also be caused by a bug in your code.

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