/esp32s3-no-std-async-tls-mqtt

Sending MQTT messages containing sensor data from BME680 to any MQTT broker that requires MQTT over TLS (AWS IoT Core) in no_std Rust! 🦀

Primary LanguageRustApache License 2.0Apache-2.0

📡 esp32s3 no_std Async TLS MQTT

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


📋 Table of Contents


🎯 About The Project

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.

🔝 back to top


📦 Built With Crates

🔝 back to top


🔧 Prerequisites

Software Requirements

To run this project, you'll need to install the following:

  • Rust 🦀
  • Install espup tool and follow the instructions there 🛠

Hardware Requirements

🔝 back to top


🚀 Getting Started

1. 🔌 Hardware Setup

Before running the program, make sure your hardware is properly set up.

  • Connect the BME680 Sensor to ESP32S3-BOX device:

    • SDA to G41 on the device
    • SCL to G40 on the device
    • 2-5V to 3v3 on the device
    • GND to GND 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.


2. 🛡️ Setting up MQTT Configuration and Certificates

2.1 MQTT Endpoint and Client ID

  • Endpoint Address: Write your MQTT broker's endpoint address into endpoint.txt in the secrets/ 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 the secrets/ folder.

2.2 MQTT over TLS

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 the secrets/ 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 the secrets/ folder.
  • Private Key: This key matches the client certificate and must be kept private. Save it as client_private.pem.key in the secrets/ 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 and endpoint.txt. Don't commit these!



3. 📶 WiFi Setup and Program Execution

To set up your WiFi credentials and execute the program, you have two options:

Option 1: Use the Script 📜

  1. Edit the Script: Open run_with_wifi_credentials.sh and enter your WiFi SSID and PASSWORD.
  2. 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!

Option 2: Manual Setup 🤖

  1. Export Environment Variables: Manually set the environment variables for your WiFi credentials.
    export SSID=your_wifi_ssid
    export PASSWORD=your_wifi_password
  2. 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.

🔝 back to top