*** Just simply add 2~3 lines, then your MCU (ESP8266 or ESP32) can be a MQTT enabled IoT device***
from MyMQTT_TBChain import myThingsBoard
mq = myThingsBoard(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 to publish data to ThingsBoard MQTT broker in very easy and fast way. Just add few lines of statements as above, a MCU will send message to the MQTT server. ***
*** Before moving to next demonstration, you should have your ThingsBoard 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_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 ThingsBoard
from MyMQTT_TBChain import myThingsBoard
mq = myThingsBoard(YOUR_DEVICE_ID, user=YOUR_DEVICE_TOKEN)
mq.connect()
mq.publish({'temperature': 28.7, 'humidity': 87.5})
Example 2: Download/Read data from ThingsBoard
*** Please read my other article “So Easy MicroPython — ThingsBoard, IoT Cloud Platform” . ***
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 😘 😘 !!