6 Commits

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

+ 36
- 0
include/DebugLog.h View File

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
     void clear(void);
20
     void clear(void);
21
     void setBacklight(uint8_t val);
21
     void setBacklight(uint8_t val);
22
     
22
     
23
+    void saveSplash(void);
24
+    void enableSplash(void);
25
+    void disableSplash(void);
26
+    
23
     // 0 no cursor, 1 underline, 2 blinking, 3 both
27
     // 0 no cursor, 1 underline, 2 blinking, 3 both
24
     void cursor(int style);
28
     void cursor(int style);
25
     
29
     

+ 3
- 1
include/WifiStuff.h View File

8
 
8
 
9
 void wifi_set_message_buffer(String a, String b, String c, String d);
9
 void wifi_set_message_buffer(String a, String b, String c, String d);
10
 void wifi_schedule_websocket(void);
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
 #endif
15
 #endif
14
 
16
 

+ 3
- 0
include/config.h View File

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

+ 3
- 0
include/config_pins.h View File

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

+ 2
- 0
platformio.ini View File

19
 lib_deps =
19
 lib_deps =
20
     Wire
20
     Wire
21
     https://github.com/Links2004/arduinoWebSockets
21
     https://github.com/Links2004/arduinoWebSockets
22
+    https://github.com/rlogiacco/CircularBuffer
22
 
23
 
23
 [env:esp32_main]
24
 [env:esp32_main]
24
 platform = espressif32
25
 platform = espressif32
30
 lib_deps =
31
 lib_deps =
31
     Wire
32
     Wire
32
     https://github.com/Links2004/arduinoWebSockets
33
     https://github.com/Links2004/arduinoWebSockets
34
+    https://github.com/rlogiacco/CircularBuffer
33
 
35
 
34
 [env:arduino_ui]
36
 [env:arduino_ui]
35
 platform = atmelavr
37
 platform = atmelavr

+ 61
- 0
src/DebugLog.cpp View File

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
 
2
 
3
 #include "config.h"
3
 #include "config.h"
4
 #include "config_pins.h"
4
 #include "config_pins.h"
5
+#include "DebugLog.h"
5
 #include "Functionality.h"
6
 #include "Functionality.h"
6
 
7
 
7
 #ifdef FUNCTION_UI
8
 #ifdef FUNCTION_UI
195
 
196
 
196
 void ui_i2c_request(void) {
197
 void ui_i2c_request(void) {
197
     if (keybuffer.isEmpty()) {
198
     if (keybuffer.isEmpty()) {
198
-        Wire.write(-4);
199
+        Wire.write(252);
199
         return;
200
         return;
200
     }
201
     }
201
     
202
     
202
-    Serial.print("ui_i2c_request: ");
203
+    debug.print("ui_i2c_request: ");
203
     
204
     
204
     while (!keybuffer.isEmpty()) {
205
     while (!keybuffer.isEmpty()) {
205
         int n = keybuffer.shift();
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
         if (n == -1) {
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
         Wire.write(n);
219
         Wire.write(n);
218
     }
220
     }
219
     
221
     
220
-    Serial.println();
222
+    debug.println();
221
 }
223
 }
222
 
224
 
223
 void ui_i2c_receive(int count) {
225
 void ui_i2c_receive(int count) {
231
     }
233
     }
232
     
234
     
233
     if (count <= 0) {
235
     if (count <= 0) {
234
-        Serial.println("ui_i2c_receive: count is 0");
236
+        debug.println("ui_i2c_receive: count is 0");
235
         return;
237
         return;
236
     }
238
     }
237
     
239
     
238
     if (buff[0] == 0x01) {
240
     if (buff[0] == 0x01) {
239
         if (count < 3) {
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
             return;
243
             return;
242
         }
244
         }
243
         
245
         
244
         int n = buff[1];
246
         int n = buff[1];
245
         int wait = buff[2] * 10;
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
         blink_lcd(n, wait);
250
         blink_lcd(n, wait);
249
     } else if (buff[0] == 0x02) {
251
     } else if (buff[0] == 0x02) {
250
-        Serial.println("ui_i2c_receive: backspace command");
252
+        debug.println("ui_i2c_receive: backspace command");
251
         backspace();
253
         backspace();
252
     } else if (buff[0] == 0x03) {
254
     } else if (buff[0] == 0x03) {
253
         if (count < 3) {
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
             return;
257
             return;
256
         }
258
         }
257
         
259
         
262
             s += buff[3 + i];
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
         linebuffer[line] = s;
268
         linebuffer[line] = s;
267
     } else if (buff[0] == 0x04) {
269
     } else if (buff[0] == 0x04) {
268
         if (count < 2) {
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
             return;
272
             return;
271
         }
273
         }
