Code Setup

CircuitPython Library Installation

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle matching your version of CircuitPython. PyPortal requires at least CircuitPython version 4.0.0.

Before continuing make sure your board's lib folder has the following files and folders copied over.

  • adafruit_azureiot
  • adafruit_bitmap_font
  • adafruit_bus_device
  • adafruit_display_text
  • adafruit_esp32spi
  • adafruit_register
  • adafruit_seesaw
  • neopixel

Add CircuitPython Code and Project Assets

In the embedded code element below, click on the Download: Project Zip link, and save the .zip archive file to your computer.

Then, uncompress the .zip file, it will unpack to a folder named PyPortal_Azure_Plant_Monitor.

Copy the contents of the PyPortal_Azure_Plant_Monitor directory to your PyPortal's CIRCUITPY drive.

PyPortal Azure IoT Plant Monitor
Log plant vitals to Microsoft Azure IoT with
your PyPortal

Author: Brent Rubell for Adafruit Industries, 2019
import time
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import neopixel
from adafruit_azureiot import IOT_Hub
from adafruit_seesaw.seesaw import Seesaw

# gfx helper
import azure_gfx_helper

# Get wifi details and more from a file
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in, please add them there!")

# PyPortal ESP32 Setup
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

# Soil Sensor Setup
i2c_bus = busio.I2C(board.SCL, board.SDA)
ss = Seesaw(i2c_bus, addr=0x36)

# Create an instance of the Azure IoT Hub
hub = IOT_Hub(wifi, secrets['azure_iot_hub'], secrets['azure_iot_sas'], secrets['azure_device_id'])

# init. graphics helper
gfx = azure_gfx_helper.Azure_GFX(False)

while True:
        # read moisture level
        moisture_level = ss.moisture_read()
        # read temperature
        temperature = ss.get_temp()
        # display soil sensor values on pyportal
        temperature = gfx.display_temp(temperature)
        print('Sending data to Azure')
        gfx.display_azure_status('Sending data...')
        gfx.display_azure_status('Data sent!')
        print('Data sent!')
    except (ValueError, RuntimeError) as e:
        print("Failed to get data, retrying\n", e)

This is what the final contents of the CIRCUITPY drive will look like:

Install the Mu Editor

This guide requires you to edit and interact with CircuitPython code. While you can use any text editor of your choosing,  Mu is a simple code editor that works with the Adafruit CircuitPython boards. It's written in Python and works on Windows, MacOS, Linux and Raspberry Pi. The serial console is built right in, so you get immediate feedback from your board's serial output!

Before proceeding, click the button below to install the Mu Editor. There are versions for PC, mac, and Linux.

Secrets File Setup

First, use Mu to open up a file on your CIRCUITPY drive. Next, you're going to edit the file to enter your local WiFi credentials along with data about your Azure Device and Hub.

Make the following changes to the code below in the file:

  • Replace MY_WIFI_SSID with the name of your WiFi SSID
  • Replace MY_WIFI_PASSWORD with your WiFi's password
  • Replace MY_AZURE_HUB_NAME with the name of your Azure IoT Hub.
  • Replace MY_AZURE_HUB_SAS_TOKEN with the Shared Access Signature token you copied down earlier.
  • Replace MY_AZURE_DEVICE_ID with the name of your Azure IoT Device.
Download: file
# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'MY_WIFI_SSID',
    'password' : 'MY_WIFI_PASSWORD',
    'azure_iot_hub' : 'MY_AZURE_HUB_NAME',
    'azure_iot_sas' : 'MY_AZURE_HUB_SAS_TOKEN',
    'azure_device_id' : 'MY_AZURE_DEVICE_ID'

You can then close, saving the updated file onto the device.


Setting up the PyPortal with the Azure IoT Hub is finished! You do not need to repeat this process again unless you change your IoT Hub name, or your SAS token expires.

This guide was first published on May 24, 2019. It was last updated on May 24, 2019. This page (Code Setup) was last updated on Jan 05, 2020.