So Easy MicroPython - ThingSpeak, IoT Cloud Platform

by Yungger Chen in Teachers > University+

697 Views, 0 Favorites, 0 Comments

So Easy MicroPython - ThingSpeak, IoT Cloud Platform

header.jpeg

*** Just add 2~3 lines, then your MCU will be an IoT device of ThingSpeak enabled***


from My_NetHTTP_ThingSpeak import myThingSpeak
ts = myThingSpeak(write=TS_WRITE_KEY, channel=TS_CHANNEL_ID)
ts.send([temp, humi])


*** My_NetHTTP_ThingSpeak is a library designed to let MCU upload and download data to/from ThingSpeak Cloud platform in very easy and fast way. For example as the codes, the MCU can send out data of temperature and humidity to ThingSpeak. ***

*** Before next demonstration, you should have your account on the cloud platform, we will not take more time to introduce it step by step since anyone can easily get help from official website ***

Upload Libraries to Your MCU

First, You need to upload the libraries to your MCU board:

for MicroPython v1.19 and above:

  • My_NetHTTP: A basic library for HTTP Restful API communication
  • My_NetHTTP_ThingSpeak: A library for accessing data on ThingSpeak platform
  • My_Wifi: A basic library for WiFi connection (Optional, if your code already make the MCU connect to WiFi)

or MicroPython v1.18 and under:

*** If you have no idea how to upload File to your MCU, read my another article "So Easy MicroPython - ESP8266/ESP32 MCU File Management" first.

*** If you are interested in MyWifi library or no idea how to use it, read my another article "So Easy MicroPython - My WiFi Connection"

Example 1:Upload Data to ThingSpeak Platform

upload_data.jpg
from My_Wifi import myWifi
from My_NetHTTP_ThingSpeak import myThingSpeak

wifi = myWifi()
if wifi.connect(MY_WIFI_SSID, MY_WIFI_PSWD):

ts = myThingSpeak(write='ThingSpeak_WRITE_KEY', channel='ThingSpeak_CHANNEL_ID')
humi = 78.9
temp = 34.2
ts.send([temp, humi])

wifi.disconnect(3)

*** 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:Read Data From ThingSpeak Platform

download_data.jpg
from My_Wifi import myWifi
from My_NetHTTP_ThingSpeak import myThingSpeak

wifi = myWifi()
if wifi.connect(MY_WIFI_SSID, MY_WIFI_PSWD):

ts = myThingSpeak(read='ThingSpeak_READ_KEY', channel='ThingSpeak_CHANNEL_ID')
print('\n\nRead DATA:', ts.read(2)) # the last two records of data, for example
print('\n\nRead DATA Detail:', ts.readDetail(1)) # the latest record of detail data, for example

wifi.disconnect(3)

That's So Easy, Hope to Help a Little Bit !

So Easy MicroPython - ThingSpeak, IoT Cloud platform
channels.jpg

Posted by Yungger

If it help you, and want to give some encouragement, just go HERE 😘 😘 !!