I’m JC, a Firmware Developer at Xively.
We recently released an ESP32 port and demo for our MQTT library, and we’d like to share it with you. We hope you can make the most of it, re-using the code for your own projects.
My intention with this post is to help you get an ESP32 connected to the internet using MQTT+TLS quickly and securely.
The Xively C Client is a fully Open Source library (BSD 3-clause), and the TLS library we’re using in this port is WolfSSL. It’s a good example of secure MQTT communication on an ESP32, and a quick introduction to the Xively platform and the Xively Client library.
You should know there’s a very detailed step by step tutorial of this demo in our Developers Centre: https://developer.xively.com/docs/esp32
Since you’re all familiar with the ESP32 platform, I won't go into as much detail in this post.
Create a Free Xively Developer Account
- Register a free account here: https://app.xively.com/register
Create your Xively Digital Device
You need to create a "Device" in Xively to get the MQTT credentials for your device:
- Log into your free Xively Account
- Create the default ESP32 device template
- Create a device based on the ESP32 Device Template
- Copy your newly created Device Credentials. You'll need them later
Get the Xively C Client MQTT library
Code: Select all
git clone https://github.com/xively/xively-client-c.git
Flashing the Pre-Built Binaries for this Example
If you only want to test this demo, you don’t even need to build it. We’ve provided pre-built binaries, so you can flash them directly with esptool.py using these commands:
Code: Select all
cd xively-client-c/examples/esp32/xively_demo
python $IDF_PATH/components/esptool_py/esptool/esptool.py \
--chip esp32 --port /dev/tty.SLAB_USBtoUART --baud 115200 \
--before default_reset --after hard_reset write_flash -z \
--flash_mode dio --flash_freq 40m --flash_size detect \
0x1000 ./build/bootloader/bootloader.bin \
0x10000 ./build/xively_demo.bin \
0x8000 ./build/partitions_singleapp.bin
Building the Libraries and Example
- Build the libxively.a and libwolfssl.a static libraries:
Code: Select all
cd xively-client-c
make PRESET=ESP32
2. Build and flash the ESP32 example:
Code: Select all
cd xively-client-c/examples/esp32/xively_demo
make flash
Run the Demo
The demo’s functionality is rather simple to avoid bloating the code. It uses 2 peripherals: An LED (GPIO17-GND) and a button (GPIO0-GND). Control the LED from the internet publishing “1” or “0” to the …/LED topic; when you press the button, the device will publish “1” or “0” to …/Button (toggles on each button press).
The demo also gathers the credentials from the user at runtime (via UART), and saves them to Non-Volatile Storage so you don’t need to re-enter them every time. If you’d like to update them later, reboot the device and press the GPIO0 button while the LED is blinking rapidly.
Relevant Links
- Create a free Xively Developer account
- ESP32 demo tutorial - detailed step by step
- Xively C Client MQTT library: xively-client-c — It will automatically download WolfSSL when you build the ESP32 "PRESET"
- What is MQTT
- ESP32 example inside the Xively C Client’s repository
- Xively Client’s ESP32 BSP folder (in case you’d like to see it)
Even though this tutorial uses Xively, the library can be pointed at other standard MQTTv3.1 brokers. You may have to modify the Root certificates included in the Client, but the basic MQTT functionality adheres to the specification. The Xively-specific features built on top of it can be disabled.
I'll soon start working on adding OTA Firmware Updates to the ESP32 example. Xively's Secure File Transfer protocol allows you to download new firmware images over the same MQTT connection, without losing regular connectivity or responsiveness.
If you'd like to know more about Xively, our MQTT library, key features of this demo (such as OCSP support in the TLS library), etc. I'm happy to help :)
Thanks!
Juan Carlos Jimenez
Authorized Contractor of LogMeIn Inc.