On Tuesday we announced the Golioth ESP-IDF SDK that delivers all of Golioth’s excellent features to ESP32 projects built on Espressif’s FreeRTOS-based ESP-IDF ecosystem. The APIs included in our SDK make it dead simple to set up an encrypted connection with Golioth and begin sending and receiving data, controlling the device remotely, sending your logging messages up to the cloud, and of course performing Over-the-Air (OTA) updates on remote devices.
Today we dive into the code as Nick Miller, Golioth’s lead firmware engineer, takes us on a guided tour.
What does Golioth ESP-IDF SDK deliver?
All of the best features of Golioth’s device management cloud are available in our ESP-IDF SDK. The set of APIs are quite clever and take all of the heavy lift out of your hands. This includes:
- Set, get, and observe data endpoints on the cloud
- Write log data back to the cloud
- Handle Over-the-Air (OTA) firmware updates
- API calls–in both synchronous and asynchronous options–to suit your needs
Setup: Install ESP-IDF and clone the Golioth repo
To get started you need have the ESP-IDF installed and clone the Golioth ESP-IDF SDK. Instructions are available in the readme of our git repository, and there is also a quickstart on our docs site.
Our new SDK is a component for FreeRTOS, the real-time operating system used by the ESP-IDF. It’s the same operating system and build tools you’re used to, with the Golioth SDK sitting on top so that your devices can interact with the Golioth servers.
Stepping through the Golioth-Basics example
The best way to test-drive is with the Golioth-Basics example that is included in the SDK. It demonstrates assigning Golioth credentials to your device, sending/receiving data, observing data, sending log messages, and performing over-the-air (OTA) firmware updates. The golioth_basics.c file is thoroughly commented to explain each API call in detail.
The example begins by initializing non-volatile storage, configuring a serial shell, checking for credentials, and connecting to WiFi. At that point we can start using the Golioth APIs.
Creating the Golioth system client
// Now we are ready to connect to the Golioth cloud. // // To start, we need to create a client. The function golioth_client_create will // dynamically create a client and return a handle to it. // // The client itself runs in a separate task, so once this function returns, // there will be a new task running in the background. // // As soon as the task starts, it will try to connect to Golioth using the // CoAP protocol over DTLS, with the PSK ID and PSK for authentication. golioth_client_t client = golioth_client_create(nvs_read_golioth_psk_id(), nvs_read_golioth_psk());
Everything starts of by instantiating a client to handle the connection for you. This client will be passed to all of the API calls so that the SDK knows where to send them.
Sending log messages
// We can also log messages "synchronously", meaning the function will block // until one of 3 things happen (whichever comes first): // // 1. We receive a response to the request from the server // 2. The user-provided timeout expires // 3. The default client task timeout expires (GOLIOTH_COAP_RESPONSE_TIMEOUT_S) // // In this case, we will block for up to 2 seconds waiting for the server response. // We'll check the return code to know whether a timeout happened. // // Any function provided by this SDK ending in _sync will have the same meaning. golioth_status_t status = golioth_log_warn_sync(client, "app_main", "Sync log", 5);
Here you can see a log being written to Golioth. Notice that the client created in the previous code block is used as the first parameter. This logging call is synchronous, and will wait to ensure the log was received by the Golioth servers. There is also an asynchronous version available that provides the option to run a callback function when the log is received by Golioth.
Setting up OTA firmware updates
// For OTA, we will spawn a background task that will listen for firmware // updates from Golioth and automatically update firmware on the device using // Espressif's OTA library. // // This is optional, but most real applications will probably want to use this. golioth_fw_update_init(client, _current_version);
OTA firmware updates are handled for you by the SDK. The line of code shown here is all it takes to register for updates. The app will then observe the firmware version available on the server. It will automatically begin the update process whenever you roll out a new firmware release on the Golioth Cloud.
Sending and receiving data
// There are a number of different functions you can call to get and set values in // LightDB state, based on the type of value (e.g. int, bool, float, string, JSON). golioth_lightdb_set_int_async(client, "my_int", 42, NULL, NULL);
// To asynchronously get a value from LightDB, a callback function must be provided golioth_lightdb_get_async(client, "my_int", on_get_my_int, NULL);
The bread and butter of the IoT industry is the ability to send and received data. This code demonstrates asynchronous set and get functions. Notice that the get API call registers on_get_my_int
as a callback function that will be executed to handle the data that arrives back from the Golioth servers.
A get command runs just once to fetch the requested data from Golioth. Another extremely useful approach is to observe the data using the golioth_lightdb_observe_async()
. It works the same way as an asynchronous get call, but it will execute your callback every time the data on the server changes.
Putting it all together
In the second half of the video, Nick takes us through the process running the demo. He starts with setting up the ESP-IDF environment and compiling to code, and continues all the way through to viewing the device data on the web console.
You’re going to love working with the Golioth ESP-IDF SDK. It’s designed to deal with all the complexity of securely connecting and controlling your IoT devices. The API calls are easy to understand, and they make it painless to add Golioth to existing and future ESP-IDF based projects. Give it a try today using our free Dev Tier.
We’d love to hear what you’re planning to build. You can connect with us on the Golioth Discord server, ask questions over on the Golioth Forums, and share your demos by tagging the Golioth account on Twitter.