-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266_Fan.ino
executable file
·219 lines (191 loc) · 5.12 KB
/
ESP8266_Fan.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
#include <FS.h>
#include <DNSServer.h>
#include <NTPClient.h>
#include <Timezone.h>
#include <time.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include "src/ESP8266_Fan.h"
#include "src/html.h"
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 0, 60000);
Timezone myTZ(myDST, mySTD);
TimeChangeRule *tcr;
// Création des objets / create Objects
ESP8266WebServer server ( 80 );
// Handle request for jsonapi document ("/fan.json")
void apiHandler() {
server.send ( 200, "application/json", jsonApiHandler() );
}
String jsonApiHandler() {
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
JsonObject& fanstate = json.createNestedObject("fanstate");
fanstate["curLocal_time"] = currentTime("local");
fanstate["curUTC_time"] = currentTime("utc");
fanstate["autooff_time"] = autooff_time;
String output;
json.printTo(output);
return output;
}
String indexPage() {
if ( server.hasArg("timer") ) {
checkTimer(server.arg("timer"));
}
return main_page;
}
void checkTimer(String timer) {
if ( timer == "1" ) {
Serial.println("AutoOff in 1 Hour");
autooff_time = currentTime("local") + 3600;
} else if ( timer == "2" ) {
Serial.println("AutoOff in 2 Hours");
autooff_time = currentTime("local") + 3600 * 2;
} else if ( timer == "3" ) {
Serial.println("AutoOff in 4 Hours");
autooff_time = currentTime("local") + 3600 * 4;
} else {
Serial.println("AutoOff at specific time");
//2018/07/14 23:00
//2018-07-14T22:27
unsigned long requested_time = timer.toInt();
autooff_time = myTZ.toLocal(requested_time, &tcr);
}
}
void handleAutoOff() {
if (autooff_time) {
if (autooff_time >= currentTime("local")) {
if (autooff_time == currentTime("local") && triggered == false) {
handlePower();
triggered = true;
}
} else {
triggered = false;
}
}
}
void handleRoot() {
if ( server.hasArg("power") ) {
handlePower();
} else if ( server.hasArg("breeze") ) {
handleBreeze();
} else if ( server.hasArg("speed") ) {
handleSpeed();
} else if ( server.hasArg("swing") ) {
handleSwing();
} else {
server.send ( 200, "text/html", indexPage() );
}
}
void handlePower() {
updateGPIO(0, "1");
delay(200);
updateGPIO(0, "0");
}
void handleBreeze() {
updateGPIO(1, "1");
delay(200);
updateGPIO(1, "0");
}
void handleSpeed() {
if ( server.arg("speed") == "1" ) {
handlePower();
delay(200);
updateGPIO(2, "1");
delay(200);
updateGPIO(2, "0");
} else if ( server.arg("speed") == "2" ) {
handlePower();
delay(200);
updateGPIO(2, "1");
delay(200);
updateGPIO(2, "0");
delay(200);
updateGPIO(2, "1");
delay(200);
updateGPIO(2, "0");
} else if ( server.arg("speed") == "3" ) {
handlePower();
delay(200);
updateGPIO(2, "1");
delay(200);
updateGPIO(2, "0");
delay(200);
updateGPIO(2, "1");
delay(200);
updateGPIO(2, "0");
delay(200);
updateGPIO(2, "1");
delay(200);
updateGPIO(2, "0");
}
}
void handleSwing() {
updateGPIO(3, "1");
delay(200);
updateGPIO(3, "0");
}
long currentTime(String type) {
if (type == "local") {
time_t utc = timeClient.getEpochTime();
time_t local = myTZ.toLocal(utc, &tcr);
return local;
} else if (type == "utc") {
time_t utc = timeClient.getEpochTime();
return utc;
}
}
// format and print a time_t value, with a time zone appended.
void dateTime(time_t t, const char *tz)
{
char buf[32];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
Serial.println(buf);
}
void updateGPIO(int gpio, String DxValue) {
Serial.println("");
Serial.println("Update GPIO "); Serial.print(GPIOPIN[gpio]); Serial.print(" -> "); Serial.println(DxValue);
if ( DxValue == "1" ) {
digitalWrite(GPIOPIN[gpio], HIGH);
server.send ( 200, "text/html", indexPage() );
} else if ( DxValue == "0" ) {
digitalWrite(GPIOPIN[gpio], LOW);
server.send ( 200, "text/html", indexPage() );
} else {
Serial.println("Err Led Value");
}
}
void setup() {
for ( int x = 0 ; x < 5 ; x++ ) {
pinMode(GPIOPIN[x], OUTPUT);
}
Serial.begin ( 115200 );
WiFi.hostname( host );
WiFiManager wifiManager;
wifiManager.setBreakAfterConfig(true);
if (!wifiManager.autoConnect("ESP_Fan")) {
Serial.println("failed to connect, we will fire up config mode");
delay(3000);
wifiManager.startConfigPortal("ESP_Fan");
delay(5000);
}
Serial.print ( "IP address: " ); Serial.println ( WiFi.localIP() );
/*return index page which is stored in serverIndex */
server.on ( "/", handleRoot );
server.on ( "/fan.json", apiHandler );
server.begin();
Serial.println ( "HTTP server started" );
timeClient.begin();
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
timeClient.update();
handleAutoOff();
}