3D Printed Case
You can refer to the 3D Printing page in the Disconnected CO2 Data Logger guide to print a case with mounting holes for the SCD40.
Coding the QT Py ESP32-S2 CO2 Monitor
First, setup your QT Py ESP32-S2 with CircuitPython. Then, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT
from os import getenv
import time
import json
import digitalio
import supervisor
import board
import rtc
import socketpool
import wifi
import adafruit_ntp
from adafruit_azureiot import IoTHubDevice
import adafruit_scd4x
# Get WiFi details, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
if None in [ssid, password]:
raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
print("Connecting to WiFi...")
wifi.radio.connect(ssid, password)
print("Connected to WiFi!")
# ntp clock - update tz_offset to your timezone
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
rtc.RTC().datetime = ntp.datetime
if time.localtime().tm_year < 2022:
print("Setting System Time in UTC")
rtc.RTC().datetime = ntp.datetime
else:
print("Year seems good, skipping set time.")
esp = None
pool = socketpool.SocketPool(wifi.radio)
# Create an IoT Hub device client and connect
device = IoTHubDevice(pool, esp, getenv("device_connection_string"))
print("Connecting to Azure IoT Hub...")
# Connect to IoT Central
device.connect()
print("Connected to Azure IoT Hub!")
# setup for I2C
i2c = board.STEMMA_I2C()
# setup for SCD40
scd4x = adafruit_scd4x.SCD4X(i2c)
# start measuring co2 with SCD40
scd4x.start_periodic_measurement()
co2 = scd4x.CO2
# setup boot button as input
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
# clock to count down to sending data to Azure
azure_clock = 500
# button debounce state
button_pressed = False
while True:
try:
# button debouncing
if button.value and button_pressed:
button_pressed = False
# if you press boot
if not button.value and not button_pressed:
# pack message
message = {"CO2": co2,
"QT Connected": 1}
# send co2 measurement
device.send_device_to_cloud_message(json.dumps(message))
# measure co2
co2 = scd4x.CO2
# when the azure clock runs out
if azure_clock > 500:
print("getting msg")
# pack message
message = {"CO2": co2,
"QT Connected": 1}
print("sending json")
device.send_device_to_cloud_message(json.dumps(message))
print("data sent")
# reset azure clock
azure_clock = 0
# if no clocks are running out
# increase counts by 1
else:
azure_clock += 1
# ping azure
device.loop()
# if something disrupts the loop, reconnect
# pylint: disable=broad-except
except (ValueError, RuntimeError, OSError, ConnectionError) as e:
print("Network error, reconnecting\n", str(e))
time.sleep(10)
supervisor.reload()
continue
# delay
time.sleep(1)
Upload the Code and Libraries to the QT Py ESP32-S2
After downloading the Project Bundle, plug your QT Py ESP32-S2 into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the QT Py ESP32-S2's CIRCUITPY drive.
- lib folder
- code.py
Your QT Py ESP32-S2 CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
settings.toml
You will need to create and add a settings.toml file to your CIRCUITPY drive. Your settings.toml file will need to include the following information:
CIRCUITPY_WIFI_SSID="your-wifi-ssid" CIRCUITPY_WIFI_PASSWORD="your-wifi-password" device_connection_string="YOUR-DEVICE-CONNECTION-STRING-HERE"
You'll locate your Primary Connection String from your devices page in your IoT Hub. Make sure to refer to the Add IoT Hub Devices page in this guide to see the process for accessing the Primary Connection String.
Page last edited April 16, 2025
Text editor powered by tinymce.