So Easy MicroPython — ThingSpeak, IoT Cloud Platform

Yungger
3 min readApr 25, 2021

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

from MyREST_ThingSpeak import myThingSpeak
my_ts = myThingSpeak(write=YOUR_WRITE_KEY, id=YOUR_CHANNEL_ID)
my_ts.send([38, 85]) # example data = [temperature, humidity]

*** MyREST_ThingSpeak is a very useful library I designed to let your MCU upload and download data to/from ThingSpeak Cloud platform in very easy and fast way. Just like the codes above, your MCU will send out data of temperature and humidity to ThingSpeak. ***

*** Before moving to next demonstration, please to have applied your ThingSpeak account first. ***

Step 1: Upload library to your MCU

First, download and upload the libraries to your MCU !

  • MyREST: A basic library for using Restful API communication
  • MyREST_ThingSpeak: A specific function library for accessing data on ThingSpeak 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:Upload/Write data to ThingSpeak

from MyREST_ThingSpeak import myThingSpeak
my_ts = myThingSpeak(write=YOUR_WRITE_KEY, id=YOUR_CHANNEL_ID)
my_ts.send([38, 85]) # example data = [temperature, humidity]
upload 2 records of weather data to ThingSpeak

*** If you are using Free version of ThingSpeak service, please be aware of every sending data should wait for time interval of 15 seconds at least. ***

Example 2:Download/Read data from ThingSpeak

from MyREST_ThingSpeak import myThingSpeak
my_ts = myThingSpeak(read=TS_READ_KEY, id=TS_CHANNEL_ID)
print('\n\nRead DATA:', my_ts.read(2)) # the last two records of data for example
download the latest 2 records of weather data fromThingSpeak

DEMO:

This demo shows how to upload testing dataset to ThingSpeak, and the live chart updated simultaneously.

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

--

--