Browse Source

Added debug log output to web with history buffer

Thomas Buck 3 years ago
parent
commit
2e2bf09f49
10 changed files with 215 additions and 71 deletions
  1. 36
    0
      include/DebugLog.h
  2. 3
    1
      include/WifiStuff.h
  3. 2
    0
      platformio.ini
  4. 61
    0
      src/DebugLog.cpp
  5. 31
    28
      src/Functionality.cpp
  6. 8
    4
      src/Keymatrix.cpp
  7. 11
    10
      src/Plants.cpp
  8. 3
    2
      src/Statemachine.cpp
  9. 51
    18
      src/WifiStuff.cpp
  10. 9
    8
      src/main.cpp

+ 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_

+ 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
 

+ 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
+}

+ 31
- 28
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
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();
211
             n = -3;
212
             n = -3;
212
         }
213
         }
213
         
214
         
214
-        Serial.print(n);
215
-        Serial.print(", ");
215
+        debug.print(n);
216
+        debug.print(", ");
216
         
217
         
217
         Wire.write(n);
218
         Wire.write(n);
218
     }
219
     }
219
     
220
     
220
-    Serial.println();
221
+    debug.println();
221
 }
222
 }
222
 
223
 
223
 void ui_i2c_receive(int count) {
224
 void ui_i2c_receive(int count) {
231
     }
232
     }
232
     
233
     
233
     if (count <= 0) {
234
     if (count <= 0) {
234
-        Serial.println("ui_i2c_receive: count is 0");
235
+        debug.println("ui_i2c_receive: count is 0");
235
         return;
236
         return;
236
     }
237
     }
237
     
238
     
238
     if (buff[0] == 0x01) {
239
     if (buff[0] == 0x01) {
239
         if (count < 3) {
240
         if (count < 3) {
240
-            Serial.println("ui_i2c_receive: blink lcd too short");
241
+            debug.println("ui_i2c_receive: blink lcd too short");
241
             return;
242
             return;
242
         }
243
         }
243
         
244
         
244
         int n = buff[1];
245
         int n = buff[1];
245
         int wait = buff[2] * 10;
246
         int wait = buff[2] * 10;
246
         
247
         
247
-        Serial.println("ui_i2c_receive: blink lcd command");
248
+        debug.println("ui_i2c_receive: blink lcd command");
248
         blink_lcd(n, wait);
249
         blink_lcd(n, wait);
249
     } else if (buff[0] == 0x02) {
250
     } else if (buff[0] == 0x02) {
250
-        Serial.println("ui_i2c_receive: backspace command");
251
+        debug.println("ui_i2c_receive: backspace command");
251
         backspace();
252
         backspace();
252
     } else if (buff[0] == 0x03) {
253
     } else if (buff[0] == 0x03) {
253
         if (count < 3) {
254
         if (count < 3) {
254
-            Serial.println("ui_i2c_receive: display far too short");
255
+            debug.println("ui_i2c_receive: display far too short");
255
             return;
256
             return;
256
         }
257
         }
257
         
258
         
262
             s += buff[3 + i];
263
             s += buff[3 + i];
263
         }
264
         }
264
         
265
         
265
-        Serial.println("ui_i2c_receive: display command");
266
+        debug.println("ui_i2c_receive: display command");
266
         linebuffer[line] = s;
267
         linebuffer[line] = s;
267
     } else if (buff[0] == 0x04) {
268
     } else if (buff[0] == 0x04) {
268
         if (count < 2) {
269
         if (count < 2) {
269
-            Serial.println("ui_i2c_receive: num input too short");
270
+            debug.println("ui_i2c_receive: num input too short");
270
             return;
271
             return;
271
         }
272
         }
272
         
273
         
273
         int8_t num_input = buff[1];
274
         int8_t num_input = buff[1];
274
         
275
         
275
-        Serial.println("ui_i2c_receive: num input");
276
+        debug.println("ui_i2c_receive: num input");
276
         write_to_all(linebuffer[0].c_str(), linebuffer[1].c_str(),
277
         write_to_all(linebuffer[0].c_str(), linebuffer[1].c_str(),
277
                      linebuffer[2].c_str(), linebuffer[3].c_str(),
278
                      linebuffer[2].c_str(), linebuffer[3].c_str(),
278
                      num_input);
279
                      num_input);
279
     } else {
280
     } else {
280
-        Serial.println("ui_i2c_receive: unknown command");
281
+        debug.println("ui_i2c_receive: unknown command");
281
         return;
282
         return;
282
     }
