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

@@ -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"
]
}

View File

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

View File

@@ -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();
}
*/
*/
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';
}
}