So Easy MicroPython — MQTT IDEASChain, IoT data platform

Yungger
3 min readApr 27, 2021

--

*** Just simply add a few lines, then your MCU (ESP8266 or ESP32) can be a MQTT enabled IoT device***

from MyMQTT_TBChain import myIDEASChain
mq = myIDEASChain(YOUR_DEVICE_ID, user=YOUR_DEVICE_TOKEN)
my.connect()
mq.publish({field1: value1,,, fieldN: value1})

*** MyMQTT_TBChain is a library I designed for a MCU (ESP8266/ESP32) to upload/download data to/from the IDEASChain MQTT broker in very easy and fast way. Just as simple as the codes above, your MCU can publish/subscribe messages from the platform***

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

Step 1: Upload libraries to your MCU

First, download the libraries by its link and upload them to your MCU !

  • MyMQTT: A basic library for using MQTT, a publish-subscribe protocol
  • MyMQTT_TBChain: A specific function library for accessing data on IDEASChain platform
  • MyWifi: A basic library for WiFi connection (No need, if your MCU can connect to WiFi already by your own script)

*** 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 IDEASChain

from MyMQTT_TBChain import myIDEASChain
mq = myIDEASChain(YOUR_DEVICE_ID, user=YOUR_DEVICE_TOKEN)
mq.connect()
mq.publish({'temperature': 28.7, 'humidity': 87.5})

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

import utime as time
from MyMQTT_TBChain import myIDEASChain
mq = myIDEASChain(YOUR_DEVICE_ID, user=YOUR_DEVICE_TOKEN)
mq.connect()
mq.subscribe()
while True:
message = mq.subCheck() # check if new message published
if message is not None:
print('\n *** New data from MQTT:', message)
time.sleep_ms(300)

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

import utime as time
from MyMQTT_TBChain import myIDEASChain
def mqttCallback(topic, msg):
print("*** Callback ===> Received message:{}".format(msg))
mq = myIDEASChain(YOUR_DEVICE_ID, user=YOUR_DEVICE_TOKEN)
mq.setCallback(mqttCallback)
mq.connect()
mq.subscribe()
while True:
mq.subCheck() # check if new 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 IDEASChain 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 the temp. and humidity from my DHT11 sensor to MQTT Broker
  4. Wait for 5 seconds and then publish another new dataset

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 😘 😘 !!

--

--