Argon MQTT and TH02 sensor issue

Hi,

I am just doing a demo where a Particle Argon read temperature and humidity with a TH02 (I2C) sensor from Grove and should send them as a Json object to a Mosquitto broker.

When I try to do all together (reading sensor AND publishing) my argon blink green, then light blue for 1 second and then turns red.

When I try just to read the sensor and Serial.print the value, it works perfect.
When I just publish a String to Mosquitto, it works perfect
Just when I try both.

I had exactlly the same issue when trying on a Photon.

I have firmware 1.4.4 on both Argon and Photon.

The library used for MQTT is the standard Particle one 0.4.29
The TH02 library is the one from Grove inserted as attached files to the project

The code to do both is :

// This project is to read TH02 sensor temperature and humidity and to send a Json to a Mosquitto broker
// Date : 27 fevrier 2020
// Author : fxleytens


// Include both libraries to the sketch
// MQTT is the standard Particle one 0.4.29
// The TH02 are 2 files attached TH02_dev.h and TH02_dev.cpp copied from grove github https://github.com/Seeed-Studio/Grove_Temper_Humidity_TH02

#include <MQTT.h>
#include "TH02_dev.h"

// Create the MQTT configuration and object

void callback(char* topic, byte* payload, unsigned int length);

MQTT client("my.mosquitto.broker", 1883, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    }

// Define variable to handle the sensor reading and create the Json object

    float temperature;
    float humidity;
    char publishString[16];
    

void setup() {

// For debug purposes

    Serial.begin(115200);

// Start the TH02, delay is just for security to make sure it has started

    TH02.begin();
    delay(1000);
    
// Connect to the Mosquitto broker

    client.connect(System.deviceID(), "username", "password");
    delay(1000);
    
}

void loop() {

// Read the sensor humidity and temperature
    float humidity = TH02.ReadHumidity();
    float temperature = TH02.ReadTemperature();

// Debug by Serial.printing the values
    Serial.print("Temperature = ");
    Serial.println(temperature);
    Serial.print("Humidite = ");
    Serial.println(humidity);

//Create the Json object and publishing it to the Mosquitto broker    
    sprintf(publishString,"{\"temperature\" : %4.2f, \"humidity\": %4.2f}", temperature, humidity);
    client.publish("fxl/mesures",publishString);

// Wait 5 sec. to send next reading
    delay(5000);
    
}

Hany help will be very helpful
Thanks

Not sure what you are doing with your void callback() or why.
You merely copy the incoming payload into a char[] just to discard that in the next moment :confused:

Your publishString is only declared to be 16 bytes long and then you try to dump a lot more data into that - that’s bound to corrupt some other data and I’m not surprised you get into an SOS panic state.

If you use snprintf(str, sizeof(str), ...) instead of sprintf(str, ...) at least that won’t happen and you probably see the problem in your output (as it would be truncated instead of crashing the whole program).

And your publishString should be at least 48 bytes for the data you intend to copy to it.

Hi ScruffR

MANY MANY thanks for your help. In fact I just changed my publishString array size and now, it works perfect.

I also removed the first void callback(… line

Thanks again

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.