In this post I’ll show you how to use the Arduino Uno Wifi (Developer Edition) with a DHT11 sensor to send MQTT messages.
For this tutorial you require the following:
- Arduino Uno Wifi (Developer Edition)
- DHT11 Sensor (although you can edit it to work with others)
- Wifi connection already setup for the Arduino
- A MQTT Server application (like Mosquitto and MQTT.fx) setup
Setting up the arduino with a MQTT Broker
Mosquitto is kind enough to provide a free testing environment to try out MQTT without having to install your own Mosquitto instance (more info)
Connect to the webpage of your Arduino and configure the Connectivity
You’ll have to update your client-id to be unique (remember you are sharing this server with the rest of the world)
Install and configure MQTT.fx
Once installed configure a connection to test.mosquitto.org
As always, source code for this tutorial is available in the learn-arduino Github repository
Running the code
git pull https://github.com/trojanc/learn-arduino cd learn-arduino/mqtt-dht11 platformio init platformio run
Once you have it running you should see the results publish on the Serial output, as well as in MQTT.fx when connected and subscribed
No output on Serial
Make sure your Arduino has a connection to the MQTT Broker. It might “freeze” for a while when busy trying to connect.
Make sure about the pin to which your DHT11 sensor is connected (and that it’s connected correctly)
Random data on the topic
Because you are sharing the test mosquitto instance with the rest of the world, some one else might be publishing to the same topic. Someone might be trying this example at the exact time your are and might be publishing their readings.
To solve this issue, just change the topic on which you publish to something else
No MQTT messages
Make sure the arduino is actually connected to the internet, and that the status on the arduino shows it is connected to the broker.
Make sure you PC is connected to the internet and connected to test.mosquitto.org port 1883.
Double check the topic your Arduino is publishing too, and the topic that MQTT.fx is subscribed to.