-
Notifications
You must be signed in to change notification settings - Fork 1
/
precipitationSensorESP32.ino
348 lines (300 loc) · 10.3 KB
/
precipitationSensorESP32.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
/***************************************************************************\
*
* Arduino project "precipitationSensor" © Copyright [email protected]
*
* 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, either version 3 of the License, or
* (at your option) any later version. 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 received a copy of the GNU General Public License along with this
* program in file 'License.txt'.
* Additional information about licensing can be found at:
* http://www.gnu.org/licenses
*
* IDE download : https://www.arduino.cc/en/Main/Software
* FHEM forum thread : https://forum.fhem.de/index.php/topic,73016.0.html
* Contact : [email protected]
*
* Project description:
* ====================
* This project uses an radar sensor modul a preamp and an ESP32 to classify the type
* of precipitation/hydrometeors by measuring the speed/doppler frequency.
*
* The recommended radar sensor modules are the IPM-170 and RSM-1700 because of their
* radial symmetric radiation pattern.T
* The IPM-165 or CDM-324 may be used instead, if direction independency does not matter.
*
* ADC sampling rate: 40960 sps (2x oversampling)
* Sampling period: 50 ms
* FFT points/samples: 1024
* FFT resolution: 20 Hz
* FFT frequency range: DC...10220 Hz (limited by LPF to 8 kHz)
*
* Arduino IDE board settings:
* ===========================
* ESP32 CPU clock speed: 80 MHz
*
* Pins used:
* ==========
* 32 ... 39 Senor (ADC) (one of them, defined on the setup page)
* 27 Watchdog
* 21 SDA + 4k7 PU BME280
* 22 SCL + 4k7 PU BME280
*
* Version history:
* ================
* See: https://github.com/chunter1/precipitationSensorESP32/commits/master
*
\***************************************************************************/
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include "GlobalDefines.h"
#include "StateManager.h"
#include "Settings.h"
#include "WebFrontend.h"
#include "AccessPoint.h"
#include "Tools.h"
#include "Watchdog.h"
#include <ArduinoOTA.h>
#include "OTAUpdate.h"
#include "Update.h"
#include "Publisher.h"
#include "SensorData.h"
#include "DataPort.h"
#include "Statistics.h"
#include "SigProc.h"
#include "ConnectionKeeper.h"
#include "Wire.h"
#include "BME280.h"
StateManager stateManager;
Settings settings;
WebFrontend frontend(80, &settings);
AccessPoint accessPoint(IPAddress(192, 168, 222, 1), IPAddress(192, 168, 222, 1), IPAddress(255, 255, 225, 0), "ps");
Watchdog watchdog;
OTAUpdate ota;
Publisher publisher;
SensorData sensorData(NR_OF_BINS, NR_OF_BIN_GROUPS);
DataPort dataPort;
Statistics statistics;
SigProc sigProc;
ConnectionKeeper connectionKeeper;
BME280 bme280;
float thresholdOffset;
float countThreshold;
byte adcPin;
//uint mountingAngle;
void TryConnectWIFI(String ctSSID, String ctPass, byte nbr, uint timeout, String staticIP, String staticMask, String staticGW, String hostName) {
if (ctSSID.length() > 0 && ctSSID != "---") {
unsigned long startMillis = millis();
WiFi.disconnect(true);
WiFi.begin(ctSSID.c_str(), ctPass.c_str());
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(false);
if (staticIP.length() > 7 && staticMask.length() > 7) {
if (staticGW.length() < 7) {
WiFi.config(Tools::IPAddressFromString(staticIP), Tools::IPAddressFromString(staticGW), (uint32_t)0);
}
else {
WiFi.config(Tools::IPAddressFromString(staticIP), Tools::IPAddressFromString(staticGW), Tools::IPAddressFromString(staticMask));
}
}
delay(50);
Serial.println("Hostname: " + hostName);
WiFi.setHostname(hostName.c_str());
Serial.println("Connect " + String(timeout) + " seconds to an AP (SSID " + String(nbr) + ")");
digitalWrite(DEBUG_GPIO_ISR, HIGH);
byte retryCounter = 0;
while (retryCounter < timeout * 2 && WiFi.status() != WL_CONNECTED) {
retryCounter++;
delay(500);
Serial.print(".");
digitalWrite(DEBUG_GPIO_ISR, retryCounter % 2 == 0);
watchdog.Handle();
}
if (WiFi.status() == WL_CONNECTED) {
stateManager.SetWiFiConnectTime((millis() - startMillis) / 1000.0);
}
digitalWrite(DEBUG_GPIO_ISR, LOW);
}
}
static bool StartWifi(Settings *settings) {
bool result = false;
Serial.println("Start WIFI_STA");
String hostName = settings->Get("HostName", "precipitationSensor");
Serial.print("HostName is: ");
Serial.println(hostName);
stateManager.SetHostname(hostName);
String staticIP = settings->Get("StaticIP", "");
String staticMask = settings->Get("StaticMask", "");
String staticGW = settings->Get("StaticGW", "");
if (staticIP.length() < 7 || staticMask.length() < 7) {
Serial.println("Using DHCP");
}
else {
Serial.println("Using static IP");
Serial.print("IP: ");
Serial.println(staticIP);
Serial.print("Mask: ");
Serial.println(staticMask);
Serial.print("Gateway: ");
Serial.println(staticGW);
}
TryConnectWIFI(settings->Get("ctSSID1", DEFAULT_SSID), settings->Get("ctPASS1", DEFAULT_PASSWORD), 1, settings->GetInt("ctTO1", 15), staticIP, staticMask, staticGW, hostName);
String ctSSID2 = settings->Get("ctSSID2", "---");
if (WiFi.status() != WL_CONNECTED && ctSSID2.length() > 0 && ctSSID2 != "---") {
TryConnectWIFI(ctSSID2, settings->Get("ctPASS2", "---"), 2, settings->GetInt("ctTO2", settings->GetInt("ctTO2", 15)), staticIP, staticMask, staticGW, hostName);
}
if (WiFi.status() == WL_CONNECTED) {
result = true;
Serial.println();
Serial.println("connected :-)");
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP: ");
Serial.println(WiFi.localIP().toString());
}
else {
Serial.println();
Serial.println("We got no connection :-(");
// Open access point for 15 minutes
accessPoint.Begin(900);
}
Serial.println("Starting frontend");
frontend.SetPassword(settings->Get("FrontPass1", ""));
frontend.Begin(&stateManager, &bme280);
Serial.println("Starting OTA");
ota.Begin(frontend.GetWebServer(), [] {
sigProc.StopCapture();
});
Serial.println("Starting data port");
dataPort.Begin(81);
return result;
}
void HandleCriticalAction(bool isCritical) {
if (isCritical) {
sigProc.StopCapture();
}
else {
sigProc.StartCapture();
}
}
void setup() {
uint32_t startProcessTS;
Serial.begin(115200);
delay(50);
Serial.println();
Serial.println("Chip ID: " + Tools::GetChipId());
Serial.println("Chip Revision: " + Tools::GetChipRevision());
watchdog.Begin(27, 1);
// Get the settings
settings.Begin([](bool isCritical) {
HandleCriticalAction(isCritical);
});
settings.BaseData.NrOfBins = NR_OF_BINS;
settings.BaseData.NrOfBinGroups = NR_OF_BIN_GROUPS;
settings.Read();
// Get the measurement settings
thresholdOffset = settings.GetFloat("ThresholdOffset", DEFAULT_THRESHOLD_OFFSET);
countThreshold = settings.GetFloat("CountThreshold", DEFAULT_COUNT_THRESHOLD);
// Get the upper group-boundaries and the precipitation amount factor from the settings
for (byte nbr = 0; nbr < settings.BaseData.NrOfBinGroups; nbr++) {
sensorData.binGroup[nbr].lastBin = settings.GetUInt("BG" + String(nbr) + "T", defaultBinGroupBoundary[nbr]);
sensorData.binGroup[nbr].firstBin = (nbr == 0) ? 1 : sensorData.binGroup[nbr - 1].lastBin + 1;
sensorData.binGroup[nbr].preciAmountFactor = settings.GetUInt("BG" + String(nbr) + "F", defaultPreciAmountFactor[nbr]);
}
settings.LoadCalibration(&sensorData);
//// Test
Serial.println("Ref: ");
for (byte i = 0; i < NR_OF_BIN_GROUPS; i++) {
Serial.print(String(sensorData.binGroup[i].magThresh) + " ");
}
Serial.println();
stateManager.Begin(PROGVERS, PROGNAME);
Wire.begin();
Wire.setClock(800000);
if (bme280.TryInitialize(0x76)) {
Serial.println("BME280 detected");
bme280.SetAltitudeAboveSeaLevel(settings.GetInt("Altitude", 0));
}
StartWifi(&settings);
connectionKeeper.Begin([](bool isCritical) {
HandleCriticalAction(isCritical);
});
pinMode(DEBUG_GPIO_ISR, OUTPUT);
pinMode(DEBUG_GPIO_MAIN, OUTPUT);
startProcessTS = millis() + (settings.GetUInt("StartupDelay", 5) * 1000);
while (millis() < startProcessTS) {
ota.Handle();
watchdog.Handle();
}
// Initialize the publisher
publisher.Begin(&settings, &dataPort, &bme280);
// Initialize signal processing
sigProc.Begin(&settings, &sensorData, &statistics, &publisher);
// Initialize statistics
statistics.Begin(&settings, &sensorData);
// Go
sigProc.StartCapture();
Serial.println("Setup done");
}
String CommandHandler(String command) {
String result;
command = Tools::UTF8ToASCII(command);
if (command.startsWith("alive")) {
result = "alive";
}
else if (command.startsWith("thresholdOffset=")) {
thresholdOffset = command.substring(9).toFloat();
settings.Add("ThresholdOffset", thresholdOffset);
Serial.println(thresholdOffset);
}
else if (command.startsWith("countThreshold=")) {
countThreshold = command.substring(9).toFloat();
settings.Add("CountThreshold", countThreshold);
Serial.println(countThreshold);
}
else if (command.startsWith("version")) {
result = "version=" + String(PROGVERS);
}
else if (command.startsWith("uptime")) {
result = "uptime=" + stateManager.GetUpTime();
}
else if (command.startsWith("reboot")) {
ESP.restart();
}
else if (command.startsWith("savesettings")) {
settings.Write();
}
else if (command.startsWith("calibrate")) {
statistics.Calibrate();
settings.SaveCalibration(&sensorData);
}
else if (command.startsWith("resetPreciAmount")) {
statistics.ResetPreciAmountAcc();
}
return result;
}
void loop() {
stateManager.SetLoopStart();
watchdog.Handle();
if (connectionKeeper.IsConnected()) {
ota.Handle();
if (dataPort.IsEnabled()) {
dataPort.Handle(CommandHandler);
}
}
if (accessPoint.IsRunning()) {
accessPoint.Handle();
}
else {
sigProc.Handle();
connectionKeeper.Handle();
}
frontend.Handle();
stateManager.SetLoopEnd();
}