Trim float values for MQTT
This commit is contained in:
@@ -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';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user