• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

mqtt newby question

Q&A Forum › Category: Raspberry Pi › mqtt newby question
0 Vote Up Vote Down
Steve Fisch asked 4 months ago

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

Question Tags: mqtt publixh / receive
11 Answers
0 Vote Up Vote Down
Best Answer
Sara Santos Staff answered 4 months ago

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

0 Vote Up Vote Down
Steve Fisch answered 4 months ago

I am not getting any values at all. What value should I attempt to get when I subscribe?

0 Vote Up Vote Down
Sara Santos Staff answered 4 months ago

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

0 Vote Up Vote Down
Steve Fisch answered 4 months ago

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

0 Vote Up Vote Down
Steve Fisch answered 4 months ago

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.  
 

0 Vote Up Vote Down
Sara Santos Staff answered 4 months ago

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

0 Vote Up Vote Down
Steve Fisch answered 4 months ago

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

0 Vote Up Vote Down
Bob Green answered 4 months ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 4 months ago

Hi Steve.
Did you take a look at Bob’s suggestion?
Regards,
Sara

0 Vote Up Vote Down
Steve Fisch answered 3 months ago

Thanks, Bob! I am using your suggestion. I also realized that I had not completed everything necessary to get my Mosquitto Broker working properly – that, along with your suggestion, fixed my problem.

Thanks, Bob and Sara.

Please close this issue.

Steve

0 Vote Up Vote Down
Steve Fisch answered 3 months ago

Thanks, Bob! I am using your suggestion. I also realized that I had not completed everything necessary to get my Mosquitto Broker working properly – that, along with your suggestion, fixed my problem.

Thanks, Bob and Sara.

Please close this issue.

Steve

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.