In this tutorial I’ll show you how to send MQTT data to Adafruit IO. The hardware that I’m using is a LoPy (only its WiFi interface) and an analog temperature sensor TMP36. The circuit is rather simple:
The capacitor between the A/D pin and the ground is used to eliminate noise, but you can skip that if you don’t care. For the same reason (increasing the measurement accuracy) I’m averaging 100 sample A/D measurements. Measuring only once works too, but it’s less accurate.
Enough with the theory, let’s move on to the code. I’ll reuse most of Pycom’s MQTT tutorial code. The main difference is that I’ll connect to Adafruit IO instead of HiveMQ. So if you don’t have an Adafruit account yet, now is a good time to create one. Here’s the code:
Note that I’m sending the temperature value every 30 seconds which is an overkill. In practice you should use a more pragmatic interval. By default Adafruit IO feed creates a private feed showing the measurements and a nice graph. I’ve changed it to public so that you can take a look too.
Happy Hacking :)