Browse Source

small changes while debugging wifi issues

Thomas Buck 3 years ago
parent
commit
066601c189
3 changed files with 40 additions and 29 deletions
  1. 12
    12
      include/config.h
  2. 27
    17
      src/main.cpp
  3. 1
    0
      src/relais.cpp

+ 12
- 12
include/config.h View File

15
 #define __ESP_ENV_CONFIG__
15
 #define __ESP_ENV_CONFIG__
16
 
16
 
17
 // Sketch version
17
 // Sketch version
18
-const char* esp_relais_version = "0.1.0";
18
+#define ESP_RELAIS_VERSION "0.1.0"
19
 
19
 
20
 // location of sensor, used in DB and hostname
20
 // location of sensor, used in DB and hostname
21
-//const char* sensor_location = "livingroom";
22
-//const char* sensor_location = "bedroom";
23
-const char* sensor_location = "bathroom";
24
-//const char* sensor_location = "kitchen";
25
-//const char* sensor_location = "hallway";
26
-//const char* sensor_location = "tent";
27
-//const char* sensor_location = "storage";
28
-//const char* sensor_location = "greenhouse";
29
-//const char* sensor_location = "test";
21
+//#define SENSOR_LOCATION "livingroom"
22
+//#define SENSOR_LOCATION "bedroom"
23
+//#define SENSOR_LOCATION "bathroom"
24
+//#define SENSOR_LOCATION "kitchen"
25
+//#define SENSOR_LOCATION "hallway"
26
+//#define SENSOR_LOCATION "tent"
27
+//#define SENSOR_LOCATION "storage"
28
+//#define SENSOR_LOCATION "greenhouse"
29
+#define SENSOR_LOCATION "test"
30
 
30
 
31
 #define SENSOR_HOSTNAME_PREFIX "relais-"
31
 #define SENSOR_HOSTNAME_PREFIX "relais-"
32
 
32
 
33
 // WiFi AP settings
33
 // WiFi AP settings
34
-const char* ssid = "INSERT_SSID_HERE";
35
-const char* password = "INSERT_PASSPHRASE_HERE";
34
+#define WIFI_SSID "INSERT_SSID_HERE"
35
+#define WIFI_PASS "INSERT_PASSPHRASE_HERE"
36
 
36
 
37
 // MQTT settings
37
 // MQTT settings
38
 #define MQTT_HOST "10.23.42.14"
38
 #define MQTT_HOST "10.23.42.14"

+ 27
- 17
src/main.cpp View File

83
         // small light from 20:00 to 10:00
83
         // small light from 20:00 to 10:00
84
         relais_state[0] = true;
84
         relais_state[0] = true;
85
         relais_state[1] = false;
85
         relais_state[1] = false;
86
-        auto_set_state = F("Small Light at Night (") + timeClient.getFormattedTime() + F(")");
86
+        auto_set_state = String(F("Small Light at Night (")) + String(timeClient.getFormattedTime()) + String(F(")"));
87
     } else {
87
     } else {
88
         // big light from 10:00 to 20:00
88
         // big light from 10:00 to 20:00
89
         relais_state[0] = false;
89
         relais_state[0] = false;
90
         relais_state[1] = true;
90
         relais_state[1] = true;
91
-        auto_set_state = F("Big Light while Daylight (") + timeClient.getFormattedTime() + F(")");
91
+        auto_set_state = String(F("Big Light while Daylight (")) + String(timeClient.getFormattedTime()) + String(F(")"));
92
     }
92
     }
93
 
93
 
