In this project, we want to connect some Sensirion sensors to a microcontroller and read data from them. We then want advertise the incoming data via BLE so that Sensirion's MyAmbience app can display our sensor values.
This project also demonstrates how this can be done using Zehpyr RTOS.
Hardware setupThe hardware is the easy part. You just need to connect the two sensors together with the SparkFun QWIIC 50mm cable and from one of the sensors with the SparkFun QWIIC breakout cable to the devkit as shown in the picture below.
- Black (GND) goes to GND
- Red (VDD) goes to 3V3
- Blue (SDA) goes to pin 21
- Yellow (SCL) goes to pin 22
Be careful to get the wiring correct. Please note, that using the wrong wiring could damage your sensor!
If you are not using the same sensor boards as described in this project, you may need to add pull-up resistors for the I2C.
Zephyr
We use Zephyr RTOS for this Project.
For this project we will use the freestanding app model for the directory structure.
What is Zephyr?
Zephyr is a free, open-source real time operating system (RTOS).
An RTOS is an operating system that has been specially developed to efficiently manage time-critical tasks. It guarantees that certain tasks are executed within a defined time (real time).
Zephyr has the big advantage that code can easily run on different hardware than it was developed for.
Create new project
This project uses Zephyr 4.2. You can try to use a newer version of Zephyr, be aware that changes in the API or configuration structure may cause the current code to break or require adjustments.
First follow the Getting started guide until step 5 (Get Zephyr).
In step 5 it's recommended to do west init in a local folder called "zephyrprojects" and use this command instead:
west init -m https://github.com/zephyrproject-rtos/zephyr --mr v4.2.0 zephyr_4.2
cd zephyr_4.2
west update
Afterwards you can finish the getting started guide.
Espressif HAL requires WiFi and Bluetooth binary blobs in order work. So run the command below to retrieve those files.
west blobs fetch hal_espressif
If you want you can then run the blinky example with the command below. But you can't see the LED blinking on the ESP32-DEVKITC-32E.
west build -b esp32_devkitc/esp32/procpu samples/basic/blinky
west flash
After you finished this process, copy and paste the ble_gadget from the repository linked below in your zephyrprojects folder.
Your zephyrprojects forlder should now look like this:
zephyrprojects/├── zephyr_repo/└── ble_gadget/
Build the application
Finally the last step is to build and flash our application.
cd ble_gadget
SET ZEPHYR_BASE=%HOMEPATH%\zephyrprojects\zephyr_repo\zephyr
west build -b esp32_devkitc/esp32/procpu app
west flash
To specify the location where zephyr is installed, we need to set the ZEPHYR_BASE
environment variable. You need to run this command once every time you open a new Terminal window.
Note: You may need to change %HOMEPATH%
to the path where your zephyrprojects folder is located.
Now you should see the device as SCD-Gadget AA:BB in your MyAmbience app as shown in the picture below.
What is BLE advertisement?
BLE (Bluetooth Low Energy) advertisement is a way for Bluetooth devices to broadcast small packets of data to nearby devices without direct connection.Devices can send advertisements periodically, and other devices (in our case the MyAmbience app) can listen for them. This process is efficient in terms of power consumption, making it ideal for devices like Sensirion gadgets.
Sensirion's BLE protocol
You can find the detailed documentation HERE.Sensirion's BLE advertising protocol includes these 3 elements:
Flags:
The first data element contains the advertisement flags. In our case it's the only general discoverable
flag, which has the value 0x06.
Manufacturer data:
The second data element contains the manufacturer data. The first word is the company identifier, which is 0x06D5. Everything else depends on the sample type you have selected. In our case, it is sample type 10. You can find more information on this in Live Data in the detailed documentation.
Complete local name:
The third data element contains the complete local name, which must be "S" or begin with "S" in order to be recognized as a "DiY gadget" by the MyAmbience app.
CodeIn this section we will quickly go through the key elements of the provided code and explain their role.
Kconfigfile:
In the prj.conf file you must enable configurations based on the drivers you use.
You can find these configurations in the Kconfig-Search.
Overlay file:
An overlay file is an extension of the original Devicetree. Devicetree is a very powerful tool in Zephyr. Devicetree essentially describes the hardware that you are using and on top of that is the application code. This is the reason why you can use the same functions for the same thing on every board.So you can basically run any code on any board. Of course there are some requirements, in our case Bluetooth and I2C capability.
The app.overlay file specifies the sensors used by the application. Within the code, these sensors are referenced using aliases such as rht and co2, which must be properly defined in the overlay file. Each sensor requires specific configuration settings to function correctly. For detailed information on the supported sensors and their configuration options, go to the Zephyr documentation.
Get sensor data:
As already mentioned, it is fairly easy to use sensors if a driver is available.
Perform a measurement:
sensor_sample_fetch();
Get the measured values form the driver:
sensor_channel_get();
Threads:
One of the key advantages of using an RTOS like Zephyr is its ability to manage simultaneously running tasks through multithreading. In this application, this capability allows the SHT4x sensor to execute multiple short-duration measurements in parallel with the SCD4x sensor, which performs a longer, blocking measurement. This concurrent execution improves system efficiency by maximizing sensor utilization and minimizing idle CPU time.
For more details read Threads documentation.
Comments