From 61c072bbccd482dbbd89a7ae7faf6025a8373f8b Mon Sep 17 00:00:00 2001 From: lexa Date: Sat, 23 Apr 2022 13:26:18 +0300 Subject: [PATCH] Trim float values for MQTT --- ESP12SmallRoom/.vscode/extensions.json | 17 +++++++------ ESP12SmallRoom/platformio.ini | 1 + ESP12SmallRoom/src/main.cpp | 33 ++++++++++++++++++++++++ ESP_BigRoom/src/main.cpp | 33 ++++++++++++++++++++++++ ESP_MidRoom/.vscode/extensions.json | 17 +++++++------ ESP_MidRoom/include/main.h | 1 + ESP_MidRoom/src/main.cpp | 33 +++++++++++++++++++++++- Kuhnya/.vscode/extensions.json | 17 +++++++------ Kuhnya/include/main.h | 1 + Kuhnya/src/main.cpp | 35 ++++++++++++++++++++++++++ VT_ESP8266/.vscode/extensions.json | 17 +++++++------ 11 files changed, 176 insertions(+), 29 deletions(-) diff --git a/ESP12SmallRoom/.vscode/extensions.json b/ESP12SmallRoom/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/ESP12SmallRoom/.vscode/extensions.json +++ b/ESP12SmallRoom/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/ESP12SmallRoom/platformio.ini b/ESP12SmallRoom/platformio.ini index 51d2c9f..4601b38 100644 --- a/ESP12SmallRoom/platformio.ini +++ b/ESP12SmallRoom/platformio.ini @@ -21,3 +21,4 @@ lib_deps = PubSubClient @ ^2.8 jwrw/ESP_EEPROM @ ^2.0.0 marvinroger/AsyncMqttClient @ ^0.9.0 + adafruit/Adafruit HTU21DF Library @ ^1.0.5 diff --git a/ESP12SmallRoom/src/main.cpp b/ESP12SmallRoom/src/main.cpp index 61bc7fd..3e4f1f7 100644 --- a/ESP12SmallRoom/src/main.cpp +++ b/ESP12SmallRoom/src/main.cpp @@ -57,6 +57,7 @@ void onMqttSubscribe(uint16_t packetId, uint8_t qos); void onMqttUnsubscribe(uint16_t packetId); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttPublish(uint16_t packetId); +void trim(char *s); void setup() { Serial.begin(9600); @@ -209,11 +210,13 @@ void publishMin() //digitalWrite(LED_BLUE, HIGH); if(!isnan(temp)){ dtostrf(temp, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"smallroom/temp", 0, false, strFVal); //client.publish(TOPIC"smallroom/temp", strFVal); } if(!isnan(rel_hum)){ dtostrf(rel_hum, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"smallroom/rel_hum", 0, false, strFVal); //client.publish(TOPIC"smallroom/rel_hum", strFVal); } @@ -240,11 +243,13 @@ void publishSec() //digitalWrite(LED_GREEN, HIGH); if(!isnan(temp)){ dtostrf(temp, 7, 3, strFVal); + trim(strFVal); mqttClient.publish("/hometest/smallroom/temp", 0, false, strFVal); //client.publish("/hometest/smallroom/temp", strFVal); } if(!isnan(rel_hum)){ dtostrf(rel_hum, 7, 3, strFVal); + trim(strFVal); mqttClient.publish("/hometest/smallroom/rel_hum", 0, false, strFVal); //client.publish("/hometest/smallroom/rel_hum", strFVal); } @@ -332,4 +337,32 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties } void onMqttPublish(uint16_t packetId) { +} +void trim(char *s) +{ + // удаляем пробелы и табы с начала строки: + unsigned int i=0,j; + while((s[i]==' ')||(s[i]=='\t')) + { + i++; + } + if(i>0) + { + for(j=0; j < strlen(s); j++) + { + s[j]=s[j+i]; + } + s[j]='\0'; + } + + // удаляем пробелы и табы с конца строки: + i=strlen(s)-1; + while((s[i]==' ')||(s[i]=='\t')) + { + i--; + } + if(i < (strlen(s)-1)) + { + s[i+1]='\0'; + } } \ No newline at end of file diff --git a/ESP_BigRoom/src/main.cpp b/ESP_BigRoom/src/main.cpp index e205b3d..7766ba9 100644 --- a/ESP_BigRoom/src/main.cpp +++ b/ESP_BigRoom/src/main.cpp @@ -24,6 +24,7 @@ void onMqttSubscribe(uint16_t packetId, uint8_t qos); void onMqttUnsubscribe(uint16_t packetId); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttPublish(uint16_t packetId); +void trim(char *s); leds bled(LED_B, 300, true); @@ -277,17 +278,20 @@ void loop() { // Serial.println("Temp: " + String(temp)); // Serial.println("Hum: " + String(hum)); dtostrf(temp, 6, 1, s); + trim(s); bled.start(); mqttClient.publish(TOPIC"temp", 1, false, s); } if(hum != 0.0f){ dtostrf(hum, 6, 1, s); + trim(s); mqttClient.publish(TOPIC"hum", 1, false, s); //sendDataI(msgLight, LightLev); //minuts = 0; } bled.start(); dtostrf(cRun / 60000.0, 6, 1, s); + trim(s); mqttClient.publish(TOPIC"mins", 1, false, s); itoa(WiFi.RSSI(), s, 10); mqttClient.publish(TOPIC"RSSI", 1, false, s); @@ -461,3 +465,32 @@ void onMqttPublish(uint16_t packetId) { // Serial.println(packetId); //g_led.start(); } + +void trim(char *s) +{ + // удаляем пробелы и табы с начала строки: + int i=0,j; + while((s[i]==' ')||(s[i]=='\t')) + { + i++; + } + if(i>0) + { + for(j=0; j < strlen(s); j++) + { + s[j]=s[j+i]; + } + s[j]='\0'; + } + + // удаляем пробелы и табы с конца строки: + i=strlen(s)-1; + while((s[i]==' ')||(s[i]=='\t')) + { + i--; + } + if(i < (strlen(s)-1)) + { + s[i+1]='\0'; + } +} \ No newline at end of file diff --git a/ESP_MidRoom/.vscode/extensions.json b/ESP_MidRoom/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/ESP_MidRoom/.vscode/extensions.json +++ b/ESP_MidRoom/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/ESP_MidRoom/include/main.h b/ESP_MidRoom/include/main.h index 2036f02..47a18d6 100644 --- a/ESP_MidRoom/include/main.h +++ b/ESP_MidRoom/include/main.h @@ -26,3 +26,4 @@ uint32_t co2; bool firstRun; float p, t, h; int light, lightPrev; +void trim(char *s); \ No newline at end of file diff --git a/ESP_MidRoom/src/main.cpp b/ESP_MidRoom/src/main.cpp index f85f7ed..95d72f7 100644 --- a/ESP_MidRoom/src/main.cpp +++ b/ESP_MidRoom/src/main.cpp @@ -232,9 +232,11 @@ void loop() { itoa(adc, v, 10); mqttClient.publish(TOPIC"/light", 1, false, v); dtostrf(t, 5, 1,v); + trim(v); mqttClient.publish(TOPIC"/temp", 1, false, v); //Serial1.println("Publish1"); dtostrf(h, 5, 1,v); + trim(v); mqttClient.publish(TOPIC"/humid", 1, false, v); //Serial1.println("Publish2"); if(firstRun){ @@ -250,6 +252,7 @@ void loop() { } else if(minCnt % 20 == 0){ dtostrf(millis() / 60000.0, 7, 2, v); + trim(v); //ultoa(millis(), v, 10); if(mqttClient.connected()){ g_led.start(); @@ -347,4 +350,32 @@ void onMqttPublish(uint16_t packetId) { //g_led.start(); } - */ \ No newline at end of file + */ +void trim(char *s) +{ + // удаляем пробелы и табы с начала строки: + unsigned int i=0,j; + while((s[i]==' ')||(s[i]=='\t')) + { + i++; + } + if(i>0) + { + for(j=0; j < strlen(s); j++) + { + s[j]=s[j+i]; + } + s[j]='\0'; + } + + // удаляем пробелы и табы с конца строки: + i=strlen(s)-1; + while((s[i]==' ')||(s[i]=='\t')) + { + i--; + } + if(i < (strlen(s)-1)) + { + s[i+1]='\0'; + } +} \ No newline at end of file diff --git a/Kuhnya/.vscode/extensions.json b/Kuhnya/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/Kuhnya/.vscode/extensions.json +++ b/Kuhnya/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/Kuhnya/include/main.h b/Kuhnya/include/main.h index a051b25..313ebdc 100644 --- a/Kuhnya/include/main.h +++ b/Kuhnya/include/main.h @@ -79,3 +79,4 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event); void onMqttConnect(bool sessionPresent); void onMqttDisconnect(AsyncMqttClientDisconnectReason reason); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); +void trim(char *s); diff --git a/Kuhnya/src/main.cpp b/Kuhnya/src/main.cpp index 5980869..48ed44b 100644 --- a/Kuhnya/src/main.cpp +++ b/Kuhnya/src/main.cpp @@ -257,18 +257,22 @@ void publishMin() digitalWrite(BLUE, HIGH); if(!isnan(tempOut)){ dtostrf(tempOut, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"kuh/temp_out", 1, false, strFVal); } if(!isnan(tempIn)){ dtostrf(tempIn, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"kuh/temp_in", 1, false, strFVal); } if(!isnan(hum)){ dtostrf(hum, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"kuh/humidity", 1, false, strFVal); } if(!isnan(press)){ dtostrf(press, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"kuh/pressure", 1, false, strFVal); } //itoa(lightSP, strFVal, 10); @@ -279,14 +283,17 @@ void publishMin() mqttClient.publish(TOPIC"kuh/light_cur", 1, false, strFVal); if(!isnan(tempHol)){ dtostrf(tempHol, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"kuh/hol_top", 1, false, strFVal); } if(!isnan(tempHoM)){ dtostrf(tempHoM, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"kuh/hol_down", 1, false, strFVal); } if(!isnan(tempMor)){ dtostrf(tempMor, 6, 1, strFVal); + trim(strFVal); mqttClient.publish(TOPIC"kuh/moroz", 1, false, strFVal); } ultoa(crun, strFVal, 10); @@ -394,3 +401,31 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties EEPROM.commit(); } } +void trim(char *s) +{ + // удаляем пробелы и табы с начала строки: + unsigned int i=0,j; + while((s[i]==' ')||(s[i]=='\t')) + { + i++; + } + if(i>0) + { + for(j=0; j < strlen(s); j++) + { + s[j]=s[j+i]; + } + s[j]='\0'; + } + + // удаляем пробелы и табы с конца строки: + i=strlen(s)-1; + while((s[i]==' ')||(s[i]=='\t')) + { + i--; + } + if(i < (strlen(s)-1)) + { + s[i+1]='\0'; + } +} \ No newline at end of file diff --git a/VT_ESP8266/.vscode/extensions.json b/VT_ESP8266/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/VT_ESP8266/.vscode/extensions.json +++ b/VT_ESP8266/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +}