Hello all,
I use ESP8266 ESP-01S 5V WiFi Relay Module to switch on/off my lamp in basement and it is work fine. But some time i forgot to do that before to go downstairs and want to add some button on the relay to do that manually. I did program my ESP-01 :
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#define RELAY0_PIN 0
//------------------------------------------
//WIFI i server HTTP
ESP8266WebServer server(80);
#define WIFI_SSID "*********"
#define WIFI_PASSWORD "**************"
#define HOST_NAME "onoff"
void setup() {
pinMode(RELAY0_PIN, OUTPUT);
//Setup UART
Serial.begin(115200);
Serial.println("");
Serial.println("START");
//Setup WIFI
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
WiFi.hostname( HOST_NAME );
Serial.print("Connecting to WIFI");
//Wait for WIFI connection
while( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println( WIFI_SSID );
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//Setup HTTP server
server.on("/", handleRoot);
server.on("/sw", handleSW);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void SW(byte num, bool sw){
switch( num ){
case 0: digitalWrite(RELAY0_PIN, !sw); break;
}
}
void handleRoot(){
String html = "<!DOCTYPE html>\r\n";
html += "<html>\r\n";
html += "<head>\r\n";
html += "<meta charset='UTF-8'>\r\n";
html += "<title>ESP8266</title>\r\n";
html += "<body>\r\n";
html += "ESP8266 - ";
html += HOST_NAME;
html += "\r\n";
if( !digitalRead(RELAY0_PIN) ){
html += "<a href='sw?sw=off' style='display: inline-block; width: 100px; background-color: red; text-align: center;'>OFF</a>\r\n";
}else{
html += "<a href='sw?sw=on' style='display: inline-block; width: 100px; background-color: green; text-align: center;'>ON</a>\r\n";
}
server.send(200, "text/html", html);
}
void handleSW(){
if (server.arg("sw")== "on"){
SW(0, HIGH);
}else{
SW(0, LOW);
}
String ip = WiFi.localIP().toString();
server.sendHeader("Location", String("http://") + ip, true);
server.send ( 302, "text/plain", "");
server.client().stop();
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
There is no really sketch, but the picture is kind of good idea, what i have :
So, obviously i use Pin 0 on the ESP and have one more pin, which maybe is possible to use it.
I believe I will change my code, but can you help me, how to wire/connect the additional button like this one :