ESP32 Thermostat Example

In this guide we will take a look at an example application that shows how to use the Nabto Edge platform with an ESP32 MCU. The application we will be looking at is the Thermostat example. You will learn to install Thermostat firmware onto your ESP32 board and then connect to it using an Android or iOS phone.

You may want to familiarize yourself with Nabto Edge by going through the Thermostat example guides using a desktop PC in place of an embedded board first.

Both guides walk you through creating a Nabto Edge device in the Cloud Console. The remainder of this guide will assume you have completed one of the two above guides and as such have some familiarity with the Cloud Console.

What you will need

  1. An ESP32 board. For this guide we’ve used a NodeMCU ESP32 board, but the choice is up to you.
  2. An Android or iOS phone
  3. A wifi network.

Setting up your ESP32

First you should set up your ESP32 development tools as described on the Espressif documentation website. In the rest of this guide, we will assume the following things.

  1. Your ESP32 is connected to a computer
  2. You have a terminal with the idf.py command available.

Setting up the Thermostat example

Go to the edge-esp32 github repository. This repository contains integration code to enable Nabto Edge development on ESP32. It also includes some example programs, including the Thermostat example that we will need. You should recursively clone this repository onto your computer and cd to the Thermostat folder, e.g.

git clone --recurse-submodules https://github.com/nabto/edge-esp32.git edge-esp32
cd edge-esp32/examples/thermostat

To configure the example, run idf.py menuconfig and navigate through to Example Configuration using the menu. You will be greeted with the following screen.

You should change the “Nabto device ID” and “Nabto product ID” fields into your device and product IDs that you can get from the cloud console. You should also type in your Wi-Fi SSID and password to allow your board to connect to Wi-Fi. Thermostat also specifically has Power status LED GPIO number, this is so that the Thermostat example can blink an LED if one is available. Consult your board’s documentation for GPIO numbers. Finally, you will also need to fill in the Nabto raw private key field.

To fill in the raw private key, head over to our edge-key-tool repository. This is a tool that lets you generate a raw private key offline, you can download the latest version from the Github Releases page on the repository. Once you have the tool downloaded, running it will generate a raw private key and an accompanying fingerprint as following.

$ ./edge_key_tool

Generating new key
PEM Private Key:
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIOHgci8/iDXNrSuk0e3Eb1ytHgsO1th9jAH1KjG1QbMHoAoGCCqGSM49
AwEHoUQDQgAEd4zDOZUvAJzDSvILys26pyCEDkPEAFDELREera2gFf6DaXbSQOYk
PMBDZcS2EZDhviYkimMy1WCrZeozy6px+Q==
-----END EC PRIVATE KEY-----

Raw Private Key:
e1e0722f3f8835cdad2ba4d1edc46f5cad1e0b0ed6d87d8c01f52a31b541b307

Fingerprint:
2c41eb1afad4c17d45ec25c774211a7daf429f170435cc6f4b9db81aee1ce308

As you can see you get both a PEM encoded key and a RAW encoded key. For ESP32 you should use the RAW encoding as it is shorter. Copy the string into your raw private key field in the menuconfig. The fingerprint should be entered into the Nabto Cloud Console under your device’s settings. See the Android or iOS guides linked in the first section for reference on how to do this.

Once you have entered the raw private key into your ESP32 config and registered the fingerprint in the Nabto Cloud Console, you can exit menuconfig and run idf.py flash to build and flash Thermostat to your board. Once flashing has completed, you can monitor output with idf.py monitor. When the example is up and running, it should output the following line:

I (44705) device_event_handler: Attached to the basestation

Connecting with a phone

Once the example is up and running, it is very simple to connect and play around with the Thermostat example.

For Android you will want to install the Edge Thermostat app from Google Play

For iOS you will want to install the Thermostat app from the App Store

Note that to make it easier for yourself, your phone should be connected to the same network the device is on. This is not a strict requirement as Nabto Edge supports remote connections from separate networks, but it will enable your phone to automatically discover the device using mDNS, which will simplify pairing significantly.

The app pairing procedure is outlined in detail in the thermostat guides for iOS and Android, respectively.