I know my code is lacking, but as a “newby” I would apprecate any help.
I am using a sensor that creates a floating constant called htu.readHumidity() to which I assign the floating variable hum.
I want to publish an mqtt topic called _hum (a string conversion from the floating hum),
Here’s the relevant code:
hum = htu.readHumidity();
Serial.print(“straight from semsor”);
Serial.print(“Hum: “); Serial.println(hum);
char _hum[8]; // Buffer big enough for 7-character float
char _temperature [8];
dtostrf(hum, 8, 8, _hum);
Serial.print(“after conversion”);
Serial.print(“_Humidity: “);
Serial.println(_hum);
client.publish(“new_packet/humidity”, _hum);
delay(10000);
What am I doing wrong? I cannot see that published variable on my raspberry pi broker or an ios app called “MQTTool.”
As always, thanks in advance.
Steve
Hi.
Can you be a little more specific about your issue?
Do you get any values being published, but the wrong ones? Or you don’t get any values at all?
Regards,
Sara
I am not getting any values at all. What value should I attempt to get when I subscribe?
Can you double-check that you are subscribed to the right topic? It must be exactly the same. Double-check for extra spaces too.
Many times issues like is are because of a typo on the topic name.
Are you using any tutorial as a guidance?
Regards,
Sara
I am subscribing to (no quotes -and I have checked spelling many times).
My publish statement is:
client.publish(“new_packet/humidity”, _hum,2)
I am subscribing to
new_packet/humidity
I know I am missing something that’s obvious.
Thanks again,
Steve
I now have client.publish working, but I want to only publish readings when it changes. Here’s my entire looping code.
void loop() {
temp = htu.readTemperature();
hum = htu.readHumidity();
Serial.print(“Temp: “); Serial.println(temp);
Serial.print(“Hum: “); Serial.println(hum);
delay(100);
char _hum[8]; // Buffer big enough for 7-character float
char _temperature [8];
dtostrf(hum, 3, 2, _hum); // Leave room for too large numbers!
dtostrf(temp, 3, 2, _temperature); // Leave room for too large numbers!
client.publish(“monitor6/moisture”, _hum, 1);
client.publish(“monitor6/moisture”, _temperature, 1);
delay(2000);
}
As always thanks for your help for this almost newby.
Steve
I know I am doing something wrong, but, can’t figure it out.
Hi.
Do you know what was the issue with the publishing?
If you only want to publish when there are new values, you need two variables. One to save the current reading and another one to save the last reading. Then, you need to compare both variables. By adding an if statement with the comparison you can publish when the reading is new.
For example, here’s a pseudo-code for that:
initialize this variable at the start of the code:
old_temperature = 0;
Then, in the loop():
void loop(){ new_temperature = htu.readTemperature(); if (new_temperature != old_temperature) { //publish your readings (add the code to publish readings...) // the old_temperature is now the new_temperature old_temperature = new_temperature } }
I hope this helps.
Regards,
Sara
Sara,
I know i am missing something obvious in trying to get my mqtt monitor to publish a new humidity reading when the humidity changes. I set both hum and old_hum to 0 at the beginning of the sketch.
Here is the pertinent code:
void loop() {
hum = htu.readHumidity();
char _hum[8]; // Buffer big enough for 7-character float
dtostrf(hum, 3, 2, _hum);
while(old_hum !=hum){
Serial.print(“Sensor reading”);
Serial.print(“Hum: “);
Serial.println(hum);
Serial.print(“previous “);
Serial.println (old_hum);
client.publish(“packet/humid”, _hum,1);
old_hum = hum;
delay(5000);
}
//end while
}
//end loop
Please help me understand what I need to add (or change) to get a new reading when it changes.
As always, many thanks in advance,
Steve
comparing floats is very unwise, as floats are not exact values. you will find the float value changes, possibly by a small amount every reading. You would be far better off to convert the floats to integers by multiplying the float value by 100. if you convert both the old value and new value to integers (uint16_t) then you can do reliable comparisons. also send your data as integer values and convert back to floats on the receving device. (/100.00).
Rather than constantly comparing values looking for a change you could take a reading, say, every 5 minutes, using mills() and then do a compare to see if the reading has changed