-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266_Morning_Light.ino
217 lines (170 loc) · 5.53 KB
/
ESP8266_Morning_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
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <NTPClient.h>
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
void handleRoot(); // function prototypes for HTTP handlers
void handleChangeColor();
void handleLightOn();
void handleLightOff();
void handleAlarmOn();
void handleAlarmOff();
void handleNotFound();
const int redPin = 14; // 13 corresponds to GPIO13
const int greenPin = 12; // 12 corresponds to GPIO12
const int bluePin = 13; // 14 corresponds to GPIO14
unsigned long oldTime = 0;
const unsigned long interval = 1000;
int alarmHours = 7;
int alarmMinutes = 0;
int alarmState = 0;
String red = "255";
String green = "255";
String blue = "255";
// Winter Time
#define NTP_OFFSET 60 * 60 // In seconds
// Summer Time
//#define NTP_OFFSET 60 * 60 * 2 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "europe.pool.ntp.org"
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
DynamicJsonDocument doc(200);
int animationState = 0;
#define animationInterval (500)
unsigned long lastAnimationUpdate = 0;
int rowFrame = 0;
void setup(void){
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
wifiMulti.addAP("SSID", "PASSWORD");
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
server.on("/", HTTP_GET, handleRoot);
server.on("/changeColor", HTTP_POST, handleChangeColor);
server.on("/lightOn", HTTP_GET, handleLightOn);
server.on("/lightOff", HTTP_GET, handleLightOff);
server.on("/alarmOn", HTTP_POST, handleAlarmOn);
server.on("/alarmOff", HTTP_GET, handleAlarmOff);
server.onNotFound(handleNotFound);
server.begin(); // Actually start the server
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
unsigned long currentTime = millis();
if (currentTime - oldTime >= interval) {
timeClient.update();
int hour = timeClient.getHours();
int minutes = timeClient.getMinutes();
Serial.println(timeClient.getFormattedTime());
if (hour == alarmHours && minutes == alarmMinutes && alarmState == 1 && animationState == 0) {
animationState = 1;
}
oldTime = currentTime;
}
if (animationState == 1 && (millis() - lastAnimationUpdate > animationInterval)) {
drawSunriseFrame();
lastAnimationUpdate = millis();
}
}
void handleRoot() {
server.send(200, "text/html", rootHtml());
}
void handleChangeColor() {
if( !server.hasArg("red") || ! server.hasArg("blue") || server.arg("red") == NULL || server.arg("blue") == NULL) {
server.send(400, "text/plain", "400: Invalid Request");
return;
}
red = server.arg("red");
green = server.arg("green");
blue = server.arg("blue");
String data;
JsonObject root = doc.to<JsonObject>();
root["red"] = red;
root["green"] = green;
root["blue"] = blue;
root["message"] = "Color Changed";
serializeJson(doc, data);
changeColor(red, green, blue);
server.send(200, "text/json", data);
}
void handleLightOn() {
turnLightOn();
String data;
JsonObject root = doc.to<JsonObject>();
root["message"] = "Lights on";
serializeJson(doc, data);
server.send(200, "text/html", data);
}
void handleLightOff() {
turnLightOff();
String data;
JsonObject root = doc.to<JsonObject>();
root["message"] = "Lights off";
serializeJson(doc, data);
server.send(200, "text/html", data);
}
void handleAlarmOn() {
if( !server.hasArg("hours") || ! server.hasArg("minutes") || server.arg("hours") == NULL || server.arg("minutes") == NULL) {
server.send(400, "text/plain", "400: Invalid Request");
return;
}
alarmHours = server.arg("hours").toInt();
alarmMinutes = server.arg("minutes").toInt();
alarmState = 1;
rowFrame = 0;
String data;
JsonObject root = doc.to<JsonObject>();
root["message"] = "Alarm set";
serializeJson(doc, data);
server.send(200, "text/json", data);
}
void handleAlarmOff() {
alarmState = 0;
animationState = 0;
rowFrame = 0;
String data;
JsonObject root = doc.to<JsonObject>();
root["message"] = "Alarm off";
serializeJson(doc, data);
server.send(200, "text/json", data);
}
void handleNotFound(){
server.send(404, "text/plain", "404: Not found");
}
void turnLightOn() {
analogWrite(redPin, red.toInt());
analogWrite(greenPin, green.toInt());
analogWrite(bluePin, blue.toInt());
}
void turnLightOff() {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
animationState = 0;
alarmState = 0;
rowFrame = 0;
}
void changeColor(String r, String g, String b) {
analogWrite(redPin, r.toInt());
analogWrite(greenPin, g.toInt());
analogWrite(bluePin, b.toInt());
}