I am having problem with programming chinese smart plug. I want to make simple toggle switch that turns on relay, if it is off and vice versa. I have succeed making on and off switches, so I think the problem must be with multiple "if" commands.
Here is the problematic part of my code:
if (request.indexOf("/RelayTOGGLE") != -1){ //if command "RelayToggle" is received
if(value == 0){ // value represents state of the relay off=0
digitalWrite(RelayOFF, HIGH); //turns on relay
delay(10);
digitalWrite(RelayON,LOW );
value = 1;
}
if(value == 1){
digitalWrite(RelayON, HIGH); //turns off relay
delay(10);
digitalWrite(RelayOFF, LOW);
value = 0;
}
}
With this code RelayToggle only turns off the relay but doesnt turn it on. Any help is appreciated!
#include <ESP8266WiFi.h>
const char* ssid = "possuwlan";
const char* password = "censored";
int RelayON = 12;
int RelayOFF = 5;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
delay(10);
pinMode(RelayON, OUTPUT);
pinMode(RelayOFF, OUTPUT);
digitalWrite(RelayON, HIGH);
digitalWrite(RelayOFF, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = 0;
if (request.indexOf("/RelayON") != -1) { // this works fine, relay turns on when command RelayON is
digitalWrite(RelayOFF, HIGH); sent.
delay(10);
digitalWrite(RelayON, LOW);
value = 1;
}
if (request.indexOf("/RelayOFF") != -1){ //this works fine too, relay turns off when command RealyOF
digitalWrite(RelayON, HIGH); is sent.
delay(10);
digitalWrite(RelayOFF, LOW);
value = 0;
}
if (request.indexOf("/RelayTOGGLE") != -1){ //Here I am trying to toggle the relay.
if(value == 0){
digitalWrite(RelayOFF, HIGH);
delay(10);
digitalWrite(RelayON,LOW );
value = 1;
}
if(value == 1){
digitalWrite(RelayON, HIGH);
delay(10);
digitalWrite(RelayOFF, LOW);
value = 0;
}
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Relay is now: ");
if(value == 1 ) {
client.print("On");
} else {
client.print("Off");
}
client.println("
");
client.println("Click <a href=\"/RelayON\">here</a> Turn relay ON
");
client.println("Click <a href=\"/RelayOFF\">here</a> Turn relay OFF
");
client.println("Click <a href=\"/RelayTOGGLE\">here</a> Toggle relay
");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}
I agree value is a bad name for the relaystate, I will change it. I am controlling two different pins, because the smart plug that I am modifying does have RelayON and RelayOFF pins. By experiementing I have succeed to turn on the relay by applying low to RelayON pin and high to RelayOFF pin.
Hmmhm, I made the relaystate static and uploaded the code. However the toggle command still doesn't work properly. When the relay is on and toggle command is given relay turns off as it should. But when the relay is off and toggle commad is given, nothing happens :o .
#include <ESP8266WiFi.h>
const char* ssid = "possuwlan";
const char* password = "censored";
int RelayON = 12;
int RelayOFF = 5;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
delay(10);
pinMode(RelayON, OUTPUT);
pinMode(RelayOFF, OUTPUT);
digitalWrite(RelayON, HIGH);
digitalWrite(RelayOFF, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
digitalWrite(4, HIGH);
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
static int relaystate = 0;
if (request.indexOf("/RelayON") != -1) {
digitalWrite(RelayOFF, HIGH);
delay(10);
digitalWrite(RelayON, LOW);
relaystate = 1;
}
if (request.indexOf("/RelayOFF") != -1){
digitalWrite(RelayON, HIGH);
delay(10);
digitalWrite(RelayOFF, LOW);
relaystate = 0;
}
if (request.indexOf("/RelayTOGGLE") != -1){
if(relaystate == 0){
digitalWrite(RelayOFF, HIGH);
delay(10);
digitalWrite(RelayON,LOW );
relaystate = 1;
}
if(relaystate == 1){
digitalWrite(RelayON, HIGH);
delay(10);
digitalWrite(RelayOFF, LOW);
relaystate = 0;
}
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Relay is now: ");
if(relaystate == 1 ) {
client.print("On");
} else {
client.print("Off");
}
client.println("
");
client.println("Click <a href=\"/RelayON\">here</a> Turn relay ON
");
client.println("Click <a href=\"/RelayOFF\">here</a> Turn relay OFF
");
client.println("Click <a href=\"/RelayTOGGLE\">here</a> Toggle relay
");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}
But when the relay is off and toggle commad is given, nothing happens
At the very least, a Serial.print() should happen. If that doesn't, then the logic is wrong. If it does, then the actions to be performed in the block are wrong. Which problem are we dealing with?
I was a bit inaccurate when I said "nothing happened". Serialprint happens following is printed:"GET /RelayTOGGLE HTTP/1.1
" ,when RelayTOGGLE command is sent. However the relay does't turn on.
When this poorly indented (use Tools + Auto Format to fix that) code is executed, you suspect that the first if statement will evaluate to true. Does it really? Add another Serial.print() statement after the {.
Serial.println("Kittens are cute");
Then, if you see "Kittens are cute" being printed, you know that the if statement evaluated to true.
What you don't know then is what is in relaystate (really should be relayState). There are a couple of ways to find out. Figure out one of them, and learn whether relaystate contains what you think it does, or not.
The technique is called divide and conquer, and will allow you to isolate a problem (not just this one) very quickly.
if (request.indexOf("/RelayTOGGLE") != -1) {
Serial.println("Kittens are cute");
if (relaystate == 0) {
digitalWrite(RelayOFF, HIGH);
delay(10);
digitalWrite(RelayON, LOW );
relaystate = 1;
}
if (relaystate == 1) {
digitalWrite(RelayON, HIGH);
delay(10);
digitalWrite(RelayOFF, LOW);
relaystate = 0;
}
}
I added Serial.println("Kittens are cute");, but "kittens are cute" isn't printed when RelayToggle command is sent, so something wrong with that? Thanks for the tips!