I’m relatively new to this and brand new to the use of MQTT.
I am working on a solution to prevent the basement freezer being left open by one of my four children once again…
I have the OneWire circuit created on a breadboard. I have deployed this basic code:
I can verify that the Photon event data is recording the temperature data correctly. From there I took the sample code in MQTT, added the IP, username, and password of my Home Assistant Mosquitto MQTT, blended the code from the OneWire circuit and this was the result… I am getting alternating values in MQTT Explorer jumping between the current temperature and and “32.000000”. I have yet to figure out why it is doing this… Any help and guidance you can offer is greatly appreciated.
// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>
#include <MQTT.h>
#include <DS18.h>
// Location of temp sensor
DS18 sensor(D0);
void callback(char* topic, byte* payload, unsigned int length);
//MQTT server IP address
byte server[] = {192,168,1,40};
MQTT client(server, 1883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Serial.print( "Got a message : " );
Serial.print( p );
}
// setup
void setup() {
// connect to the server
client.connect("sparkclient", "mosquitto", "^PqEPQN*derBs57QJmD*Z&TY56V5KY");
// publish/subscribe
if (client.isConnected()) {
Serial.print("\nConnected");
client.publish("MQTTTemp/temperature", String(sensor.fahrenheit()));
client.subscribe("inTopic/message");
}
}
// loop
void loop() {
if (client.isConnected())
client.loop();
if (sensor.read()) {
Particle.publish("temperature", String(sensor.fahrenheit()), PRIVATE);
}
// connect to the server
client.connect("sparkclient", "mosquitto", "^PqEPQN*derBs57QJmD*Z&TY56V5KY");
// publish/subscribe
if (client.isConnected()) {
Serial.print("\nConnected");
client.publish("MQTTTemp/temperature", String(sensor.fahrenheit()));
client.subscribe("inTopic/message");
}
}
Thank you Friedl, I am reporting data in degrees F so it is bouncing between 68 and 32 every time it takes a measurement.
I changed the code for my void loop as follows, I think I am heading in the right direction. I also added a 5 second delay, no need to take so many measurements. From what I can tell, my original code did not have the sensor.read before the client.publish so it was reporting 32.000000 for some reason, once I added that it is sending the right info to MQTT but my current code is no longer publishing data to particle.publish…
I’m using the data in Mosquitto MQTT on a PI for some home automation, in this case send me a text and set off a buzzer (a little surprise for the kids)…
void loop() {
// 5 second delay
delay(5000);
if (client.isConnected())
if (sensor.read()) {
client.publish("freezer-temp/temperature", String(sensor.fahrenheit()));
}
client.loop();
if (sensor.read()) {
Particle.publish("temperature", String(sensor.fahrenheit()), PRIVATE);
}
}
Ok it makes sense. Here is how I implemented the temperature readings utlizing the same sensor with Dallas Spark and OneWire libraries. I call the temperature function in void loop (); then.
void temperature() {
sensors.requestTemperatures();
Celsius = sensors.getTempCByIndex(0);
Serial.print(Celsius);
Serial.println(" *C ");
Blynk.virtualWrite(V2, Celsius); // Blynk Mobile app - Monitor temp Data
Particle.publish("Temp", String::format("{\"data\":%.3f}", Celsius), PRIVATE); // Publish temp data to Particle Cloud
}
I did not use MQTT, but the readings sent to particle cloud is correct, so it is only a matter of sending to MQTT as apposed to publishing to Particle Cloud.
In your case and assuming you want to publish to Particle Cloud as well as MQTT, I would rather try something like this: (untested)
void loop() {
// 5 second delay
delay(5000); // maybe try NON-BLOCKING delays instead of blocking delays
if (client.isConnected())
if (sensor.read()) {
client.publish("freezer-temp/temperature", String(sensor.fahrenheit()));
Particle.publish("temperature", String(sensor.fahrenheit()), PRIVATE);
}
client.loop();
}
If you have not succeeded, I will try your code tomorrow, quite late here now :). Also, maybe have a look at the Maker Kit tutorial #4 here. It should get you going in the right direction. Just add your MQTT-stuff
I have it running now and the temperature value is not jumping around, what I determined was that in my void setup() I was publishing the temperature value but not ever reading the sensor prior to the publish. So, once I added the sensor read it is publishing a real temperature reading now rather than a default.
Here’s where I’m at with the code:
// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>
#include <MQTT.h>
#include <DS18.h>
// Location of temp sensor
DS18 sensor(D0);
void callback(char* topic, byte* payload, unsigned int length);
//MQTT server IP address
byte server[] = {192,168,1,40};
MQTT client(server, 1883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Serial.print( "Got a message : " );
Serial.print( p );
}
// setup
void setup() {
// connect to the server
client.connect("sparkclient", "mosquitto", "^PqEPQN*derBs57QJmD*Z&TY56V5KY");
// publish/subscribe
if (client.isConnected()) {
if (sensor.read()) {
Particle.publish("temperature", String(sensor.fahrenheit()), PRIVATE);
Serial.print("\nConnected");
client.publish("freezer-temp/temperature", String(sensor.fahrenheit()));
client.subscribe("freezer-temp/message");
}
}
}
// loop
void loop() {
// 5 second delay
delay(5000);
if (client.isConnected())
if (sensor.read()) {
Particle.publish("free memory", String(System.freeMemory()), PRIVATE);
Particle.publish("temperature", String(sensor.fahrenheit()), PRIVATE);
client.publish("freezer-temp/temperature", String(sensor.fahrenheit()));
}
client.loop();
}
The issue I am having now is after a few hours the Photon is still connected but it stops sending messages to paricle.publish and MQTT and I don’t know why… If I reboot everything is happy again for a while then it stops. Thoughts?
Thanks for the direction, I've changed the code a bit, I hope I am heading in the right direction. I'm fairly new to programming so any additional dialog you can offer as I learn is much appreciated.
// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>
#include <MQTT.h>
#include <DS18.h>
// Location of temp sensor
DS18 sensor(D0);
void callback(char* topic, byte* payload, unsigned int length);
// Timer variables
int period = 10000; //10 seconds
unsigned long timeNow = 0;
// Temperature variable
float freezerTemp = 0.00;
// MQTT server IP address
byte server[] = {192,168,1,40};
MQTT client(server, 1883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Serial.print( "Got a message : " );
Serial.print( p );
}
// setup
void setup() {
Serial.begin(115200);
// connect to the server
client.connect("sparkclient", "mosquitto", "^PqEPQN*derBs57QJmD*Z&TY56V5KY");
// publish/subscribe
if (client.isConnected()) {
Serial.print("\nConnected");
}
}
// loop
void loop() {
sensor.read();
freezerTemp = sensor.fahrenheit();
if (millis() > timeNow + period){
timeNow = millis();
Serial.println("freezer-temp");
//Particle.publish("free memory", String(System.freeMemory());
Particle.publish("freezer-temp", String(freezerTemp));
client.publish("freezer-temp/temperature", String(freezerTemp));
}
if (client.isConnected())
client.loop();
}
I’m not using the same library for reading the DS18 but looking at one of your earlier posts where you had good readings, you could adapt my loop() this way to see if it helps
void loop() {
static uint32_t msLastPub = 0;
if (millis() - msLastPub < period || !sensor.read()) // bail out when it's not yet time or the reading is not OK
return;
msLastPub = millis();
freezerTemp = sensor.fahrenheit();
...
}
Have a look at my code in https://github.com/jakubczaplicki/aqua_iot
I am using Particle Photon to measure temperature in an aquarium. It sends value via MQTT to Home Assistant (local network), publishes measurement to Particle Cloud and presents measurement value on a screen.