I have a ESP32 that I use Arduino on to control some relays with a web server.
Right now I am checking the relay state like this:
// We now create a URI for the request
String url = "/read.php?id=";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + "ESP" + ESPid + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
Serial.println(" ");
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
line = client.readStringUntil('\r'); // String
Serial.println(line);
}
if (line.toInt() == 100)
{
digitalWrite(Rele_1, LOW);
digitalWrite(Rele_2, HIGH);
digitalWrite(Rele_3, HIGH);
The part with:
if (line.toInt() == 100)
{
digitalWrite(Rele_1, LOW);
digitalWrite(Rele_2, HIGH);
digitalWrite(Rele_3, HIGH);
Will be copied and 100 will be changed for every possible variation.
This is not a god way to do it, when I get more relays on it. Each relay will have a 1 or 0 to start and stop.
Is there a way to split the 100 to control the relay? Like if a push 1&0&0
from my web server?
Is there a way to split 1&0&0
or r1=1&r2=0&r3=0
into variables to control the relays (pins)?
The solution i choose, was to switch from this:
if (line.toInt() == 100)
To
if (line.charAt(1) == '1') // Relay 1.
{