From e61381f0ef6a373c6f89378e38b0d6fcb067f7e8 Mon Sep 17 00:00:00 2001 From: epiller Date: Wed, 24 Feb 2021 12:14:15 +0100 Subject: [PATCH] Klimerko Firmware v1.3.1 - Utilize new version of AllThingsTalk WiFi SDK (v2.1.2) which fixes a memory leak issue and utilizes a new way of checking if WiFi is connected. - Minor fixes to variable types & ogranization - Update website link --- Klimerko_Firmware/Klimerko_Firmware.ino | 49 +++++++++---------- .../src/AllThingsTalk/ATT_ESP8266.h | 18 +++---- .../src/AllThingsTalk/CborPayload.cpp | 2 + .../src/AllThingsTalk/library.properties | 2 +- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/Klimerko_Firmware/Klimerko_Firmware.ino b/Klimerko_Firmware/Klimerko_Firmware.ino index 85e6b3b..da5eff8 100644 --- a/Klimerko_Firmware/Klimerko_Firmware.ino +++ b/Klimerko_Firmware/Klimerko_Firmware.ino @@ -29,32 +29,32 @@ #include #include -String firmwareVersion = "1.3.0"; -int sendInterval = 15; // [MINUTES] Default sensor data sending interval -int wakeInterval = 30; // [SECONDS] Seconds to activate sensor before reading it -int averageSamples = 10; // Number of samples used to average values from sensors -int deviceRestartWait = 3; // Seconds to wait when restarting NodeMCU -bool noSleep = false; -// Pins to which the PMS7003 is connected to -const uint8_t pmsTX = D5; -const uint8_t pmsRX = D6; -const char *airQuality, *airQualityRaw; -unsigned long currentTime, lastReadTime, lastSendTime; -int avgTemperature, avgHumidity, avgPressure, avgPM1, avgPM25, avgPM10; +String firmwareVersion = "1.3.1"; +uint8_t sendInterval = 15; // [MINUTES] Default sensor data sending interval +const uint8_t wakeInterval = 30; // [SECONDS] Seconds to activate sensor before reading it +const uint8_t averageSamples = 10; // Number of samples used to average values from sensors +const uint8_t deviceRestartWait = 3; // Seconds to wait when restarting NodeMCU +const uint8_t pmsTX = D5; +const uint8_t pmsRX = D6; +bool noSleep = false; bool firstReading = true; -bool pmsWoken = false; +bool pmsWoken = false; +const char *airQuality, *airQualityRaw; String wifiName, wifiPassword, deviceId, deviceToken; String wifiNameTemp, wifiPasswordTemp, deviceIdTemp, deviceTokenTemp; -int wifiName_EEPROM_begin = 0; -int wifiName_EEPROM_end = 255; -int wifiPassword_EEPROM_begin = 256; -int wifiPassword_EEPROM_end = 511; -int deviceId_EEPROM_begin = 512; -int deviceId_EEPROM_end = 767; -int deviceToken_EEPROM_begin = 768; -int deviceToken_EEPROM_end = 1024; -int EEPROMsize = 1024; -String dataDivider = ";"; +int avgTemperature, avgHumidity, avgPressure, avgPM1, avgPM25, avgPM10; +unsigned long currentTime, lastReadTime, lastSendTime; +// Memory addresses +const uint16_t wifiName_EEPROM_begin = 0; +const uint16_t wifiName_EEPROM_end = 255; +const uint16_t wifiPassword_EEPROM_begin = 256; +const uint16_t wifiPassword_EEPROM_end = 511; +const uint16_t deviceId_EEPROM_begin = 512; +const uint16_t deviceId_EEPROM_end = 767; +const uint16_t deviceToken_EEPROM_begin = 768; +const uint16_t deviceToken_EEPROM_end = 1024; +const uint16_t EEPROMsize = 1024; +String dataDivider = ";"; // Assets to be published to char* PM1_ASSET = "pm1"; char* PM2_5_ASSET = "pm2-5"; @@ -99,7 +99,7 @@ void setup() { Serial.println(""); Serial.println(" ------------------------- Project 'KLIMERKO' ----------------------------"); Serial.println("| https://github.com/DesconBelgrade/Klimerko |"); - Serial.println("| www.vazduhgradjanima.rs |"); + Serial.println("| www.klimerko.org |"); Serial.print("| Firmware Version: "); Serial.print(firmwareVersion); Serial.println(" |"); @@ -300,7 +300,6 @@ void pmsPower(bool state) { while(millis() < now + 100); pmsWoken = false; pms.sleep(); - } } diff --git a/Klimerko_Firmware/src/AllThingsTalk/ATT_ESP8266.h b/Klimerko_Firmware/src/AllThingsTalk/ATT_ESP8266.h index 39797a9..f9abf0e 100644 --- a/Klimerko_Firmware/src/AllThingsTalk/ATT_ESP8266.h +++ b/Klimerko_Firmware/src/AllThingsTalk/ATT_ESP8266.h @@ -278,7 +278,7 @@ void Device::disconnect() { // Main method to connect to WiFi void Device::connectWiFi() { - if (WiFi.status() != WL_CONNECTED) { + if (!WiFi.localIP().isSet()) { connectionLedFadeStart(); WiFi.mode(WIFI_STA); if (wifiHostnameSet) { @@ -289,7 +289,7 @@ void Device::connectWiFi() { debug("Connecting to WiFi:", ' '); debug(wifiCreds->getSsid(), '.'); WiFi.begin(wifiCreds->getSsid(), wifiCreds->getPassword()); - while (WiFi.status() != WL_CONNECTED) { + while (!WiFi.localIP().isSet()) { debug("", '.'); delay(10000); } @@ -307,7 +307,7 @@ void Device::connectWiFi() { // Checks and recovers WiFi if lost void Device::maintainWiFi() { if (!disconnectedWiFi) { - if (WiFi.status() != WL_CONNECTED) { + if (!WiFi.localIP().isSet()) { connectionLedFadeStart(); debug("WiFi Connection Dropped! Reason:", ' '); switch(WiFi.status()) { @@ -349,7 +349,7 @@ void Device::maintainWiFi() { // Main method for disconnecting from WiFi void Device::disconnectWiFi() { - if (WiFi.status() == WL_CONNECTED) { + if (WiFi.localIP().isSet()) { disconnectAllThingsTalk(); WiFi.disconnect(); disconnectedWiFi = true; @@ -516,7 +516,7 @@ void Device::connectAllThingsTalk() { connectWiFi(); // WiFi needs to be present of course debug("Connecting to AllThingsTalk", '.'); while (!client.connected()) { - if (WiFi.status() != WL_CONNECTED) { + if (!WiFi.localIP().isSet()) { debug(" "); // Cosmetic only. maintainWiFi(); // In case WiFi connection is lost while connecting to ATT } @@ -605,7 +605,7 @@ void Device::disconnectAllThingsTalk() { // Called from loop; Reports wifi signal to ATTalk Maker at specified interval void Device::reportWiFiSignal() { - if (rssiReporting && WiFi.status() == WL_CONNECTED) { + if (rssiReporting && WiFi.localIP().isSet()) { if (millis() - rssiPrevTime >= rssiReportInterval*1000) { send(wifiSignalAsset, wifiSignal()); rssiPrevTime = millis(); @@ -861,7 +861,7 @@ void Device::mqttCallback(char* p_topic, byte* p_payload, unsigned int p_length) // Send data as CBOR bool Device::send(CborPayload &payload) { - if (WiFi.status() == WL_CONNECTED) { + if (WiFi.localIP().isSet()) { if (client.connected()) { char topic[128]; snprintf(topic, sizeof topic, "%s%s%s", "device/", deviceCreds->getDeviceId(), "/state"); @@ -880,7 +880,7 @@ bool Device::send(CborPayload &payload) { // Send data as Binary Payload bool Device::send(BinaryPayload &payload) { - if (WiFi.status() == WL_CONNECTED) { + if (WiFi.localIP().isSet()) { if (client.connected()) { char topic[128]; snprintf(topic, sizeof topic, "%s%s%s", "device/", deviceCreds->getDeviceId(), "/state"); @@ -898,7 +898,7 @@ bool Device::send(BinaryPayload &payload) { } template bool Device::send(char *asset, T payload) { - if (WiFi.status() == WL_CONNECTED) { + if (WiFi.localIP().isSet()) { if (client.connected()) { char topic[128]; snprintf(topic, sizeof topic, "%s%s%s%s%s", "device/", deviceCreds->getDeviceId(), "/asset/", asset, "/state"); diff --git a/Klimerko_Firmware/src/AllThingsTalk/CborPayload.cpp b/Klimerko_Firmware/src/AllThingsTalk/CborPayload.cpp index 0fec708..41288dc 100644 --- a/Klimerko_Firmware/src/AllThingsTalk/CborPayload.cpp +++ b/Klimerko_Firmware/src/AllThingsTalk/CborPayload.cpp @@ -16,6 +16,8 @@ CborPayload::~CborPayload() { } void CborPayload::reset() { + delete writer; + delete output; output = new CborStaticOutput(buffer, capacity); writer = new CborWriter(*output); assetCount = 0; diff --git a/Klimerko_Firmware/src/AllThingsTalk/library.properties b/Klimerko_Firmware/src/AllThingsTalk/library.properties index 1a87ea1..95a981a 100644 --- a/Klimerko_Firmware/src/AllThingsTalk/library.properties +++ b/Klimerko_Firmware/src/AllThingsTalk/library.properties @@ -1,5 +1,5 @@ name=AllThingsTalk WiFi SDK -version=2.1 +version=2.1.2 author=AllThingsTalk maintainer=Vanja sentence=Connect and control your device with AllThingsTalk