-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSensatio_FW_ESP8266.cpp
281 lines (234 loc) · 6.57 KB
/
Sensatio_FW_ESP8266.cpp
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
/**************************************************************************/
/*!
@file Sensatio_FW_ESP8266.cpp
@author M. Fegerl (Sensate Digital Solutions GmbH)
@license GPL (see LICENSE file)
The Sensatio ESP8266 firmware is used to connect ESP8266 based hardware
with the Sensatio Cloud and the Sensatio apps.
----> https://www.sensatio.io
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
@section HISTORY
v45 - Fixed Pressure Measurement for BME280 Sensors
v44 - More Memory Improvements
v43 - Fixed data transmit issues in configurations with many sensors
v42 - Fixed low memory issues in configurations with many sensors and a ST7735 Bug
v41 - Changed IDE, Sensatio, Renamed Display Class to support more types
v40 - New Display Structure to enable Display Rotation, different Styles etc.
v39 - ReAdded Support for VEML6075 and SI1145 UVI Sensors, added auto-reinit if sensor fails
v38 - Changed automatic Update to only if required Update, removed VEML6075 and SI1145 UV Sensors
v36 - Greatly improved reliability of connectivity
v35 - Added Support for VEML6075 and SI1145 UVI Sensors
v34 - Added Generic Analog Sensor Support
v33 - Added Digital Sensor Switch Support, Improved MQTT Setup Routine
v32 - Added MQTT Support!
v31 - Fixed an issue with DHT11 Sensors
v30 - Added support for SSD1306 I2C Displays
v29 - First Public Release
*/
/**************************************************************************/
#include "Sensatio_FW_ESP8266.h"
VisualisationHelper* vHelper;
Display* display = NULL;
int currentVersion = 45;
boolean printMemory = false;
//String board = "Generic";
//char firmwareType[] = "ESP8266";
//char firmwareType[] = "ESP8266-1M";
String board = "NodeMCU";
char firmwareType[] = "ESP8266-NodeMCU";
// String board = "ESP12s";
// char firmwareType[] = "ESP8266-ESP12s";
// String board = "ESP07";
// char firmwareType[] = "ESP8266-ESP07";
// String board = "D1Mini";
// char firmwareType[] = "ESP8266-D1Mini";
String name = "Bridge";
String ucType = "ESP8266";
String variant = "SensatioV"+String(currentVersion)+board;
String apiVersion = "v1";
int powerMode;
long powerOnDelay = 0;
String powerSavePort;
boolean invPowerSavePort = false;
bool isResetting = false;
unsigned long previousDelayMillis = 0;
unsigned long previousTickerMillis = 0;
unsigned long currentMillis;
State state = Boot;
int displayMode;
boolean displayEnabled;
int displayType;
int displayHeight;
int displayWidth;
int displayRotation;
bool firstSensorData;
extern struct rst_info resetInfo;
extern uint8_t i2cSDAPort;
extern uint8_t i2cSCLPort;
extern MQTT* mqtt;
extern String urlString;
extern String requestDataString;
extern String payload;
#define tickerInterval 250
#define delayInterval 10000
void setup()
{
Serial.begin(9600);
Serial.println("---------------------------");
Serial.println(variant);
Serial.println("---------------------------");
Serial.println("Startup: ");
Serial.println(getUUID()); // @suppress("Invalid arguments")
urlString.reserve(300);
requestDataString.reserve(200);
payload.reserve(1000);
restoreBridgeConfig();
vHelper = new VisualisationHelper();
doPowerSavingInit(true);
Serial.println("Display Type:"+String(displayType));
Serial.println("Display Enabled:"+String(displayEnabled));
Serial.println("Display Rotation:"+String(displayRotation));
Serial.println("Display Mode:"+String(displayMode));
Serial.println("Power Mode:"+String(powerMode));
Serial.println("Power On Delay:"+String(powerOnDelay));
Serial.println("Power Save Port:"+String(powerSavePort));
Serial.println("Power Save Port Inv:"+String(invPowerSavePort));
if(displayType!=0)
{
boolean rotateDisplay = (displayRotation == 180);
switch(displayType)
{
case 3:
display = new DisplayST7735(rotateDisplay, displayType);
break;
default: // Fallback to OLED init
display = new DisplayOLED128(displayWidth, displayHeight, rotateDisplay, displayType,"",i2cSDAPort,i2cSCLPort);
break;
}
if(!displayEnabled)
display->clear(true);
}
if(display!=NULL)
display->drawProductLogo();
state = Connect_WiFi;
initSensate();
}
void loop()
{
if(!isResetting)
{
//First: Do whatever should be done in every loop!
runLoop();
//Then: Do short delayed actions (once every #tickerInterval miliseconds)
currentMillis = millis();
if(currentMillis - previousTickerMillis >= tickerInterval)
{
previousTickerMillis = currentMillis;
runTick();
}
//Then: Do long delayed actions (once every #delayInterval miliseconds)
currentMillis = millis();
if(currentMillis - previousDelayMillis >= delayInterval)
{
previousDelayMillis = currentMillis;
runDelayed();
}
}
}
void runLoop() {
switch(state)
{
case Setup:
loopDNS();
checkWiFiStatus();
case Connected_WiFi:
case Init_Configuration:
case Operating:
loopRestserver();
if(mqtt!=NULL)
mqtt->loop();
break;
default:
break;
}
}
void runTick() {
// Serial.print("-T-");
switch(state)
{
case Operating:
loopDisplay(currentMillis);
loopSensor(currentMillis);
break;
default:
break;
}
if(printMemory)
{
Serial.print("HEAP: ");
Serial.println(ESP.getFreeHeap());
}
if(ESP.getFreeHeap()<1000)
{
Serial.println("RUNNING OUT OF HEAP - RESTART");
restart();
}
}
void runDelayed() {
// Serial.print("----D----");
if(state!=Operating)
initSensate();
else
checkStatus();
}
void initSensate() {
if(state==Connect_WiFi)
{
Serial.println("STATE: Connect_WiFi");
connectToNetwork();
}
if(state==Check_Firmware)
{
if(resetInfo.reason != REASON_DEEP_SLEEP_AWAKE)
{
Serial.println("STATE: Check_Firmware");
tryFirmwareUpdate("");
}
else
{
Serial.println("Skip Check_Firmware (Wake from Sleep)");
state=Connected_WiFi;
}
}
if(state==Connected_WiFi)
{
Serial.println("STATE: Connected_WiFi");
startRestServer();
}
if(state==Init_Setup)
{
Serial.println("STATE: Init_Setup");
startRestServer();
startDNS();
}
if(state==Register)
{
if(resetInfo.reason != REASON_DEEP_SLEEP_AWAKE)
{
Serial.println("STATE: Register_Bridge");
initSSL();
registerBridge();
}
else
{
Serial.println("Skip Register_Bridge (Wake from Sleep)");
initSSL();
state=Init_Configuration;
}
}
if(state==Init_Configuration)
{
Serial.println("STATE: Get_Configuration");
getBridgeConfig();
}
}