Sfoglia il codice sorgente

Fist version actually running on ESP, now with working WiFi stuff

Thomas Buck 3 anni fa
parent
commit
3182c9ffaa
6 ha cambiato i file con 202 aggiunte e 140 eliminazioni
  1. 2
    0
      include/WifiStuff.h
  2. 1
    0
      include/config.h
  3. 0
    15
      include/config_pins.h
  4. 147
    120
      src/Functionality.cpp
  5. 47
    5
      src/WifiStuff.cpp
  6. 5
    0
      src/main.cpp

+ 2
- 0
include/WifiStuff.h Vedi File

@@ -6,6 +6,8 @@
6 6
 void wifi_setup();
7 7
 void wifi_run();
8 8
 
9
+void wifi_set_message_buffer(String a, String b, String c, String d);
10
+
9 11
 #endif
10 12
 
11 13
 #endif // _WIFI_STUFF_H_

+ 1
- 0
include/config.h Vedi File

@@ -25,6 +25,7 @@
25 25
 #define LED_ERROR_BLINK_INTERVAL 100
26 26
 
27 27
 #define OWN_I2C_ADDRESS 0x42
28
+#define I2C_BUS_SPEED 400000
28 29
 #define I2C_BUF_SIZE 32
29 30
 
30 31
 #endif // _CONFIG_H_

+ 0
- 15
include/config_pins.h Vedi File

@@ -57,8 +57,6 @@
57 57
 
58 58
 #ifdef FUNCTION_CONTROL
59 59
 
60
-#ifdef FUNCTION_UI
61
-
62 60
 #define VALVE_COUNT 5
63 61
 #define VALVE_PINS 9, 11, 12, 13, 14
64 62
 
@@ -68,19 +66,6 @@
68 66
 #define SWITCH_COUNT 2
69 67
 #define SWITCH_PINS 18, 19
70 68
 
71
-#else
72
-
73
-#define VALVE_COUNT 5
74
-#define VALVE_PINS 9, 11, 12, 13, 14
75
-
76
-#define PUMP_COUNT 3
77
-#define PUMP_PINS 15, 16, 17
78
-
79
-#define SWITCH_COUNT 2
80
-#define SWITCH_PINS 18, 19
81
-
82
-#endif
83
-
84 69
 #endif // FUNCTION_CONTROL
85 70
 
86 71
 #endif // PLATFORM_ESP

+ 147
- 120
src/Functionality.cpp Vedi File

@@ -38,6 +38,7 @@ bool doing_multi_input = false;
38 38
 
39 39
 #include "Plants.h"
40 40
 #include "Statemachine.h"
41
+#include "WifiStuff.h"
41 42
 
42 43
 Plants plants(VALVE_COUNT, PUMP_COUNT, SWITCH_COUNT);
43 44
 int valve_pins[VALVE_COUNT] = { VALVE_PINS };
@@ -50,6 +51,144 @@ Statemachine sm(write_to_all, backspace);
50 51
 
51 52
 // ----------------------------------------------------------------------------
52 53
 
