Sending MQTT messages containing sensor data from BME680 to any MQTT broker that requires MQTT over TLS (AWS IoT Core) in no_std Rust! π¦
π Based on: esp32c3-no-std-async-mqtt-demo
This program allows the user to get measured data from a BME680 sensor π‘ and send them in MQTT messages π¬ via a TLS session to an MQTT Broker (in this case AWS IoT Core). All of this is done in Rust π¦ without the use of the standard library. The program uses the ESP32S3-BOX Devkit.
- esp-hal ποΈ for peripheral access to the chip
- esp-wifi πΆ for wifi connection
- embassy π for async
- esp-mbedtls π for TLS
- rust-mqtt π¬ for MQTT
- bme-680 π‘ for data retrieval
To run this project, you'll need to install the following:
- Any ESP32S3-BOX devkit π
- BME680 environmental sensor π‘
Before running the program, make sure your hardware is properly set up.
-
Connect the BME680 Sensor to ESP32S3-BOX device:
SDA
toG41
on the deviceSCL
toG40
on the device2-5V
to3v3
on the deviceGND
toGND
on the device
All 4 wires should be next to each other in the end.
-
Connect the ESP32S3-BOX to your computer:
- Use a USB-C cable to establish the connection.
- Endpoint Address: Write your MQTT broker's endpoint address into
endpoint.txt
in thesecrets/
folder. - Client ID: If you are using a private broker like AWS MQTT, a client ID is also required. Paste the client ID into
client_id.txt
in thesecrets/
folder.
If your MQTT broker requires secure connections, you'll need to provide the necessary TLS X.509 certificates:
- Trusted Root Certificate: This is generally needed to verify the broker's identity. Paste the content of your root certificate into a file named
rootCA.pem
in thesecrets/
folder. - Client Certificate: This is your device's certificate, which is presented to the broker during the handshake. Save it as
client_cert.pem.crt
in thesecrets/
folder. - Private Key: This key matches the client certificate and must be kept private. Save it as
client_private.pem.key
in thesecrets/
folder.
The certificate and key files are read in the code snippet below after crate imports in src/main.rs
:
const CERT: &'static str = concat!(include_str!("../secrets/rootCA.pem"), "\0");
const CLIENT_CERT: &'static str = concat!(include_str!("../secrets/client_cert.pem.crt"), "\0");
const PRIVATE_KEY: &'static str = concat!(include_str!("../secrets/client_private.pem.key"), "\0");
You can rename the files however you like, but you need to change the paths in consts accordingly.
π¨ Warning: Never commit your secrets and certificates to GitHub or any other public repository. The .gitignore file is set up to ignore these files, but you must also ensure not to manually include them in commits.
After setting up these files, you should have 2 tracked changes only, in
client_id.txt
andendpoint.txt
. Don't commit these!
To set up your WiFi credentials and execute the program, you have two options:
- Edit the Script: Open
run_with_wifi_credentials.sh
and enter your WiFi SSID and PASSWORD. - Run the Script: Execute the script to set the environment variables and run the program.
./run_with_wifi_credentials.sh
π¨ 3rd tracked change should appear in VSCode Source Control tab. Again, don't commit any of the 3 changes!
- Export Environment Variables: Manually set the environment variables for your WiFi credentials.
export SSID=your_wifi_ssid export PASSWORD=your_wifi_password
- Run the Program: Use the following command to run the program.
cargo run --release
Choose one of these options to set up your WiFi and execute the program.