-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patha90_main.ino
224 lines (183 loc) · 7.4 KB
/
a90_main.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
/*******************************************************************************
# #
# LYT-SCRIPTED, an alternative firmware for LYT8266 RGBW LED lamps #
# #
# #
# Copyright (C) 2017 Tom Stöveken #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
*******************************************************************************/
#include <ESP8266HTTPClient.h>
#include <Ticker.h>
Ticker timer1, timer2;
bool checkURL = true;
/******************************************************************************
Description.: read config file from SPIFFS
Input Value.: -
Return Value: true if config read, false in case of error
******************************************************************************/
bool loadConfig() {
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile || (configFile.size() > 1024)) {
g_hostname = "LYT8266";
g_remoteurl = "http://lyt.naaa.de/";
state = CONSTANTCOLOR;
g_red = 255;
g_green = 0;
g_blue = 0;
g_delay_before_going_remotecontrolled = 0;
g_send_WLAN_keep_alive_packet = true;
return false;
}
int size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
StaticJsonBuffer<500> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success()) {
return false;
}
g_hostname = strdup(json["hostname"]);
g_remoteurl = strdup(json["remoteurl"]);
/* read state from config file, will be UNDEF if string from config is not found */
state = UNDEF;
for(int i=0; i<LENGTH_OF(state_map); i++) {
if(state_map[i].state_as_string == json["state"]) {
state = state_map[i].state;
break;
}
}
// depending on the value of variable g_delay_before_going_remotecontrolled
// this will be reverted after the specified amount of seconds
if(state == REMOTEURL) {
state = REMOTEURL_POSTPONED;
}
g_delay_before_going_remotecontrolled = json["delay_before_going_remotecontrolled"];
g_send_WLAN_keep_alive_packet = json["send_WLAN_keep_alive_packet"];
g_red = json["r"];
g_green = json["g"];
g_blue = json["b"];
return true;
}
/******************************************************************************
Description.: Access an URL and set the passed variables according to the
values specified at the URL
Input Value.: references to RGB variables, the variables are set if URL is read
Return Value: true if URL read, false in case of errors
******************************************************************************/
bool loadURL(String URL, uint8_t& r, uint8_t& g, uint8_t& b) {
HTTPClient http;
http.begin(URL.c_str());
int httpCode = http.GET();
String payload;
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Log("[HTTP] GET... code: " + String(httpCode));
// file found at server
if(httpCode == HTTP_CODE_OK) {
payload = http.getString();
Log("Payload: " + payload);
}
} else {
Log("[HTTP] GET... failed, error: " + http.errorToString(httpCode));
}
http.end();
DynamicJsonBuffer jsonBuffer;
// accessing the char array is not intended use, but ArduinoJSON works with the string in place
// so it should be fine
JsonObject& json = jsonBuffer.parseObject(payload.c_str());
if (!json.success()) {
return false;
}
r = json["r"];
g = json["g"];
b = json["b"];
return true;
}
/******************************************************************************
Description.: prepares everything, sets the light up first and then does the
more time consuming tasks
Input Value.: -
Return Value: -
******************************************************************************/
void setup(void){
state = BOOTUP;
Serial.begin(115200);
Serial.println("LYT8266 starting up\nCompiled at: " __DATE__ " - " __TIME__);
log_messages.resize(LOG_LENGTH, "-");
Log("LYT8266 starting up");
Log("Compiled at: " __DATE__ " - " __TIME__);
if( !SPIFFS.begin() ) {
Log("SPIFFS not mounted correctly, retrying...");
delay(1000);
if( !SPIFFS.begin() ) {
Log("mounting failed twice, formatting and then restarting");
SPIFFS.format();
ESP.restart();
} else {
Log("SPIFFS mounted at second try, proceeding as usual");
}
}
// read configuration file
bool r = loadConfig();
Log("loadConfig() --> result: "+String(r));
// apply hostname
wifi_station_set_hostname((char *)g_hostname.c_str());
setup_LEDs();
setup_wifi();
setup_webserver();
// setup timer (Ticker) to check every 60 seconds an URL for color values
timer1.attach(60.0, [](){
checkURL = true;
});
// Count down value if URL check is to be postponed
timer2.attach(1.0, [](){
// postpone URL check if a delay was set
if (g_delay_before_going_remotecontrolled > 0) {
Log("Postponed URL check, because g_delay_before_going_remotecontrolled is "+ String(g_delay_before_going_remotecontrolled));
if(state == REMOTEURL) {
state = REMOTEURL_POSTPONED;
}
g_delay_before_going_remotecontrolled -= 1;
return;
}
// if control reaches this point the delay has passed, clean up and stop this timer
g_delay_before_going_remotecontrolled = 0;
if(state == REMOTEURL_POSTPONED) {
state = REMOTEURL;
}
// job is done, remove this lambda function from timer2
timer2.detach();
});
}
/******************************************************************************
Description.: execute the different subtasks, none may block or not return
Input Value.: -
Return Value: -
******************************************************************************/
void loop(void){
loop_wifi();
loop_webserver();
loop_LEDs();
if(state == REMOTEURL && checkURL) {
loadURL(g_remoteurl, g_red, g_green, g_blue);
checkURL = false;
}
delay(1);
}