-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX10mini_wifi.ino
213 lines (181 loc) · 7.02 KB
/
X10mini_wifi.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Inspired by: EasyloT sketch tweeked by Barnabybear - original @
* http://iot-playground.com/2-uncategorised/40-esp8266-wifi-relay-switch-arduino-ide
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request.
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include "X10RF.h"
#include <ESP8266WiFi.h>
//#define LED LED_BUILTIN
#define X10 5
X10RF moduleX10RF(X10);
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(8001);
const char* ssid = "***************";
const char* password = "*******************";
// Static address. Update these with values suitable for your network.
IPAddress ip(192,168,1,68); // Node static local IP
IPAddress gateway(192,168,1,254); // your router local address
IPAddress subnet(255,255,255,0); // subnet mask
String state1 = "UNKNOWN";
String state2 = "UNKNOWN";
String state4 = "UNKNOWN";
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
//pinMode(2, OUTPUT);
//digitalWrite(2, 1); // turn LED off
// prepare GPIO5 FOR X10
pinMode(X10, OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet); // used if static address
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.print("Server started @ ");
Serial.println(WiFi.localIP());
Serial.println("");
// Blink the LED
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
delay(100);
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
/////////// device 1////////////////
if (req.indexOf("/a/1/off") != -1)
val = 10;
else if (req.indexOf("/a/1/on") != -1)
val = 11;
else if (req.indexOf("/a/1/bright") != -1)
val = 12; //(!digitalRead(X10));
else if (req.indexOf("/a/1/dim") != -1)
val = 13; //(digitalRead(X10));
//////////// device 2///////////////
else if (req.indexOf("/a/2/off") != -1)
val = 20;
else if (req.indexOf("/a/2/on") != -1)
val = 21;
else if (req.indexOf("/a/2/bright") != -1)
val = 22;
else if (req.indexOf("/a/2/dim") != -1)
val = 23;
//////////// device 4///////////////
else if (req.indexOf("/a/4/off") != -1)
val = 40;
else if (req.indexOf("/a/4/on") != -1)
val = 41;
else if (req.indexOf("/a/4/bright") != -1)
val = 42;
else if (req.indexOf("/a/4/dim") != -1)
val = 43;
//////////// homepage///////////////
else if (req.indexOf("/") != -1)
val = 0;
else {
Serial.println("Invalid Request");
client.print("HTTP/1.1 404\r\n");
client.stop();
return;
}
// Set GPIO5 (X10) according to the request
/////////////// device 1 ////////////////
if (val == 11)
{moduleX10RF.x10_sendcmd('A', 1, ON) ; state1 = "ON";}
else if (val == 10)
{ moduleX10RF.x10_sendcmd('A', 1, OFF) ; state1 = "OFF";}
else if (val == 12)
{ moduleX10RF.x10_sendcmd('A', 1, BRIGHT) ; state1 = "ON";}
else if (val == 13)
{ moduleX10RF.x10_sendcmd('A', 1, DIM) ; state1 = "ON";}
////////////// device 2 ////////////////
else if (val == 21)
{ moduleX10RF.x10_sendcmd('A', 2, ON) ; state2 = "ON";}
else if (val == 20)
{ moduleX10RF.x10_sendcmd('A', 2, OFF) ; state2 = "OFF";}
else if (val == 22)
{ moduleX10RF.x10_sendcmd('A', 2, BRIGHT) ; state2 = "ON";}
else if (val == 23)
{ moduleX10RF.x10_sendcmd('A', 2, DIM) ; state2 = "ON";}
////////////// device 4 /////////////////
else if (val == 41)
{ moduleX10RF.x10_sendcmd('A', 4, ON) ; state4 = "ON";}
else if (val == 40)
{ moduleX10RF.x10_sendcmd('A', 4, OFF) ; state4 = "OFF";}
else if (val == 42)
{ moduleX10RF.x10_sendcmd('A', 4, BRIGHT) ; state4 = "ON";}
else if (val == 43)
{ moduleX10RF.x10_sendcmd('A', 4, DIM) ; state4 = "ON";}
client.flush();
// Prepare the response (= HTML page)
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n ";
s += "<head><title>NodeMCU Web Server (Beta)</title></head>";
s += "<body style=\"font-family:\'Georgia\'; background-image: url(\'http://stockfresh.com/files/k/karenr/m/86/6303549_stock-photo-teal-white-and-gray-polka-dot-tile-pattern-repeat-background.jpg\');\">";
s += "<div style=\'background-color:lightcyan; color:black; text-align:center; padding:10px; margin:40px;\'>";
s += "<center><h1>NodeMCU Web Server (Beta)</h1>";
s += "<img src=\"https://cdn4.iconfinder.com/data/icons/vecico-connectivity/288/wifi_Logo-128.png\"></center>";
s += "</div>";
s += "<div style=\'background-color:lightcyan; color:black; text-align:center; padding:10px; margin:40px;\'>";
s += "<h1>Device 1<br>";
s += "<a href=\"/a/1/on\">ON</a> | ";
s += "<a href=\"/a/1/off\">OFF</a><br>";
s += "</h1>";
s += "<h3>Description: Plafonnier<br>";
s += "Max Power: 175 W<br>";
s += "State: " + state1 + "</h3>";
s += "</div>";
s += "<div style=\'background-color:lightcyan; color:black; text-align:center; padding:10px; margin:40px;\'>";
s += "<h1>Device 2<br>";
s += "<a href=\"/a/2/on\">ON</a> | ";
s += "<a href=\"/a/2/off\">OFF</a> | ";
s += "<a href=\"/a/2/bright\">UP</a> | ";
s += "<a href=\"/a/2/dim\">DOWN</a><br>";
s += "</h1>";
s += "<h3>Description: Table de nuit<br>";
s += "Max Power: 180 W<br>";
s += "State: " + state2 + "</h3>";
s += "</div>";
s += "<div style=\'background-color:lightcyan; color:black; text-align:center; padding:10px; margin:40px;\'>";
s += "<h1>Device 4<br>";
s += "<a href=\"/a/4/on\">ON</a> | ";
s += "<a href=\"/a/4/off\">OFF</a> | ";
s += "<a href=\"/a/4/bright\">UP</a> | ";
s += "<a href=\"/a/4/dim\">DOWN</a><br>";
s += "</h1>";
s += "<h3>Description: Jardin<br>";
s += "Max Power: 150 W<br>";
s += "State: " + state4 + "</h3>";
s += "</div>";
s += "</body></html>\n";
// Send the response to the client
client.print(s);
delay(10);
//Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}