From 84c0c2f8a4f9227a1f1e5c0ddc0435b96dc6f0ac Mon Sep 17 00:00:00 2001 From: balu Date: Fri, 22 Oct 2021 16:18:40 +0200 Subject: [PATCH 01/25] starts Webportal always Does also start the webportal if wifi is already configures makes it easier to change settings if needed --- src/esp8266-vindriktning-particle-sensor.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/esp8266-vindriktning-particle-sensor.ino b/src/esp8266-vindriktning-particle-sensor.ino index 0265ac2..9a576e3 100644 --- a/src/esp8266-vindriktning-particle-sensor.ino +++ b/src/esp8266-vindriktning-particle-sensor.ino @@ -119,6 +119,7 @@ void loop() { ArduinoOTA.handle(); SerialCom::handleUart(state); mqttClient.loop(); + wifiManager.process(); const uint32_t currentMillis = millis(); if (currentMillis - statusPublishPreviousMillis >= statusPublishInterval) { @@ -161,6 +162,8 @@ void setupWifi() { // This is most likely a logic error which could be fixed otherwise Config::load(); } + + wifiManager.startWebPortal(); } void resetWifiSettingsAndReboot() { From d7cfa5e782c215d08285716f36ded35c5c338722 Mon Sep 17 00:00:00 2001 From: balu Date: Fri, 22 Oct 2021 16:20:10 +0200 Subject: [PATCH 02/25] extend possible characters for mqtt Password --- src/Config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Config.h b/src/Config.h index 4904b11..da3e89f 100644 --- a/src/Config.h +++ b/src/Config.h @@ -7,7 +7,7 @@ namespace Config { char mqtt_server[80] = "example.tld"; char username[24] = ""; - char password[24] = ""; + char password[72] = ""; void save() { DynamicJsonDocument json(512); From 2bd6ae6a9430fec5668e234ec9ae12de34e0cbec Mon Sep 17 00:00:00 2001 From: balu Date: Fri, 29 Oct 2021 22:56:05 +0200 Subject: [PATCH 03/25] rename src for Arduino IDE --- {src => esp8266-vindriktning-particle-sensor}/Config.h | 0 {src => esp8266-vindriktning-particle-sensor}/SerialCom.h | 0 {src => esp8266-vindriktning-particle-sensor}/Types.h | 0 .../esp8266-vindriktning-particle-sensor.ino | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename {src => esp8266-vindriktning-particle-sensor}/Config.h (100%) rename {src => esp8266-vindriktning-particle-sensor}/SerialCom.h (100%) rename {src => esp8266-vindriktning-particle-sensor}/Types.h (100%) rename {src => esp8266-vindriktning-particle-sensor}/esp8266-vindriktning-particle-sensor.ino (100%) diff --git a/src/Config.h b/esp8266-vindriktning-particle-sensor/Config.h similarity index 100% rename from src/Config.h rename to esp8266-vindriktning-particle-sensor/Config.h diff --git a/src/SerialCom.h b/esp8266-vindriktning-particle-sensor/SerialCom.h similarity index 100% rename from src/SerialCom.h rename to esp8266-vindriktning-particle-sensor/SerialCom.h diff --git a/src/Types.h b/esp8266-vindriktning-particle-sensor/Types.h similarity index 100% rename from src/Types.h rename to esp8266-vindriktning-particle-sensor/Types.h diff --git a/src/esp8266-vindriktning-particle-sensor.ino b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino similarity index 100% rename from src/esp8266-vindriktning-particle-sensor.ino rename to esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino From 840e86ee024a56d9990832b17c53ecb2efb04e30 Mon Sep 17 00:00:00 2001 From: balu Date: Fri, 29 Oct 2021 22:58:08 +0200 Subject: [PATCH 04/25] disabled subscribe to command --- .../esp8266-vindriktning-particle-sensor.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino index 9a576e3..1bc9703 100644 --- a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino +++ b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino @@ -34,7 +34,7 @@ char identifier[24]; #define AVAILABILITY_OFFLINE "offline" char MQTT_TOPIC_AVAILABILITY[128]; char MQTT_TOPIC_STATE[128]; -char MQTT_TOPIC_COMMAND[128]; +//char MQTT_TOPIC_COMMAND[128]; char MQTT_TOPIC_AUTOCONF_WIFI_SENSOR[128]; char MQTT_TOPIC_AUTOCONF_PM25_SENSOR[128]; @@ -62,7 +62,7 @@ void setup() { snprintf(identifier, sizeof(identifier), "VINDRIKTNING-%X", ESP.getChipId()); snprintf(MQTT_TOPIC_AVAILABILITY, 127, "%s/%s/status", FIRMWARE_PREFIX, identifier); snprintf(MQTT_TOPIC_STATE, 127, "%s/%s/state", FIRMWARE_PREFIX, identifier); - snprintf(MQTT_TOPIC_COMMAND, 127, "%s/%s/command", FIRMWARE_PREFIX, identifier); + //snprintf(MQTT_TOPIC_COMMAND, 127, "%s/%s/command", FIRMWARE_PREFIX, identifier); snprintf(MQTT_TOPIC_AUTOCONF_PM25_SENSOR, 127, "homeassistant/sensor/%s/%s_pm25/config", FIRMWARE_PREFIX, identifier); snprintf(MQTT_TOPIC_AUTOCONF_WIFI_SENSOR, 127, "homeassistant/sensor/%s/%s_wifi/config", FIRMWARE_PREFIX, identifier); @@ -179,7 +179,7 @@ void mqttReconnect() { publishAutoConfig(); // Make sure to subscribe after polling the status so that we never execute commands with the default data - mqttClient.subscribe(MQTT_TOPIC_COMMAND); + //mqttClient.subscribe(MQTT_TOPIC_COMMAND); break; } delay(5000); From 9bc23e1e57693e215d72a7bde8b6509ece31918f Mon Sep 17 00:00:00 2001 From: balu Date: Fri, 29 Oct 2021 23:02:59 +0200 Subject: [PATCH 05/25] add Topic param --- esp8266-vindriktning-particle-sensor/Config.h | 11 +++++++++-- .../esp8266-vindriktning-particle-sensor.ino | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/esp8266-vindriktning-particle-sensor/Config.h b/esp8266-vindriktning-particle-sensor/Config.h index da3e89f..5a8e697 100644 --- a/esp8266-vindriktning-particle-sensor/Config.h +++ b/esp8266-vindriktning-particle-sensor/Config.h @@ -3,17 +3,23 @@ #include #include +#define JSON_SIZE 512 + namespace Config { char mqtt_server[80] = "example.tld"; + char mqtt_topic[80] = "esp8266-vindriktning-particle-sensor/%s"; char username[24] = ""; char password[72] = ""; + void save() { - DynamicJsonDocument json(512); + DynamicJsonDocument json(JSON_SIZE); json["mqtt_server"] = mqtt_server; + json["mqtt_topic"] = mqtt_topic; json["username"] = username; json["password"] = password; + File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { @@ -35,10 +41,11 @@ namespace Config { std::unique_ptr buf(new char[size]); configFile.readBytes(buf.get(), size); - DynamicJsonDocument json(512); + DynamicJsonDocument json(JSON_SIZE); if (DeserializationError::Ok == deserializeJson(json, buf.get())) { strcpy(mqtt_server, json["mqtt_server"]); + strcpy(mqtt_topic, json["mqtt_topic"]); strcpy(username, json["username"]); strcpy(password, json["password"]); } diff --git a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino index 1bc9703..58ecee9 100644 --- a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino +++ b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino @@ -21,6 +21,7 @@ PubSubClient mqttClient; WiFiManagerParameter custom_mqtt_server("server", "mqtt server", Config::mqtt_server, sizeof(Config::mqtt_server)); WiFiManagerParameter custom_mqtt_user("user", "MQTT username", Config::username, sizeof(Config::username)); WiFiManagerParameter custom_mqtt_pass("pass", "MQTT password", Config::password, sizeof(Config::password)); +WiFiManagerParameter custom_mqtt_topic("topic", "MQTT Topic", Config::mqtt_topic, sizeof(Config::mqtt_topic)); uint32_t lastMqttConnectionAttempt = 0; const uint16_t mqttConnectionInterval = 60000; // 1 minute = 60 seconds = 60000 milliseconds From be0ace4e076c06b0c1709d7e67fe940d5205ec63 Mon Sep 17 00:00:00 2001 From: balu Date: Fri, 29 Oct 2021 23:08:18 +0200 Subject: [PATCH 06/25] disabled autoconf --- .../esp8266-vindriktning-particle-sensor.ino | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino index 58ecee9..7341685 100644 --- a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino +++ b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino @@ -157,6 +157,8 @@ void setupWifi() { if (shouldSaveConfig) { Config::save(); + //reset save flag + shouldSaveConfig = false; } else { // For some reason, the read values get overwritten in this function // To combat this, we just reload the config @@ -177,7 +179,7 @@ void mqttReconnect() { for (uint8_t attempt = 0; attempt < 3; ++attempt) { if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_AVAILABILITY, 1, true, AVAILABILITY_OFFLINE)) { mqttClient.publish(MQTT_TOPIC_AVAILABILITY, AVAILABILITY_ONLINE, true); - publishAutoConfig(); + //publishAutoConfig(); // Make sure to subscribe after polling the status so that we never execute commands with the default data //mqttClient.subscribe(MQTT_TOPIC_COMMAND); @@ -210,7 +212,7 @@ void publishState() { void mqttCallback(char* topic, uint8_t* payload, unsigned int length) { } -void publishAutoConfig() { +/*void publishAutoConfig() { char mqttPayload[2048]; DynamicJsonDocument device(256); DynamicJsonDocument autoconfPayload(1024); @@ -254,4 +256,4 @@ void publishAutoConfig() { mqttClient.publish(&MQTT_TOPIC_AUTOCONF_PM25_SENSOR[0], &mqttPayload[0], true); autoconfPayload.clear(); -} +}*/ From 122dcbd110340c43e4495ec66ba0167123c8f6b6 Mon Sep 17 00:00:00 2001 From: balu Date: Fri, 29 Oct 2021 23:41:22 +0200 Subject: [PATCH 07/25] disabled all mqtt stuff that i personaly do not need --- .../esp8266-vindriktning-particle-sensor.ino | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino index 7341685..d47cd83 100644 --- a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino +++ b/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino @@ -30,15 +30,15 @@ uint32_t statusPublishPreviousMillis = 0; const uint16_t statusPublishInterval = 30000; // 30 seconds = 30000 milliseconds char identifier[24]; -#define FIRMWARE_PREFIX "esp8266-vindriktning-particle-sensor" -#define AVAILABILITY_ONLINE "online" -#define AVAILABILITY_OFFLINE "offline" -char MQTT_TOPIC_AVAILABILITY[128]; +/**#define FIRMWARE_PREFIX "esp8266-vindriktning-particle-sensor"*/ +#define AVAILABILITY_ONLINE "{ \"state\" : \"online\" }" +#define AVAILABILITY_OFFLINE "{ \"state\" : \"offline\" }" +//char MQTT_TOPIC_AVAILABILITY[128]; char MQTT_TOPIC_STATE[128]; //char MQTT_TOPIC_COMMAND[128]; -char MQTT_TOPIC_AUTOCONF_WIFI_SENSOR[128]; -char MQTT_TOPIC_AUTOCONF_PM25_SENSOR[128]; +//char MQTT_TOPIC_AUTOCONF_WIFI_SENSOR[128]; +//char MQTT_TOPIC_AUTOCONF_PM25_SENSOR[128]; bool shouldSaveConfig = false; @@ -61,19 +61,21 @@ void setup() { delay(3000); snprintf(identifier, sizeof(identifier), "VINDRIKTNING-%X", ESP.getChipId()); - snprintf(MQTT_TOPIC_AVAILABILITY, 127, "%s/%s/status", FIRMWARE_PREFIX, identifier); - snprintf(MQTT_TOPIC_STATE, 127, "%s/%s/state", FIRMWARE_PREFIX, identifier); + //snprintf(MQTT_TOPIC_AVAILABILITY, 127, "%s/%s/status", FIRMWARE_PREFIX, identifier); //snprintf(MQTT_TOPIC_COMMAND, 127, "%s/%s/command", FIRMWARE_PREFIX, identifier); - - snprintf(MQTT_TOPIC_AUTOCONF_PM25_SENSOR, 127, "homeassistant/sensor/%s/%s_pm25/config", FIRMWARE_PREFIX, identifier); - snprintf(MQTT_TOPIC_AUTOCONF_WIFI_SENSOR, 127, "homeassistant/sensor/%s/%s_wifi/config", FIRMWARE_PREFIX, identifier); + //snprintf(MQTT_TOPIC_AUTOCONF_PM25_SENSOR, 127, "homeassistant/sensor/%s/%s_pm25/config", FIRMWARE_PREFIX, identifier); + //snprintf(MQTT_TOPIC_AUTOCONF_WIFI_SENSOR, 127, "homeassistant/sensor/%s/%s_wifi/config", FIRMWARE_PREFIX, identifier); WiFi.hostname(identifier); Config::load(); - + setupWifi(); setupOTA(); + + snprintf(MQTT_TOPIC_STATE, 127, Config::mqtt_topic, identifier); + Serial.printf("MQTT Topic State: %s\n", MQTT_TOPIC_STATE); + mqttClient.setServer(Config::mqtt_server, 1883); mqttClient.setKeepAlive(10); mqttClient.setBufferSize(2048); @@ -121,6 +123,21 @@ void loop() { SerialCom::handleUart(state); mqttClient.loop(); wifiManager.process(); + if(shouldSaveConfig){ + //save if needed + Serial.println("Get Values from web and save\n"); + strcpy(Config::mqtt_server, custom_mqtt_server.getValue()); + strcpy(Config::username, custom_mqtt_user.getValue()); + strcpy(Config::password, custom_mqtt_pass.getValue()); + strcpy(Config::mqtt_topic, custom_mqtt_topic.getValue()); + Config::save(); + snprintf(MQTT_TOPIC_STATE, 127, Config::mqtt_topic, identifier); + Serial.printf("MQTT Topic State: %s\n", MQTT_TOPIC_STATE); + //reset save flag + shouldSaveConfig = false; + + + } const uint32_t currentMillis = millis(); if (currentMillis - statusPublishPreviousMillis >= statusPublishInterval) { @@ -146,6 +163,7 @@ void setupWifi() { wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_user); wifiManager.addParameter(&custom_mqtt_pass); + wifiManager.addParameter(&custom_mqtt_topic); WiFi.hostname(identifier); wifiManager.autoConnect(identifier); @@ -154,6 +172,7 @@ void setupWifi() { strcpy(Config::mqtt_server, custom_mqtt_server.getValue()); strcpy(Config::username, custom_mqtt_user.getValue()); strcpy(Config::password, custom_mqtt_pass.getValue()); + strcpy(Config::mqtt_topic, custom_mqtt_topic.getValue()); if (shouldSaveConfig) { Config::save(); @@ -177,8 +196,8 @@ void resetWifiSettingsAndReboot() { void mqttReconnect() { for (uint8_t attempt = 0; attempt < 3; ++attempt) { - if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_AVAILABILITY, 1, true, AVAILABILITY_OFFLINE)) { - mqttClient.publish(MQTT_TOPIC_AVAILABILITY, AVAILABILITY_ONLINE, true); + if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_STATE, 1, true, AVAILABILITY_OFFLINE)) { + mqttClient.publish(MQTT_TOPIC_STATE, AVAILABILITY_ONLINE, true); //publishAutoConfig(); // Make sure to subscribe after polling the status so that we never execute commands with the default data From 7dfd85b483da85c2ede0fe0b8482da0313454dfe Mon Sep 17 00:00:00 2001 From: balu Date: Sat, 30 Oct 2021 00:11:40 +0200 Subject: [PATCH 08/25] Using new name for config file to prefent bootloop if old config file is present --- esp8266-vindriktning-particle-sensor/Config.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/esp8266-vindriktning-particle-sensor/Config.h b/esp8266-vindriktning-particle-sensor/Config.h index 5a8e697..0cdd2b8 100644 --- a/esp8266-vindriktning-particle-sensor/Config.h +++ b/esp8266-vindriktning-particle-sensor/Config.h @@ -14,14 +14,20 @@ namespace Config { void save() { + Serial.println("save config\n"); DynamicJsonDocument json(JSON_SIZE); json["mqtt_server"] = mqtt_server; json["mqtt_topic"] = mqtt_topic; json["username"] = username; json["password"] = password; + + Serial.println("== Save == \n"); + Serial.printf("mqtt_server: %s\n", mqtt_server); + Serial.printf("mqtt_topic: %s\n", mqtt_topic); + Serial.printf("mqtt_user: %s\n", username); - File configFile = SPIFFS.open("/config.json", "w"); + File configFile = SPIFFS.open("/conf.json", "w"); if (!configFile) { return; } @@ -31,10 +37,13 @@ namespace Config { } void load() { + Serial.println("load config\n"); if (SPIFFS.begin()) { - if (SPIFFS.exists("/config.json")) { - File configFile = SPIFFS.open("/config.json", "r"); + SPIFFS.remove("/config.json"); + } + if (SPIFFS.exists("/conf.json")) { + File configFile = SPIFFS.open("/conf.json", "r"); if (configFile) { const size_t size = configFile.size(); @@ -48,6 +57,10 @@ namespace Config { strcpy(mqtt_topic, json["mqtt_topic"]); strcpy(username, json["username"]); strcpy(password, json["password"]); + Serial.println("== Load == \n"); + Serial.printf("mqtt_server: %s\n", mqtt_server); + Serial.printf("mqtt_topic: %s\n", mqtt_topic); + Serial.printf("mqtt_user: %s\n", username); } } } From b3bd06ae4c97a7ec2929e75ce310796d22b87029 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Mon, 1 Nov 2021 14:05:39 +0100 Subject: [PATCH 09/25] set serial monitor speed correct --- platformio.ini | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 88ee58e..85a6f7d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,4 +15,5 @@ framework = arduino lib_deps = bblanchon/ArduinoJson @ ^6.18.3 knolleary/PubSubClient @ ^2.8 - tzapu/WiFiManager @ ^0.16.0 \ No newline at end of file + tzapu/WiFiManager @ ^0.16.0 +monitor_speed = 115200 \ No newline at end of file From 6f498514e26de88de00cbf9ae9d49f05895221dd Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Mon, 1 Nov 2021 14:06:00 +0100 Subject: [PATCH 10/25] move to correct folder for vsc --- {esp8266-vindriktning-particle-sensor => src}/Config.h | 0 {esp8266-vindriktning-particle-sensor => src}/SerialCom.h | 0 {esp8266-vindriktning-particle-sensor => src}/Types.h | 0 .../esp8266-vindriktning-particle-sensor.ino | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename {esp8266-vindriktning-particle-sensor => src}/Config.h (100%) rename {esp8266-vindriktning-particle-sensor => src}/SerialCom.h (100%) rename {esp8266-vindriktning-particle-sensor => src}/Types.h (100%) rename {esp8266-vindriktning-particle-sensor => src}/esp8266-vindriktning-particle-sensor.ino (100%) diff --git a/esp8266-vindriktning-particle-sensor/Config.h b/src/Config.h similarity index 100% rename from esp8266-vindriktning-particle-sensor/Config.h rename to src/Config.h diff --git a/esp8266-vindriktning-particle-sensor/SerialCom.h b/src/SerialCom.h similarity index 100% rename from esp8266-vindriktning-particle-sensor/SerialCom.h rename to src/SerialCom.h diff --git a/esp8266-vindriktning-particle-sensor/Types.h b/src/Types.h similarity index 100% rename from esp8266-vindriktning-particle-sensor/Types.h rename to src/Types.h diff --git a/esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino b/src/esp8266-vindriktning-particle-sensor.ino similarity index 100% rename from esp8266-vindriktning-particle-sensor/esp8266-vindriktning-particle-sensor.ino rename to src/esp8266-vindriktning-particle-sensor.ino From ddc41602e458f0470b59353b7d80863966998a11 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Wed, 10 Nov 2021 09:03:00 +0100 Subject: [PATCH 11/25] use wifimanager beta for always up config --- platformio.ini | 2 +- src/esp8266-vindriktning-particle-sensor.ino | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index 85a6f7d..fa856d7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,5 +15,5 @@ framework = arduino lib_deps = bblanchon/ArduinoJson @ ^6.18.3 knolleary/PubSubClient @ ^2.8 - tzapu/WiFiManager @ ^0.16.0 + tzapu/WiFiManager @ 2.0.5-beta monitor_speed = 115200 \ No newline at end of file diff --git a/src/esp8266-vindriktning-particle-sensor.ino b/src/esp8266-vindriktning-particle-sensor.ino index d47cd83..6014764 100644 --- a/src/esp8266-vindriktning-particle-sensor.ino +++ b/src/esp8266-vindriktning-particle-sensor.ino @@ -74,6 +74,7 @@ void setup() { setupOTA(); snprintf(MQTT_TOPIC_STATE, 127, Config::mqtt_topic, identifier); + Serial.printf("MQTT server: %s\n", Config::mqtt_server); Serial.printf("MQTT Topic State: %s\n", MQTT_TOPIC_STATE); mqttClient.setServer(Config::mqtt_server, 1883); @@ -135,7 +136,6 @@ void loop() { Serial.printf("MQTT Topic State: %s\n", MQTT_TOPIC_STATE); //reset save flag shouldSaveConfig = false; - } From fedb1cf7f62ee313981c27a3f8d19ff958a5f892 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo <79205621+wvtbg@users.noreply.github.com> Date: Thu, 25 Nov 2021 16:05:29 +0100 Subject: [PATCH 12/25] Create platformio.yml add build for platform io --- .github/workflows/platformio.yml | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 .github/workflows/platformio.yml diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml new file mode 100644 index 0000000..0bba49e --- /dev/null +++ b/.github/workflows/platformio.yml @@ -0,0 +1,40 @@ +name: PlatformIO CI + +on: [push] + +jobs: + build: + + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + + steps: + - uses: actions/checkout@v2 + - name: Cache pip + uses: actions/cache@v2 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + - name: Cache PlatformIO + uses: actions/cache@v2 + with: + path: ~/.platformio + key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} + + - name: Set up Python + uses: actions/setup-python@v2 + + - name: Install PlatformIO + run: | + python -m pip install --upgrade pip + pip install --upgrade platformio + + - name: Install library dependencies + run: pio lib -g install 1 + + - name: Run PlatformIO + run: pio ci --lib="." --board=d1_mini From 054dfbf783148db6c6ecf6ff13d049e9c425ded4 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 16:13:30 +0100 Subject: [PATCH 13/25] dont test but run --- .github/workflows/platformio.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index 0bba49e..60cea41 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -37,4 +37,4 @@ jobs: run: pio lib -g install 1 - name: Run PlatformIO - run: pio ci --lib="." --board=d1_mini + run: pio run From 710198f65cac776c4b5e65258a951a31351cca4d Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 16:20:02 +0100 Subject: [PATCH 14/25] manually download wifimanager beta version --- .github/workflows/platformio.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index 60cea41..074eae2 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -34,7 +34,10 @@ jobs: pip install --upgrade platformio - name: Install library dependencies - run: pio lib -g install 1 + run: | + pio lib -g install 1 + wget https://github.com/tzapu/WiFiManager/archive/refs/tags/2.0.5-beta.zip -O /tmp/WiFiManager.zip + unzip /tmp/WiFiManager.zip -d /tmp/ - name: Run PlatformIO - run: pio run + run: pio run --lib="/tmp/WiFiManager" From e8249c777d3db1c836fd89e0c27bd587a1dce296 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 16:27:38 +0100 Subject: [PATCH 15/25] lib no option on run --- .github/workflows/platformio.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index 074eae2..bf2b095 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -40,4 +40,4 @@ jobs: unzip /tmp/WiFiManager.zip -d /tmp/ - name: Run PlatformIO - run: pio run --lib="/tmp/WiFiManager" + run: pio run \ No newline at end of file From dc8497d175aa3113e49c72fb0421f8a143dd852f Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 16:36:43 +0100 Subject: [PATCH 16/25] call install lib to get wifimanager --- .github/workflows/platformio.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index bf2b095..9ebfb5e 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -35,9 +35,8 @@ jobs: - name: Install library dependencies run: | - pio lib -g install 1 wget https://github.com/tzapu/WiFiManager/archive/refs/tags/2.0.5-beta.zip -O /tmp/WiFiManager.zip - unzip /tmp/WiFiManager.zip -d /tmp/ + pio lib install /tmp/WiFiManager.zip - name: Run PlatformIO run: pio run \ No newline at end of file From 2f70519dacc3f0ac9bea339249e24f2a6f91b7f3 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 16:42:41 +0100 Subject: [PATCH 17/25] i'm lost --- .github/workflows/platformio.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index 9ebfb5e..68a2a13 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -39,4 +39,6 @@ jobs: pio lib install /tmp/WiFiManager.zip - name: Run PlatformIO - run: pio run \ No newline at end of file + run: pio ci + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} \ No newline at end of file From f46caa387ea374e1a52987affb1db6ead1a0047d Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 16:58:03 +0100 Subject: [PATCH 18/25] do check and install --- .github/workflows/platformio.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index 68a2a13..e854800 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -37,8 +37,14 @@ jobs: run: | wget https://github.com/tzapu/WiFiManager/archive/refs/tags/2.0.5-beta.zip -O /tmp/WiFiManager.zip pio lib install /tmp/WiFiManager.zip + pio lib install 1 + - name: Run PlatformIO Check + run: pio check + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} + - name: Run PlatformIO - run: pio ci + run: pio run env: - PLATFORMIO_CI_SRC: ${{ matrix.example }} \ No newline at end of file + PLATFORMIO_CI_SRC: ${{ matrix.example }} \ No newline at end of file From fbb76ed5c192b45195063f7c29b74883285d250c Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 17:01:11 +0100 Subject: [PATCH 19/25] skip check --- .github/workflows/platformio.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index e854800..e86823a 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -39,11 +39,6 @@ jobs: pio lib install /tmp/WiFiManager.zip pio lib install 1 - - name: Run PlatformIO Check - run: pio check - env: - PLATFORMIO_CI_SRC: ${{ matrix.example }} - - name: Run PlatformIO run: pio run env: From f790468d5364dcde952865b288568f0b63da12d0 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 17:03:17 +0100 Subject: [PATCH 20/25] only build on linux as wget is used --- .github/workflows/platformio.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index e86823a..e83f46d 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -8,7 +8,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + os: [ubuntu-latest] steps: - uses: actions/checkout@v2 From bea422e62239ba44bd932603e27422e21af203b9 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 17:14:54 +0100 Subject: [PATCH 21/25] add static code analyse --- .github/workflows/platformio.yml | 8 ++++++-- platformio.ini | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index e83f46d..3817b91 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -38,8 +38,12 @@ jobs: wget https://github.com/tzapu/WiFiManager/archive/refs/tags/2.0.5-beta.zip -O /tmp/WiFiManager.zip pio lib install /tmp/WiFiManager.zip pio lib install 1 - + - name: Run PlatformIO run: pio run env: - PLATFORMIO_CI_SRC: ${{ matrix.example }} \ No newline at end of file + PLATFORMIO_CI_SRC: ${{ matrix.example }} + - name: Run Static Code Checks PlatformIO + run: pio check + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} diff --git a/platformio.ini b/platformio.ini index fa856d7..245ccd9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,6 +12,7 @@ platform = espressif8266 board = d1_mini framework = arduino +check_tool = cppcheck, clangtidy, pvs-studio lib_deps = bblanchon/ArduinoJson @ ^6.18.3 knolleary/PubSubClient @ ^2.8 From 86ba2489ee4c5fa0761545285e241c9375ecc93d Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Thu, 25 Nov 2021 17:30:10 +0100 Subject: [PATCH 22/25] disable some checks as not compliant yet --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 245ccd9..7d48644 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,7 @@ platform = espressif8266 board = d1_mini framework = arduino -check_tool = cppcheck, clangtidy, pvs-studio +check_tool = clangtidy ;cppcheck, clangtidy, pvs-studio lib_deps = bblanchon/ArduinoJson @ ^6.18.3 knolleary/PubSubClient @ ^2.8 From bf772ef5b2c67db78fa4693b45a115135cb0f430 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Fri, 26 Nov 2021 11:45:04 +0100 Subject: [PATCH 23/25] fix storage and read of data after startup --- src/esp8266-vindriktning-particle-sensor.ino | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/esp8266-vindriktning-particle-sensor.ino b/src/esp8266-vindriktning-particle-sensor.ino index 6014764..b10d72e 100644 --- a/src/esp8266-vindriktning-particle-sensor.ino +++ b/src/esp8266-vindriktning-particle-sensor.ino @@ -69,6 +69,11 @@ void setup() { WiFi.hostname(identifier); Config::load(); + + if (strlen(Config::mqtt_server) > 0 ) custom_mqtt_server.setValue(Config::mqtt_server, strlen(Config::mqtt_server)); + if (strlen(Config::username) > 0 ) custom_mqtt_user.setValue(Config::username, strlen(Config::username)); + if (strlen(Config::password) > 0 ) custom_mqtt_pass.setValue(Config::password, strlen(Config::password)); + if (strlen(Config::mqtt_topic) > 0 ) custom_mqtt_topic.setValue(Config::mqtt_topic, strlen(Config::mqtt_topic)); setupWifi(); setupOTA(); @@ -165,6 +170,9 @@ void setupWifi() { wifiManager.addParameter(&custom_mqtt_pass); wifiManager.addParameter(&custom_mqtt_topic); + wifiManager.setSaveConfigCallback(saveConfigCallback); + wifiManager.setPreSaveConfigCallback(saveConfigCallback); + WiFi.hostname(identifier); wifiManager.autoConnect(identifier); mqttClient.setClient(wifiClient); From 5119b01f48a0968ac50aa3d3b2aa2a9fa952a5dd Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Fri, 26 Nov 2021 11:45:33 +0100 Subject: [PATCH 24/25] update to littlefs filesystem --- platformio.ini | 1 + src/Config.h | 13 +++++-------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index 7d48644..80f6088 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,6 +12,7 @@ platform = espressif8266 board = d1_mini framework = arduino +board_build.filesystem = littlefs check_tool = clangtidy ;cppcheck, clangtidy, pvs-studio lib_deps = bblanchon/ArduinoJson @ ^6.18.3 diff --git a/src/Config.h b/src/Config.h index 0cdd2b8..dba963b 100644 --- a/src/Config.h +++ b/src/Config.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include "LittleFS.h" #define JSON_SIZE 512 @@ -27,7 +27,7 @@ namespace Config { Serial.printf("mqtt_user: %s\n", username); - File configFile = SPIFFS.open("/conf.json", "w"); + File configFile = LittleFS.open("/conf.json", "w"); if (!configFile) { return; } @@ -38,12 +38,9 @@ namespace Config { void load() { Serial.println("load config\n"); - if (SPIFFS.begin()) { - if (SPIFFS.exists("/config.json")) { - SPIFFS.remove("/config.json"); - } - if (SPIFFS.exists("/conf.json")) { - File configFile = SPIFFS.open("/conf.json", "r"); + if (LittleFS.begin()) { + if (LittleFS.exists("/conf.json")) { + File configFile = LittleFS.open("/conf.json", "r"); if (configFile) { const size_t size = configFile.size(); From b10fd3e095e5062aeac89050ac6b2cd477c87378 Mon Sep 17 00:00:00 2001 From: Wouter van Toledo Date: Fri, 3 Dec 2021 10:47:55 +0100 Subject: [PATCH 25/25] re enable home assistant topics and messages --- src/esp8266-vindriktning-particle-sensor.ino | 26 ++++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/esp8266-vindriktning-particle-sensor.ino b/src/esp8266-vindriktning-particle-sensor.ino index b10d72e..2a630d1 100644 --- a/src/esp8266-vindriktning-particle-sensor.ino +++ b/src/esp8266-vindriktning-particle-sensor.ino @@ -30,15 +30,15 @@ uint32_t statusPublishPreviousMillis = 0; const uint16_t statusPublishInterval = 30000; // 30 seconds = 30000 milliseconds char identifier[24]; -/**#define FIRMWARE_PREFIX "esp8266-vindriktning-particle-sensor"*/ +#define FIRMWARE_PREFIX "esp8266-vindriktning-particle-sensor" #define AVAILABILITY_ONLINE "{ \"state\" : \"online\" }" #define AVAILABILITY_OFFLINE "{ \"state\" : \"offline\" }" -//char MQTT_TOPIC_AVAILABILITY[128]; +char MQTT_TOPIC_AVAILABILITY[128]; char MQTT_TOPIC_STATE[128]; -//char MQTT_TOPIC_COMMAND[128]; +char MQTT_TOPIC_COMMAND[128]; -//char MQTT_TOPIC_AUTOCONF_WIFI_SENSOR[128]; -//char MQTT_TOPIC_AUTOCONF_PM25_SENSOR[128]; +char MQTT_TOPIC_AUTOCONF_WIFI_SENSOR[128]; +char MQTT_TOPIC_AUTOCONF_PM25_SENSOR[128]; bool shouldSaveConfig = false; @@ -61,10 +61,10 @@ void setup() { delay(3000); snprintf(identifier, sizeof(identifier), "VINDRIKTNING-%X", ESP.getChipId()); - //snprintf(MQTT_TOPIC_AVAILABILITY, 127, "%s/%s/status", FIRMWARE_PREFIX, identifier); - //snprintf(MQTT_TOPIC_COMMAND, 127, "%s/%s/command", FIRMWARE_PREFIX, identifier); - //snprintf(MQTT_TOPIC_AUTOCONF_PM25_SENSOR, 127, "homeassistant/sensor/%s/%s_pm25/config", FIRMWARE_PREFIX, identifier); - //snprintf(MQTT_TOPIC_AUTOCONF_WIFI_SENSOR, 127, "homeassistant/sensor/%s/%s_wifi/config", FIRMWARE_PREFIX, identifier); + snprintf(MQTT_TOPIC_AVAILABILITY, 127, "%s/%s/status", FIRMWARE_PREFIX, identifier); + snprintf(MQTT_TOPIC_COMMAND, 127, "%s/%s/command", FIRMWARE_PREFIX, identifier); + snprintf(MQTT_TOPIC_AUTOCONF_PM25_SENSOR, 127, "homeassistant/sensor/%s/%s_pm25/config", FIRMWARE_PREFIX, identifier); + snprintf(MQTT_TOPIC_AUTOCONF_WIFI_SENSOR, 127, "homeassistant/sensor/%s/%s_wifi/config", FIRMWARE_PREFIX, identifier); WiFi.hostname(identifier); @@ -206,10 +206,10 @@ void mqttReconnect() { for (uint8_t attempt = 0; attempt < 3; ++attempt) { if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_STATE, 1, true, AVAILABILITY_OFFLINE)) { mqttClient.publish(MQTT_TOPIC_STATE, AVAILABILITY_ONLINE, true); - //publishAutoConfig(); + publishAutoConfig(); // Make sure to subscribe after polling the status so that we never execute commands with the default data - //mqttClient.subscribe(MQTT_TOPIC_COMMAND); + mqttClient.subscribe(MQTT_TOPIC_COMMAND); break; } delay(5000); @@ -239,7 +239,7 @@ void publishState() { void mqttCallback(char* topic, uint8_t* payload, unsigned int length) { } -/*void publishAutoConfig() { +void publishAutoConfig() { char mqttPayload[2048]; DynamicJsonDocument device(256); DynamicJsonDocument autoconfPayload(1024); @@ -283,4 +283,4 @@ void mqttCallback(char* topic, uint8_t* payload, unsigned int length) { } mqttClient.publish(&MQTT_TOPIC_AUTOCONF_PM25_SENSOR[0], &mqttPayload[0], true); autoconfPayload.clear(); -}*/ +}