ESP32 (37) – https OTA

In the previous post of this tutorial, I explained how it is possible to update your board Over-The-Air thanks to a feature of the Freshen IoT dashboard.

Today I’ll show you how to update the firmware running on an esp32 chip using only components included in the esp-idf framework, without the need of any external tools or platforms.

OTA API

The esp-idf framework offers a set of native functions to implement, in your program, the ability to be updated over the air.

Those functions are grouped in the app_update component and to use them in your program you have to include the corresponding header file:

#include "esp_ota_ops.h"

Altough the use of the native functions is not very difficult (on Github you can find an example program), Espressif developers have added a component to the framework that makes it even easier the over the air update if the new firmware is located on a web site.

The component is named esp_https_ota.

esp_https_ota

The esp_https_ota component uses the OTA API to update the firmware of your board, downloading the binary file that contains the new firmware from a web site. As the name suggests, the only requirement (for security reason) is that the web site supports the secure version of the protocol (HTTPS).

The component is able to automatically identify an OTA partition in the flash memory that is not in use and to save the new firmware in that partition. It then configures the chip to boot from that partition:

https_ota_001

The use is very simple. First create an esp_http_client_config_t struct to configure the URL of the file with the new firmware and the SSL certificate of the server (or the certificate of the CA that signed it):

esp_http_client_config_t ota_client_config = {
  .url = "https://mysite.com/newfirmware.bin",
  .cert_pem = server_cert_pem_start,
};

You have to provide the certificate in PEM format. To store the certificate in your program, you can leverage the embedding binary files functionality of the firmware, as I already explained in a previous tutorial.

Then you only have to call the function:

esp_err_t ret = esp_https_ota(&ota_client_config);

to start the update process. If – when the process is complete – the ret variable contains a positive result (ESP_OK), you can reboot the chip to run the new firmware:

esp_restart();

A real application would probably need to periodically check if a new firmware is available and, only in that case, to start the update process. How can it be done?

In the program I wrote for this post and that is explained in the video below, I’m going to show a way widely used also in commercial products… enjoy the show ;)

as usual, the source code of the program is available in my Github repository

Tutorial: Git with Eclipse

egit-with-eclipse

A detailed tutorial on Eclipse with the EGit plugin by Erich Styger:

There are things which are game changer in the world of software development: one such event was when I started using a VCS (Version Control System): it changed for me how I keep and store my projects and settings. It even changed the way how I deal with non-software related items like documents or other valuable things: I started storing them in to a VCS too.

Via MCU on Eclipse.

Tutorial: Adding the SSD1306 OLED screen to an Arduino logger (without a library)

SSD1306 OLED screen on a DIY Arduino Based Data logger

Edward Mallon writes:

While I loved the Nokia 5110 LCD’s readability in full sun, the pressure sensitivity was a real problem for the underwater units. So I started noodling around with some cheap OLED screens from eBay.
With the exception of the init & XY functions (which are more complicated on the 1306 controller) the rest of the code ported over from the Nokia screen with no changes at all.  My guess at this point is that the shift-out method will work with most of the other cheap OLED screens, provided they don’t exceed the pin current limits implied by my method.

More details on Underwater Arduino Data Loggers blog.

ESP32 AM radio transmitter

am-modulation

A how-to on making a simple AM radio transmitter using the ESP32 microcontroller by Bitluni:

AM Radio transmissions are based on a carrier signal which is modulated by the audio signal. It’s a very basic principle but prone to noise from the environment. Using the ESP32 it is really simple to generate an analog signal using the built-in DACs. With the provided code here just a wire as an antenna has to be connected to the pin 25 of the ESP32. The transmission will end up on the AM frequency ~835kHz.

Via Bitluni’s Lab.

Check out the video after the break.

ESP32 (36) – OTA with Freshen

In one of the first posts of this tutorial, I wrote about the bootloader and about how the flash memory is organized. That article says:

This allows to implement an over-the-air (OTA) application update process: you send the new version of your application to the esp32 chip; the version is stored in a new app partition

There are different ways to implement OTA updates for your application… today I’ll show you how to do it in an easy way, thanks to a cloud service named Freshen.

Freshen

Freshen is an IoT backend, that is a cloud dashboard (published on the Internet) to manage IoT devices.

After having connected your devices to Frashen (I’ll show you later how to do it) you can:

  • display the list of the devices and their status
  • send a command to a device
  • manage the files stored in a device
  • update the firmware over-the-air (OTA)

Freshen is developed by Cesanta, the company that also develops the Mongoose library and the MongooseOS, very used in the embedded world and compatible with the esp32 chip. The service has different fees, including a free one:

freshen-001s

To use Freshen, first you have to register, using your Github or Google account:

freshen-002

Client library

Cesanta offers a client library you can use to connect your IoT project based on the esp32 chip to Freshen. The library consists only in a header file (freshen.h), and is available on the official site.

