6 Commits

Author SHA1 Message Date
  Thomas Buck 9b3b5e21b4 fix read/write of special symbols via I2C 3 weeks ago
  Thomas Buck 10e1c8bab5 Only send websocket broadcasts when someone is connected 3 weeks ago
  Thomas Buck 14242852ac Make I2C pins configurable on ESP32 3 weeks ago
  Thomas Buck cfb10e9620 Make sure SerialLCD does not show a splash screen 3 weeks ago
  Thomas Buck 08c22aff7e Make sensor inputs invertable 3 weeks ago
  Thomas Buck 2e2bf09f49 Added debug log output to web with history buffer 3 weeks ago

+ 36
- 0
include/DebugLog.h View File

@@ -0,0 +1,36 @@
1
+#ifndef _DEBUG_LOG_H_
2
+#define _DEBUG_LOG_H_
3
+
4
+#include <Arduino.h>
5
+
6
+#ifdef PLATFORM_ESP
7
+#include <CircularBuffer.h>
8
+#define DEBUG_LOG_HISTORY_SIZE 1024
9
+#endif // PLATFORM_ESP
10
+
11
+class DebugLog {
12
+public:
13
+#ifdef PLATFORM_ESP
14
+    String getBuffer(void);
15
+#endif // PLATFORM_ESP
16
+    
17
+    void print(String s);
18
+    void print(int n);
19
+    
20
+    void println(void);
21
+    void println(String s);
22
+    void println(int n);
23
+    
24
+private:
25
+    void sendToTargets(String s);
26
+    
27
+#ifdef PLATFORM_ESP
28
+    void addToBuffer(String s);
29
+    
30
+    CircularBuffer<char, DEBUG_LOG_HISTORY_SIZE> buffer;
31
+#endif // PLATFORM_ESP
32
+};
33
+
34
+extern DebugLog debug;
35
+
36
+#endif // _DEBUG_LOG_H_

+ 4
- 0
include/SerialLCD.h View File

@@ -20,6 +20,10 @@ public:
20 20
     void clear(void);
21 21
     void setBacklight(uint8_t val);
22 22
     
23
+    void saveSplash(void);
24
+    void enableSplash(void);
25
+    void disableSplash(void);
26
+    
23 27
     // 0 no cursor, 1 underline, 2 blinking, 3 both
24 28
     void cursor(int style);
25 29
     

+ 3
- 1
include/WifiStuff.h View File

@@ -8,7 +8,9 @@ void wifi_run();
8 8
 
9 9
 void wifi_set_message_buffer(String a, String b, String c, String d);
10 10
 void wifi_schedule_websocket(void);
11
-void wifi_send_websocket(void);
11
+void wifi_send_status_broadcast(void);
12
+
13
+void wifi_send_websocket(String s);
12 14
 
13 15
 #endif
14 16
 

+ 3
- 0
include/config.h View File

@@ -25,6 +25,9 @@
25 25
 #define LED_CONNECT_BLINK_INTERVAL 250
26 26
 #define LED_ERROR_BLINK_INTERVAL 100
27 27
 
28
+#define INVERT_SENSOR_BOTTOM
29
+//#define INVERT_SENSOR_TOP
30
+
28 31
 #define OWN_I2C_ADDRESS 0x42
29 32
 #define I2C_BUS_SPEED 400000
30 33
 #define I2C_BUF_SIZE 32

+ 3
- 0
include/config_pins.h View File

@@ -69,6 +69,9 @@
69 69
 #define SWITCH_COUNT 2
70 70
 #define SWITCH_PINS 22, 23
71 71
 
72
+#define I2C_SDA_PIN 21
73
+#define I2C_SCL_PIN 22
74
+
72 75
 #endif // FUNCTION_CONTROL
73 76
 
74 77
 #endif // PLATFORM_ESP

+ 2
- 0
platformio.ini View File

@@ -19,6 +19,7 @@ build_flags = -D PLATFORM_ESP -D FUNCTION_CONTROL
19 19
 lib_deps =
20 20
     Wire
21 21
     https://github.com/Links2004/arduinoWebSockets
22
+    https://github.com/rlogiacco/CircularBuffer
22 23
 
23 24
 [env:esp32_main]
24 25
 platform = espressif32
@@ -30,6 +31,7 @@ upload_port = /dev/ttyUSB1
30 31
 lib_deps =
31 32
     Wire
