An Arduino sketch for the Adafruit SCD4X CO2 sensor and Adafruit SCD-30 CO2 sensor running on an ESP32 with an http server returning Prometheus compatible responses, and sensor readings sent via Bluetooth BLE.
Related software:
WiFi:
cp secrets.tmpl.h secrets.h
secrets.h
Arduino:
Tools > Manage Libraries...
add the library: Sensirion I2C SCD4x
SCD-30
CO2 sensor, add the library: Adafruit SCD30
#define USESCD30
or #define USESCD4X
cd ~/Documents/Arduino/library
and then git clone https://github.com/Sensirion/arduino-ble-gadget.git
cd ~/Documents/Arduino/library
and then git clone https://github.com/h2zero/NimBLE-Arduino.git
Upload
to send the code to your ESP32Note: As of 9th April 2024 this was the dependency versions that compiled on a Sparkfun ESP32 Thing:
git clone git@github.com:Sensirion/arduino-ble-gadget.git
into ~/Arduino/libraries
and then git checkout tags/1.2.0
and add #include <string>
to ByteArray.h
git clone git@github.com:Sensirion/arduino-upt-core.git Sensirion_UPT_Core
into ~/Arduino/libraries
and then git checkout 919ef8b
git clone git@github.com:h2zero/NimBLE-Arduino.git
into ~/Arduino/libraries
and then git checkout release/1.4
ESP32
boards library at v2.0.11
iOS/macOS Bluetooth app:
I built an iOS/macOS app so location data can be saved with CO2 reading data.
Project code: ios-ble-co2-sensor
This code also works with the standard Sensirion MyAmbience app:
If you open the Arduino serial monitor you'll see:
While starting up the LED will light up in this sequence
While running:
Once your ESP32 has connected to your SSID, it will respond over port 80 with a Prometheus response. e.g. curl 192.168.1.100:80/metrics
# HELP ambient_temperature Ambient temperature
# TYPE ambient_temperature gauge
ambient_temperature 19.95
# HELP ambient_humidity Ambient humidity
# TYPE ambient_humidity gauge
ambient_humidity 59.20
# HELP co2 CO2
# TYPE co2 gauge
co2 1333
# HELP battery_voltage Battery voltage
# TYPE battery_voltage gauge
battery_voltage 3.15
My ESP32-C3 with the SCD-41 CO2 sensor running off of an old Nokia BP-6MT battery.
Shoutout to Core Electronics for being so fast with their delivery of the Adafruit SCD-41.
SimpleWiFiServer
by Jan Hendrik BerlinexampleUsage
license:Copyright (c) 2021, Sensirion AG All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.