-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsp_lights_RBK.ino
250 lines (223 loc) · 6.66 KB
/
Esp_lights_RBK.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
/* ~ SmartLights ~ -> ESP8266 and LED strip
╦═╗╔╗ ╦╔═
By-- ╠╦╝╠╩╗╠╩╗ 2014 Rakshith BK
╩╚═╚═╝╩ ╩
*
* No licences no copyrights, well may be MIT.
* Use modify distribute and do whatever you want
* And even though this code will not launch any Nukes,
* I'm not responsible for any destruction caused by it.!
*
* For Details and working -- https://www.instructables.com/id/SmartLights-ESP8266-and-Led-Strip/
*/
#include <ESP8266WiFi.h>
//#include <ESP8266.h>
//////////////////// enter your details here /////////////////////////////////
const char* ssid = "--your wifi name--";
const char* password = "--wifi password--";
const char* channel_id = "--your channel id--"; //you will find this id and key
const char* apiKey = "--your api key--"; // in your thingspeak account.
/////////////////////////////////////////////////////////////////////////////
const char* host = "api.thingspeak.com";
const int httpPort = 80;
int led = 2 ; // ESP01 Pin 2 for output
String line1, line2 ;
String url1, url2 ;
int action = 0 ;
unsigned long latency = 0 ;
void check_field1() ;
void check_field2() ;
void mail();
void Strip_out();
void facebook();
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
pinMode(led, OUTPUT);
analogWrite(led, 0);
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
url1 = "/channels/";
url1 += channel_id;
url1 += "/fields/1/last.txt?api_key=";
url1 += apiKey;
url2 = "/channels/";
url2 += channel_id;
url2 += "/fields/2/last.txt?api_key=";
url2 += apiKey;
delay(1000);
check_field1() ;
Serial.println("initializing strip..!") ;
Strip_out() ;
}
////////////////////////////////////////////////////////////////
void loop() {
// checks online every 5 minutes
//// 60,000 x 5 = 5 minutes
if((millis() - latency > 60000*5)) {
latency = millis();
check_field2();
delay(300);
check_field1();
yield();
}
}
///////////////////////////////////////////////////////////////////////
// check_field1 -> This is for listening for basic commands from IFTTT
// These include ON, OFF and brightness controll.
// It also checks for weather and changes the lights accordingly..!
//////////////////////////////////////////////////////////////////////
void check_field1() {
client.connect(host, httpPort);
client.print(String("GET ") + url1 + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: SmartLights-ESP8266\r\n"+
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
while(client.available()){
line1 = client.readStringUntil('\r');
if(line1.indexOf("Dim") > 0) {
action = 1 ;
}
else if(line1.indexOf("Bright") > 0){
action = 2 ;
}
else if(line1.indexOf("ON")>0) {
action = 3 ; // turn on lights
}
else if(line1.indexOf("OFF")>0) {
action = 4 ; // turn off lights
}
else if(line1.indexOf("Rain")>0) {
action = 5 ; //slow dim-glow of strip
}
}
Strip_out() ;
}
////////////////////////////////////////////////////////////
// Check_field2 -> looks for any notification
// from connected social media.
////////////////////////////////////////////////////////////
void check_field2() {
client.connect(host, httpPort);
client.print(String("GET ") + url2 + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: SmartLights-ESP8266\r\n" +
"Connection: close\r\n\r\n");
unsigned long Timeout = millis();
while (client.available() == 0) {
if (millis() - Timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
line2 = client.readStringUntil('\r');
if(line2.indexOf("Mail") > 0){
Mail();
Serial.println("notification -- mail");
}
else if(line2.indexOf("Facebook")>0) {
facebook();
Serial.println("notification -- facebook");
}
}
}
///////////////////////////////////////////////////////////////
void Mail() {
int i =0 ;
for(i=0; i<=1023; i=i+40) {
analogWrite(led, i);
delay(100) ;
}
for(i=1023; i>=0; i=i-40) {
analogWrite(led, i);
delay(100) ;
}
Strip_out();
}
///////////////////////////////////////////////////////////
void facebook() {
int i=0;
for(i=0; i<=1023; i=i+20) {
analogWrite(led, i);
delay(50);
}
analogWrite(led,0);
delay(250);
analogWrite(led,1023);
delay(250);
analogWrite(led,0);
delay(250);
for(i=1023; i>=0; i=i-40) {
analogWrite(led, i);
delay(100);
}
Strip_out();
}
////////////////////////////////////////////////////////////////
void Strip_out() {
while(action == 0) { //This action will be active if there is a problem
analogWrite(led, 0); //in connecting to the WiFi or the server.
delay(700);
analogWrite(led, 512);
delay(700);
analogWrite(led, 1023);
delay(700);
if( millis() - latency > 60000) {
latency = millis();
Serial.println("Something went wrong.!");
Serial.println("Please check your server details.!");
check_field1() ;
}
}
if(action == 1) {
analogWrite(led,512);// For DIM settings
}
else if(action == 2) {
analogWrite(led, 768); // For Bright settings
}
else if(action == 3) {
analogWrite(led, 1023);// For ON
}
else if(action == 4) {
analogWrite(led, 0);// For OFF
}
while(action == 5) {// For rain
int i ;
for(i=0; i<=1023; i++) {
analogWrite(led, i);
delay(3);
}
for(i=1023; i>=0; i--) {
analogWrite(led, i) ;
delay(3);
}
if( millis() - latency > 60000) {
latency = millis() ;
check_field1() ;
}
}
}
////////////////////////////////////////////////////////////////