ESP-IDF implementation of reading and publishing sensor data via 2 MPU6050s connected to an ESP32 over MQTT TLS(MQTTS).
- Built referring Espressif MQTT SSL implementation example and Espressif MPU6050 sensor implementation example.
- Modifies the original MPU6050 component present in ESP component registry to provide a library to support a dual MPU6050 configuration.
- Integrates the newer "driver/i2c_master.h" driver in the original MPU6050 component source code.
- Provides easy Broker Configuration and WiFi SSID and Password configuration via menuconfig.
- Navigate to the main/cert/ folder and run the following commands(requires openssl installed):
cd main/cert/ # Generate private key(client key) openssl genrsa -out client.key # Generate client certificate signing request(client.csr) based on the private key # Enter the country, organization, and name when prompted. Leave fields blank by typing a dot (.) if you don’t want to fill them. openssl req -out client.csr -key client.key -new
- Now generate the client certificate(client.crt) at https://test.mosquitto.org/ssl/ by pasting the contents of client.csr there. You can get the contents from the terminal itself using:
more client.csr
- Store the generated client.crt file in the main/cert/ folder as well.
- Fetch the mosquitto.org CA(Certificate Authority) certificate (mosquitto.org.pem). You can also do this directly from the website as well and then storing the downloaded file in the main/cert/ folder.
curl -o mosquitto.org.crt https://test.mosquitto.org/ssl/mosquitto.org.crt
- Install Eclipse Mosquitto on your desktop from https://mosquitto.org/download/
- Tip: For Ubuntu use the ppa instead of snap. Here's why.
- Test by subscribing to a secure server on a different terminal(in the same folder):
mosquitto_sub -h test.mosquitto.org -t "testcert" -p 8884 --cafile mosquitto.org.crt --key client.key --cert client.crt -d - Publish to the topic to test:
mosquitto_pub --cafile mosquitto.org.crt --key client.key --cert client.crt -h test.mosquitto.org -m "Hello World!" -t "testcert" -p 8884 -d
- Once you have verified that it works create a new file with the name mqtt_eclipseprojects_io.pem and copy the contents of the client.key, the client.crt and the mosquitto.org.crt in it(in the same order). You can do this by running this command from the main/ directory:
cat cert/client.key cert/client.crt cert/mosquitto.org.crt > mqtt_eclipseprojects_io.pem
- After cloning the repository, set the intended target. For an ESP32 this can be done:
idf.py set-target esp32- This will help add the additionally defined options for Broker configuration and WiFi configuration in the default menuconfig.
idf.py buildidf.py menuconfig- Navigate to Broker Configuration and change the Broker URL if required. Use enter to enter and esc to exit.
- Navigate to WiFi Configuration and enter the SSID and Password for your network.
- Enter 'S' to save the configuration.
- Rebuild:
idf.py build- Connect the ESP32 via USB and upload the code and monitor using:
idf.py flash monitor- Open a new terminal window and check that data is being received by subscribing to the MQTTS topic. Make sure you are in the directory containing mosquitto.org.crt (main/cert/ in this repository)before running this.
mosquitto_sub -h test.mosquitto.org -p 8883 --cafile mosquitto.org.crt -t "sensors1635/mpu6050"