272
         
274
         
273
         int8_t num_input = buff[1];
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
         write_to_all(linebuffer[0].c_str(), linebuffer[1].c_str(),
278
         write_to_all(linebuffer[0].c_str(), linebuffer[1].c_str(),
277
                      linebuffer[2].c_str(), linebuffer[3].c_str(),
279
                      linebuffer[2].c_str(), linebuffer[3].c_str(),
278
                      num_input);
280
                      num_input);
279
     } else {
281
     } else {
280
-        Serial.println("ui_i2c_receive: unknown command");
282
+        debug.println("ui_i2c_receive: unknown command");
281
         return;
283
         return;
282
     }
284
     }
283
 }
285
 }
287
 void ui_setup(void) {
289
 void ui_setup(void) {
288
     keys.setPins(keymatrix_pins);
290
     keys.setPins(keymatrix_pins);
289
     
291
     
290
-    Serial.println("Setting up LCD, please wait");
292
+    debug.println("Setting up LCD, please wait");
291
     delay(1000); // give LCD some time to boot
293
     delay(1000); // give LCD some time to boot
292
     lcd.init();
294
     lcd.init();
295
+    lcd.disableSplash();
293
     
296
     
294
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
297
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
295
     lcd.write(0, "Waiting for serial");
298
     lcd.write(0, "Waiting for serial");
300
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
303
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
301
     
304
     
302
 #ifndef FUNCTION_CONTROL
305
 #ifndef FUNCTION_CONTROL
303
-    Serial.println("Initializing I2C Slave");
306
+    debug.println("Initializing I2C Slave");
304
     Wire.begin(OWN_I2C_ADDRESS);
307
     Wire.begin(OWN_I2C_ADDRESS);
305
     Wire.onReceive(ui_i2c_receive);
308
     Wire.onReceive(ui_i2c_receive);
306
     Wire.onRequest(ui_i2c_request);
309
     Wire.onRequest(ui_i2c_request);
330
             }
333
             }
331
             
334
             
332
             int n = ke.getNum();
335
             int n = ke.getNum();
333
-            Serial.print("Got keypad input: \"");
336
+            debug.print("Got keypad input: \"");
334
             
337
             
335
             if (n < 0) {
338
             if (n < 0) {
336
-                Serial.print((n == -1) ? '*' : '#');
339
+                debug.print((n == -1) ? '*' : '#');
337
             } else {
340
             } else {
338
-                Serial.print(n);
341
+                debug.print(n);
339
                 
342
                 
340
                 if (doing_multi_input) {
343
                 if (doing_multi_input) {
341
                     char s[2] = { (char)(n + '0'), '\0' };
344
                     char s[2] = { (char)(n + '0'), '\0' };
343
                 }
346
                 }
344
             }
347
             }
345
             
348
             
346
-            Serial.println("\"");
349
+            debug.println("\"");
347
             
350
             
348
             blink_lcd(1, 100);
351
             blink_lcd(1, 100);
349
             handle_input(n);
352
             handle_input(n);
432
     
435
     
433
 #ifndef FUNCTION_UI
436
 #ifndef FUNCTION_UI
434
     
437
     
435
-    Serial.println("Initializing I2C Master");
438
+    debug.println("Initializing I2C Master");
436
     Wire.setClock(I2C_BUS_SPEED);
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
     Wire.begin();
444
     Wire.begin();
445
+#endif // defined(I2C_SDA_PIN) && defined(I2C_SCL_PIN)
438
     
446
     
439
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
447
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
440
-    Serial.println("Wait for Serial");
448
+    debug.println("Wait for Serial");
441
     while (!Serial);
449
     while (!Serial);
442
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
450
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
443
     
451
     
453
     