283
     }
283
 }
284
 }
287
 void ui_setup(void) {
288
 void ui_setup(void) {
288
     keys.setPins(keymatrix_pins);
289
     keys.setPins(keymatrix_pins);
289
     
290
     
290
-    Serial.println("Setting up LCD, please wait");
291
+    debug.println("Setting up LCD, please wait");
291
     delay(1000); // give LCD some time to boot
292
     delay(1000); // give LCD some time to boot
292
     lcd.init();
293
     lcd.init();
293
     
294
     
300
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
301
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
301
     
302
     
302
 #ifndef FUNCTION_CONTROL
303
 #ifndef FUNCTION_CONTROL
303
-    Serial.println("Initializing I2C Slave");
304
+    debug.println("Initializing I2C Slave");
304
     Wire.begin(OWN_I2C_ADDRESS);
305
     Wire.begin(OWN_I2C_ADDRESS);
305
     Wire.onReceive(ui_i2c_receive);
306
     Wire.onReceive(ui_i2c_receive);
306
     Wire.onRequest(ui_i2c_request);
307
     Wire.onRequest(ui_i2c_request);
330
             }
331
             }
331
             
332
             
332
             int n = ke.getNum();
333
             int n = ke.getNum();
333
-            Serial.print("Got keypad input: \"");
334
+            debug.print("Got keypad input: \"");
334
             
335
             
335
             if (n < 0) {
336
             if (n < 0) {
336
-                Serial.print((n == -1) ? '*' : '#');
337
+                debug.print((n == -1) ? '*' : '#');
337
             } else {
338
             } else {
338
-                Serial.print(n);
339
+                debug.print(n);
339
                 
340
                 
340
                 if (doing_multi_input) {
341
                 if (doing_multi_input) {
341
                     char s[2] = { (char)(n + '0'), '\0' };
342
                     char s[2] = { (char)(n + '0'), '\0' };
343
                 }
344
                 }
344
             }
345
             }
345
             
346
             
346
-            Serial.println("\"");
347
+            debug.println("\"");
347
             
348
             
348
             blink_lcd(1, 100);
349
             blink_lcd(1, 100);
349
             handle_input(n);
350
             handle_input(n);
432
     
433
     
433
 #ifndef FUNCTION_UI
434
 #ifndef FUNCTION_UI
434
     
435
     
435
-    Serial.println("Initializing I2C Master");
436
+    debug.println("Initializing I2C Master");
436
     Wire.setClock(I2C_BUS_SPEED);
437
     Wire.setClock(I2C_BUS_SPEED);
437
     Wire.begin();
438
     Wire.begin();
438
     
439
     
439
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
440
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
440
-    Serial.println("Wait for Serial");
441
+    debug.println("Wait for Serial");
441
     while (!Serial);
442
     while (!Serial);
442
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
443
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
443
     
444
     
455
     while (Wire.available()) {
456
     while (Wire.available()) {
456
         char c = Wire.read();
457
         char c = Wire.read();
457
         
458
         
459
+            debug.print("control_run: got input '");
460
+            debug.print(c);
461
+            debug.println("'");
458
         // for some reason it seems as if we always get -1 here,
462
         // for some reason it seems as if we always get -1 here,
459
         // so we cant send our input (-2 to 9) as is.
463
         // 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.
464
         // so -4 is no-data, -3 is -1, and the rest as-is.
463
                 c = -1;
467
                 c = -1;
464
             }
468
             }
465
         
469
         
466
-            Serial.print("control_run: got input '");
467
-            Serial.print((int)c);
468
-            Serial.println("'");
469
         
470
         
470
             sm.input(c);
471
             sm.input(c);
471
         }
472
         }
481
 #ifndef FUNCTION_UI
482
 #ifndef FUNCTION_UI
482
 
483
 
483
 void blink_lcd(int n, int wait) {
484
 void blink_lcd(int n, int wait) {
484
-    Serial.println("blink_lcd i2c");
485
+    debug.println("blink_lcd i2c");
485
     
486
     
486
     Wire.beginTransmission(OWN_I2C_ADDRESS);
487
     Wire.beginTransmission(OWN_I2C_ADDRESS);
487
     Wire.write(0x01); // blink command
488
     Wire.write(0x01); // blink command
491
 }
492
 }
492
 
493
 
