Skip to content

ESP-IDF implementation of reading and publishing sensor data via 2 MPU6050s connected to an ESP32 over MQTT TLS(MQTTS).

License

Notifications You must be signed in to change notification settings

scar027/mpu-2-mqtt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mpu-2-mqtt

ESP-IDF implementation of reading and publishing sensor data via 2 MPU6050s connected to an ESP32 over MQTT TLS(MQTTS).

Get MQTT Certificates for TLS

  1. 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
  2. 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
  3. Store the generated client.crt file in the main/cert/ folder as well.
  4. 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
  5. Install Eclipse Mosquitto on your desktop from https://mosquitto.org/download/
  • Tip: For Ubuntu use the ppa instead of snap. Here's why.
  1. 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
  2. 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
  3. 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

Set Target

  1. After cloning the repository, set the intended target. For an ESP32 this can be done:
idf.py set-target esp32

Build the project

  1. This will help add the additionally defined options for Broker configuration and WiFi configuration in the default menuconfig.
idf.py build

Configure MQTT Broker and WiFi SSID and Password

idf.py menuconfig
  1. Navigate to Broker Configuration and change the Broker URL if required. Use enter to enter and esc to exit.
  2. Navigate to WiFi Configuration and enter the SSID and Password for your network.
  3. Enter 'S' to save the configuration.
  4. Rebuild:
idf.py build

Flash and Monitor

  1. Connect the ESP32 via USB and upload the code and monitor using:
idf.py flash monitor

Verify by receiving

  1. 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"

About

ESP-IDF implementation of reading and publishing sensor data via 2 MPU6050s connected to an ESP32 over MQTT TLS(MQTTS).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published