Browse Source

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

Thomas Buck 3 years ago
parent
commit
3182c9ffaa
6 changed files with 202 additions and 140 deletions
  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 View File

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

+ 1
- 0
include/config.h View File

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

+ 0
- 15
include/config_pins.h View File

57
 
57
 
58
 #ifdef FUNCTION_CONTROL
58
 #ifdef FUNCTION_CONTROL
59
 
59
 
60
-#ifdef FUNCTION_UI
61
-
62
 #define VALVE_COUNT 5
60
 #define VALVE_COUNT 5
63
 #define VALVE_PINS 9, 11, 12, 13, 14
61
 #define VALVE_PINS 9, 11, 12, 13, 14
64
 
62
 
68
 #define SWITCH_COUNT 2
66
 #define SWITCH_COUNT 2
69
 #define SWITCH_PINS 18, 19
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
 #endif // FUNCTION_CONTROL
69
 #endif // FUNCTION_CONTROL
85
 
70
 
86
 #endif // PLATFORM_ESP
71
 #endif // PLATFORM_ESP

+ 147
- 120
src/Functionality.cpp View File

38
 
38
 
39
 #include "Plants.h"
39
 #include "Plants.h"
40
 #include "Statemachine.h"
40
 #include "Statemachine.h"
41
+#include "WifiStuff.h"
41
 
42
 
42
 Plants plants(VALVE_COUNT, PUMP_COUNT, SWITCH_COUNT);
43
 Plants plants(VALVE_COUNT, PUMP_COUNT, SWITCH_COUNT);
43
 int valve_pins[VALVE_COUNT] = { VALVE_PINS };
44
 int valve_pins[VALVE_COUNT] = { VALVE_PINS };
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
 #ifdef FUNCTION_UI
192
 #ifdef FUNCTION_UI
54
 
193
 
55
 #ifndef FUNCTION_CONTROL
194
 #ifndef FUNCTION_CONTROL