493
 void backspace(void) {
494
 void backspace(void) {
494
-    Serial.println("backspace i2c");
495
+    debug.println("backspace i2c");
495
     
496
     
496
     Wire.beginTransmission(OWN_I2C_ADDRESS);
497
     Wire.beginTransmission(OWN_I2C_ADDRESS);
497
     Wire.write(0x02); // backspace command
498
     Wire.write(0x02); // backspace command
502
                   const char *c, const char *d, int num_input) {
503
                   const char *c, const char *d, int num_input) {
503
     const char *lines[4] = { a, b, c, d };
504
     const char *lines[4] = { a, b, c, d };
504
     
505
     
505
-    Serial.println("write_to_all i2c");
506
+    debug.println("write_to_all i2c");
506
     
507
     
507
     for (int i = 0; i < 4; i++) {
508
     for (int i = 0; i < 4; i++) {
508
         Wire.beginTransmission(OWN_I2C_ADDRESS);
509
         Wire.beginTransmission(OWN_I2C_ADDRESS);
527
     
528
     
528
     write_lcd_to_serial(a, b, c, d);
529
     write_lcd_to_serial(a, b, c, d);
529
     
530
     
531
+#ifdef PLATFORM_ESP
530
     wifi_set_message_buffer(a, b, c, d);
532
     wifi_set_message_buffer(a, b, c, d);
531
-    wifi_send_websocket();
533
+    wifi_send_status_broadcast();
534
+#endif // PLATFORM_ESP
532
 }
535
 }
533
 
536
 
534
 #endif // ! FUNCTION_UI
537
 #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

+ 11
- 10
src/Plants.cpp View File

1
 #include <Arduino.h>
1
 #include <Arduino.h>
2
+#include "DebugLog.h"
2
 #include "Plants.h"
3
 #include "Plants.h"
3
     
4
     
4
 // valves: no of plants + 1 for water inlet
5
 // valves: no of plants + 1 for water inlet
60
 }
61
 }
61
 
62
 
62
 void Plants::openWaterInlet(void) {
63
 void Plants::openWaterInlet(void) {
63
-    Serial.println("Plants::openWaterInlet");
64
+    debug.println("Plants::openWaterInlet");
64
     valves.setPin(countPlants(), true);
65
     valves.setPin(countPlants(), true);
65
 }
66
 }
66
 
67
 
67
 void Plants::closeWaterInlet(void) {
68
 void Plants::closeWaterInlet(void) {
68
-    Serial.println("Plants::closeWaterInlet");
69
+    debug.println("Plants::closeWaterInlet");
69
     valves.setPin(countPlants(), false);
70
     valves.setPin(countPlants(), false);
70
 }
71
 }
71
 
72
 
74
 }
75
 }
75
 
76
 
