-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChange_Light.ino
328 lines (243 loc) · 6.63 KB
/
Change_Light.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef Pins_Arduino_h
#define Pins_Arduino_h
#include "../generic/common.h"
#define PIN_WIRE_SDA (4)
#define PIN_WIRE_SCL (5)
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
#endif /* Pins_Arduino_h */
#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
}
os_timer_t myTimer;
int lightValue[] = {0,0};
bool tickOccured;
const char* ssid = "";
const char* password = "";
const char* host = "10.0.0.6";
const int httpPort = 80;
void wifiCheck(){
digitalWrite(LED_BUILTIN, HIGH);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(D2, HIGH);
delay(250);
digitalWrite(D2, LOW);
delay(250);
Serial.print(".");
}
}
void wifiConnect(){
delay(100);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
wifiCheck();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
pinMode(D2,OUTPUT);
pinMode(D7,INPUT_PULLUP);
pinMode(D1,INPUT_PULLUP);
pinMode(D3,INPUT_PULLUP);
Serial.begin(115200);
tickOccured = false;
user_init();
wifiConnect();
}
void timerCallback(void *pArg) {
tickOccured = true;
} // End of timerCallback
void user_init(void) {
os_timer_setfn(&myTimer, timerCallback, NULL);
os_timer_arm(&myTimer, 20000, true);
}
void requestStatus(int light, WiFiClient client){
// We now create a URI for the request
digitalWrite(D2, HIGH);
String url = "/api/QYCMf3dLRS0WrN7mORGm7msvEX4HampeoRFiv6Ih/lights/"+String(light);
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close \r\n\r\n");
digitalWrite(D2, LOW);
}
////// Value Checkers
String getJSONResponse(int light, WiFiClient client){
requestStatus(light, client);
while(client.available()){
String line = client.readStringUntil('\r');
if(line.indexOf("state")>=0){
Serial.print("arrived");
delay(50);
return line;
}
}
}
void parseJSONResponse(String line, WiFiClient client){
int active;
if(line.indexOf("\"on\":true")>=0){
active = 1;
}else if(line.indexOf("\"on\":false")>=0){
active = 0;
}
int currentChar = line.indexOf("\"bri\":")+8;
int value = int(line[currentChar]);
int bri[3] = {-1,-1,-1};
int counter = 0;
while (value>47 && value<58){
bri[counter] = value-48;
currentChar +=1;
counter +=1;
value = int(line[currentChar]);
}
int brightness;
if (bri[2] == -1){
if (bri[1] == -1){
brightness = bri[0];
}
else{
brightness = bri[0]*10 + bri[1];
}
}else{
brightness = bri[0]*100+bri[1]*10+bri[2];
}
lightValue[0] = active;
lightValue[1] = brightness;
}
void checkValues(int light, WiFiClient client){
parseJSONResponse(getJSONResponse(light, client),client);
}
////// Value Checkers
////// Sender Function
void sendRequest(int light, String content, WiFiClient client){
String url = "/api/QYCMf3dLRS0WrN7mORGm7msvEX4HampeoRFiv6Ih/lights/"+String(light)+"/state";
if(client.connect(host, httpPort)){
client.print(String("PUT ") + url + " HTTP/1.1\r\n" +
"Connection: close\r\n" +
"Host: " + host + "\r\n" +
"Content-Length: "+content.length()+"\r\n "+
"Content-Type: text/plain;charset=UTF-8\r\n\r\n"+
content);
}
}
// Sender Function END
//////# Toggle Light Function
void lightToggle(int light, WiFiClient client){
if(lightValue[0] == 1){
String content = "{\"on\":false}";
sendRequest(light, content, client);
lightValue[0]=0;
}
else{
String content = "{\"on\":true}";
sendRequest(light, content, client);
lightValue[0]=1;
}
}
//////// Toggle Light Function END
////# Brightness Up Function
void brightnessUp(int light, WiFiClient client){
int bri = lightValue[1]+5;
bri = 254<bri ? 254:bri;
String content = "{\"bri\":"+String(bri)+"}";
sendRequest(light, content, client);
int check2 = digitalRead(D1);
while (check2==LOW){
bri = bri+8;
bri = 254<bri ? 254:bri;
String content = "{\"bri\":"+String(bri)+"}";
sendRequest(light, content, client);
delay(100);
check2 = digitalRead(D1);
}
lightValue[1] = bri;
}
////# Brightness Down Function
void brightnessDown(int light, WiFiClient client){
int bri = lightValue[1]-5;
bri = 0>bri ? 0:bri;
String content = "{\"bri\":"+String(bri)+"}";
sendRequest(light, content, client);
int check3 = digitalRead(D3);
while (check3==LOW){
bri = bri-8;
bri = 0>bri ? 0:bri;
String content = "{\"bri\":"+String(bri)+"}";
sendRequest(light, content, client);
delay(100);
check3 = digitalRead(D3);
}
lightValue[1] = bri;
}
////////////MAIN LOOP///////////
int check = 0;
int check2 = 0;
int check3 = 0;
int currentLight = 0;
int value = 0;
void loop() {
if (tickOccured == true){
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
checkValues(3, client );
if (lightValue[0]==0){
}
tickOccured = false;
}
// put your main code here, to run repeatedly:
check = digitalRead(D7);
if (check == LOW){
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
lightToggle(3, client);
delay(250);
}
check2 = digitalRead(D1);
if (check2 == LOW){
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
brightnessUp(3, client);
delay(250);
}
check3 = digitalRead(D3);
if (check3 == LOW){
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
brightnessDown(3, client);
}
}