Skip to content

Commit ab2f464

Browse files
committed
added changes by electronicsguy Sujay Phadke
1 parent df1e35d commit ab2f464

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

esp8266_rcswitch.ino

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
* server_ip is the IP address of the ESP8266 module, will be
77
* printed to Serial when the module is connected.
88
*/
9-
#include <RCSwitch.h>
9+
#include "RCSwitch.h"
1010
#include <ESP8266WiFi.h>
1111

1212
RCSwitch mySwitch = RCSwitch();
1313

14-
1514
const char* ssid = "SSID";
16-
const char* password = "PASSWD";
15+
const char* password = "PASSWORD";
1716

1817
// Create an instance of the server
1918
// specify the port to listen on as an argument
@@ -26,14 +25,11 @@ void setup() {
2625
// prepare GPIO2
2726
pinMode(2, OUTPUT);
2827

29-
30-
3128
// Transmitter is connected to ESP8266 PIN #2
3229
mySwitch.enableTransmit(2);
3330
//EtekCity ZAP Remote Outlets use pulse of approximately 189
3431
mySwitch.setPulseLength(189);
3532

36-
3733
// Connect to WiFi network
3834
Serial.println();
3935
Serial.println();
@@ -59,7 +55,6 @@ void setup() {
5955

6056
void loop() {
6157

62-
6358
// Check if a client has connected
6459
WiFiClient client = server.available();
6560
if (!client) {
@@ -79,28 +74,45 @@ void loop() {
7974

8075
// Match the request
8176
int val;
82-
if (req.indexOf("/sw1/0") != -1)
83-
{val = 0;
84-
// Insert your "OFF" code here, before ", 24)"
85-
mySwitch.send(XXXXXXX, 24);}
86-
else if (req.indexOf("/sw1/1") != -1)
87-
{val = 1;
88-
// Insert your "ON" code here, before ", 24)"
89-
mySwitch.send(XXXXXXX, 24);}
77+
if (req.indexOf("/sw1/0") != -1) {
78+
val = 0;
79+
// Insert your "OFF" code here, before ", 24)"
80+
mySwitch.send(12345, 24);}
81+
else if (req.indexOf("/sw1/1") != -1) {
82+
val = 1;
83+
// Insert your "ON" code here, before ", 24)"
84+
mySwitch.send(54321, 24);
85+
}
86+
else if (req.indexOf("favicon.ico") != -1) {
87+
// ignore favicon requests from browser
88+
val = -1;
89+
}
9090
else {
91-
Serial.println("invalid request");
92-
client.stop();
93-
return;
91+
val = -10;
92+
Serial.println("invalid request");
9493
}
9594

96-
9795
client.flush();
9896

9997
// Prepare the response
100-
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nSwitch 1 is now ";
101-
s += (val)?"ON":"OFF";
102-
s += "</html>\n";
103-
98+
// Handle (ignore) favicon requests with a 404 response
99+
// https://github.com/esp8266/Arduino/issues/2080
100+
String s;
101+
if (val == -1){
102+
s = "HTTP/1.1 404 \r\n";
103+
}
104+
// handle invalid requests
105+
else if (val == -10){
106+
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nInvalid request! ";
107+
s += "\r\nEnter URL as: \"&lt;ip-address&gt;/sw1/&lt;VAL&gt;\" where &lt;VAL&gt; is 0 or 1";
108+
s += "</html>\n";
109+
}
110+
// correct requests
111+
else{
112+
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nSwitch 1 is now ";
113+
s += (val)?"ON":"OFF";
114+
s += "</html>\n";
115+
}
104116
// Send the response to the client
105117
client.print(s);
106118
delay(1);
@@ -109,4 +121,3 @@ void loop() {
109121
// The client will actually be disconnected
110122
// when the function returns and 'client' object is detroyed
111123
}
112-

0 commit comments

Comments
 (0)