76
 void Plants::startFertilizer(int id) {
77
 void Plants::startFertilizer(int id) {
77
-    Serial.print("Plants::startFertilizer ");
78
-    Serial.println(id);
78
+    debug.print("Plants::startFertilizer ");
79
+    debug.println(id);
79
     
80
     
80
     if ((id >= 0) && (id < countFertilizers())) {
81
     if ((id >= 0) && (id < countFertilizers())) {
81
         pumps.setPin(id, true);
82
         pumps.setPin(id, true);
83
 }
84
 }
84
 
85
 
85
 void Plants::stopFertilizer(int id) {
86
 void Plants::stopFertilizer(int id) {
86
-    Serial.print("Plants::stopFertilizer ");
87
-    Serial.println(id);
87
+    debug.print("Plants::stopFertilizer ");
88
+    debug.println(id);
88
     
89
     
89
     if ((id >= 0) && (id < countFertilizers())) {
90
     if ((id >= 0) && (id < countFertilizers())) {
90
         pumps.setPin(id, false);
91
         pumps.setPin(id, false);
102
 }
103
 }
103
 
104
 
104
 void Plants::startPlant(int id) {
105
 void Plants::startPlant(int id) {
105
-    Serial.print("Plants::startPlant ");
106
-    Serial.println(id);
106
+    debug.print("Plants::startPlant ");
107
+    debug.println(id);
107
     
108
     
108
     if ((id >= 0) && (id < countPlants())) {
109
     if ((id >= 0) && (id < countPlants())) {
109
         valves.setPin(id, true);
110
         valves.setPin(id, true);
111
 }
112
 }
112
 
113
 
113
 void Plants::stopPlant(int id) {
114
 void Plants::stopPlant(int id) {
114
-    Serial.print("Plants::stopPlant ");
115
-    Serial.println(id);
115
+    debug.print("Plants::stopPlant ");
116
+    debug.println(id);
116
     
117
     
117
     if ((id >= 0) && (id < countPlants())) {
118
     if ((id >= 0) && (id < countPlants())) {
118
         valves.setPin(id, false);
119
         valves.setPin(id, false);

+ 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
 }

+ 51
- 18
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) {
51
     String a = message_buffer_a ;
52
     String a = message_buffer_a ;
52
     String b = message_buffer_b;
53
     String b = message_buffer_b;
53
     String c = message_buffer_c;
54
     String c = message_buffer_c;
117
     ws += "\"\n";
118
     ws += "\"\n";
118
     ws += "}";
119
     ws += "}";
119
     
120
     
120
-    socket.broadcastTXT(ws);
121
+    wifi_send_websocket(ws);
122
+}
123
+
124
+void wifi_send_websocket(String s) {
125
+    socket.broadcastTXT(s);
121
 }
126
 }
122
 
127
 
123
 void handleRoot() {
128
 void handleRoot() {
181
     message += F("font-family: monospace;\n");
186
     message += F("font-family: monospace;\n");
182
     message += F("}\n");
187
     message += F("}\n");
183
     
188
     
189
+    message += F(".log {\n");
190
+    message += F("max-height: 300px;\n");
191
+    message += F("padding: 0 1.0em;\n");
192
+    message += F("margin: 1em 0;\n");
193
+    message += F("border: 1px dashed black;\n");
194
+    message += F("font-family: monospace;\n");
195
+    message += F("overflow-y: scroll;\n");
196
+    message += F("word-break: break-all;\n");
197
+    message += F("}\n");
198
+    
199
+    message += F("#logbuf {\n");
200
+    message += F("white-space: break-spaces;\n");
201
+    message += F("}\n");
202
+    
184
     message += F(".pad {\n");
203
     message += F(".pad {\n");
185
     message += F("background: black;\n");
204
     message += F("background: black;\n");
186
     message += F("border: 3px solid black;\n");
205
     message += F("border: 3px solid black;\n");
376
     message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
395
     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");
396
     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");
397
     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");
398
+    message += F("</div></div>\n");
399
+    
400
+    message += F("<div class='log'><pre id='logbuf'>\n");
401
+    message += debug.getBuffer();
402
+    message += F("</pre></div>\n");
403
+    message += F("</body>\n");
381
     
404
     
382
     message += F("<script type='text/javascript'>\n");
405
     message += F("<script type='text/javascript'>\n");
383
     message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
406
     message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
384
     message += F("socket.onmessage = function(e) {\n");
407
     message += F("socket.onmessage = function(e) {\n");
408
+    message += F(    "if (e.data.startsWith('log:')) {\n");
409
+    message += F(        "var log = document.getElementById('logbuf');\n");
410
+    message += F(        "var div = document.getElementsByClassName('log')[0];\n");
411
+    message += F(        "log.innerHTML += e.data.substring(4);\n");
412
+    message += F(        "if (log.innerHTML.length > (1024 * 1024)) {\n");
413
+    message += F(            "log.innerHTML = log.innerHTML.substring(1024 * 1024);\n");
414
+    message += F(        "}\n");
415
+    message += F(        "div.scrollTop = div.scrollHeight;\n");
416
+    message += F(        "return;\n");
417
+    message += F(    "}\n");
385
     message += F(    "var msg = JSON.parse(e.data);\n");
418
     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");
419
     message += F(    "var str = msg.a + '\\n' + msg.b + '\\n' + msg.c + '\\n' + msg.d;\n");
387
     message += F(    "console.log(str);\n");
420
     message += F(    "console.log(str);\n");
462
 #if defined(ARDUINO_ARCH_ESP8266)
495
 #if defined(ARDUINO_ARCH_ESP8266)
463
 
496
 
464
     // Connect to WiFi AP
497
     // Connect to WiFi AP
465
-    Serial.println("WiFi: initializing");
498
+    debug.println("WiFi: initializing");
466
     WiFi.hostname(hostname);
499
     WiFi.hostname(hostname);
467
     WiFi.mode(WIFI_STA);
500
     WiFi.mode(WIFI_STA);
468
     
501
     
469
-    Serial.print("WiFi: connecting");
502
+    debug.print("WiFi: connecting");
470
     WiFi.begin(WIFI_SSID, WIFI_PW);
503
     WiFi.begin(WIFI_SSID, WIFI_PW);
471
     while (WiFi.status() != WL_CONNECTED) {
504
     while (WiFi.status() != WL_CONNECTED) {
472
-        Serial.print(".");
505
+        debug.print(".");
473
         delay(LED_CONNECT_BLINK_INTERVAL);
506
         delay(LED_CONNECT_BLINK_INTERVAL);
474
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
507
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
475
     }
508
     }
476
-    Serial.println();
509
+    debug.println();
477
     
510
     
478
 #elif defined(ARDUINO_ARCH_ESP32)
511
 #elif defined(ARDUINO_ARCH_ESP32)
479
 
512
 
480
     // Set hostname workaround
513
     // Set hostname workaround
481
-    Serial.println("WiFi: set hostname");
514
+    debug.println("WiFi: set hostname");
482
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
515
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
483
     WiFi.setHostname(hostname.c_str());
516
     WiFi.setHostname(hostname.c_str());
484
     
517
     
485
     // Workaround for WiFi connecting only every 2nd reset
518
     // Workaround for WiFi connecting only every 2nd reset
486
     // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
519
     // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
487
-    Serial.println("WiFi: connection work-around");
520
+    debug.println("WiFi: connection work-around");
488
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
521
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
489
         if (info.disconnected.reason == 202) {
522
         if (info.disconnected.reason == 202) {
490
             esp_sleep_enable_timer_wakeup(10);
523
             esp_sleep_enable_timer_wakeup(10);
494
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
527
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
495
 
528
 
496
     // Connect to WiFi AP
529
     // Connect to WiFi AP
497
-    Serial.println("WiFi: SSID=" WIFI_SSID);
498
-    Serial.print("WiFi: connecting");
530
+    debug.println("WiFi: SSID=" WIFI_SSID);
531
+    debug.print("WiFi: connecting");
499
     WiFi.mode(WIFI_STA);
532
     WiFi.mode(WIFI_STA);
500
     WiFi.begin(WIFI_SSID, WIFI_PW);
533
     WiFi.begin(WIFI_SSID, WIFI_PW);
501
     while (WiFi.status() != WL_CONNECTED) {
534
     while (WiFi.status() != WL_CONNECTED) {
502
-        Serial.print(".");
535
+        debug.print(".");
503
         delay(LED_CONNECT_BLINK_INTERVAL);
536
         delay(LED_CONNECT_BLINK_INTERVAL);
504
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
537
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
505
     }
538
     }
506
-    Serial.println();
539
+    debug.println();
507
     
540
     
508
     // Set hostname workaround
541
     // Set hostname workaround
509
-    Serial.println("WiFi: set hostname work-around");
542
+    debug.println("WiFi: set hostname work-around");
510
     WiFi.setHostname(hostname.c_str());
543
     WiFi.setHostname(hostname.c_str());
511
 
544
 
512
 #endif
545
 #endif
513
 
546
 
514
     // Setup HTTP Server
547
     // Setup HTTP Server
515
-    Serial.println("WiFi: initializing HTTP server");
548
+    debug.println("WiFi: initializing HTTP server");
516
     MDNS.begin(hostname.c_str());
549
     MDNS.begin(hostname.c_str());
517
     updater.setup(&server);
550
     updater.setup(&server);
518
     server.on("/", handleRoot);
551
     server.on("/", handleRoot);
522
     socket.begin();
555
     socket.begin();
523
     socket.onEvent(webSocketEvent);
556
     socket.onEvent(webSocketEvent);
524
     
557
     
525
-    Serial.println("WiFi: setup done");
558
+    debug.println("WiFi: setup done");
526
 }
559
 }
527
 
560
 
528
 void wifi_run() {
561
 void wifi_run() {
538
     
571
     
539
     if ((millis() - last_websocket_update_time) >= WEBSOCKET_UPDATE_INTERVAL) {
572
     if ((millis() - last_websocket_update_time) >= WEBSOCKET_UPDATE_INTERVAL) {
540
         last_websocket_update_time = millis();
573
         last_websocket_update_time = millis();
541
-        wifi_send_websocket();
574
+        wifi_send_status_broadcast();
542
     }
575
     }
543
     
576
     
544
     // reset ESP every 6h to be safe
577
     // 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