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 …
The RPi shows a successful connection ..
The subscriber code includes print output…
But I do not see the output on the RPi.
sudo mosquitto console shows …
I’m hoping there is a straightforward solution.
Thanks!
Hi.
How did you install mosquito broker?
What code/commands are you running on the Raspberry Pi to receive the data?
Regards,
Sara
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?
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
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);
}
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