To launch the communication between multiple ESP32 securely via MQTT, we need:
- a KMS (Key Management Service) implemented in Python,
- a secure MQTT client implemented on the ESP32,
- a real Mosquitto broker,
- and a simple local network (WiFi).
- Python 3.13 installed
- uv installed (lightweight package/venv manager)
Verify:
python --version
uv --version- Install Mosquitto broker
Verify:
mosquitto -hYou have to be in the kms folder.
Create a new uv environment and install dependencies:
uv venv --python 3.13Activate the uv environment and then install dependencies:
uv pip install -r requirements.txtIn another terminal, launch the Mosquitto broker:
sudo mosquitto -c /etc/mosquitto/mosquitto.conf -vThe content of /etc/mosquitto/mosquitto.conf should be:
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest stdout
include_dir /etc/mosquitto/conf.d
and the folder /etc/mosquitto/conf.d should contain a file named esp32.conf with the content:
listener 1883 0.0.0.0
allow_anonymous true
You have to be in the folder where mosquitto.conf is located (root folder of the project):
mosquitto -c mosquitto.conf -vCreate a .env file in the kms folder with the following content:
WIFI_SSID=NAME
WIFI_PASSWORD=PASSWORD
MQTT_BROKER=192.168.x.x
MQTT_PORT=1883
If you are on linux, make sure port 1883 is open:
sudo ufw allow 1883In the uv environment, launch the kms server in the kms folder:
uv run -m kms_serveruvicorn fastapi_server:app --reload --port 8000NB: Make sure you are in the kms folder and that your environment is activate
With the Arduino IDE, flash the firmware located in the firmware folder to each ESP32.
Press and hold the button on each ESP32 for 5 seconds to reset the configuration. The reset is confirmed by the OLED display and the white led turning on after 5 seconds. After reset, the ESP32 will reboot and start the configuration process again.
Now that you have reset each ESP32, you need to give them data to connect to the WiFi, the IP address of the Mosquitto broker,... For that you have two options:
Use the Arduino Serial Monitor to input the data for each ESP32. Copy the one line json given by the KMS server for each ESP32 and paste it in the Serial Monitor of each ESP32, then press enter.
Replace the values in the kms/provision_esp.py script according to data given by the KMS server for one ESP32, then run the script:
uv run -m provision_espDo it for each ESP32 by changing the values in the script according to data given by the KMS server for each ESP32. Don't forget to change the SERIAL_PORT variable according to the port of each ESP32.