Is there a person who remodel this sketch for a plugin for EspEasy?
I really want this.
Code: Select all
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a switch depending on the request
* http://server_ip/sw1/0 will turn sw1 off
* http://server_ip/sw1/1 will turn sw1 on
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <RCSwitch.h>
#include <ESP8266WiFi.h>
RCSwitch mySwitch = RCSwitch();
const char* ssid = "your_ssid";
const char* password = "your_password";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);
// Transmitter is connected to ESP8266 PIN #2
mySwitch.enableTransmit(2);
//EtekCity ZAP Remote Outlets use pulse of approximately 189
mySwitch.setPulseLength(189);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
// 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.println(WiFi.localIP());
}
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 req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
int swi;
// **** SWITCH 1 ****
if (req.indexOf("/sw1/0") != -1)
{val = 0;
swi = 1;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(5260604, 24);}
else if (req.indexOf("/sw1/1") != -1)
{val = 1;
swi = 1;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(5260595, 24);}
// **** SWITCH 2 ****
else if (req.indexOf("/sw2/0") != -1)
{val = 0;
swi = 2;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(1234567, 24);}
else if (req.indexOf("/sw2/1") != -1)
{val = 1;
swi = 2;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(2345678, 24);}
// **** SWITCH 3 ****
else if (req.indexOf("/sw3/0") != -1)
{val = 0;
swi = 3;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(3456789, 24);}
else if (req.indexOf("/sw3/1") != -1)
{val = 1;
swi = 3;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(4567890, 24);}
// **** SWITCH 4 ****
else if (req.indexOf("/sw4/0") != -1)
{val = 0;
swi = 4;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(5678901, 24);}
else if (req.indexOf("/sw4/1") != -1)
{val = 1;
swi = 4;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(6789012, 24);}
// **** SWITCH 5 ****
else if (req.indexOf("/sw5/0") != -1)
{val = 0;
swi = 5;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(7890123, 24);}
else if (req.indexOf("/sw5/1") != -1)
{val = 1;
swi = 5;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(8901234, 24);}
// **** SWITCH 6 ****
else if (req.indexOf("/sw6/0") != -1)
{val = 0;
swi = 6;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(9012345, 24);}
else if (req.indexOf("/sw6/1") != -1)
{val = 1;
swi = 6;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(1098765, 24);}
// **** SWITCH 7 ****
else if (req.indexOf("/sw7/0") != -1)
{val = 0;
swi = 7;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(4478268, 24);}
else if (req.indexOf("/sw7/1") != -1)
{val = 1;
swi = 7;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(4478259, 24);}
// **** SWITCH 8 ****
else if (req.indexOf("/sw8/0") != -1)
{val = 0;
swi = 8;
// Insert your "OFF" code here, before ", 24)"
mySwitch.send(87124, 24);}
else if (req.indexOf("/sw8/1") != -1)
{val = 1;
swi = 8;
// Insert your "ON" code here, before ", 24)"
mySwitch.send(87121, 24);}
else {
Serial.println("invalid request");
client.stop();
return;
}
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nSwitch ";
s += String(swi, DEC);
s += " is now ";
s += (val)?"ON":"OFF";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}