How to Connect the ESP32 MicroPython to Arduino IoT Cloud

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

  1. Go to https://thonny.org/, then select and download the Thonny IDE that is compatible with your operating system.
  2. Complete the Thonny IDE installation using installer.
  3. Open the Thonny IDE.

Firmware Installtion

  1. Go to https://micropython.org/download/, then select and download the latest MicroPython firmware that matches your board (my board is ESP32 Devkit V1).
  2. Connect your board with your PC using USB cable.
  3. Back to Thonny IDE > Run > Configure Interpreter > Interpreter.
  4. Select your board interpreter from the drop-down list (my board is ESP32, so I selected MicroPython ESP32).
  5. Select your port. (Make sure your board is connected to your PC and your board USB driver is already installed on your PC.)
  6. Click Install or update MicroPython at the right-buttom menu.
  7. Select your port.
  8. Select your firmware image file and click install to begin the firmware installation.
  9. 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

  1. Back to Thonny > Manage Packages.
  2. Then search the library name (senml ,cbor2 and logging ) and press enter.
  3. Select and install the library.
  4. Repeat until you have already installed the first three libraries.

Arduino IoT Cloud Installation

  1. Go to https://github.com/arduino/arduino-iot-cloud-py.
  2. Click code > and select Download ZIP.
  3. Extract the ZIP file.
  4. 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
  1. Back to Thonny IDE, In the MicroPython Device window at the left-buttom window, open the /lib folder.
  2. 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 Cloud Setup

  1. Go to https://cloud.arduino.cc/.
  2. Sign in or register your Arduino IoT Cloud account.
  3. Click Getting Started.
  4. Go to Things and create new things.
  5. At the Associated Device, click Select Device, then select the Manual (Any Device) option and click continue.
  6. Name your device.
  7. Download the credential as a.pdf file (the key cannot be recovered) and check "I saved my device id and secrets key"
  8. Click continue, then finish the device setup.

Create Credential File

  1. Back to Thonny IDE, create a new file.

  2. 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"
    
  3. Replace the DEVICE_ID and CLOUD_PASSWORD with your cloud credential from the download.pdf file.

  4. Save this file to your MicroPython Device (outside the /lib).

LED Control Test

Cloud Variable Setup

  1. 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
  1. Click add variable

Dashboard and Widget Setup

  1. Go to Dashboard and create new dashboard.
  2. Click Add to add a new widget.
  3. Add the Switch Widget.
  4. Link the widget variable to led_state (your cloud variable name).
  5. Click Done

Example Code

  1. Create a new file.
  2. 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()
  1. run or upload the file to your board.
  2. 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 :slight_smile:

Reference

https://docs.arduino.cc/arduino-cloud/guides/micropython/

https://docs.arduino.cc/arduino-cloud/guides/python/

Thanks for this article and it was a really important help to understand how I can connect ESP32 of Micropython. But I am experiencing following problem and not successful yet.

WARNING:root:Connection failed 'SSLContext' object has no attribute 'check_hostname', retrying after 1.0s
WARNING:root:Connection failed 'SSLContext' object has no attribute 'check_hostname', retrying after 1.2s

I checked library and code but all looks fine.
How can I solve this issue?

1 Like

I tested with the Arduino IoT cloud library version 1.1, and so far it worked so well, but when I updated the library to the latest version (now it is 1.2), I got the same issue as you encountered.

When I look into the official repository, I see that they update the library with SSLContext, and I think that is not complete somehow or that it might not be compatible with the old code.

So I just use the older version, which is working for now.

Thanks. By using previous version library instead of 1.20 version, I could finally register my ESP32 using micropython to Arduino IOT cloud.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.