54
+void write_lcd_to_serial(const char *a, const char *b,
55
+                  const char *c, const char *d) {
56
+#ifdef DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
57
+    int la = strlen(a);
58
+    int lb = strlen(b);
59
+    int lc = strlen(c);
60
+    int ld = strlen(d);
61
+    
62
+    Serial.println();
63
+    Serial.println(" ----------------------");
64
+    
65
+    Serial.print("| ");
66
+    Serial.print(a);
67
+    if (la < 20) {
68
+        for (int i = 0; i < (20 - la); i++) {
69
+            Serial.print(' ');
70
+        }
71
+    }
72
+    Serial.println(" |");
73
+    
74
+    Serial.print("| ");
75
+    Serial.print(b);
76
+    if (lb < 20) {
77
+        for (int i = 0; i < (20 - lb); i++) {
78
+            Serial.print(' ');
79
+        }
80
+    }
81
+    Serial.println(" |");
82
+    
83
+    Serial.print("| ");
84
+    Serial.print(c);
85
+    if (lc < 20) {
86
+        for (int i = 0; i < (20 - lc); i++) {
87
+            Serial.print(' ');
88
+        }
89
+    }
90
+    Serial.println(" |");
91
+    
92
+    Serial.print("| ");
93
+    Serial.print(d);
94
+    if (ld < 20) {
95
+        for (int i = 0; i < (20 - ld); i++) {
96
+            Serial.print(' ');
97
+        }
98
+    }
99
+    Serial.println(" |");
100
+    
101
+    Serial.println(" ----------------------");
102
+    Serial.println("Please provide keypad input:");
103
+#endif // DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
104
+}
105
+
106
+void handle_input(int n) {
107
+#ifdef FUNCTION_CONTROL
108
+    
109
+    sm.input(n);
110
+    
111
+#else
112
+    
113
+    keybuffer.push(n);
114
+    
115
+#endif // FUNCTION_CONTROL
116
+}
117
+
118
+void input_serial(void) {
119
+#ifdef DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
120
+    if (Serial.available() > 0) {
121
+        
122
+#ifdef FUNCTION_UI
123
+        last_input_time = millis();
124
+        if (!backlight_state) {
125
+            backlight_state = true;
126
+            lcd.setBacklight(255);
127
+        }
128
+#endif // FUNCTION_UI
129
+        
130
+        int c = Serial.read();
131
+        if (c == '*') {
132
+            Serial.write(c);
133
+            Serial.write('\n');
134
+            
135
+#ifdef FUNCTION_UI
136
+            if (doing_multi_input) {
137
+                char s[2] = { (char)(c), '\0' };
138
+                lcd.write(s);
139
+            }
140
+#endif // FUNCTION_UI
141
+            
142
+            handle_input(-1);
143
+        } else if  (c == '#') {
144
+            Serial.write(c);
145
+            Serial.write('\n');
146
+            
147
+#ifdef FUNCTION_UI
148
+            if (doing_multi_input) {
149
+                char s[2] = { (char)(c), '\0' };
150
+                lcd.write(s);
151
+            }
152
+#endif // FUNCTION_UI
153
+            
154
+            handle_input(-2);
155
+        } else if  (c == '\n') {
156
+            Serial.write('#');
157
+            Serial.write('\n');
158
+            
159
+#ifdef FUNCTION_UI
160
+            if (doing_multi_input) {
161
+                char s[2] = { '#', '\0' };
162
+                lcd.write(s);
163
+            }
164
+#endif // FUNCTION_UI
165
+            
166
+            handle_input(-2);
167
+        } else if (c == '\b') {
168
+            Serial.write(c);
169
+            handle_input(-1);
170
+        } else if ((c >= '0') && (c <= '9')) {
171
+            Serial.write(c);
172
+            
173
+#ifdef FUNCTION_UI
174
+            if (!doing_multi_input) {
175
+                Serial.write('\n');
176
+            }
177
+            
178
+            if (doing_multi_input) {
179
+                char s[2] = { (char)(c), '\0' };
180
+                lcd.write(s);
181
+            }
182
+#endif // FUNCTION_UI
183
+            
184
+            handle_input(c - '0');
185
+        }
186
+    }
187
+#endif // DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
188
+}
189
+
190
+// ----------------------------------------------------------------------------
191
+
53 192
 #ifdef FUNCTION_UI
54 193
 
55 194
 #ifndef FUNCTION_CONTROL
@@ -176,18 +315,6 @@ void ui_setup(void) {
176 315
 #endif // ! FUNCTION_CONTROL
177 316
 }
178 317
 
