-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDammformblechVsGitterrolle.ino
513 lines (424 loc) · 13 KB
/
DammformblechVsGitterrolle.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// standard C++ date/time
#include <time.h>
#include <sys/time.h>
// soilMoisture sensor
#include <Wire.h>
// DHT22
#include <DHT.h>
// SD card reader & file system
#include <SPI.h>
#include <SD.h>
#include <FS.h>
#include <SPIFFS.h>
// WiFi
#include <WiFi.h>
//RTC
#include <RTClib.h>
//Webserver
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
// DHT22
#define DHT_SENSOR_1_PIN 26 // ESP32 pin connected to first DHT22 sensor
#define DHT_SENSOR_2_PIN 27 // ESP32 pin connected to second DHT22 sensor
#define DHT_SENSOR_TYPE DHT22
#define ABSOLUTE_ZERO -273
#define SOIL_MOISTURE_SENSOR_1_PIN 34
#define SOIL_MOISTURE_SENSOR_2_PIN 35
// LED
#define LED_PIN 2
#define DATA_FILENAME "/measurements.csv"
#define CONFIG_FILENAME "/config.txt"
#define DELIMITER ";"
#define CONFIG_SSID "wifiSsid"
#define CONFIG_PASS "wifiPass"
#define CONFIG_AP "ap"
#define MOISTURE_ONE_AIR_VALUE "moistureOneAirValue"
#define MOISTURE_ONE_WATER_VALUE "moistureOneWaterValue"
#define MOISTURE_TWO_AIR_VALUE "moistureTwoAirValue"
#define MOISTURE_TWO_WATER_VALUE "moistureTwoWaterValue"
//TODO destroy objects / check for leaks
//TODO: ABSOLUTE_ZERO as tempC is error state, show warning in website
//TODO: Set date via website, //rtc.adjust(DateTime(2022, 3, 30, 21, 29, 0));
//TODO: Karte entfernen und wieder einlegen -> keine Werte geschrieben: Prüfen ob Wert geschrieben, sonst error state
//TODO: "Failed to open file for writing" -> wie behandeln?
RTC_DS3231 rtc;
// WiFi
char ssid[] = "DammformblechVsGitterrolle";
char password[] = "";
boolean accessPoint = false;
AsyncWebServer server(80);
struct Measurement {
int timestamp;
float tempC1;
float tempC2;
int soilMoistureValue1;
int soilmoisturePercent1;
int soilMoistureValue2;
int soilmoisturePercent2;
};
struct SoilMoistureMeasurement {
int soilMoistureValue;
int soilmoisturePercent;
};
struct SoilMoistureSensor {
int airValue;
int waterValue;
int sensorPin;
};
SoilMoistureSensor soilMoistureSensor1 = {.airValue = 2363, .waterValue = 435, .sensorPin = SOIL_MOISTURE_SENSOR_1_PIN};
SoilMoistureSensor soilMoistureSensor2 = {.airValue = 2363, .waterValue = 435, .sensorPin = SOIL_MOISTURE_SENSOR_2_PIN};
SoilMoistureSensor& sensor1 = soilMoistureSensor1;
SoilMoistureSensor& sensor2 = soilMoistureSensor2;
// DHT22
DHT dht_sensor_1(DHT_SENSOR_1_PIN, DHT_SENSOR_TYPE);
DHT dht_sensor_2(DHT_SENSOR_2_PIN, DHT_SENSOR_TYPE);
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR int state = 0;
/**
Log wake up cause to serial.
*/
void serialWakeUpCause() {
esp_sleep_wakeup_cause_t wakeUpCause = esp_sleep_get_wakeup_cause();
Serial.print("Woke up ");
switch (wakeUpCause)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("from deep sleep, caused by GPIO_NUM_25 (RTC_IO)"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("from deep sleep, caused by ESP_SLEEP_WAKEUP_TIMER"); break;
default : Serial.printf("caused by %d.\n", wakeUpCause); break;
}
}
void controlActivity() {
esp_sleep_wakeup_cause_t wakeUpCause = esp_sleep_get_wakeup_cause();
switch (wakeUpCause)
{
case ESP_SLEEP_WAKEUP_EXT0 : mountSdCard(); measurement(); startWiFi(); startWebServer(); break; // button was pressed, enter WiFi mode
case ESP_SLEEP_WAKEUP_TIMER : mountSdCard(); measurement(); goToSleep(); break; // timer woke up, measurement only
default : mountSdCard(); measurement(); goToSleep(); break; // boot case
}
// instead of waiting one cycle, directly show errror state
if (state != 0) {
showStatusWithLed();
return;
}
}
void setTimeFromClock() {
if (! rtc.begin()) {
Serial.println("Error reading from real time clock.");
Serial.flush();
switchToErrorState(2);
}
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
timeval tv;
DateTime now = rtc.now();
tv.tv_sec = now.unixtime();
tv.tv_usec = 0;
settimeofday(&tv, NULL);
}
void getFormattedDateTime(char* result) {
struct timeval readNow;
gettimeofday(&readNow, NULL); // get from ESP32 RTC
time_t readTime = readNow.tv_sec;
char buf[20];
strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%SZ", gmtime(&readTime));
for (int i = 0; i < 20; ++i) {
result[i] = buf[i];
}
}
void logTime() {
char buf[20];
getFormattedDateTime(buf);
Serial.printf("Current date and time: %s\n", buf);
}
void measurement() {
struct Measurement current = {0, 0, 0, 0, 0, 0, 0};
dht_sensor_1.begin(); // initialize first DHT sensor
dht_sensor_2.begin(); // initialize second DHT sensor
current.tempC1 = measureDht22(dht_sensor_1);
current.tempC2 = measureDht22(dht_sensor_2);
SoilMoistureMeasurement soulMoisture1 = measureSoilMoisture(soilMoistureSensor1);
current.soilMoistureValue1 = soulMoisture1.soilMoistureValue;
current.soilmoisturePercent1 = soulMoisture1.soilmoisturePercent;
SoilMoistureMeasurement soulMoisture2 = measureSoilMoisture(soilMoistureSensor2);
current.soilMoistureValue2 = soulMoisture2.soilMoistureValue;
current.soilmoisturePercent2 = soulMoisture2.soilmoisturePercent;
struct timeval readNow;
gettimeofday(&readNow, NULL);
current.timestamp = readNow.tv_sec;
Serial.printf("timestamp: %i, tempC1: %f, soilMoistureValue1: %i, soilmoisturePercent1 %i, tempC2: %f, soilMoistureValue2: %i, soilmoisturePercent2 %i \n", current.timestamp, current.tempC1, current.soilMoistureValue1, current.soilmoisturePercent1, current.tempC2, current.soilMoistureValue2, current.soilmoisturePercent2 );
saveMeasurement(current);
blink(1, 1000);
}
void saveMeasurement(Measurement measurement) {
File dataFile = SD.open(DATA_FILENAME, FILE_APPEND);
if (!dataFile) {
Serial.println("Failed to open file for writing");
return;
}
String delimiter = DELIMITER;
time_t measurementTime = measurement.timestamp;
char buf[20];
strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%SZ", gmtime(&measurementTime));
String output = String(buf + delimiter + measurement.tempC1 + delimiter + measurement.soilMoistureValue1 + delimiter + measurement.soilmoisturePercent1 + delimiter + measurement.tempC2 + delimiter + measurement.soilMoistureValue2 + delimiter + measurement.soilmoisturePercent2) ;
Serial.println(output);
dataFile.println(output);
dataFile.close();
}
void readConfig() {
Serial.println("\nReading config:");
File configFile = SD.open(CONFIG_FILENAME, FILE_READ);
if (!configFile) {
Serial.printf("Failed to read config file '%s'.\n", CONFIG_FILENAME);
switchToErrorState(3);
return;
}
String line;
String key;
String value;
while (configFile.available() != 0) {
line = configFile.readStringUntil('\n');
line.trim();
int index = line.indexOf('=');
key = line.substring(0, index);
value = line.substring(index + 1, line.length() + 1);
if (key.length() == 0 && value.length() == 0) {
continue;
}
if (key.equals(CONFIG_SSID)) {
value.toCharArray(ssid, value.length() + 1);
Serial.printf("SSID: %s\n", ssid);
}
else if (key.equals(CONFIG_PASS)) {
value.toCharArray(password, value.length() + 1);
Serial.printf("Password: %s \n", password);
}
else if (key.equals(CONFIG_AP)) {
if (value.equals("true")) {
accessPoint = true;
}
else {
accessPoint = false;
}
Serial.printf("Access Point: %i\n", accessPoint);
}
else if (key.equals(MOISTURE_ONE_AIR_VALUE)) {
sensor1.airValue = value.toInt();
Serial.printf("moistureOneAirValue: %i\n", sensor1.airValue);
}
else if (key.equals(MOISTURE_ONE_WATER_VALUE)) {
sensor1.waterValue = value.toInt();
Serial.printf("moistureOneWaterValue: %i\n", sensor1.waterValue);
}
else if (key.equals(MOISTURE_TWO_AIR_VALUE)) {
sensor2.airValue = value.toInt();
Serial.printf("moistureTwoAirValue: %i\n", sensor2.airValue);
}
else if (key.equals(MOISTURE_TWO_WATER_VALUE)) {
sensor2.waterValue = value.toInt();
Serial.printf("moistureTwoWaterValue: %i\n", sensor2.waterValue);
}
else {
Serial.println("No match: " + line);
}
}
configFile.close();
Serial.println();
}
boolean mountSdCard() {
if (!SD.begin(5)) {
Serial.println("Card Mount Failed");
switchToErrorState(1);
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
switchToErrorState(1);
}
if (state != 0) {
return false;
}
else {
return true;
}
}
void setup() {
Serial.begin(115200);
delay(3000);
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
pinMode(SOIL_MOISTURE_SENSOR_1_PIN, INPUT_PULLDOWN);
pinMode(SOIL_MOISTURE_SENSOR_2_PIN, INPUT_PULLDOWN);
Serial.println("setup()");
// LED blink setup
pinMode(LED_PIN, OUTPUT);
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
serialWakeUpCause();
setTimeFromClock();
logTime();
mountSdCard();
readConfig();
if (state != 0) {
showStatusWithLed();
Serial.printf("Error state: %i \n", state);
goToSleep();
}
else {
controlActivity();
}
}
void goToSleep() {
int sleepSecs;
if (state != 0) {
sleepSecs = 3; // time to sleep in error state
}
else {
sleepSecs = 300; // time to sleep in working state
}
esp_sleep_enable_timer_wakeup(sleepSecs * 1000000);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_25, 0); //1 = High, 0 = Low
Serial.println("Going to sleep now.");
delay(1000);
esp_deep_sleep_start();
}
/**
Executed in webserver active state
*/
void loop() {
logTime();
int awakeSeconds = 300;
Serial.printf("Staying awake for %i seconds.\n", awakeSeconds);
delay(awakeSeconds * 1000);
server.end();
WiFi.mode( WIFI_MODE_NULL );
btStop();
measurement();
goToSleep();
}
/**
Blinks LED at GPIO 2 with given interval, needs GPIO
Needs LED_PIN defined and setup with pinMode(LED_PIN, OUTPUT);
int count: how many times to blink
int intervall: time in millis
*/
void blink(int count, int interval) {
for (int pos = 0; pos < count; pos++) {
digitalWrite(LED_PIN, HIGH);
delay(interval);
digitalWrite(LED_PIN, LOW);
delay(interval);
}
}
void switchToErrorState(int i) {
Serial.println("In error state now.");
state = i;
}
void showStatusWithLed() {
if (state != 0) {
blink(9 + state, 200);
}
}
SoilMoistureMeasurement measureSoilMoisture(SoilMoistureSensor sensor) {
SoilMoistureMeasurement result = {0, 0};
// soilMoisture initial values
int soilMoistureValue;
int soilmoisturePercent;
soilMoistureValue = analogRead(sensor.sensorPin);
result.soilMoistureValue = soilMoistureValue;
Serial.print(" Pin: ");
Serial.print(sensor.sensorPin);
Serial.print(" | Analog value: ");
Serial.print(soilMoistureValue);
Serial.print(" | Percentage: ");
soilmoisturePercent = map(soilMoistureValue, sensor.airValue, sensor.waterValue, 0, 100);
result.soilmoisturePercent = soilmoisturePercent;
if (soilmoisturePercent > 100)
{
Serial.println("100 %");
}
else if (soilmoisturePercent < 0)
{
Serial.println("0 %");
}
else if (soilmoisturePercent >= 0 && soilmoisturePercent <= 100)
{
Serial.print(soilmoisturePercent);
Serial.println("%");
}
return result;
}
float measureDht22(DHT dhtSensor) {
// read temperature in Celsius
float tempC = dhtSensor.readTemperature();
// check whether the reading is successful or not
if ( isnan(tempC) ) {
Serial.println("Failed to read from DHT sensor!");
return ABSOLUTE_ZERO;
} else {
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println("°C");
return tempC;
}
}
void connectToWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.printf("SSID: '%s' password: '%s' \n", ssid, password);
Serial.println("AP mode: " + accessPoint);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setupAccessPoint()
{
Serial.print("Setting up Access Point.. ");
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("IP address: ");
Serial.println(IP);
}
void startWiFi() {
if (accessPoint) {
setupAccessPoint();
}
else {
connectToWiFi();
}
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>Blech vs. Rolle</title>
</head>
<body>
<h1>Dammformblech vs. Gitterrolle</h1>
<p>%TIMEPLACEHOLDER%</p>
<p><a href ="./measurements.csv">measurements.csv</a></p>
</body>
</html>
)rawliteral";
String processor(const String& var){
if(var == "TIMEPLACEHOLDER"){
char buf[20];
getFormattedDateTime(buf);
String formattedDateTime = "";
formattedDateTime += buf;
return formattedDateTime;
}
return String();
}
void startWebServer() {
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on(DATA_FILENAME, HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SD, DATA_FILENAME, "text/csv");
});
server.serveStatic("/", SD, "/");
server.begin();
}