-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgardenled.ino
224 lines (188 loc) · 6.59 KB
/
gardenled.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
#include <Arduino.h>
#include <WiFi.h>
#include "time.h"
#include "Dusk2Dawn.h"
#include <WebServer.h>
// set wifi credentials
const char* ssid = "xxx";
const char* password = "xxx";
unsigned long previousMillis = 0;
unsigned long interval = 30000;
// set NTP variables
const char* ntpServer = "europe.pool.ntp.org";
// init time variables
int minsToday;
struct tm timeinfo;
int sunChecked;
int GNMrise;
int GNMset;
bool DST = 1;
const int pwmFull = 127;
const int pwmHigh = 32;
const int pwmLow = 1;
const int morning = 7*60;
// setting PWM properties
const int ledPin = 2; // GPIO2
const int freq = 300;
const int ledChannel = 0;
const int resolution = 7;
Dusk2Dawn GNM(53, 7, 1);
// Set web server port number to 80
WebServer server(80);
// Auxiliar variables to store the current output state
bool ledState = LOW;
unsigned long ledTime = 0;
void setup()
{
// Serial.begin(115200);
// configure LED PWM functionalitites
ledcAttach(ledPin, freq, resolution);
//connect to WiFi
// Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println(" CONNECTED");
// Serial.println(WiFi.localIP());
//init and get the time
configTime(3600, DST*3600, ntpServer);
server.on("/xxx", handle_OnConnect);
server.on("/xxx/ledfull", handle_ledFull);
server.on("/xxx/lednormal", handle_ledNormal);
server.on("/xxx/leddemo", handle_ledDemo);
server.on("/xxx/time", handle_time);
server.on("/xxx/bright", handle_bright);
server.onNotFound(handle_NotFound);
server.begin();
// Serial.println("HTTP server started");
blinkDemo(); //end of setup
}
void loop()
{
unsigned long currentMillis = millis();
// if WiFi is down, try reconnecting
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval)) {
// Serial.print(millis());
// Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
previousMillis = currentMillis;
}
getLocalTime(&timeinfo);
if (sunChecked != (timeinfo.tm_mon+1) * (timeinfo.tm_mday+1) && timeinfo.tm_hour > 3) {
if (timeinfo.tm_mon+1 > 3 && timeinfo.tm_mon+1 < 10) {DST = 1;}
else if (timeinfo.tm_mon+1 == 3) {
if (timeinfo.tm_mday >= 25 && timeinfo.tm_mday - (timeinfo.tm_wday + 25) >= 0){DST = 1;}
else {DST = 0;}
}
else if (timeinfo.tm_mon+1 == 10) {
if (timeinfo.tm_mday >= 25 && timeinfo.tm_mday - (timeinfo.tm_wday + 25) >= 0){DST = 0;}
else {DST = 1;}
}
else {DST = 0;}
configTime(3600, DST*3600, ntpServer);
GNMrise = GNM.sunrise(timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday, DST);
GNMset = GNM.sunset(timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday, DST);
sunChecked = (timeinfo.tm_mon+1) * (timeinfo.tm_mday+1);
}
minsToday = timeinfo.tm_hour * 60 + timeinfo.tm_min;
if (ledState == HIGH) {
if (millis() - ledTime < 15*60000) {
ledcWrite(ledPin, pwmFull);
} else {
ledState = LOW;
}
} else {
if (minsToday < GNMrise) {
ledcWrite(ledPin, pwmLow);
}
if (minsToday >= GNMrise && minsToday < GNMset && GNMrise < morning) {
ledcWrite(ledPin, 0);
}
if (minsToday >= morning && minsToday < GNMrise && GNMrise >= morning) {
ledcWrite(ledPin, pwmHigh);
}
if (minsToday >= GNMrise && minsToday < GNMset && GNMrise >= morning) {
ledcWrite(ledPin, 0);
}
if (minsToday >= GNMset) {
ledcWrite(ledPin, pwmHigh);
}
}
server.handleClient();
delay(1000);
}
void handle_OnConnect() {
// Serial.println("Client connect to /");
server.send(200, "text/html", SendHTML(ledState));
}
void handle_ledFull() {
// Serial.println("LEDs tijdelijk vol aan");
ledState = HIGH;
ledTime = millis();
ledcWrite(ledPin, pwmFull);
server.send(200, "text/html", SendHTML(ledState));
}
void handle_ledNormal() {
// Serial.println("LEDs normaal");
ledState = LOW;
server.send(200, "text/html", SendHTML(ledState));
}
void handle_ledDemo() {
// Serial.println("LED Demo");
server.send(200, "text/plain", "Running demo");
blinkDemo();
}
void handle_time() {
String timeString = String(minsToday);
String riseString = String(GNMrise);
String setString = String(GNMset);
String allString = String(timeString + " " + riseString + " " + setString);
server.send(200, "text/plain", allString);
}
void handle_bright() {
String brightString = String(ledcRead(ledPin));
server.send(200, "text/plain", brightString);
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
void blinkDemo(){
for (int i=0; i < 10; i++) {
delay(100);
ledcWrite(ledPin, pwmHigh);
delay(100);
ledcWrite(ledPin, pwmFull);
delay(100);
ledcWrite(ledPin, pwmHigh);
delay(100);
ledcWrite(ledPin, pwmLow);
}
}
String SendHTML(uint8_t ledstat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #3498db;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #3498db;}\n";
ptr +=".button-on:active {background-color: #2980b9;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>Tuinverlichting</h1>\n";
ptr +="<h3>om deze 15 minuten aan te zetten</h3>\n";
if(ledState)
{ptr +="<p>LED Status: Full</p><a class=\"button button-off\" href=\"/xxx/lednormal\">Go normal</a>\n";}
else
{ptr +="<p>LED Status: Normal</p><a class=\"button button-on\" href=\"/xxx/ledfull\">Go full</a>\n";}
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}