179
-void handle_input(int n) {
180
-#ifdef FUNCTION_CONTROL
181
-    
182
-    sm.input(n);
183
-    
184
-#else
185
-    
186
-    keybuffer.push(n);
187
-    
188
-#endif // FUNCTION_CONTROL
189
-}
190
-
191 318
 void input_keypad(void) {
192 319
     keys.scan();
193 320
     while (keys.hasEvent()) {
@@ -224,66 +351,6 @@ void input_keypad(void) {
224 351
     }
225 352
 }
226 353
 
227
-void input_serial(void) {
228
-#ifdef DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
229
-    if (Serial.available() > 0) {
230
-        last_input_time = millis();
231
-        if (!backlight_state) {
232
-            backlight_state = true;
233
-            lcd.setBacklight(255);
234
-        }
235
-        
236
-        int c = Serial.read();
237
-        if (c == '*') {
238
-            Serial.write(c);
239
-            Serial.write('\n');
240
-            
241
-            if (doing_multi_input) {
242
-                char s[2] = { (char)(c), '\0' };
243
-                lcd.write(s);
244
-            }
245
-            
246
-            handle_input(-1);
247
-        } else if  (c == '#') {
248
-            Serial.write(c);
249
-            Serial.write('\n');
250
-            
251
-            if (doing_multi_input) {
252
-                char s[2] = { (char)(c), '\0' };
253
-                lcd.write(s);
254
-            }
255
-            
256
-            handle_input(-2);
257
-        } else if  (c == '\n') {
258
-            Serial.write('#');
259
-            Serial.write('\n');
260
-            
261
-            if (doing_multi_input) {
262
-                char s[2] = { '#', '\0' };
263
-                lcd.write(s);
264
-            }
265
-            
266
-            handle_input(-2);
267
-        } else if (c == '\b') {
268
-            Serial.write(c);
269
-            handle_input(-1);
270
-        } else if ((c >= '0') && (c <= '9')) {
271
-            Serial.write(c);
272
-            if (!doing_multi_input) {
273
-                Serial.write('\n');
274
-            }
275
-            
276
-            if (doing_multi_input) {
277
-                char s[2] = { (char)(c), '\0' };
278
-                lcd.write(s);
279
-            }
280
-            
281
-            handle_input(c - '0');
282
-        }
283
-    }
284
-#endif // DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
285
-}
286
-
287 354
 void ui_run(void) {
288 355
     input_keypad();
289 356
     input_serial();
@@ -322,54 +389,7 @@ void write_to_all(const char *a, const char *b,
322 389
         doing_multi_input = false;
323 390
     }
324 391
     
325
-#ifdef DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
326
-    int la = strlen(a);
327
-    int lb = strlen(b);
328
-    int lc = strlen(c);
329
-    int ld = strlen(d);
330
-    
331
-    Serial.println();
332
-    Serial.println(" ----------------------");
333
-    
334
-    Serial.print("| ");
335
-    Serial.print(a);
336
-    if (la < 20) {
337
-        for (int i = 0; i < (20 - la); i++) {
338
-            Serial.print(' ');
339
-        }
340
-    }
341
-    Serial.println(" |");
342
-    
343
-    Serial.print("| ");
344
-    Serial.print(b);
345
-    if (lb < 20) {
346
-        for (int i = 0; i < (20 - lb); i++) {
347
-            Serial.print(' ');
348
-        }
349
-    }
350
-    Serial.println(" |");
351
-    
352
-    Serial.print("| ");
353
-    Serial.print(c);
354
-    if (lc < 20) {
355
-        for (int i = 0; i < (20 - lc); i++) {
356
-            Serial.print(' ');
357
-        }
358
-    }
359
-    Serial.println(" |");
360
-    
361
-    Serial.print("| ");
362
-    Serial.print(d);
363
-    if (ld < 20) {
364
-        for (int i = 0; i < (20 - ld); i++) {
365
-            Serial.print(' ');
366
-        }
367
-    }
368
-    Serial.println(" |");
369
-    
370
-    Serial.println(" ----------------------");
371
-    Serial.println("Please provide keypad input:");
372
-#endif // DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
392
+    write_lcd_to_serial(a, b, c, d);
373 393
 }
374 394
 
375 395
 void backspace(void) {
@@ -401,9 +421,11 @@ void control_setup(void) {
401 421
 #ifndef FUNCTION_UI
402 422
     
403 423
     Serial.println("Initializing I2C Master");
424
+    Wire.setClock(I2C_BUS_SPEED);
404 425
     Wire.begin();
405 426
     
406 427
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
428
+    Serial.println("Wait for Serial");
407 429
     while (!Serial);
408 430
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
409 431
     
@@ -436,6 +458,8 @@ void control_run(void) {
436 458
             sm.input(c);
437 459
         }
438 460
     }
461
+    
462
+    input_serial();
439 463
 
440 464
 #endif // ! FUNCTION_UI
441 465
     
@@ -488,6 +512,9 @@ void write_to_all(const char *a, const char *b,
488 512
     Wire.write(0x04); // display command
489 513
     Wire.write((int8_t)num_input);
490 514
     Wire.endTransmission();
515
+    
516
+    wifi_set_message_buffer(a, b, c, d);
517
+    write_lcd_to_serial(a, b, c, d);
491 518
 }
492 519
 
493 520
 #endif // ! FUNCTION_UI

+ 47
- 5
src/WifiStuff.cpp Vedi File

@@ -45,11 +45,38 @@ void handleRoot() {
45 45
     message += F("<h1>Giess-o-mat</h1>\n");
46 46
     
47 47
     message += F("\n<pre>\n");
48
-    message += message_buffer_a + '\n';
49
-    message += message_buffer_b + '\n';
50
-    message += message_buffer_c + '\n';
51
-    message += message_buffer_d + '\n';
52
-    message += F("\n</pre>\n");
48
+    message += F(" ----------------------\n");
49
+    
50
+    message += F("| ");
51
+    message += message_buffer_a;
52
+    for (int i = 0; i < (20 - message_buffer_a.length()); i++) {
53
+        message += ' ';
54
+    }
55
+    message += F(" |\n");
56
+    
57
+    message += F("| ");
58
+    message += message_buffer_b;
59
+    for (int i = 0; i < (20 - message_buffer_b.length()); i++) {
60
+        message += ' ';
61
+    }
62
+    message += F(" |\n");
63
+    
64
+    message += F("| ");
65
+    message += message_buffer_c;
66
+    for (int i = 0; i < (20 - message_buffer_c.length()); i++) {
67
+        message += ' ';
68
+    }
69
+    message += F(" |\n");
70
+    
71
+    message += F("| ");
72
+    message += message_buffer_d;
73
+    for (int i = 0; i < (20 - message_buffer_d.length()); i++) {
74
+        message += ' ';
75
+    }
76
+    message += F(" |\n");
77
+    
78
+    message += F(" ----------------------\n");
79
+    message += F("</pre>\n");
53 80
     
54 81
     message += F("\n<p>\n");
55 82
     message += F("State: ");
@@ -121,22 +148,29 @@ void wifi_setup() {
121 148
 #if defined(ARDUINO_ARCH_ESP8266)
122 149
 
123 150
     // Connect to WiFi AP
151
+    Serial.println("WiFi: initializing");
124 152
     WiFi.hostname(hostname);
125 153
     WiFi.mode(WIFI_STA);
154
+    
155
+    Serial.print("WiFi: connecting");
126 156
     WiFi.begin(WIFI_SSID, WIFI_PW);
127 157
     while (WiFi.status() != WL_CONNECTED) {
158
+        Serial.print(".");
128 159
         delay(LED_CONNECT_BLINK_INTERVAL);
129 160
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
130 161
     }
162
+    Serial.println();
131 163
     
132 164
 #elif defined(ARDUINO_ARCH_ESP32)
133 165
 
134 166
     // Set hostname workaround
167
+    Serial.println("WiFi: set hostname");
135 168
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
136 169
     WiFi.setHostname(hostname.c_str());
137 170
     
138 171
     // Workaround for WiFi connecting only every 2nd reset
139 172
     // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
173
+    Serial.println("WiFi: connection work-around");
140 174
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
141 175
         if (info.disconnected.reason == 202) {
142 176
             esp_sleep_enable_timer_wakeup(10);
@@ -146,24 +180,32 @@ void wifi_setup() {
146 180
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
147 181
 
148 182
     // Connect to WiFi AP
183
+    Serial.println("WiFi: SSID=" WIFI_SSID);
184
+    Serial.print("WiFi: connecting");
149 185
     WiFi.mode(WIFI_STA);
150 186
     WiFi.begin(WIFI_SSID, WIFI_PW);
151 187
     while (WiFi.status() != WL_CONNECTED) {
188
+        Serial.print(".");
152 189
         delay(LED_CONNECT_BLINK_INTERVAL);
153 190
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
154 191
     }
192
+    Serial.println();
155 193
     
156 194
     // Set hostname workaround
195
+    Serial.println("WiFi: set hostname work-around");
157 196
     WiFi.setHostname(hostname.c_str());
158 197
 
159 198
 #endif
160 199
 
161 200
     // Setup HTTP Server
201
+    Serial.println("WiFi: initializing HTTP server");
162 202
     MDNS.begin(hostname.c_str());
163 203
     updater.setup(&server);
164 204
     server.on("/", handleRoot);
165 205
     server.begin();
166 206
     MDNS.addService("http", "tcp", 80);
207
+    
208
+    Serial.println("WiFi: setup done");
167 209
 }
168 210
 
169 211
 void wifi_run() {

+ 5
- 0
src/main.cpp Vedi File

@@ -16,14 +16,17 @@ void setup() {
16 16
     Serial.println("Version: " FIRMWARE_VERSION);
17 17
 
18 18
 #ifdef FUNCTION_UI
19
+    Serial.println("Initializing UI");
19 20
     ui_setup();
20 21
 #endif // FUNCTION_UI
21 22
     
22 23
 #ifdef FUNCTION_CONTROL
24
+    Serial.println("Initializing Control");
23 25
     control_setup();
24 26
 #endif // FUNCTION_CONTROL
25 27
     
26 28
 #ifdef PLATFORM_ESP
29
+    Serial.println("Initializing WiFi");
27 30
     wifi_setup();
28 31
 #endif // PLATFORM_ESP
29 32
     
@@ -34,9 +37,11 @@ void setup() {
34 37
     
35 38
 #ifndef FUNCTION_UI
36 39
     // give ui unit some time to initialize
40
+    Serial.println("Waiting for UI to boot");
37 41
     delay(3000);
38 42
 #endif // ! FUNCTION_UI
39 43
     
44
+    Serial.println("Starting state machine");
40 45
     control_begin();
41 46
     
42 47
 #endif // FUNCTION_CONTROL

Loading…
Annulla
Salva