How-to: Using MQTT with Adafruit.IO and Thingspeak

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by Mike Teachman » Fri Dec 09, 2016 10:11 pm

I've been learning how to use MQTT with two free hobbyist cloud data services. The MQTT protocol is supported in a built-in library in the Micropython binaries -- this protocol can be used send data from your ESP8266, over WIFI, to a free cloud database. For example, your ESP board could read an I2C temperature sensor every minute, then send the data to a cloud database, connecting via WiFi. Then, the cloud stored data can be plotted on a dashboard and viewed using any web browser. The two databases that I have worked with so far are:

1. Adafruit.io https://io.adafruit.com/
2. Thingspeak https://thingspeak.com/

In case anyone can take advantage of this project, I posted the Python code to Github. The code shows:

1. connecting to a WiFi network
2. connecting to the cloud data service using MQTT protocol
3. reading a temperature sensor via I2C
4. reading the number of free bytes in the micropython heap
5. sending the temperature and heap values to a cloud data service using MQTT
6. repeat steps 4 and 5 with a configurable period (e.g. every 30s)

There is a Github repository for each db implementation:

Adafruit: https://github.com/MikeTeachman/micropy ... tt-esp8266
Thingspeak: https://github.com/MikeTeachman/micropy ... tt-esp8266

Please post here if you have any questions, or suggestions.

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by platforma » Mon Dec 12, 2016 10:49 am

Very informative, I was going to try adafruit io at some point, thanks for sharing this!

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by weldeng » Fri Dec 16, 2016 1:39 am

Thanks for posting. I am trying the thingspeak version and not having much luck. Entering line by line in WebRepl to see where the problem might be I get the first error as follows:


>>> from umqtt.simple import MQTTClient
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name MQTTClient

Any suggestions on what I am doing wrong?

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by Mike Teachman » Fri Dec 16, 2016 6:50 pm

One thought -- is your ESP running a micropython binary release that includes the MQTT library?

I have used MQTT with two binaries, where the MQTT library is built-in:
esp8266-20160909-v1.8.4.bin
esp8266-20161110-v1.8.6.bin

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by weldeng » Sat Dec 17, 2016 2:22 am

Mike Teachman wrote:One thought -- is your ESP running a micropython binary release that includes the MQTT library?

I have used MQTT with two binaries, where the MQTT library is built-in:
esp8266-20160909-v1.8.4.bin
esp8266-20161110-v1.8.6.bin

I am running esp8266-20161017-v1.8.5.bin I will flash with esp8266-20161110-v1.8.6.bin on a different board and try again
Last edited by weldeng on Sat Dec 17, 2016 3:37 am, edited 1 time in total.

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by Mike Teachman » Sat Dec 17, 2016 3:16 am

I downloaded and installed the binary version you are using and tried it. For some reason I can't reproduce the issue that you see. It looks OK on my Feather Huzzah both from the REPL and WebREPL

Code: Select all

WebREPL daemon started on ws://192.168.4.1:8266
WebREPL daemon started on ws://0.0.0.0:8266
Started webrepl in normal mode
could not open file 'main.py' for reading

MicroPython v1.8.5-10-g0e69e6b on 2016-10-17; ESP module with ESP8266
Type "help()" for more information.
>>> from umqtt.simple import MQTTClient
>>> dir(MQTTClient)
['set_last_will', '_recv_len', 'ping', '__qualname__', 'subscribe', '__init__', 'set_callback', 'disconnect', '_send_str', 'connect', 'publish', 'wait_msg', '__module__', 'check_msg']
>>>
Sorry ! I don't have any other ideas at this time. Hopefully other expert debuggers will read this and offer suggestions...

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by weldeng » Sat Dec 17, 2016 3:48 am

[quote="Mike Teachman"]

MicroPython v1.8.5-10-g0e69e6b on 2016-10-17; ESP module with ESP8266
Type "help()" for more information.
>>> from umqtt.simple import MQTTClient
>>> dir(MQTTClient)
['set_last_will', '_recv_len', 'ping', '__qualname__', 'subscribe', '__init__', 'set_callback', 'disconnect', '_send_str', 'connect', 'publish', 'wait_msg', '__module__', 'check_msg']
>>>[/code]

I flashed a different board with v1.8.6 and got the same results as you above. I may have a hardware issue with my other board. Will mess with it some more this weekend. Thanks

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by weldeng » Sat Dec 17, 2016 4:13 pm

weldeng wrote:[

I flashed a different board with v1.8.6 and got the same results as you above. I may have a hardware issue with my other board. Will mess with it some more this weekend. Thanks
Mike

The other board fixed the issue. I was able to read a couple channels off an ADC1115 ADC and publish to Thingspeak. I never would have figured this MQTT stuff out without your work. Thanks again!!

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: How-to: Using MQTT with Adafruit.IO and Thingspeak

Post by Mike Teachman » Sun Dec 10, 2017 9:37 pm

I updated the example code for using MQTT with the Thingspeak service:
  • added Subscribe example code. Thinkspeak added support for MQTT Subscribe in Nov 2017
  • updated Publish example code to become current with the latest Micropython release
  • removed I2C sensor references from example code. This allows example code to be run with no added hardware.
  • improved the repo notes
https://github.com/MikeTeachman/micropy ... tt-esp8266

Post Reply