6
6
* server_ip is the IP address of the ESP8266 module, will be
7
7
* printed to Serial when the module is connected.
8
8
*/
9
- #include < RCSwitch.h>
9
+ #include " RCSwitch.h"
10
10
#include < ESP8266WiFi.h>
11
11
12
12
RCSwitch mySwitch = RCSwitch();
13
13
14
-
15
14
const char * ssid = " SSID" ;
16
- const char * password = " PASSWD " ;
15
+ const char * password = " PASSWORD " ;
17
16
18
17
// Create an instance of the server
19
18
// specify the port to listen on as an argument
@@ -26,14 +25,11 @@ void setup() {
26
25
// prepare GPIO2
27
26
pinMode (2 , OUTPUT);
28
27
29
-
30
-
31
28
// Transmitter is connected to ESP8266 PIN #2
32
29
mySwitch.enableTransmit (2 );
33
30
// EtekCity ZAP Remote Outlets use pulse of approximately 189
34
31
mySwitch.setPulseLength (189 );
35
32
36
-
37
33
// Connect to WiFi network
38
34
Serial.println ();
39
35
Serial.println ();
@@ -59,7 +55,6 @@ void setup() {
59
55
60
56
void loop () {
61
57
62
-
63
58
// Check if a client has connected
64
59
WiFiClient client = server.available ();
65
60
if (!client) {
@@ -79,28 +74,45 @@ void loop() {
79
74
80
75
// Match the request
81
76
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
+ }
90
90
else {
91
- Serial.println (" invalid request" );
92
- client.stop ();
93
- return ;
91
+ val = -10 ;
92
+ Serial.println (" invalid request" );
94
93
}
95
94
96
-
97
95
client.flush ();
98
96
99
97
// Prepare the response
100
- String s = " HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n <!DOCTYPE HTML>\r\n <html>\r\n Switch 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\n Content-Type: text/html\r\n\r\n <!DOCTYPE HTML>\r\n <html>\r\n Invalid request! " ;
107
+ s += " \r\n Enter URL as: \" <ip-address>/sw1/<VAL>\" where <VAL> is 0 or 1" ;
108
+ s += " </html>\n " ;
109
+ }
110
+ // correct requests
111
+ else {
112
+ s = " HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n <!DOCTYPE HTML>\r\n <html>\r\n Switch 1 is now " ;
113
+ s += (val)?" ON" :" OFF" ;
114
+ s += " </html>\n " ;
115
+ }
104
116
// Send the response to the client
105
117
client.print (s);
106
118
delay (1 );
@@ -109,4 +121,3 @@ void loop() {
109
121
// The client will actually be disconnected
110
122
// when the function returns and 'client' object is detroyed
111
123
}
112
-
0 commit comments