Blog 2Smart (EN)

How to write firmware for an IoT device based on an SDK

IoT device inside housing
After adding a new IoT device to the 2Smart Cloud platform (in our example, a smart Wi-Fi relay), testing the prototype, and assembling the device on the Printed circuit board, you have a complete product! The relay is controlled from the mobile app, voice commands in Google and Apple ecosystems, a Telegram bot, and phone calls. The first series of the device has been installed by several users who are fully satisfied with the functionality and user-friendliness. 

However, the ESPHome-based device firmware has some limitations, with the main one being unable to update the firmware over the air. To install a new firmware version, each of the devices must be connected to the computer via a USB cable, which can be inconvenient at times.

The perfect solution was the use of the 2Smart Cloud platform SDK development kit. With its help, you can prepare custom firmware. Along with the ability to update the entire series of devices over the air, SDK provides extra features like a built-in web interface with device settings, sending notifications about the device’s status, and many more. Thanks to the SDK, all of this is available "out of the box" and immediately after installing the firmware on the devices.

A notable advantage of the SDK is that it already contains the basic functions of a standard device. The vendor just needs to get a suitable, ready-made example code and use it for their device based on the tutorial. This will help describe the business logic of the device's operation. There is no need to understand the mechanics of the 2Smart Cloud IoT platform when doing this.

Preparing Custom firmware based on the SDK


For the Wi-Fi relay, the Custom firmware is already written and the code can be found on the GitHub. Changes needed are minimal, one just needs to add product-specific parameters to the code.

Because the base firmware is completely changing (we worked with ESPHome before), we couldn’t edit the existing product on the platform. We had to prepare a new product based on Custom firmware.

1- In the Vendor Cabinet, go to “Products” and click “Add Product”.

Creating a New Product in the 2Smart Cloud Vendor Cabinet

2 - Select the microcontroller on which the device is based on. In our case, it is the ESP32.

Selecting the device microcontroller

3 - Enter the information about the device such as its name, and upload an icon.

Specifying general product information

4 - Click "Add Firmware".

Go to creating the firmware of the device

5 - Select “Custom” and click “Next”.

Selecting the base for the new firmware

6 - Click the "Connection Info" link.

Link to the parameters necessary to prepare the firmware

7 - Leave the window with the connection parameters open as some of them will need to be copied into the firmware code.

Parameters for the connection required to prepare the firmware

8 - Go to the downloaded firmware repository and open the file “/src/main.h” where you will make the following changes:

  • Correct the value of brPort to "11883" (the port specified in the connection information for the Sandbox URL).
  • Copy the product ID and paste it into the firmware name and product ID sections.
  • Specify your own product name instead of the default name in the “device name” section.

Parameters for connection in the firmware code

9 - Save the changes to the firmware code and install the firmware on your device. (The one that already has ESPHome-based firmware.)

Please note! If the firmware is already written to the microcontroller, remember to delete it by following the documentation.

Firmware Installation

10 - After successful installation, the device will create its Wi-Fi access point with the same name that was specified for the device in the firmware code. Connect to this network.

Connecting to the device Wi-Fi network to access the built-in web interface

11 - Go to http://192.168.4.1/ to open the web interface of your device.

Device web interface

12 - Copy the information from the “Connection Information” window into the fields on the “Credentials” tab:

  • In the “Login” field -  "User Login",
  • In the “Token” field - "Sandbox token",
  • In the Hostname field (under the Advanced spoiler), enter "cloud.2smart.com",
  • Enter "11883" in the Broker port field,
  • in the Device ID field, copy "Sandbox Device ID".

After completing all, click Save.

Entering the parameters for connection in the web interface of the device

13 - Confirm that your computer is still connected to the device's Wi-Fi hotspot. Click the “Wi-Fi” tab and then click “Scan”.

Search for available Wi-Fi networks

14 - Select your Wi-Fi network in the list and enter the password to access it. This step is necessary for your Sandbox device to connect to the test environment of the 2Smart Cloud platform.

Please note! Devices based on the ESP32 microcontroller support only Wi-Fi networks with a frequency range of 2.4 GHz!

Selecting the Wi-Fi network to be used by the test device

15 - Return to your Wi-Fi network and open the vendor's cabinet. On the product page, close the window with the connection information. Pay attention to the status of the device. After changing the necessary settings in the web interface of the device, the status should change to “Ready”.

Device status - ready

16 - By clicking the "Preview State" link, you will be presented with the current sensor data of the device.

Parameters for the connection required to prepare the firmware

17 - Click the "Add layout" button and configure the appearance of the mobile app to control the device. Note that after installing the firmware on the Wi-Fi relay based on the SDK, widgets are available for you to work with updates and notifications.

Proceed with customizing the interface of the mobile application

We recommend that you create a separate tab in the application interface for widgets related to firmware updates. In our example, we created six such widgets in the Firmware tab:

  • Auto Update switch - turns on the automatic firmware update,
  • Update button - force-start of firmware update.
  • TimePicker widget to set the exact time of day when the device will look for a new firmware version to automatically download.
  • Text widget with update status.
  • Text widget with the version number of the current firmware on the device.
  • Text widget with the IP address of the web panel of the device.

Mobile application interface - Firmware tab

To configure notifications, consider creating a Notifications tab and add the following widgets to it:

  • System switch for system notifications (for Wi-Fi relays, these are relay switching messages using the physical button on the housing).
  • Update switch for firmware update notifications.

Mobile application interface - Notifications tab

18 - After saving the configured interface, click the link "upload firmware" and download the ready firmware.bin file to the platform, which is located in the folder with the firmware .pio/build/esp32ev/.

Firmware file upload screen on the platform

19 - Click "Publish" and confirm your wish to place the new product on the platform.

Publication of a new product

Publication of a new product

20 - Since you have created a new device on the platform, delete the previous option from the 2Smart Cloud application and pair the device again by following the standard pairing action.

The control interface of the new device gives additional features configured in the vendor's cabinet. They include control of firmware updates over the air and the configuration of notifications from the device. In the case of a Wi-Fi relay, this is exactly what we wanted to get from Custom firmware.

Device control interface in the 2Smart mobile app. Cloud

Interface for configuring device firmware updates in the 2Smart Cloud mobile app

Device notification setup interface in the 2Smart Cloud mobile app

And that's it. We installed the Custom firmware on the same device where ESPHome was installed. After making sure that the Wi-Fi relay works smoothly and is controlled from the mobile app, we can reinstall the whole series of devices. This will be the last firmware update via USB cable. The next updates, thanks to the use of Custom firmware on the SDK, will happen over the air.

The article that follows explains how firmware updates are prepared and how the update is done over the air. Keep an eye out for more!

Links:


All articles about IoT device creation and support

  1. Learn your way around the IoT platform –  create your first device and control it from your smartphone without an MCU
  2. How to create a Wi-Fi switch to control via a mobile app and Telegram bot
  3. 4 stages of using the platform to create a new IoT device
  4. How to create an IoT device on a printed circuit board and take it from prototype to finished product
  5. How to write firmware for an IoT device based on an SDK
  6. How to update the firmware and mobile application of ready-made devices within 5 minutes
  7. Collection and analysis of statistics –  a useful tool for product improvement
Made on
Tilda