The library is fully compatible with the esp-idf framework and supports all the functionalities of the dashboard:

freshen-003

It’s also very easy to use. First copy the freshen.h file in the main folder of your project:

freshen-009

Then include the file in your program:

#include "freshen.h"

To let the library communicate with the Freshen platform in the cloud, in your program you have to periodically call the freshen_loop() function:

You can do it in a dedicated task:

void freshen_task(void *pvParameter) {
  while(1) {	
    freshen_loop(FIRMWARE_VERSION, ACCESS_TOKEN);
    vTaskDelay(2000 / portTICK_RATE_MS);
  }
}

This task calls the function every 2 seconds. Create the task in your app_main() with:

// start the freshen update task
xTaskCreate(&freshen_task, "freshen_task", 10000, NULL, 5, NULL);

The freshen_loop() function requires two parameters: the firmware version (it’s a text string at your choice, for example “v1.0″) and the access token, a code that is generated by the platform when you register a device.

Connect to the dashboard and click on Add new device:

freshen-010

A new device will be added (My Device #n). You can click on its name to display the details.

The access token is hidden… Click on click to copy to save it in the clipboard on your computer; you can then paste it in your program:

freshen-011

The device details page also allows to change the name of your device.

If you run the example program (you can download it from my Github repository) you’ll notice that, after a couple of seconds, the status of your device changes to online, to indicate that it’s correctly sending data to the dashboard:

freshen-012

In the details page, you can now list the functions that you can remotely call:

freshen-013

For example, if you call the Sys.GetInfo function you can retrieve the information about firmware version, architecture, compile date…:

freshen-014

OTA

To be able to update the firmware of your device over the air, you have to select an appropriate partition layout. For example you can choose the “OTA” layout that is already included in the framework:

freshen-006

Let’s now modify the firmware version, to be “1.1”:

freshen-015

then compile the program but don’t use the flash command. Note the path of the binary (compiled) file:

freshen-016

Select your device in the dashboard and click on OTA update selected:

freshen-017

 

Choose the .bin file that contains the new version of your firmware.

After a few moments, you’ll probably see the device going offline and return online. If the update process was successful, call the Sys.GetInfo function again and you’ll see the new version:

freshen-018

 

Build a Multisensor Shield for ESP8266

Build-ESP8266-WeMos-D1-Mini-Multisensor-Shield

Rui Santos has a great write-up on building a Multisensor Shield for ESP8266, that is available on GitHub:

In this project you’ll discover how to design and create a Multisensor Shield for the ESP8266 Wemos D1 Mini board. The shield has temperature sensor (DS18B20), a PIR motion sensor, an LDR, and a terminal to connect a relay module. We’ll start by preparing all the hardware and then program it.

See the full post on Random Nerd Tutorials blog.

Tutorial: Using the Arduino’s internal EEprom to store calibration data and LCD screen fonts

24bytesleft

Edward Mallon writes:

This is a follow-up to our post about using Nokia 5110 screens on three unused analog lines with shift-out. That saved me from messing with the hardware SPI bus which we reserve for the SD cards. A secondary benefit is that the code is really lean, on the order of about 250 bytes for the default font after the compile if you already have EEprom.h in the build anyway. The font, however takes up about 500 bytes, and I wanted the smallest possible footprint so that we could add live data output to loggers that are already compile near the memory limits. As it turns out, stuffing those fonts into the internal EEprom was pretty easy to do:
Using the Arduino’s Internal EEprom to Store Calibration Data & LCD Screen Fonts

More details on Underwater Arduino Data Loggers blog.

Via the comments.

How to use I2C LCD with ESP32 on Arduino IDE

LCD-display-featured-image-600

A how-to on using an I2C LCD display with the ESP32 using Arduino IDE from Random Nerd Tutorials:

This tutorial shows how to use the I2C LCD (Liquid Crystal Display) with the ESP32 using Arduino IDE. We’ll show you how to wire the display, install the library and try sample code to write text on the LCD: static text, and scroll long messages. You can also use this guide with the ESP8266.

See the full post at randomnerdtutorials.com.

Dual ultrasonic sensors combine for 2D echolocation

F4UGJR8JIXQHU3D-600

A how-to on making a Dual-sensor ultrasonic echo locator by lingib, project instructables here:

This instructable explains how to pinpoint the location of an object using an Arduino, two ultrasonic sensors, and Heron’s formula for triangles. There are no moving parts.
Heron’s formula allows you to calculate the area of any triangle for which all sides are known. Once you know the area of a triangle, you are then able to calculate the position of a single object (relative to a known baseline) using trigonometry and Pythagoras.
The accuracy is excellent. Large detection areas are possible using commonly available HC-SR04, or HY-SRF05, ultrasonic sensors.
Construction is simple … all you require is a sharp knife, two drills, a soldering iron, and a wood saw.

Via Arduino Blog.

Check out the video after the break.