94
     for (int i = 0; i < RELAIS_COUNT; i++) {
94
     for (int i = 0; i < RELAIS_COUNT; i++) {
128
 
128
 
129
 #ifdef ENABLE_NTP
129
 #ifdef ENABLE_NTP
130
     message += F("<p>Time from NTP: ");
130
     message += F("<p>Time from NTP: ");
131
-    message += timeClient.getFormattedTime();
131
+    message += String(timeClient.getFormattedTime());
132
     message += F("</p>\n");
132
     message += F("</p>\n");
133
 
133
 
134
     message += F("<p>Auto-State: ");
134
     message += F("<p>Auto-State: ");
141
     message += F("min</p>\n");
141
     message += F("min</p>\n");
142
 #endif // ENABLE_NTP
142
 #endif // ENABLE_NTP
143
 
143
 
144
+    message += F("<p><a href=\"/\">Refresh Page</a></p>\n");
145
+
144
     message += F("\n<p>\n");
146
     message += F("\n<p>\n");
145
     message += F("Version: ");
147
     message += F("Version: ");
146
-    message += esp_relais_version;
148
+    message += ESP_RELAIS_VERSION;
147
     message += F("\n<br>\n");
149
     message += F("\n<br>\n");
148
     message += F("Location: ");
150
     message += F("Location: ");
149
-    message += sensor_location;
151
+    message += SENSOR_LOCATION;
150
     message += F("\n<br>\n");
152
     message += F("\n<br>\n");
151
     message += F("MAC: ");
153
     message += F("MAC: ");
152
     message += WiFi.macAddress();
154
     message += WiFi.macAddress();
307
             // Once connected, publish an announcement...
309
             // Once connected, publish an announcement...
308
             //mqtt.publish("outTopic", "hello world");
310
             //mqtt.publish("outTopic", "hello world");
309
             // ... and resubscribe
311
             // ... and resubscribe
310
-            mqtt.subscribe(sensor_location);
312
+            mqtt.subscribe(SENSOR_LOCATION);
311
         } else {
313
         } else {
312
             // Wait 5 seconds before retrying
314
             // Wait 5 seconds before retrying
313
             delay(5000);
315
             delay(5000);
338
 
340
 
339
     // Build hostname string
341
     // Build hostname string
340
     String hostname = SENSOR_HOSTNAME_PREFIX;
342
     String hostname = SENSOR_HOSTNAME_PREFIX;
341
-    hostname += sensor_location;
343
+    hostname += SENSOR_LOCATION;
342
 
344
 
343
 #if defined(ARDUINO_ARCH_ESP8266)
345
 #if defined(ARDUINO_ARCH_ESP8266)
344
 
346
 
345
     // Connect to WiFi AP
347
     // Connect to WiFi AP
346
-    Serial.print("Connecting to WiFi");
348
+    Serial.print("Connecting to WiFi ");
347
     WiFi.hostname(hostname);
349
     WiFi.hostname(hostname);
348
     WiFi.mode(WIFI_STA);
350
     WiFi.mode(WIFI_STA);
349
-    WiFi.begin(ssid, password);
351
+    WiFi.begin(WIFI_SSID, WIFI_PASS);
350
     while (WiFi.status() != WL_CONNECTED) {
352
     while (WiFi.status() != WL_CONNECTED) {
351
-        Serial.print(".");
353
+        Serial.print(WiFi.status());
354
+        Serial.print(" ");
355
+
352
         delay(LED_CONNECT_BLINK_INTERVAL);
356
         delay(LED_CONNECT_BLINK_INTERVAL);
353
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
357
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
354
     }
358
     }
355
     Serial.println();
359
     Serial.println();
360
+
361
+    Serial.print("Got local IP ");
362
+    Serial.println(WiFi.localIP().toString());
356
     
363
     
357
 #elif defined(ARDUINO_ARCH_ESP32)
364
 #elif defined(ARDUINO_ARCH_ESP32)
358
 
365
 
371
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
378
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
372
 
379
 
373
     // Connect to WiFi AP
380
     // Connect to WiFi AP
381
+    Serial.print("Connecting to WiFi");
374
     WiFi.mode(WIFI_STA);
382
     WiFi.mode(WIFI_STA);
375
-    WiFi.begin(ssid, password);
383
+    WiFi.begin(WIFI_SSID, WIFI_PASS);
376
     while (WiFi.status() != WL_CONNECTED) {
384
     while (WiFi.status() != WL_CONNECTED) {
385
+        Serial.print(".");
377
         delay(LED_CONNECT_BLINK_INTERVAL);
386
         delay(LED_CONNECT_BLINK_INTERVAL);
378
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
387
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
379
     }
388
     }
389
+    Serial.println();
380
     
390
     
381
     // Set hostname workaround
391
     // Set hostname workaround
382
     WiFi.setHostname(hostname.c_str());
392
     WiFi.setHostname(hostname.c_str());
440
 void writeDatabase() {
450
 void writeDatabase() {
441
     if (found_bme1) {
451
     if (found_bme1) {
442
         InfluxData measurement("environment");
452
         InfluxData measurement("environment");
443
-        measurement.addTag("location", sensor_location);
453
+        measurement.addTag("location", SENSOR_LOCATION);
444
         measurement.addTag("placement", "1");
454
         measurement.addTag("placement", "1");
445
         measurement.addTag("device", WiFi.macAddress());
455
         measurement.addTag("device", WiFi.macAddress());
446
         measurement.addTag("sensor", "bme280");
456
         measurement.addTag("sensor", "bme280");
453
     }
463
     }
454
     if (found_bme2) {
464
     if (found_bme2) {
455
         InfluxData measurement("environment");
465
         InfluxData measurement("environment");
456
-        measurement.addTag("location", sensor_location);
466
+        measurement.addTag("location", SENSOR_LOCATION);
457
         measurement.addTag("placement", "2");
467
         measurement.addTag("placement", "2");
458
         measurement.addTag("device", WiFi.macAddress());
468
         measurement.addTag("device", WiFi.macAddress());
459
         measurement.addTag("sensor", "bme280");
469
         measurement.addTag("sensor", "bme280");
467
 
477
 
468
     if (found_sht) {
478
     if (found_sht) {
469
         InfluxData measurement("environment");
479
         InfluxData measurement("environment");
470
-        measurement.addTag("location", sensor_location);
480
+        measurement.addTag("location", SENSOR_LOCATION);
471
         measurement.addTag("device", WiFi.macAddress());
481
         measurement.addTag("device", WiFi.macAddress());
472
         measurement.addTag("sensor", "sht21");
482
         measurement.addTag("sensor", "sht21");
473
 
483
 
481
         int moisture = moisture_read(i);
491
         int moisture = moisture_read(i);
482
         if (moisture < moisture_max()) {
492
         if (moisture < moisture_max()) {
483
             InfluxData measurement("moisture");
493
             InfluxData measurement("moisture");
484
-            measurement.addTag("location", sensor_location);
494
+            measurement.addTag("location", SENSOR_LOCATION);
485
             measurement.addTag("device", WiFi.macAddress());
495
             measurement.addTag("device", WiFi.macAddress());
486
             measurement.addTag("sensor", String(i + 1));
496
             measurement.addTag("sensor", String(i + 1));
487
 
497
 
537
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
547
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
538
     }
548
     }
539
     
549
     
540
-    // reset ESP every 6h to be safe
541
-    if (millis() >= (6 * 60 * 60 * 1000)) {
550
+    // reset ESP every 48h to be safe
551
+    if (millis() >= (48 * 60 * 60 * 1000)) {
542
         ESP.restart();
552
         ESP.restart();
543
     }
553
     }
544
 }
554
 }

+ 1
- 0
src/relais.cpp View File

12
  */
12
  */
13
 
13
 
14
 #include <Arduino.h>
14
 #include <Arduino.h>
15
+#include "config.h"
15
 #include "relais.h"
16
 #include "relais.h"
16
 
17
 
17
 #if defined(ARDUINO_ARCH_ESP8266)
18
 #if defined(ARDUINO_ARCH_ESP8266)

Loading…
Cancel
Save