So Easy MicroPython — MQTT ThingSpeak, IoT

Yungger
3 min readApr 27, 2021

--

*** Just simply add 2~3 lines into your code, then your MCU board will be a ThingSpeak enabled IoT device using MQTT protocol***

from MyMQTT_ThingSpeak import myThingSpeak
mq = myThingSpeak(write=TS_WRITE_KEY, id=TS_CHANNEL_ID)
mq.connect()
mq.publish([31.2, 67.6]) # example data = [temperature, humidity]

*** MyMQTT_ThingSpeak is a library I designed for a MCU to upload and download data to/from ThingSpeak Cloud platform in very easy and fast way. Just add few lines of codes as above, your MCU can send out data of temperature and humidity to ThingSpeak. ***

*** Before moving to next demonstration, you should have your ThingSpeak account already. ***

Step 1: Upload libraries to your MCU

First, download and upload the libraries to your MCU !

  • MyMQTT: A basic library for using MQTT, a publish-subscribe protocol
  • MyMQTT_ThingSpeak: A specific function library for accessing data on ThingSpeak platform
  • MyWifi: A basic library for WiFi connection (No need, if your code can make the MCU connect to WiFi already)

*** If no idea how to upload files to your MCU board, you can read my other article “So Easy — ESP8266/ESP32 File Management” first. ***

Step 2: Lets’ learn some examples

Example 0: Let MCU connect to your WiFi AP first

from MyWifi import myWifi
my = myWifi(YOUR_WIFI_SSID, YOUR_WIFI_PWD)
my.connect()

As an IoT device, to make your MCU connect to Internet is essentially true always. And how to do it, no matter to use your own codes or use MyWifi library. JUST need to connect it to Internet, since called an “IoT” device !!!

*** If you want to know how easy I can simply add 2~3 lines to the script, have my MCU connect to WiFi , then you might be interested to refer my other article “So Easy MicroPython — WiFi Connection, MCU as IoT” . ***

Example 1:(Publish) Upload/Write data to ThingSpeak

from MyMQTT_ThingSpeak import myThingSpeak
mq = myThingSpeak(write=TS_WRITE_KEY, id=TS_CHANNEL_ID)
mq.connect()
mq.publish([31.2, 67.6]) # example data = [temperature, humidity]

*** Need a “Write API Key”, when publish a message ***

Example 2:(Subscribe) Download/Read data from ThingSpeak

import utime as time
from MyMQTT_ThingSpeak import myThingSpeak
mq = myThingSpeak(id=TS_CHANNEL_ID, read=TS_READ_KEY, password=TS_MQTT_KEY)
mq.connect()
mq.subscribe()
while True:
message = mq.subCheck() # check new subscribed message
if message is not None:
print('\n *** Subscribe (Download) data from the ThingSpeak MQTT:', message)
time.sleep_ms(300)

*** Need a “Read API Key” and “MQTT API Key”, when read messages ***

Example 3:(Callback) Download/Read data and do the action

import utime as time
from MyMQTT_ThingSpeak import myThingSpeak
def mqttCallback(topic, msg):
if msg != "":
data = json.loads(msg)
print("*** Callback ===> Channel:{}, Temp:{}, Humi:{}, Created:{}".format(data['channel_id'], data['field1'], data['field2'], data['created_at']))

mq = myThingSpeak(id=TS_CHANNEL_ID, read=TS_READ_KEY, password=TS_MQTT_KEY)
mq.setCallback(mqttCallback)
mq.connect()
mq.subscribe()
while True:
mq.subCheck() # check if new subscribed message published
time.sleep_ms(300)

*** Automatically do the mqttCallback(), when a new message published***

DEMO:

This demo shows how to upload a testing dataset of a specific publish “Topic” to the ThingSpeak using MQTT, and do the the callback action as soon as receive the message of the same “Topic” subscribed.

  1. Setup a callback as new message received
  2. Subscribe the Topic
  3. Publish testing dataset of temperature and humidity to the Topic
  4. Check the new message of the Topic published, if received do the callback
live data updated among console, MQTT client, and the ThingSpeak.

That’s all, so easy right ? Hope this helps !

Posted by Yungger

If it really help you, you are welcome to clap your hands clicking on top-left icon, or want to buy me a coffee to encourage me to write more, I thank you too 😘 😘 !!

--

--