Trim float values for MQTT

This commit is contained in:
2022-04-23 13:26:18 +03:00
parent 5c5ef929fa
commit 61c072bbcc
11 changed files with 176 additions and 29 deletions

View File

@@ -3,5 +3,8 @@
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"platformio.platformio-ide" "platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
] ]
} }

View File

@@ -21,3 +21,4 @@ lib_deps =
PubSubClient @ ^2.8 PubSubClient @ ^2.8
jwrw/ESP_EEPROM @ ^2.0.0 jwrw/ESP_EEPROM @ ^2.0.0
marvinroger/AsyncMqttClient @ ^0.9.0 marvinroger/AsyncMqttClient @ ^0.9.0
adafruit/Adafruit HTU21DF Library @ ^1.0.5

View File

@@ -57,6 +57,7 @@ void onMqttSubscribe(uint16_t packetId, uint8_t qos);
void onMqttUnsubscribe(uint16_t packetId); void onMqttUnsubscribe(uint16_t packetId);
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void onMqttPublish(uint16_t packetId); void onMqttPublish(uint16_t packetId);
void trim(char *s);
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@@ -209,11 +210,13 @@ void publishMin()
//digitalWrite(LED_BLUE, HIGH); //digitalWrite(LED_BLUE, HIGH);
if(!isnan(temp)){ if(!isnan(temp)){
dtostrf(temp, 6, 1, strFVal); dtostrf(temp, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"smallroom/temp", 0, false, strFVal); mqttClient.publish(TOPIC"smallroom/temp", 0, false, strFVal);
//client.publish(TOPIC"smallroom/temp", strFVal); //client.publish(TOPIC"smallroom/temp", strFVal);
} }
if(!isnan(rel_hum)){ if(!isnan(rel_hum)){
dtostrf(rel_hum, 6, 1, strFVal); dtostrf(rel_hum, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"smallroom/rel_hum", 0, false, strFVal); mqttClient.publish(TOPIC"smallroom/rel_hum", 0, false, strFVal);
//client.publish(TOPIC"smallroom/rel_hum", strFVal); //client.publish(TOPIC"smallroom/rel_hum", strFVal);
} }
@@ -240,11 +243,13 @@ void publishSec()
//digitalWrite(LED_GREEN, HIGH); //digitalWrite(LED_GREEN, HIGH);
if(!isnan(temp)){ if(!isnan(temp)){
dtostrf(temp, 7, 3, strFVal); dtostrf(temp, 7, 3, strFVal);
trim(strFVal);
mqttClient.publish("/hometest/smallroom/temp", 0, false, strFVal); mqttClient.publish("/hometest/smallroom/temp", 0, false, strFVal);
//client.publish("/hometest/smallroom/temp", strFVal); //client.publish("/hometest/smallroom/temp", strFVal);
} }
if(!isnan(rel_hum)){ if(!isnan(rel_hum)){
dtostrf(rel_hum, 7, 3, strFVal); dtostrf(rel_hum, 7, 3, strFVal);
trim(strFVal);
mqttClient.publish("/hometest/smallroom/rel_hum", 0, false, strFVal); mqttClient.publish("/hometest/smallroom/rel_hum", 0, false, strFVal);
//client.publish("/hometest/smallroom/rel_hum", strFVal); //client.publish("/hometest/smallroom/rel_hum", strFVal);
} }
@@ -333,3 +338,31 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
void onMqttPublish(uint16_t packetId) { 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';
}
}

View File

@@ -24,6 +24,7 @@ void onMqttSubscribe(uint16_t packetId, uint8_t qos);
void onMqttUnsubscribe(uint16_t packetId); void onMqttUnsubscribe(uint16_t packetId);
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void onMqttPublish(uint16_t packetId); void onMqttPublish(uint16_t packetId);
void trim(char *s);
leds bled(LED_B, 300, true); leds bled(LED_B, 300, true);
@@ -277,17 +278,20 @@ void loop() {
// Serial.println("Temp: " + String(temp)); // Serial.println("Temp: " + String(temp));
// Serial.println("Hum: " + String(hum)); // Serial.println("Hum: " + String(hum));
dtostrf(temp, 6, 1, s); dtostrf(temp, 6, 1, s);
trim(s);
bled.start(); bled.start();
mqttClient.publish(TOPIC"temp", 1, false, s); mqttClient.publish(TOPIC"temp", 1, false, s);
} }
if(hum != 0.0f){ if(hum != 0.0f){
dtostrf(hum, 6, 1, s); dtostrf(hum, 6, 1, s);
trim(s);
mqttClient.publish(TOPIC"hum", 1, false, s); mqttClient.publish(TOPIC"hum", 1, false, s);
//sendDataI(msgLight, LightLev); //sendDataI(msgLight, LightLev);
//minuts = 0; //minuts = 0;
} }
bled.start(); bled.start();
dtostrf(cRun / 60000.0, 6, 1, s); dtostrf(cRun / 60000.0, 6, 1, s);
trim(s);
mqttClient.publish(TOPIC"mins", 1, false, s); mqttClient.publish(TOPIC"mins", 1, false, s);
itoa(WiFi.RSSI(), s, 10); itoa(WiFi.RSSI(), s, 10);
mqttClient.publish(TOPIC"RSSI", 1, false, s); mqttClient.publish(TOPIC"RSSI", 1, false, s);
@@ -461,3 +465,32 @@ void onMqttPublish(uint16_t packetId) {
// Serial.println(packetId); // Serial.println(packetId);
//g_led.start(); //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';
}
}

