In this topic, I will show you how to use the ESP32 installed with MicroPython to get started connecting to the Arduino IoT Cloud.
Full instruction with Canva here
What we need?
Hardware
- ESP32 Devkit V1
- USB cable
Software
- Thonny IDE
- MicroPython Firmware (version 1.22 or newer)
- Arduino IoT Cloud Account
- Arduino IoT Cloud Library
- Arduino IoT Cloud Credential
Thonny Installation
- Go to https://thonny.org/, then select and download the Thonny IDE that is compatible with your operating system.
- Complete the Thonny IDE installation using installer.
- Open the Thonny IDE.
Firmware Installtion
- Go to https://micropython.org/download/, then select and download the latest MicroPython firmware that matches your board (my board is ESP32 Devkit V1).
- Connect your board with your PC using USB cable.
- Back to Thonny IDE > Run > Configure Interpreter > Interpreter.
- Select your board interpreter from the drop-down list (my board is ESP32, so I selected MicroPython ESP32).
- Select your port. (Make sure your board is connected to your PC and your board USB driver is already installed on your PC.)
- Click Install or update MicroPython at the right-buttom menu.
- Select your port.
- Select your firmware image file and click install to begin the firmware installation.
- Wait until the firmware is installed on your board.
Library Installation
We need to install the Arduino IoT Cloud Library and its dependencies on the board in order to connect to the Arduino Cloud IoT using the following list:To install the senml, cbor2, and logging, you can use Thony IDE package manager to install, but the Arduino IoT cloud py requires manual upload.
Dependencies Installation
- Back to Thonny > Manage Packages.
- Then search the library name (senml ,cbor2 and logging ) and press enter.
- Select and install the library.
- Repeat until you have already installed the first three libraries.
Arduino IoT Cloud Installation
- Go to https://github.com/arduino/arduino-iot-cloud-py.
- Click code > and select Download ZIP.
- Extract the ZIP file.
- Go to the src directory, and you will have the arduino_iot_cloud folder, whose contents contain the following file:
- ucloud.py
- umqtt.py
- ussl.py
- __ init __.py
- Back to Thonny IDE, In the MicroPython Device window at the left-buttom window, open the /lib folder.
- Upload the entire arduino_iot_cloud folder to the /lib folder.
The /lib folder must contain the following file and folder:
- MicroPython Device /lib
- arduino_iot_cloud
- ucloud.py
- umqtt.py
- ussl.py
- __ init __.py
- cbor2
- senml
- logging.py
- arduino_iot_cloud
Arduino Cloud Setup
- Go to https://cloud.arduino.cc/.
- Sign in or register your Arduino IoT Cloud account.
- Click Getting Started.
- Go to Things and create new things.
- At the Associated Device, click Select Device, then select the Manual (Any Device) option and click continue.
- Name your device.
- Download the credential as a.pdf file (the key cannot be recovered) and check "I saved my device id and secrets key"
- Click continue, then finish the device setup.
Create Credential File
-
Back to Thonny IDE, create a new file.
-
Copy the following code into the your file:
WIFI_SSID = "your_wifi_ssid" WIFI_PASSWORD = "your_wifi_password" DEVICE_ID = b"your_device_id" CLOUD_PASSWORD = b"your_secret_id" -
Replace the DEVICE_ID and CLOUD_PASSWORD with your cloud credential from the download.pdf file.
-
Save this file to your MicroPython Device (outside the /lib).
LED Control Test
Cloud Variable Setup
- Go to Arduino Cloud and click add a new cloud variable, then setup the variable in the following detail:
- Variable Name : led_state
- Type : Boolean
- Permission : Read & Write
- Update Policy : On Change
- Click add variable
Dashboard and Widget Setup
- Go to Dashboard and create new dashboard.
- Click Add to add a new widget.
- Add the Switch Widget.
- Link the widget variable to led_state (your cloud variable name).
- Click Done
Example Code
- Create a new file.
- Coppy this following code to your file:
# Import Library
from machine import Pin
from time import sleep
import network
import logging
from arduino_iot_cloud import ArduinoCloudClient
# Import Credential
from secrets import WIFI_SSID
from secrets import WIFI_PASSWORD
from secrets import DEVICE_ID
from secrets import CLOUD_PASSWORD
# Pin Setup
led_pin = Pin(2,Pin.OUT)
# WiFi Connection Function
def wifi_connect():
if not WIFI_SSID or not WIFI_PASSWORD:
raise (Exception("Network is not configured. Set SSID and passwords in secrets.py"))
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected():
logging.info("Trying to connect. Note this may take a while...")
sleep(0.5)
logging.info(f"WiFi Connected {wlan.ifconfig()}")
# Callback Function
def onLedChange(client,value):
if value:
led_pin.value(1)
print('LED ON!')
else:
led_pin.value(0)
print('LED OFF!')
# Main Program
if __name__ == "__main__":
logging.basicConfig(datefmt="%H:%M:%S",format="%(asctime)s.%(msecs)03d %(message)s",level=logging.INFO)
wifi_connect()
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=CLOUD_PASSWORD)
client.register('led_state',on_write=onLedChange)
client.start()
- run or upload the file to your board.
- Back to dashboard and toggle your switch widget to turn on or off the board build in led at GPIO2
Conclusion
Now you can connect your MicroPython ESP32 with Arduino IoT Cloud and control your builtin led with Arduino IoT Cloud Widget.
If your have any problem/Any Question feel free to ask.
Hope this might be helpful for anyone ![]()