461
     
454
     Wire.requestFrom(OWN_I2C_ADDRESS, I2C_BUF_SIZE);
462
     Wire.requestFrom(OWN_I2C_ADDRESS, I2C_BUF_SIZE);
455
     while (Wire.available()) {
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
                 c = -1;
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
             sm.input(c);
477
             sm.input(c);
471
         }
478
         }
472
     }
479
     }
481
 #ifndef FUNCTION_UI
488
 #ifndef FUNCTION_UI
482
 
489
 
483
 void blink_lcd(int n, int wait) {
490
 void blink_lcd(int n, int wait) {
484
-    Serial.println("blink_lcd i2c");
491
+    debug.println("blink_lcd i2c");
485
     
492
     
486
     Wire.beginTransmission(OWN_I2C_ADDRESS);
493
     Wire.beginTransmission(OWN_I2C_ADDRESS);
487
     Wire.write(0x01); // blink command
494
     Wire.write(0x01); // blink command
491
 }
498
 }
492
 
499
 
493
 void backspace(void) {
500
 void backspace(void) {
494
-    Serial.println("backspace i2c");
501
+    debug.println("backspace i2c");
495
     
502
     
496
     Wire.beginTransmission(OWN_I2C_ADDRESS);
503
     Wire.beginTransmission(OWN_I2C_ADDRESS);
497
     Wire.write(0x02); // backspace command
504
     Wire.write(0x02); // backspace command
502
                   const char *c, const char *d, int num_input) {
509
                   const char *c, const char *d, int num_input) {
503
     const char *lines[4] = { a, b, c, d };
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
     for (int i = 0; i < 4; i++) {
514
     for (int i = 0; i < 4; i++) {
508
         Wire.beginTransmission(OWN_I2C_ADDRESS);
515
         Wire.beginTransmission(OWN_I2C_ADDRESS);
527
     
534
     
528
     write_lcd_to_serial(a, b, c, d);
535
     write_lcd_to_serial(a, b, c, d);
529
     
536
     
537
+#ifdef PLATFORM_ESP
530
     wifi_set_message_buffer(a, b, c, d);
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
 #endif // ! FUNCTION_UI
543
 #endif // ! FUNCTION_UI

+ 8
- 4
src/Keymatrix.cpp View File

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

+ 22
- 10
src/Plants.cpp View File

1
 #include <Arduino.h>
1
 #include <Arduino.h>
2
+
3
+#include "DebugLog.h"
2
 #include "Plants.h"
4
 #include "Plants.h"
5
+#include "config.h"
6
+#include "config_pins.h"
3
     
7
     
4
 // valves: no of plants + 1 for water inlet
8
 // valves: no of plants + 1 for water inlet
5
 // pumps: no of fertilizers
9
 // pumps: no of fertilizers
48
     bool low = switches.getPin(0);
52
     bool low = switches.getPin(0);
49
     bool high = switches.getPin(1);
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
     if ((!low) && (!high)) {
63
     if ((!low) && (!high)) {
52
         return empty;
64
         return empty;
53
     } else if (low && (!high)) {
65
     } else if (low && (!high)) {
60
 }
72
 }
61
 
73
 
62
 void Plants::openWaterInlet(void) {
74
 void Plants::openWaterInlet(void) {
63
-    Serial.println("Plants::openWaterInlet");
75
+    debug.println("Plants::openWaterInlet");
64
     valves.setPin(countPlants(), true);
76
     valves.setPin(countPlants(), true);
65
 }
77
 }
66
 
78
 
67
 void Plants::closeWaterInlet(void) {
79
 void Plants::closeWaterInlet(void) {
68
-    Serial.println("Plants::closeWaterInlet");
80
+    debug.println("Plants::closeWaterInlet");
69
     valves.setPin(countPlants(), false);
81
     valves.setPin(countPlants(), false);
70
 }
82
 }
71
 
83
 
74
 }
86
 }
75
 
87
 
76
 void Plants::startFertilizer(int id) {
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
     if ((id >= 0) && (id < countFertilizers())) {
92
     if ((id >= 0) && (id < countFertilizers())) {
81
         pumps.setPin(id, true);
93
         pumps.setPin(id, true);
83
 }
95
 }
84
 
96
 
85
 void Plants::stopFertilizer(int id) {
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
     if ((id >= 0) && (id < countFertilizers())) {
101
     if ((id >= 0) && (id < countFertilizers())) {
90
         pumps.setPin(id, false);
102
         pumps.setPin(id, false);
102
 }
114
 }
103
 
115
 
104
 void Plants::startPlant(int id) {
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
     if ((id >= 0) && (id < countPlants())) {
120
     if ((id >= 0) && (id < countPlants())) {
109
         valves.setPin(id, true);
121
         valves.setPin(id, true);
111
 }
123
 }
112
 
124
 
113
 void Plants::stopPlant(int id) {
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
     if ((id >= 0) && (id < countPlants())) {
129
     if ((id >= 0) && (id < countPlants())) {
118
         valves.setPin(id, false);
130
         valves.setPin(id, false);

+ 16
- 0
src/SerialLCD.cpp View File

1
+// see https://github.com/sparkfun/SparkFun_SerLCD_Arduino_Library
2
+
1
 #include <Arduino.h>
3
 #include <Arduino.h>
2
 #include "SerialLCD.h"
4
 #include "SerialLCD.h"
3
 
5
 
83
     delay(LCD_DELAY);
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
 void SerialLCD::write(const char *text) {
102
 void SerialLCD::write(const char *text) {
87
     lcd->print(text);
103
     lcd->print(text);
88
     delay(LCD_DELAY);
104
     delay(LCD_DELAY);

+ 3
- 2
src/Statemachine.cpp View File

1
 #include "Plants.h"
1
 #include "Plants.h"
2
+#include "DebugLog.h"
2
 #include "Statemachine.h"
3
 #include "Statemachine.h"
3
 #include "config.h"
4
 #include "config.h"
4
 
5
 
395
     uint32_t n = db.getNumber();
396
     uint32_t n = db.getNumber();
396
     db.clear();
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
     return n;
402
     return n;
402
 }
403
 }

+ 85
- 20
src/WifiStuff.cpp View File

23
 #include "config_pins.h"
23
 #include "config_pins.h"
24
 #include "Functionality.h"
24
 #include "Functionality.h"
25
 #include "SimpleUpdater.h"
25
 #include "SimpleUpdater.h"
26
+#include "DebugLog.h"
26
 #include "WifiStuff.h"
27
 #include "WifiStuff.h"
27
 
28
 
28
 UPDATE_WEB_SERVER server(80);
29
 UPDATE_WEB_SERVER server(80);
47
     last_websocket_update_time = 0;
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
     String a = message_buffer_a ;
56
     String a = message_buffer_a ;
52
     String b = message_buffer_b;
57
     String b = message_buffer_b;
53
     String c = message_buffer_c;
58
     String c = message_buffer_c;
93
     
98
     
94
     ws += F("\"switches\": [ ");
99
     ws += F("\"switches\": [ ");
95
     for (int i = 0; i < SWITCH_COUNT; i++) {
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
         ws += "\"";
115
         ws += "\"";
97
-        ws += get_plants()->getSwitches()->getPin(i) ? "1" : "0";
116
+        ws += v ? "1" : "0";
98
         ws += "\"";
117
         ws += "\"";
99
         
118
         
100
         if (i < (SWITCH_COUNT - 1)) {
119
         if (i < (SWITCH_COUNT - 1)) {
117
     ws += "\"\n";
136
     ws += "\"\n";
118
     ws += "}";
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
 void handleRoot() {
146
 void handleRoot() {
181
     message += F("font-family: monospace;\n");
204
     message += F("font-family: monospace;\n");
182
     message += F("}\n");
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
     message += F(".pad {\n");
221
     message += F(".pad {\n");
185
     message += F("background: black;\n");
222
     message += F("background: black;\n");
186
     message += F("border: 3px solid black;\n");
223
     message += F("border: 3px solid black;\n");
279
     message += F("<div class='container'>\n");
316
     message += F("<div class='container'>\n");
280
     for (int i = 0; i < SWITCH_COUNT; i++) {
317
     for (int i = 0; i < SWITCH_COUNT; i++) {
281
         message += F("<div class='switch' style='background-color: ");
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
             message += F("red");
334
             message += F("red");
284
         } else {
335
         } else {
285
             message += F("green");
336
             message += F("green");
376
     message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
427
     message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
377
     message += F("<p>Made by <a href='https://xythobuz.de'>xythobuz</a></p>\n");
428
     message += F("<p>Made by <a href='https://xythobuz.de'>xythobuz</a></p>\n");
378
     message += F("<p><a href='https://git.xythobuz.de/thomas/giess-o-mat'>Project Repository</a></p>\n");
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
     message += F("<script type='text/javascript'>\n");
437
     message += F("<script type='text/javascript'>\n");
383
     message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
438
     message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
384
     message += F("socket.onmessage = function(e) {\n");
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
     message += F(    "var msg = JSON.parse(e.data);\n");
450
     message += F(    "var msg = JSON.parse(e.data);\n");
386
     message += F(    "var str = msg.a + '\\n' + msg.b + '\\n' + msg.c + '\\n' + msg.d;\n");
451
     message += F(    "var str = msg.a + '\\n' + msg.b + '\\n' + msg.c + '\\n' + msg.d;\n");
387
     message += F(    "console.log(str);\n");
452
     message += F(    "console.log(str);\n");
462
 #if defined(ARDUINO_ARCH_ESP8266)
527
 #if defined(ARDUINO_ARCH_ESP8266)
463
 
528
 
464
     // Connect to WiFi AP
529
     // Connect to WiFi AP
465
-    Serial.println("WiFi: initializing");
530
+    debug.println("WiFi: initializing");
466
     WiFi.hostname(hostname);
531
     WiFi.hostname(hostname);
467
     WiFi.mode(WIFI_STA);
532
     WiFi.mode(WIFI_STA);
468
     
533
     
469
-    Serial.print("WiFi: connecting");
534
+    debug.print("WiFi: connecting");
470
     WiFi.begin(WIFI_SSID, WIFI_PW);
535
     WiFi.begin(WIFI_SSID, WIFI_PW);
471
     while (WiFi.status() != WL_CONNECTED) {
536
     while (WiFi.status() != WL_CONNECTED) {
472
-        Serial.print(".");
537
+        debug.print(".");
473
         delay(LED_CONNECT_BLINK_INTERVAL);
538
         delay(LED_CONNECT_BLINK_INTERVAL);
474
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
539
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
475
     }
540
     }
476
-    Serial.println();
541
+    debug.println();
477
     
542
     
478
 #elif defined(ARDUINO_ARCH_ESP32)
543
 #elif defined(ARDUINO_ARCH_ESP32)
479
 
544
 
480
     // Set hostname workaround
545
     // Set hostname workaround
481
-    Serial.println("WiFi: set hostname");
546
+    debug.println("WiFi: set hostname");
482
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
547
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
483
     WiFi.setHostname(hostname.c_str());
548
     WiFi.setHostname(hostname.c_str());
484
     
549
     
485
     // Workaround for WiFi connecting only every 2nd reset
550
     // Workaround for WiFi connecting only every 2nd reset
486
     // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
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
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
553
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
489
         if (info.disconnected.reason == 202) {
554
         if (info.disconnected.reason == 202) {
490
             esp_sleep_enable_timer_wakeup(10);
555
             esp_sleep_enable_timer_wakeup(10);
494
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
559
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
495
 
560
 
496
     // Connect to WiFi AP
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
     WiFi.mode(WIFI_STA);
564
     WiFi.mode(WIFI_STA);
500
     WiFi.begin(WIFI_SSID, WIFI_PW);
565
     WiFi.begin(WIFI_SSID, WIFI_PW);
501
     while (WiFi.status() != WL_CONNECTED) {
566
     while (WiFi.status() != WL_CONNECTED) {
502
-        Serial.print(".");
567
+        debug.print(".");
503
         delay(LED_CONNECT_BLINK_INTERVAL);
568
         delay(LED_CONNECT_BLINK_INTERVAL);
504
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
569
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
505
     }
570
     }
506
-    Serial.println();
571
+    debug.println();
507
     
572
     
508
     // Set hostname workaround
573
     // Set hostname workaround
509
-    Serial.println("WiFi: set hostname work-around");
574
+    debug.println("WiFi: set hostname work-around");
510
     WiFi.setHostname(hostname.c_str());
575
     WiFi.setHostname(hostname.c_str());
511
 
576
 
512
 #endif
577
 #endif
513
 
578
 
514
     // Setup HTTP Server
579
     // Setup HTTP Server
515
-    Serial.println("WiFi: initializing HTTP server");
580
+    debug.println("WiFi: initializing HTTP server");
516
     MDNS.begin(hostname.c_str());
581
     MDNS.begin(hostname.c_str());
517
     updater.setup(&server);
582
     updater.setup(&server);
518
     server.on("/", handleRoot);
583
     server.on("/", handleRoot);
522
     socket.begin();
587
     socket.begin();
523
     socket.onEvent(webSocketEvent);
588
     socket.onEvent(webSocketEvent);
524
     
589
     
525
-    Serial.println("WiFi: setup done");
590
+    debug.println("WiFi: setup done");
526
 }
591
 }
527
 
592
 
528
 void wifi_run() {
593
 void wifi_run() {
538
     
603
     
539
     if ((millis() - last_websocket_update_time) >= WEBSOCKET_UPDATE_INTERVAL) {
604
     if ((millis() - last_websocket_update_time) >= WEBSOCKET_UPDATE_INTERVAL) {
540
         last_websocket_update_time = millis();
605
         last_websocket_update_time = millis();
541
-        wifi_send_websocket();
606
+        wifi_send_status_broadcast();
542
     }
607
     }
543
     
608
     
544
     // reset ESP every 6h to be safe
609
     // reset ESP every 6h to be safe

+ 9
- 8
src/main.cpp View File

2
 
2
 
3
 #include "Functionality.h"
3
 #include "Functionality.h"
4
 #include "WifiStuff.h"
4
 #include "WifiStuff.h"
5
+#include "DebugLog.h"
5
 #include "config.h"
6
 #include "config.h"
6
 #include "config_pins.h"
7
 #include "config_pins.h"
7
 
8
 
12
     digitalWrite(BUILTIN_LED_PIN, HIGH);
13
     digitalWrite(BUILTIN_LED_PIN, HIGH);
13
     
14
     
14
     Serial.begin(115200);
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
 #ifdef FUNCTION_UI
19
 #ifdef FUNCTION_UI
19
-    Serial.println("Initializing UI");
20
+    debug.println("Initializing UI");
20
     ui_setup();
21
     ui_setup();
21
 #endif // FUNCTION_UI
22
 #endif // FUNCTION_UI
22
     
23
     
23
 #ifdef FUNCTION_CONTROL
24
 #ifdef FUNCTION_CONTROL
24
-    Serial.println("Initializing Control");
25
+    debug.println("Initializing Control");
25
     control_setup();
26
     control_setup();
26
 #endif // FUNCTION_CONTROL
27
 #endif // FUNCTION_CONTROL
27
     
28
     
28
 #ifdef PLATFORM_ESP
29
 #ifdef PLATFORM_ESP
29
-    Serial.println("Initializing WiFi");
30
+    debug.println("Initializing WiFi");
30
     wifi_setup();
31
     wifi_setup();
31
 #endif // PLATFORM_ESP
32
 #endif // PLATFORM_ESP
32
     
33
     
33
-    Serial.println("Ready, starting main loop");
34
+    debug.println("Ready, starting main loop");
34
     digitalWrite(BUILTIN_LED_PIN, LOW);
35
     digitalWrite(BUILTIN_LED_PIN, LOW);
35
     
36
     
36
 #ifdef FUNCTION_CONTROL
37
 #ifdef FUNCTION_CONTROL
37
     
38
     
38
 #ifndef FUNCTION_UI
39
 #ifndef FUNCTION_UI
39
     // give ui unit some time to initialize
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
     delay(3000);
42
     delay(3000);
42
 #endif // ! FUNCTION_UI
43
 #endif // ! FUNCTION_UI
43
     
44
     
44
-    Serial.println("Starting state machine");
45
+    debug.println("Starting state machine");
45
     control_begin();
46
     control_begin();
46
     
47
     
47
 #endif // FUNCTION_CONTROL
48
 #endif // FUNCTION_CONTROL

Loading…
Cancel
Save