• 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 – No output

Q&A Forum › Category: Raspberry Pi › MQTT – No output
0 Vote Up Vote Down
Jack Douglas asked 3 years ago

I have a question regarding a simple MQTT setup with esp32 client and RPi broker/subscriber using different libraries than in the course.
My esp32 MQTT client is happily sending data from a DHT22 …

https://imgur.com/vyx3PIo

The RPi shows a successful connection ..

https://imgur.com/VL2UKJg

The subscriber code includes print output…

https://imgur.com/yFDu6F9

But I do not see the output on the RPi.
sudo mosquitto console  shows …

https://imgur.com/J799SLG

I’m hoping there is a straightforward solution.
Thanks!

Question Tags: Raspberry_Pi esp32 mqtt
6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
How did you install mosquito broker?
What code/commands are you running on the Raspberry Pi to receive the data?
Regards,
Sara

0 Vote Up Vote Down
Jack Douglas answered 3 years ago

I used

sudo apt install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto.service

On the RPi

import paho.mqtt.client as mqtt
MQTT_ADDRESS = ‘10.0.0.xxx’
MQTT_USER = ‘pi’
MQTT_PASSWORD = ‘xxxxxxx’
MQTT_TOPIC = ‘home/+/+’
def on_connect(client, userdata, flags, rc):
“”” The callback for when the client receives a CONNACK response from the server.”””
print(‘Connected with result code ‘ + str(rc))
client.subscribe(MQTT_TOPIC)
def on_message(client, userdata, msg):
“””The callback for when a PUBLISH message is received from the server.”””
print(msg.topic + ‘ ‘ + str(msg.payload))
def main():
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect(MQTT_ADDRESS, 1883)
mqtt_client.loop_forever()
if __name__ == ‘__main__’:
print(‘MQTT to InfluxDB bridge’)
main()

 

I used this code because I am using a different sensor and no LED, so wasn’t clear how to modify the code in the course.   Could you also outline the ‘basic’ blocks of code needed for any RPI broker/subscriber and esp32 publisher, no matter what sensor is used?

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.

Where did you get that code for the RPi?
In the course, we use Node-RED, we don’t use a Python code. I don’t understand what you’re trying to do. Can you better explain?

To publish data via MQTT from a DHT sensor, we have this tutorial: https://randomnerdtutorials.com/esp32-mqtt-publish-dht11-dht22-arduino/

 

Unfortunately, we don’t have examples running Python on the Raspberry Pi. But, the code you shared should work as long as you’re using the same MQTT topics.

Are you using the same MQTT topics? If you want to receive the messages on the RPi, your python code needs to subscribe to the same topic that the ESP32 is publishing. From what you shared, I can’t check if the topics are the same.

Regards,
Sara

0 Vote Up Vote Down
Jack Douglas answered 3 years ago

I tried the above DHT link, but I am experiencing a strange “too many decimal points in number” error for

#define MQTT_HOST IPAddress(10.0.0.xxx)

I will try Node RED, but I just want to create a simple DHT22 esp32 publisher/client and RPi broker/subscriber. It’s a little confusing for me to figure out how to strip out the LED and Dallas sensor and convert the RedNODE example code for a DHT22 with temp and humidity topics.

If more topics are to be added, for example humidity, or if I use a BME280, there is also pressure, I would copy the last three lines for each?

// Add more topics that want your ESP32 to be subscribed to
void onMqttConnect(bool sessionPresent) {
  Serial.println(“Connected to MQTT.”);
  Serial.print(“Session present: “);
  Serial.println(sessionPresent);
  // ESP32 subscribed to esp32/Temperature topic
  uint16_t packetIdSub = mqttClient.subscribe(“Temperature”, 0);
  Serial.print(“Subscribing at QoS 0, packetId: “);
  Serial.println(packetIdSub);
}

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
You should separate the IP address numbers with commas like so:

#define MQTT_HOST IPAddress(192, 168, 1, XXX)

Yes, if you want to subscribe to more topics, you just need to multiply those lines.
Regards,
Sara

0 Vote Up Vote Down
Jack Douglas answered 3 years ago

Thank you for your help.

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

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition 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.