32 33
     https://github.com/Links2004/arduinoWebSockets
34
+    https://github.com/rlogiacco/CircularBuffer
33 35
 
34 36
 [env:arduino_ui]
35 37
 platform = atmelavr

+ 61
- 0
src/DebugLog.cpp View File

@@ -0,0 +1,61 @@
1
+#include <Arduino.h>
2
+
3
+#ifdef PLATFORM_ESP
4
+#include "WifiStuff.h"
5
+#endif // PLATFORM_ESP
6
+
7
+#include "DebugLog.h"
8
+
9
+DebugLog debug;
10
+
11
+#ifdef PLATFORM_ESP
12
+
13
+String DebugLog::getBuffer(void) {
14
+    String r;
15
+    for (unsigned int i = 0; i < buffer.size(); i++) {
16
+        r += buffer[i];
17
+    }
18
+    return r;
19
+}
20
+
21
+void DebugLog::addToBuffer(String s) {
22
+    for (unsigned int i = 0; i < s.length(); i++) {
23
+        buffer.push(s[i]);
24
+    }
25
+}
26
+
27
+#endif // PLATFORM_ESP
28
+
29
+void DebugLog::sendToTargets(String s) {
30
+    Serial.print(s);
31
+    
32
+#ifdef PLATFORM_ESP
33
+    s = "log:" + s;
34
+    wifi_send_websocket(s);
35
+#endif // PLATFORM_ESP
36
+}
37
+
38
+void DebugLog::print(String s) {
39
+#ifdef PLATFORM_ESP
40
+    addToBuffer(s);
41
+#endif // PLATFORM_ESP
42
+    
43
+    sendToTargets(s);
44
+}
45
+
46
+void DebugLog::print(int n) {
47
+    print(String(n));
48
+}
49
+
50
+void DebugLog::println(void) {
51
+    print(String('\n'));
52
+}
53
+
54
+void DebugLog::println(String s) {
55
+    s += '\n';
56
+    print(s);
57
+}
58
+
59
+void DebugLog::println(int n) {
60
+    println(String(n));
61
+}

+ 49
- 40
src/Functionality.cpp View File

@@ -2,6 +2,7 @@
2 2
 
3 3
 #include "config.h"
4 4
 #include "config_pins.h"
5
+#include "DebugLog.h"
5 6
 #include "Functionality.h"
6 7
 
7 8
 #ifdef FUNCTION_UI