View File

@@ -3,5 +3,8 @@
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"platformio.platformio-ide" "platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
] ]
} }

View File

@@ -26,3 +26,4 @@ uint32_t co2;
bool firstRun; bool firstRun;
float p, t, h; float p, t, h;
int light, lightPrev; int light, lightPrev;
void trim(char *s);

View File

@@ -232,9 +232,11 @@ void loop() {
itoa(adc, v, 10); itoa(adc, v, 10);
mqttClient.publish(TOPIC"/light", 1, false, v); mqttClient.publish(TOPIC"/light", 1, false, v);
dtostrf(t, 5, 1,v); dtostrf(t, 5, 1,v);
trim(v);
mqttClient.publish(TOPIC"/temp", 1, false, v); mqttClient.publish(TOPIC"/temp", 1, false, v);
//Serial1.println("Publish1"); //Serial1.println("Publish1");
dtostrf(h, 5, 1,v); dtostrf(h, 5, 1,v);
trim(v);
mqttClient.publish(TOPIC"/humid", 1, false, v); mqttClient.publish(TOPIC"/humid", 1, false, v);
//Serial1.println("Publish2"); //Serial1.println("Publish2");
if(firstRun){ if(firstRun){
@@ -250,6 +252,7 @@ void loop() {
} }
else if(minCnt % 20 == 0){ else if(minCnt % 20 == 0){
dtostrf(millis() / 60000.0, 7, 2, v); dtostrf(millis() / 60000.0, 7, 2, v);
trim(v);
//ultoa(millis(), v, 10); //ultoa(millis(), v, 10);
if(mqttClient.connected()){ if(mqttClient.connected()){
g_led.start(); g_led.start();
@@ -348,3 +351,31 @@ 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';
}
}

View File

@@ -3,5 +3,8 @@
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"platformio.platformio-ide" "platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
] ]
} }

View File

@@ -79,3 +79,4 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
void onMqttConnect(bool sessionPresent); void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason); void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void trim(char *s);

View File

@@ -257,18 +257,22 @@ void publishMin()
digitalWrite(BLUE, HIGH); digitalWrite(BLUE, HIGH);
if(!isnan(tempOut)){ if(!isnan(tempOut)){
dtostrf(tempOut, 6, 1, strFVal); dtostrf(tempOut, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"kuh/temp_out", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/temp_out", 1, false, strFVal);
} }
if(!isnan(tempIn)){ if(!isnan(tempIn)){
dtostrf(tempIn, 6, 1, strFVal); dtostrf(tempIn, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"kuh/temp_in", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/temp_in", 1, false, strFVal);
} }
if(!isnan(hum)){ if(!isnan(hum)){
dtostrf(hum, 6, 1, strFVal); dtostrf(hum, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"kuh/humidity", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/humidity", 1, false, strFVal);
} }
if(!isnan(press)){ if(!isnan(press)){
dtostrf(press, 6, 1, strFVal); dtostrf(press, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"kuh/pressure", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/pressure", 1, false, strFVal);
} }
//itoa(lightSP, strFVal, 10); //itoa(lightSP, strFVal, 10);
@@ -279,14 +283,17 @@ void publishMin()
mqttClient.publish(TOPIC"kuh/light_cur", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/light_cur", 1, false, strFVal);
if(!isnan(tempHol)){ if(!isnan(tempHol)){
dtostrf(tempHol, 6, 1, strFVal); dtostrf(tempHol, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"kuh/hol_top", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/hol_top", 1, false, strFVal);
} }
if(!isnan(tempHoM)){ if(!isnan(tempHoM)){
dtostrf(tempHoM, 6, 1, strFVal); dtostrf(tempHoM, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"kuh/hol_down", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/hol_down", 1, false, strFVal);
} }
if(!isnan(tempMor)){ if(!isnan(tempMor)){
dtostrf(tempMor, 6, 1, strFVal); dtostrf(tempMor, 6, 1, strFVal);
trim(strFVal);
mqttClient.publish(TOPIC"kuh/moroz", 1, false, strFVal); mqttClient.publish(TOPIC"kuh/moroz", 1, false, strFVal);
} }
ultoa(crun, strFVal, 10); ultoa(crun, strFVal, 10);
@@ -394,3 +401,31 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
EEPROM.commit(); 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';
}
}

View File

@@ -3,5 +3,8 @@
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"platformio.platformio-ide" "platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
] ]
} }