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
- TH02_dev.cpp : https://github.com/Seeed-Studio/Grove_Temper_Humidity_TH02/blob/master/TH02_dev.cpp
- TH02_dev.h : https://github.com/Seeed-Studio/Grove_Temper_Humidity_TH02/blob/master/TH02_dev.h
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