176
 #endif // ! FUNCTION_CONTROL
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
 void input_keypad(void) {
318
 void input_keypad(void) {
192
     keys.scan();
319
     keys.scan();
193
     while (keys.hasEvent()) {
320
     while (keys.hasEvent()) {
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
 void ui_run(void) {
354
 void ui_run(void) {
288
     input_keypad();
355
     input_keypad();
289
     input_serial();
356
     input_serial();
322
         doing_multi_input = false;
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
 void backspace(void) {
395
 void backspace(void) {
401
 #ifndef FUNCTION_UI
421
 #ifndef FUNCTION_UI
402
     
422
     
403
     Serial.println("Initializing I2C Master");
423
     Serial.println("Initializing I2C Master");
424
+    Wire.setClock(I2C_BUS_SPEED);
404
     Wire.begin();
425
     Wire.begin();
405
     
426
     
406
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
427
 #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
428
+    Serial.println("Wait for Serial");
407
     while (!Serial);
429
     while (!Serial);
408
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
430
 #endif // DEBUG_WAIT_FOR_SERIAL_CONN
409
     
431
     
436
             sm.input(c);
458
             sm.input(c);
437
         }
459
         }
438
     }
460
     }
461
+    
462
+    input_serial();
439
 
463
 
440
 #endif // ! FUNCTION_UI
464
 #endif // ! FUNCTION_UI
441
     
465
     
488
     Wire.write(0x04); // display command
512
     Wire.write(0x04); // display command
489
     Wire.write((int8_t)num_input);
513
     Wire.write((int8_t)num_input);
490
     Wire.endTransmission();
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
 #endif // ! FUNCTION_UI
520
 #endif // ! FUNCTION_UI

+ 47
- 5
src/WifiStuff.cpp View File

45
     message += F("<h1>Giess-o-mat</h1>\n");
45
     message += F("<h1>Giess-o-mat</h1>\n");
46
     
46
     
47
     message += F("\n<pre>\n");
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
     message += F("\n<p>\n");
81
     message += F("\n<p>\n");
55
     message += F("State: ");
82
     message += F("State: ");
121
 #if defined(ARDUINO_ARCH_ESP8266)
148
 #if defined(ARDUINO_ARCH_ESP8266)
122
 
149
 
123
     // Connect to WiFi AP
150
     // Connect to WiFi AP
151
+    Serial.println("WiFi: initializing");
124
     WiFi.hostname(hostname);
152
     WiFi.hostname(hostname);
125
     WiFi.mode(WIFI_STA);
153
     WiFi.mode(WIFI_STA);
154
+    
155
+    Serial.print("WiFi: connecting");
126
     WiFi.begin(WIFI_SSID, WIFI_PW);
156
     WiFi.begin(WIFI_SSID, WIFI_PW);
127
     while (WiFi.status() != WL_CONNECTED) {
157
     while (WiFi.status() != WL_CONNECTED) {
158
+        Serial.print(".");
128
         delay(LED_CONNECT_BLINK_INTERVAL);
159
         delay(LED_CONNECT_BLINK_INTERVAL);
129
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
160
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
130
     }
161
     }
162
+    Serial.println();
131
     
163
     
132
 #elif defined(ARDUINO_ARCH_ESP32)
164
 #elif defined(ARDUINO_ARCH_ESP32)
133
 
165
 
134
     // Set hostname workaround
166
     // Set hostname workaround
167
+    Serial.println("WiFi: set hostname");
135
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
168
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
136
     WiFi.setHostname(hostname.c_str());
169
     WiFi.setHostname(hostname.c_str());
137
     
170
     
138
     // Workaround for WiFi connecting only every 2nd reset
171
     // Workaround for WiFi connecting only every 2nd reset
139
     // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
172
     // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
173
+    Serial.println("WiFi: connection work-around");
140
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
174
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
141
         if (info.disconnected.reason == 202) {
175
         if (info.disconnected.reason == 202) {
142
             esp_sleep_enable_timer_wakeup(10);
176
             esp_sleep_enable_timer_wakeup(10);
146
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
180
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
147
 
181
 
148
     // Connect to WiFi AP
182
     // Connect to WiFi AP
183
+    Serial.println("WiFi: SSID=" WIFI_SSID);
184
+    Serial.print("WiFi: connecting");
149
     WiFi.mode(WIFI_STA);
185
     WiFi.mode(WIFI_STA);
150
     WiFi.begin(WIFI_SSID, WIFI_PW);
186
     WiFi.begin(WIFI_SSID, WIFI_PW);
151
     while (WiFi.status() != WL_CONNECTED) {
187
     while (WiFi.status() != WL_CONNECTED) {
188
+        Serial.print(".");
152
         delay(LED_CONNECT_BLINK_INTERVAL);
189
         delay(LED_CONNECT_BLINK_INTERVAL);
153
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
190
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
154
     }
191
     }
192
+    Serial.println();
155
     
193
     
156
     // Set hostname workaround
194
     // Set hostname workaround
195
+    Serial.println("WiFi: set hostname work-around");
157
     WiFi.setHostname(hostname.c_str());
196
     WiFi.setHostname(hostname.c_str());
158
 
197
 
159
 #endif
198
 #endif
160
 
199
 
161
     // Setup HTTP Server
200
     // Setup HTTP Server
201
+    Serial.println("WiFi: initializing HTTP server");
162
     MDNS.begin(hostname.c_str());
202
     MDNS.begin(hostname.c_str());
163
     updater.setup(&server);
203
     updater.setup(&server);
164
     server.on("/", handleRoot);
204
     server.on("/", handleRoot);
165
     server.begin();
205
     server.begin();
166
     MDNS.addService("http", "tcp", 80);
206
     MDNS.addService("http", "tcp", 80);
207
+    
208
+    Serial.println("WiFi: setup done");
167
 }
209
 }
168
 
210
 
169
 void wifi_run() {
211
 void wifi_run() {

+ 5
- 0
src/main.cpp View File

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

Loading…
Cancel
Save