This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathufo.ino
370 lines (305 loc) · 12.1 KB
/
ufo.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Requires Arduino IDE 1.6.8 or later
ESP8266 Arduino library v2.3.0 or later
Adafruit Huzzah ESP8266-E12, 4MB flash, uploads with 3MB SPIFFS (3MB filesystem of total 4MB) -- note that SPIFFS upload packages up everything from the "data" folder and uploads it via serial (same procedure as uploading sketch) or OTA. however, OTA is disabled by default
Use Termite serial terminal software for debugging
NOTE
ESP8266 stores last set SSID and PWD in reserved flash area
connect to SSID huzzah, and call http://192.168.4.1/api?ssid=<ssid>&pwd=<password> to set new SSID/PASSWORD
*/
//-------------------------------------------------------------------------------------------------------------------------
boolean debug = true;
//-------------------------------------------------------------------------------------------------------------------------
//#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <FS.h>
#include <EEPROM.h>
#include <Adafruit_DotStar.h>
#include <StreamString.h>
#include "DisplayCharter.h"
#include "MyRequestHandler.h"
#include "DataPolling.h"
#include "Config.h"
#include "defines.h"
//-------------------------------------------------------------------------------------------------------------------------
Adafruit_DotStar ledstrip_logo = Adafruit_DotStar(4, PIN_DOTSTAR_LOGO, PIN_DOTSTAR_CLOCK, DOTSTAR_BGR); //NOTE: in case the colors dont match, check out the last color order parameter
Adafruit_DotStar ledstrip_lowerring = Adafruit_DotStar(RING_LEDCOUNT, PIN_DOTSTAR_LOWERRING, PIN_DOTSTAR_CLOCK, DOTSTAR_BRG);
Adafruit_DotStar ledstrip_upperring = Adafruit_DotStar(RING_LEDCOUNT, PIN_DOTSTAR_UPPERRING, PIN_DOTSTAR_CLOCK, DOTSTAR_BRG);
DisplayCharter displayCharter_lowerring;
DisplayCharter displayCharter_upperring;
IPDisplay ipDisplay;
Config dTConfig(debug);
DataPolling dataPolling(&displayCharter_lowerring, &displayCharter_upperring, &ipDisplay, &dTConfig, debug);
boolean logo = true;
ESP8266WebServer httpServer(80);
boolean wifiStationOK = false;
boolean wifiStationConnected = false;
unsigned int connectedCount = 0;
boolean wifiAPisConnected = false;
boolean wifiConfigMode = true;
boolean httpClientMode = false;
//-------------------------------------------------------------------------------------------------------------------------
//format bytes
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
// Handle Factory reset and enable Access Point mode to enable configuration
// *********TODO ********************************** DIFFERENTIATE LONG PRESS (reset all!) VS SHORT PRESS (exit client only mode to enable back website)
void handleFactoryReset() {
if (digitalRead(PIN_FACTORYRESET)) {
if (millis() < 10000) { // ignore reset button during first 10 seconds of reboot to avoid looping resets due to fast booting of ESP8266
return;
}
if (debug) {
Serial.println(F("*********FACTORYRESET***********"));
//WiFi.printDiag(Serial);
}
//disable dynatrace polling mode - otherwise the IP display wont be visible
dTConfig.enabled = false;
dTConfig.Write();
WiFi.disconnect(false); //disconnect and disable station mode; delete old config
// default IP address for Access Point is 192.168.4.1
//WiFi.softAPConfig(IPAddress(192,168,4,1), IPAddress(192,168,4,1), IPAddress(255,255,255,0)); // IP, gateway, netmask -- NOT STORED TO FLASH!!
WiFi.softAP(String(DEFAULT_APSSID).c_str()); // default is open Wifi
// reset eeprom to default hostname
eepromSet(DEFAULT_HOSTNAME);
//WiFi.mode(WIFI_AP);
if (debug) {
Serial.println(String(F("Wifi reset to SSID: ")) + WiFi.SSID() + String(F(" pass: ")) + WiFi.psk());
Serial.print(F("Wifi config mode enabled: Access point enabled at open Wifi SSID: "));
Serial.println(DEFAULT_APSSID);
Serial.println(F("Restarting...."));
//WiFi.printDiag(Serial);
Serial.flush();
}
ESP.restart();
}
}
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case WIFI_EVENT_STAMODE_GOT_IP:
if (debug) Serial.println(String(F("WiFi connected. IP address: ")) + String(WiFi.localIP().toString()) + String(F(" hostname: ")) + WiFi.hostname() + String(F(" SSID: ")) + WiFi.SSID());
wifiStationOK = true;
ipDisplay.ShowIp(WiFi.localIP().toString(), &displayCharter_lowerring);
break;
case WIFI_EVENT_STAMODE_DISCONNECTED:
if (debug) Serial.println(F("WiFi client lost connection"));
wifiStationOK = false;
wifiStationConnected = false;
break;
case WIFI_EVENT_STAMODE_CONNECTED:
if (debug) Serial.println(F("WiFi client connected"));
wifiStationConnected = true;
connectedCount = 0;
break;
case WIFI_EVENT_STAMODE_AUTHMODE_CHANGE:
if (debug) Serial.println(F("WiFi client authentication mode changed."));
break;
//case WIFI_STAMODE_DHCP_TIMEOUT: THIS IS A NEW CONSTANT ENABLE WITH D SDK
// Serial.println("WiFi client DHCP timeout reached.");
//break;
case WIFI_EVENT_SOFTAPMODE_STACONNECTED:
if (debug) Serial.println(String(F("WiFi accesspoint: new client connected. Clients: ")) + String(WiFi.softAPgetStationNum()));
if (WiFi.softAPgetStationNum() > 0) {
wifiAPisConnected = true;
}
break;
case WIFI_EVENT_SOFTAPMODE_STADISCONNECTED:
if (debug) Serial.println(String(F("WiFi accesspoint: client disconnected. Clients: ")) + String(WiFi.softAPgetStationNum()));
if (WiFi.softAPgetStationNum() > 0) {
wifiAPisConnected = true;
} else {
wifiAPisConnected = false;
}
break;
case WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED:
//Serial.println("WiFi accesspoint: probe request received.");
break;
}
}
void printSpiffsContents() {
if (debug)
{
Dir dir = SPIFFS.openDir(F("/"));
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Serial.printf(String(F("FS File: %s, size: %s\n")).c_str(), fileName.c_str(), formatBytes(fileSize).c_str());
}
Serial.printf("\n");
}
}
void setupSerial() {
// checking availability of serial connection
int serialtimeout = 5000; //ms
Serial.begin ( 115200 );
while (!Serial) {
if (serialtimeout > 0) {
//serialtimeout -= 50;
} else {
debug = false;
break;
}
delay(50);
}
if (debug) {
Serial.println(F(""));
Serial.println(F(""));
Serial.println(F(""));
Serial.println(F("Welcome to Dynatrace UFO!"));
Serial.print(F("UFO Firmware Version: "));
Serial.println(FIRMWARE_VERSION);
Serial.println(String(F("ESP8266 Bootversion: ")) + String(ESP.getBootVersion()));
Serial.println(String(F("ESP8266 SDK Version: ")) + String(ESP.getSdkVersion()));
Serial.println(String(F("Resetinfo: ")) + ESP.getResetInfo());
}
}
String eepromRead() {
//EEPROM access to retreive the hostname
EEPROM.begin(EEPROM_MAXSIZE); // note this allocates a buffer in RAM
int address = 0;
//char eepromcontent[EEPROM_MAXSIZE];
String eepromcontent;
while (address < EEPROM_MAXSIZE) {
char val = EEPROM.read(address);
//eepromcontent[address] = val;
if (!val) {
break;
}
eepromcontent += val;
address++;
}
if (debug) {
Serial.println("EEPROM bytes: " + String(address) + " data: " + eepromcontent);
}
if (address >= EEPROM_MAXSIZE) { // no term zero found, so reset the EEPROM to the default
EEPROM.write(0, 'u');
EEPROM.write(1, 'f');
EEPROM.write(2, 'o');
for (address = 3; address < EEPROM_MAXSIZE; address++) {
EEPROM.write(address, 0);
}
eepromcontent = "";
}
EEPROM.end(); // commits EEPROM contents to flash if changed/written; releases allocated memory
return eepromcontent;
}
void eepromSet(String content) {
EEPROM.begin(EEPROM_MAXSIZE); // note this allocates a buffer in RAM
// write string content
int len = content.length();
if (len >= EEPROM_MAXSIZE) {
len = EEPROM_MAXSIZE-1;
}
int addr = 0;
while (addr < len) {
EEPROM.write(addr, content.charAt(addr));
addr++;
}
// fill the remaining eeprom buffer with zeros
while (addr < EEPROM_MAXSIZE) {
EEPROM.write(addr, 0);
addr++;
}
EEPROM.end(); // commits EEPROM contents to flash if changed/written; releases allocated memory
}
// initialization routines
void setup ( void ) {
setupSerial();
pinMode(PIN_FACTORYRESET, INPUT); //, INPUT_PULLUP); use INPUT_PULLUP in case we put reset to ground; currently reset is doing a 3V signal
ledsSetup();
String hostname = eepromRead(); // retrieves the hostname ; DEFAULT_HOSTNAME
// initialize ESP8266 file system
SPIFFS.begin();
printSpiffsContents();
// load non-wifi config from SPIFFS config.json file
if (dTConfig.Read()) {
if (debug) Serial.println(String(F("Dynatrace envid: ")) + dTConfig.dynatraceEnvironmentID + String(F(" apikey: ")) + dTConfig.dynatraceApiKey);
} else {
if (debug) Serial.println(F("Loading Dynatrace config failed!"));
}
// initialize Wifi based on stored config
WiFi.onEvent(WiFiEvent);
WiFi.hostname(hostname); // note, the hostname is not persisted in the ESP config like the SSID. so it needs to be set every time WiFi is started
wifiConfigMode = WiFi.getMode() & WIFI_AP;
if (debug) {
Serial.println(String(F("Connecting to Wifi SSID: ")) + WiFi.SSID() + String(F(" as host ")) + WiFi.hostname());
if (wifiConfigMode) {
Serial.println(String(F("WiFi Configuration Mode - Access Point IP address: ")) + WiFi.softAPIP().toString());
}
//WiFi.printDiag(Serial);
}
httpServer.addHandler(new MyFunctionRequestHandler(&displayCharter_lowerring, &displayCharter_upperring, &ipDisplay, &ledstrip_logo, &dTConfig, debug));
httpServer.begin();
}
unsigned int tick = 0;
unsigned long startMillis = millis();
void loop ( void ) {
tick++;
handleFactoryReset();
if (!wifiConfigMode && wifiStationOK){
unsigned long m = millis();
if (m - startMillis > dTConfig.pollingIntervalS * 1000){
startMillis = m;
dataPolling.Poll();
}
ipDisplay.ProcessTick();
}
httpServer.handleClient();
displayCharter_lowerring.Display(ledstrip_lowerring);
displayCharter_upperring.Display(ledstrip_upperring);
yield();
// show AP mode in blue to tell user to configure WIFI; especially after RESET
// blinking alternatively in blue when no client is connected to AP;
// binking both rings in blue when at least one client is connected to AP
if (wifiConfigMode) {
// AG-04-05-2017: if a client is connected do no longer show the blue lights - that prevents the Demo Use Case when somebody connects to the "ufo" hotspot and changes colors
if (wifiAPisConnected) {
}
/*if (wifiAPisConnected && (tick % 500 > 200)) {
dotstarSetColor(ledstrip_upperring, 0, 0, 255);
dotstarSetColor(ledstrip_lowerring, 0, 0, 255);
}*/ else {
if (tick % 1000 > 500) {
dotstarSetColor(ledstrip_upperring, 0, 0, 255);
dotstarSetColor(ledstrip_lowerring, 0, 0, 0);
} else {
dotstarSetColor(ledstrip_lowerring, 0, 0, 255);
dotstarSetColor(ledstrip_upperring, 0, 0, 0);
}
}
} else { // blink yellow while not connected to wifi when it should
if (!wifiStationOK)
{
unsigned int i = tick % 500;
if (!i)
{
if (wifiStationConnected && (++connectedCount >= 50))
{
if (debug) Serial.println(F("Could not get IP - restarting!"));
ESP.restart();
}
}
else if (i > 375)
{
dotstarSetColor(ledstrip_upperring, 255, 200, 0);
dotstarSetColor(ledstrip_lowerring, 255, 200, 0);
}
}
}
yield();
ledstrip_logo.show();
ledstrip_upperring.show();
ledstrip_lowerring.show();
}