@@ -195,29 +196,30 @@ void input_serial(void) {
195 196
 
196 197
 void ui_i2c_request(void) {
197 198
     if (keybuffer.isEmpty()) {
198
-        Wire.write(-4);
199
+        Wire.write(252);
199 200
         return;
200 201
     }
201 202
     
202
-    Serial.print("ui_i2c_request: ");
203
+    debug.print("ui_i2c_request: ");
203 204
     
204 205
     while (!keybuffer.isEmpty()) {
205 206
         int n = keybuffer.shift();
206 207
         
207
-        // for some reason it seems as if we always get -1 here,
208
-        // so we cant send our input (-2 to 9) as is.
209
-        // so -4 is no-data, -3 is -1, and the rest as-is.
210 208
         if (n == -1) {
211
-            n = -3;
209
+            n = 254;
210
+        } else if (n == -2) {
211
+            n = 253;
212
+        } else if ((n < 0) || (n > 9)) {
213
+            continue;
212 214
         }
213 215
         
214
-        Serial.print(n);
215
-        Serial.print(", ");
216
+        debug.print(n);
217
+        debug.print(", ");
216 218
         
217 219
         Wire.write(n);
218 220
     }
219 221
     
220
-    Serial.println();
222
+    debug.println();
221 223
 }
222 224
 
223 225
 void ui_i2c_receive(int count) {
@@ -231,27 +233,27 @@ void ui_i2c_receive(int count) {
231 233
     }
232 234
     
233 235
     if (count <= 0) {
234
-        Serial.println("ui_i2c_receive: count is 0");
236
+        debug.println("ui_i2c_receive: count is 0");
235 237
         return;
236 238
     }
237 239
     
238 240
     if (buff[0] == 0x01) {
239 241
         if (count < 3) {
240
-            Serial.println("ui_i2c_receive: blink lcd too short");
242
+            debug.println("ui_i2c_receive: blink lcd too short");
241 243
             return;
242 244
         }
243 245
         
244 246
         int n = buff[1];
245 247
         int wait = buff[2] * 10;
246 248
         
247
-        Serial.println("ui_i2c_receive: blink lcd command");
249
+        debug.println("ui_i2c_receive: blink lcd command");
248 250
         blink_lcd(n, wait);
249 251
     } else if (buff[0] == 0x02) {
250
-        Serial.println("ui_i2c_receive: backspace command");
252
+        debug.println("ui_i2c_receive: backspace command");
251 253
         backspace();
252 254
     } else if (buff[0] == 0x03) {
253 255
         if (count < 3) {
254
-            Serial.println("ui_i2c_receive: display far too short");
256
+            debug.println("ui_i2c_receive: display far too short");
255 257
             return;
256 258
         }
257 259
         
@@ -262,22 +264,22 @@ void ui_i2c_receive(int count) {
262 264
             s += buff[3 + i];
263 265
         }
264 266
         
265
-        Serial.println("ui_i2c_receive: display command");
267
+        debug.println("ui_i2c_receive: display command");
266 268
         linebuffer[line] = s;
267 269
     } else if (buff[0] == 0x04) {
268 270
         if (count < 2) {
269
-            Serial.println("ui_i2c_receive: num input too short");
271
+            debug.println("ui_i2c_receive: num input too short");
270 272
             return;
271 273
         }
272 274
         
273 275
         int8_t num_input = buff[1];
274 276
         
275
-        Serial.println("ui_i2c_receive: num input");
277
+        debug.println("ui_i2c_receive: num input");
276 278
         write_to_all(linebuffer[0].c_str(), linebuffer[1].c_str(),
277 279
                      linebuffer[2].c_str(), linebuffer[3].c_str(),
278 280
                      num_input);
279 281
     } else {
280
-        Serial.println("ui_i2c_receive: unknown command");
282
+        debug.println("ui_i2c_receive: unknown command");
281 283
         return;
282 284
     }
283 285
 }
@@ -287,9 +289,10 @@ void ui_i2c_receive(int count) {
287 289
 void ui_setup(void) {
288 290
     keys.setPins(keymatrix_pins);
289 291
     
290
-    Serial.println("Setting up LCD, please wait");
292
+    debug.println("Setting up LCD, please wait");
291 293
     delay(1000); // give LCD some time to boot
292 294
     lcd.init();
295
+    lcd.disableSplash();
293 296
     
294 297
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
295 298
     lcd.write(0, "Waiting for serial");
@@ -300,7 +303,7 @@ void ui_setup(void) {
300 303
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
301 304
     
302 305
 #ifndef FUNCTION_CONTROL
303
-    Serial.println("Initializing I2C Slave");
306
+    debug.println("Initializing I2C Slave");
304 307
     Wire.begin(OWN_I2C_ADDRESS);
305 308
     Wire.onReceive(ui_i2c_receive);
306 309
     Wire.onRequest(ui_i2c_request);
@@ -330,12 +333,12 @@ void input_keypad(void) {
330 333
             }
331 334
             
332 335
             int n = ke.getNum();
333
-            Serial.print("Got keypad input: \"");
336
+            debug.print("Got keypad input: \"");
334 337
             
335 338
             if (n < 0) {
336
-                Serial.print((n == -1) ? '*' : '#');
339
+                debug.print((n == -1) ? '*' : '#');
337 340
             } else {
338
-                Serial.print(n);
341
+                debug.print(n);
339 342
                 
340 343
                 if (doing_multi_input) {
341 344
                     char s[2] = { (char)(n + '0'), '\0' };
@@ -343,7 +346,7 @@ void input_keypad(void) {
343 346
                 }
344 347
             }
345 348
             
346
-            Serial.println("\"");
349
+            debug.println("\"");
347 350
             
348 351
             blink_lcd(1, 100);
349 352
             handle_input(n);
@@ -432,12 +435,17 @@ void control_setup(void) {
432 435
     
433 436
 #ifndef FUNCTION_UI
434 437
     
435
-    Serial.println("Initializing I2C Master");
438
+    debug.println("Initializing I2C Master");
436 439
     Wire.setClock(I2C_BUS_SPEED);
440
+    
441
+#if defined(I2C_SDA_PIN) && defined(I2C_SCL_PIN)
442
+    Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
443
+#else
437 444
     Wire.begin();
445
+#endif // defined(I2C_SDA_PIN) && defined(I2C_SCL_PIN)
438 446
     
439 447
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
440
-    Serial.println("Wait for Serial");
448
+    debug.println("Wait for Serial");
441 449
     while (!Serial);
442 450
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
443 451
     
@@ -453,20 +461,19 @@ void control_run(void) {
453 461
     
454 462
     Wire.requestFrom(OWN_I2C_ADDRESS, I2C_BUF_SIZE);
455 463
     while (Wire.available()) {
456
-        char c = Wire.read();
464
+        int c = Wire.read();
457 465
         
458
-        // for some reason it seems as if we always get -1 here,
459
-        // so we cant send our input (-2 to 9) as is.
460
-        // so -4 is no-data, -3 is -1, and the rest as-is.
461
-        if ((c >= -3) && (c <= 9) && (c != -1)) {
462
-            if (c == -3) {
466
+        if (((c >= 0) && (c <= 9)) || (c == 254) || (c == 253)) {
467
+            debug.print("control_run: got input '");
468
+            debug.print(c);
469
+            debug.println("'");
470
+            
471
+            if (c == 254) {
463 472
                 c = -1;
473
+            } else if (c == 253) {
474
+                c = -2;
464 475
             }
465 476
         
466
-            Serial.print("control_run: got input '");
467
-            Serial.print((int)c);
468
-            Serial.println("'");
469
-        
470 477
             sm.input(c);
471 478
         }
472 479
     }
@@ -481,7 +488,7 @@ void control_run(void) {
481 488
 #ifndef FUNCTION_UI
482 489
 
483 490
 void blink_lcd(int n, int wait) {
484
-    Serial.println("blink_lcd i2c");
491
+    debug.println("blink_lcd i2c");
485 492
     
486 493
     Wire.beginTransmission(OWN_I2C_ADDRESS);
487 494
     Wire.write(0x01); // blink command
@@ -491,7 +498,7 @@ void blink_lcd(int n, int wait) {
491 498
 }
492 499
 
493 500
 void backspace(void) {
494
-    Serial.println("backspace i2c");
501
+    debug.println("backspace i2c");
495 502
     
496 503
     Wire.beginTransmission(OWN_I2C_ADDRESS);
497 504
     Wire.write(0x02); // backspace command
@@ -502,7 +509,7 @@ void write_to_all(const char *a, const char *b,
502 509
                   const char *c, const char *d, int num_input) {
503 510
     const char *lines[4] = { a, b, c, d };
504 511
     
505
-    Serial.println("write_to_all i2c");
512
+    debug.println("write_to_all i2c");
506 513
     
507 514
     for (int i = 0; i < 4; i++) {
508 515
         Wire.beginTransmission(OWN_I2C_ADDRESS);
@@ -527,8 +534,10 @@ void write_to_all(const char *a, const char *b,
527 534
     
528 535
     write_lcd_to_serial(a, b, c, d);
529 536
     
537
+#ifdef PLATFORM_ESP
530 538
     wifi_set_message_buffer(a, b, c, d);
531
-    wifi_send_websocket();
539
+    wifi_send_status_broadcast();
540
+#endif // PLATFORM_ESP
532 541
 }
533 542
 
534 543
 #endif // ! FUNCTION_UI

+ 8
- 4
src/Keymatrix.cpp View File

@@ -5,6 +5,10 @@
5 5
 
6 6
 //#define DEBUG_PRINT_MATRIX_STATE
7 7
 
8
+#ifdef DEBUG_PRINT_MATRIX_STATE
9
+#include "DebugLog.h"
10
+#endif // DEBUG_PRINT_MATRIX_STATE
11
+
8 12
 Keymatrix::Event::Event(EventType _type, int _row, int _col) {
9 13
     type = _type;
10 14
     row = _row;
@@ -119,14 +123,14 @@ void Keymatrix::scan(void) {
119 123
     
120 124
 #ifdef DEBUG_PRINT_MATRIX_STATE
121 125
     for (int i = 0; i < buttons; i++) {
122
-        Serial.print(pressed[i] ? "1" : "0");
126
+        debug.print(pressed[i] ? "1" : "0");
123 127
         if (i < (buttons - 1)) {
124
-            Serial.print(" ");
128
+            debug.print(" ");
125 129
         } else {
126
-            Serial.println();
130
+            debug.println();
127 131
         }
128 132
     }
129
-#endif
133
+#endif // DEBUG_PRINT_MATRIX_STATE
130 134
     
131 135
     for (int i = 0; i < buttons; i++) {
132 136
         // debounce - compare to previous state

+ 22
- 10
src/Plants.cpp View File

@@ -1,5 +1,9 @@
1 1
 #include <Arduino.h>
2
+
3
+#include "DebugLog.h"
2 4
 #include "Plants.h"
5
+#include "config.h"
6
+#include "config_pins.h"
3 7
     
4 8
 // valves: no of plants + 1 for water inlet
5 9
 // pumps: no of fertilizers
@@ -48,6 +52,14 @@ Plants::Waterlevel Plants::getWaterlevel(void) {
48 52
     bool low = switches.getPin(0);
49 53
     bool high = switches.getPin(1);
50 54
     
55
+#ifdef INVERT_SENSOR_BOTTOM
56
+    low = !low;
57
+#endif // INVERT_SENSOR_BOTTOM
58
+    
59
+#ifdef INVERT_SENSOR_TOP
60
+    high = !high;
61
+#endif // INVERT_SENSOR_TOP
62
+    
51 63
     if ((!low) && (!high)) {
52 64
         return empty;
53 65
     } else if (low && (!high)) {
@@ -60,12 +72,12 @@ Plants::Waterlevel Plants::getWaterlevel(void) {
60 72
 }
61 73
 
62 74
 void Plants::openWaterInlet(void) {
63
-    Serial.println("Plants::openWaterInlet");
75
+    debug.println("Plants::openWaterInlet");
64 76
     valves.setPin(countPlants(), true);
65 77
 }
66 78
 
67 79
 void Plants::closeWaterInlet(void) {
68
-    Serial.println("Plants::closeWaterInlet");
80
+    debug.println("Plants::closeWaterInlet");
69 81
     valves.setPin(countPlants(), false);
70 82
 }
71 83
 
@@ -74,8 +86,8 @@ int Plants::countFertilizers(void) {
74 86
 }
75 87
 
76 88
 void Plants::startFertilizer(int id) {
77
-    Serial.print("Plants::startFertilizer ");
78
-    Serial.println(id);
89
+    debug.print("Plants::startFertilizer ");
90
+    debug.println(id);
79 91
     
80 92
     if ((id >= 0) && (id < countFertilizers())) {
81 93
         pumps.setPin(id, true);
@@ -83,8 +95,8 @@ void Plants::startFertilizer(int id) {
83 95
 }
84 96
 
85 97
 void Plants::stopFertilizer(int id) {
86
-    Serial.print("Plants::stopFertilizer ");
87
-    Serial.println(id);
98
+    debug.print("Plants::stopFertilizer ");
99
+    debug.println(id);
88 100
     
89 101
     if ((id >= 0) && (id < countFertilizers())) {
90 102
         pumps.setPin(id, false);
@@ -102,8 +114,8 @@ int Plants::countPlants(void) {
102 114
 }
103 115
 
104 116
 void Plants::startPlant(int id) {
105
-    Serial.print("Plants::startPlant ");
106
-    Serial.println(id);
117
+    debug.print("Plants::startPlant ");
118
+    debug.println(id);
107 119
     
108 120
     if ((id >= 0) && (id < countPlants())) {
109 121
         valves.setPin(id, true);
@@ -111,8 +123,8 @@ void Plants::startPlant(int id) {
111 123
 }
112 124
 
113 125
 void Plants::stopPlant(int id) {
114
-    Serial.print("Plants::stopPlant ");
115
-    Serial.println(id);
126
+    debug.print("Plants::stopPlant ");
127
+    debug.println(id);
116 128
     
117 129
     if ((id >= 0) && (id < countPlants())) {
118 130
         valves.setPin(id, false);

+ 16
- 0
src/SerialLCD.cpp View File

@@ -1,3 +1,5 @@
1
+// see https://github.com/sparkfun/SparkFun_SerLCD_Arduino_Library
2
+
1 3
 #include <Arduino.h>
2 4
 #include "SerialLCD.h"
3 5
 
@@ -83,6 +85,20 @@ void SerialLCD::position(int line, int col) {
83 85
     delay(LCD_DELAY);
84 86
 }
85 87
 
88
+void SerialLCD::saveSplash(void) {
89
+    lcd->write(0x7C);
90
+    lcd->write(0x0A);
91
+}
92
+
93
+void SerialLCD::enableSplash(void) {
94
+    lcd->write(0x7C);
95
+    lcd->write(0x30);
96
+}
97
+
98
+void SerialLCD::disableSplash(void) {
99
+    lcd->write(0x7C);
100
+    lcd->write(0x31);
101
+}
86 102
 void SerialLCD::write(const char *text) {
87 103
     lcd->print(text);
88 104
     delay(LCD_DELAY);

+ 3
- 2
src/Statemachine.cpp View File

@@ -1,4 +1,5 @@
1 1
 #include "Plants.h"
2
+#include "DebugLog.h"
2 3
 #include "Statemachine.h"
3 4
 #include "config.h"
4 5
 
@@ -395,8 +396,8 @@ uint32_t Statemachine::number_input(void) {
395 396
     uint32_t n = db.getNumber();
396 397
     db.clear();
397 398
     
398
-    Serial.print("Whole number input: ");
399
-    Serial.println(n);
399
+    debug.print("Whole number input: ");
400
+    debug.println(n);
400 401
     
401 402
     return n;
402 403
 }

+ 85
- 20
src/WifiStuff.cpp View File

@@ -23,6 +23,7 @@
23 23
 #include "config_pins.h"
24 24
 #include "Functionality.h"
25 25
 #include "SimpleUpdater.h"
26
+#include "DebugLog.h"
26 27
 #include "WifiStuff.h"
27 28
 
28 29
 UPDATE_WEB_SERVER server(80);
@@ -47,7 +48,11 @@ void wifi_schedule_websocket(void) {
47 48
     last_websocket_update_time = 0;
48 49
 }
49 50
 
50
-void wifi_send_websocket(void) {
51
+void wifi_send_status_broadcast(void) {
52
+    if (socket.connectedClients() <= 0) {
53
+        return;
54
+    }
55
+    
51 56
     String a = message_buffer_a ;
52 57
     String b = message_buffer_b;
53 58
     String c = message_buffer_c;
@@ -93,8 +98,22 @@ void wifi_send_websocket(void) {
93 98
     
94 99
     ws += F("\"switches\": [ ");
95 100
     for (int i = 0; i < SWITCH_COUNT; i++) {
101
+        bool v = get_plants()->getSwitches()->getPin(i);
102
+        
103
+#ifdef INVERT_SENSOR_BOTTOM
104
+        if (i == 0) {
105
+            v = !v;
106
+        }
107
+#endif // INVERT_SENSOR_BOTTOM
108
+        
109
+#ifdef INVERT_SENSOR_TOP
110
+        if (i == 1) {
111
+            v = !v;
112
+        }
113
+#endif // INVERT_SENSOR_TOP
114
+
96 115
         ws += "\"";
97
-        ws += get_plants()->getSwitches()->getPin(i) ? "1" : "0";
116
+        ws += v ? "1" : "0";
98 117
         ws += "\"";
99 118
         
100 119
         if (i < (SWITCH_COUNT - 1)) {
@@ -117,7 +136,11 @@ void wifi_send_websocket(void) {
117 136
     ws += "\"\n";
118 137
     ws += "}";
119 138
     
120
-    socket.broadcastTXT(ws);
139
+    wifi_send_websocket(ws);
140
+}
141
+
142
+void wifi_send_websocket(String s) {
143
+    socket.broadcastTXT(s);
121 144
 }
122 145
 
123 146
 void handleRoot() {
@@ -181,6 +204,20 @@ void handleRoot() {
181 204
     message += F("font-family: monospace;\n");
182 205
     message += F("}\n");
183 206
     
207
+    message += F(".log {\n");
208
+    message += F("max-height: 300px;\n");
209
+    message += F("padding: 0 1.0em;\n");
210
+    message += F("margin: 1em 0;\n");
211
+    message += F("border: 1px dashed black;\n");
212
+    message += F("font-family: monospace;\n");
213
+    message += F("overflow-y: scroll;\n");
214
+    message += F("word-break: break-all;\n");
215
+    message += F("}\n");
216
+    
217
+    message += F("#logbuf {\n");
218
+    message += F("white-space: break-spaces;\n");
219
+    message += F("}\n");
220
+    
184 221
     message += F(".pad {\n");
185 222
     message += F("background: black;\n");
186 223
     message += F("border: 3px solid black;\n");
@@ -279,7 +316,21 @@ void handleRoot() {
279 316
     message += F("<div class='container'>\n");
280 317
     for (int i = 0; i < SWITCH_COUNT; i++) {
281 318
         message += F("<div class='switch' style='background-color: ");
282
-        if (get_plants()->getSwitches()->getPin(i)) {
319
+        bool v = get_plants()->getSwitches()->getPin(i);
320
+        
321
+#ifdef INVERT_SENSOR_BOTTOM
322
+        if (i == 0) {
323
+            v = !v;
324
+        }
325
+#endif // INVERT_SENSOR_BOTTOM
326
+        
327
+#ifdef INVERT_SENSOR_TOP
328
+        if (i == 1) {
329
+            v = !v;
330
+        }
331
+#endif // INVERT_SENSOR_TOP
332
+        
333
+        if (v) {
283 334
             message += F("red");
284 335
         } else {
285 336
             message += F("green");
@@ -376,12 +427,26 @@ void handleRoot() {
376 427
     message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
377 428
     message += F("<p>Made by <a href='https://xythobuz.de'>xythobuz</a></p>\n");
378 429
     message += F("<p><a href='https://git.xythobuz.de/thomas/giess-o-mat'>Project Repository</a></p>\n");
379
-    message += F("</div>\n");
380
-    message += F("</div></body>\n");
430
+    message += F("</div></div>\n");
431
+    
432
+    message += F("<div class='log'><pre id='logbuf'>\n");
433
+    message += debug.getBuffer();
434
+    message += F("</pre></div>\n");
435
+    message += F("</body>\n");
381 436
     
382 437
     message += F("<script type='text/javascript'>\n");
383 438
     message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
384 439
     message += F("socket.onmessage = function(e) {\n");
440
+    message += F(    "if (e.data.startsWith('log:')) {\n");
441
+    message += F(        "var log = document.getElementById('logbuf');\n");
442
+    message += F(        "var div = document.getElementsByClassName('log')[0];\n");
443
+    message += F(        "log.innerHTML += e.data.substring(4);\n");
444
+    message += F(        "if (log.innerHTML.length > (1024 * 1024)) {\n");
445
+    message += F(            "log.innerHTML = log.innerHTML.substring(1024 * 1024);\n");
446
+    message += F(        "}\n");
447
+    message += F(        "div.scrollTop = div.scrollHeight;\n");
448
+    message += F(        "return;\n");
449
+    message += F(    "}\n");
385 450
     message += F(    "var msg = JSON.parse(e.data);\n");
386 451
     message += F(    "var str = msg.a + '\\n' + msg.b + '\\n' + msg.c + '\\n' + msg.d;\n");
387 452
     message += F(    "console.log(str);\n");
@@ -462,29 +527,29 @@ void wifi_setup() {
462 527
 #if defined(ARDUINO_ARCH_ESP8266)
463 528
 
464 529
     // Connect to WiFi AP
465
-    Serial.println("WiFi: initializing");
530
+    debug.println("WiFi: initializing");
466 531
     WiFi.hostname(hostname);
467 532
     WiFi.mode(WIFI_STA);
468 533
     
469
-    Serial.print("WiFi: connecting");
534
+    debug.print("WiFi: connecting");
470 535
     WiFi.begin(WIFI_SSID, WIFI_PW);
471 536
     while (WiFi.status() != WL_CONNECTED) {
472
-        Serial.print(".");
537
+        debug.print(".");
473 538
         delay(LED_CONNECT_BLINK_INTERVAL);
474 539
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
475 540
     }
476
-    Serial.println();
541
+    debug.println();
477 542
     
478 543
 #elif defined(ARDUINO_ARCH_ESP32)
479 544
 
480 545
     // Set hostname workaround
481
-    Serial.println("WiFi: set hostname");
546
+    debug.println("WiFi: set hostname");
482 547
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
483 548
     WiFi.setHostname(hostname.c_str());
484 549
     
485 550
     // Workaround for WiFi connecting only every 2nd reset
486 551
     // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
487
-    Serial.println("WiFi: connection work-around");
552
+    debug.println("WiFi: connection work-around");
488 553
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
489 554
         if (info.disconnected.reason == 202) {
490 555
             esp_sleep_enable_timer_wakeup(10);
@@ -494,25 +559,25 @@ void wifi_setup() {
494 559
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
495 560
 
496 561
     // Connect to WiFi AP
497
-    Serial.println("WiFi: SSID=" WIFI_SSID);
498
-    Serial.print("WiFi: connecting");
562
+    debug.println("WiFi: SSID=" WIFI_SSID);
563
+    debug.print("WiFi: connecting");
499 564
     WiFi.mode(WIFI_STA);
500 565
     WiFi.begin(WIFI_SSID, WIFI_PW);
501 566
     while (WiFi.status() != WL_CONNECTED) {
502
-        Serial.print(".");
567
+        debug.print(".");
503 568
         delay(LED_CONNECT_BLINK_INTERVAL);
504 569
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
505 570
     }
506
-    Serial.println();
571
+    debug.println();
507 572
     
508 573
     // Set hostname workaround
509
-    Serial.println("WiFi: set hostname work-around");
574
+    debug.println("WiFi: set hostname work-around");
510 575
     WiFi.setHostname(hostname.c_str());
511 576
 
512 577
 #endif
513 578
 
514 579
     // Setup HTTP Server
515
-    Serial.println("WiFi: initializing HTTP server");
580
+    debug.println("WiFi: initializing HTTP server");
516 581
     MDNS.begin(hostname.c_str());
517 582
     updater.setup(&server);
518 583
     server.on("/", handleRoot);
@@ -522,7 +587,7 @@ void wifi_setup() {
522 587
     socket.begin();
523 588
     socket.onEvent(webSocketEvent);
524 589
     
525
-    Serial.println("WiFi: setup done");
590
+    debug.println("WiFi: setup done");
526 591
 }
527 592
 
528 593
 void wifi_run() {
@@ -538,7 +603,7 @@ void wifi_run() {
538 603
     
539 604
     if ((millis() - last_websocket_update_time) >= WEBSOCKET_UPDATE_INTERVAL) {
540 605
         last_websocket_update_time = millis();
541
-        wifi_send_websocket();
606
+        wifi_send_status_broadcast();
542 607
     }
543 608
     
544 609
     // reset ESP every 6h to be safe

+ 9
- 8
src/main.cpp View File

@@ -2,6 +2,7 @@
2 2
 
3 3
 #include "Functionality.h"
4 4
 #include "WifiStuff.h"
5
+#include "DebugLog.h"
5 6
 #include "config.h"
6 7
 #include "config_pins.h"
7 8
 
@@ -12,36 +13,36 @@ void setup() {
12 13
     digitalWrite(BUILTIN_LED_PIN, HIGH);
13 14
     
14 15
     Serial.begin(115200);
15
-    Serial.println("Initializing Giess-o-mat");
16
-    Serial.println("Version: " FIRMWARE_VERSION);
16
+    debug.println("Initializing Giess-o-mat");
17
+    debug.println("Version: " FIRMWARE_VERSION);
17 18
 
18 19
 #ifdef FUNCTION_UI
19
-    Serial.println("Initializing UI");
20
+    debug.println("Initializing UI");
20 21
     ui_setup();
21 22
 #endif // FUNCTION_UI
22 23
     
23 24
 #ifdef FUNCTION_CONTROL
24
-    Serial.println("Initializing Control");
25
+    debug.println("Initializing Control");
25 26
     control_setup();
26 27
 #endif // FUNCTION_CONTROL
27 28
     
28 29
 #ifdef PLATFORM_ESP
29
-    Serial.println("Initializing WiFi");
30
+    debug.println("Initializing WiFi");
30 31
     wifi_setup();
31 32
 #endif // PLATFORM_ESP
32 33
     
33
-    Serial.println("Ready, starting main loop");
34
+    debug.println("Ready, starting main loop");
34 35
     digitalWrite(BUILTIN_LED_PIN, LOW);
35 36
     
36 37
 #ifdef FUNCTION_CONTROL
37 38
     
38 39
 #ifndef FUNCTION_UI
39 40
     // give ui unit some time to initialize
40
-    Serial.println("Waiting for UI to boot");
41
+    debug.println("Waiting for UI to boot");
41 42
     delay(3000);
42 43
 #endif // ! FUNCTION_UI
43 44
     
44
-    Serial.println("Starting state machine");
45
+    debug.println("Starting state machine");
45 46
     control_begin();
46 47
     
47 48
 #endif // FUNCTION_CONTROL

Loading…
Cancel
Save