ESP32 – Check temperature with DS18B20

One of my goals when playing with these chips is to enhance elements of smart home. One of features is measurement of home parameters including temperature. Earlier I have integrated it with Fibaro sensor, now I’ll check it with ESP32 and DS18B20.

Application setup

First step is to get empty project as described here and rename it to temperature for example. Update Makefile, set serial port and remove main.c file.

Now we need to add support for DS18B20. On GitHub there is already component which handles it but it will require minor update to compile seamlessly. Each project may contain several components organized in components subdirectory. Components are simply pieces of code which are compiled into static library and then linked into our application. Some of them are available in ESP-IDF but we can create also ours. For purpose of handling DS18B20 I used component from FeelFreeLinux, there are 2 small changes which must be applied.

cd components
git clone --recursive https://github.com/feelfreelinux/ds18b20.git
cd ds18b20
mkdir include
mv ds18b20.h include

Read More

ESP32 – Hello world reinvented

In previous post I have described Hello World example coming from ESP_IDF environment. Now let’s build Hello World from the scratch.

Template application

First of all let’s grab template application. It is available on GitHub under espressif project.

cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf-template.git

Rename esp-idf-template directory to hello_world2.

mv esp-idf-template hello_world2
cd hello_world2

Edit Makefile and change value of PROJECT_NAME variable to hello_world2.

Main files of the projects are located in main directory, additional components will be located in components by default. So let’s got to main and remove source file main.c. This file contains example how to connect to wireless network and will be interesting in future. Create the easiest existing example hello_world2.c as below. Name of the file does not matter but one of source files must contain void app_main(void) function:

#include <stdio.h>

void app_main(void)
{
  printf("Hello world\n");
}

Remember to run make menuconfig to set serial flasher connection and then flash and monitor. At the end of the results you will see something like:

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:3424
load:0x40078000,len:10388
ho 0 tail 12 room 4
load:0x40080000,len:252
0x40080000: _iram_start at ??:?
entry 0x40080034
0x40080034: _iram_start at ??:?
Hello world

It works. Now as general rule tasks should be created in form of endless loop and this peace of code hasn’t got loop. But task can delete themselves when finished. But where this task from app_main is deleted? When you quickly look into cpu_start.c file from esp-idf/components/esp32 folder you will see that right after app_main ends the task is deleted:

static void main_task(void* args)
{
    // Now that the application is about to start, disable boot watchdogs
    REG_CLR_BIT(TIMG_WDTCONFIG0_REG(0), TIMG_WDT_FLASHBOOT_MOD_EN_S);
    REG_CLR_BIT(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN);
    //Enable allocation in region where the startup stacks were located.
    heap_alloc_enable_nonos_stack_tag();
    app_main();
    vTaskDelete(NULL);
}

/Rook

ESP32 – Hello world!

Environment setup

In my development I use currently MacOS. I have set up environment using  guide from Read the Docs. Jump to this page and check for update, I put quick list which is copy of their list

  1. Instal pip package manager
    sudo easy_install pip
  2. Install pyserial for accessing serial port
    sudo pip install pyserial
  3. Download ESP32 toolchain from Espressif website and extract to esp directory

    cd
    curl -O https://dl.espressif.com/dl/xtensa-esp32-elf-osx-1.22.0-61-gab8375a-5.2.0.tar.gz
    mkdir -p ~/esp
    cd ~/esp
    tar -xzf ~/xtensa-esp32-elf-osx-1.22.0-61-gab8375a-5.2.0.tar.gz
  4. Update your PATH variable either by creating alias or setting updating .profile file
    export PATH=$PATH:$HOME/esp/xtensa-esp32-elf/bin
  5. Get ESP32 API and libraries from Espressif’s ESP-IDF repository.
    cd ~/esp
    git clone --recursive https://github.com/espressif/esp-idf.git
  6. Set IDF_PATH variable so toolchain has access to ESP-IDF
    export IDF_PATH=~/esp/esp-idf
  7. Check device name of serial connection to ESP32. As I am using Mac I run the command below twice – with unplugged and plugged board to identify correct device
    ESP32 connected to Mac
    ls /dev/tty.*
    /dev/tty.usbserial-DN01DXXH

    Note name of the port as it will be needed later.

  8. When plugged check if chip is working using for example screen command
    screen /dev/tty.usbserial-DN01DXXH 115200

    Output should be similiar to

    Example output of serial from ESP32

Getting Hello World

ESP-IDF comes with several example projects, here we start traditionally with Hello World. Let’s copy it to esp directory to test it.

cd ~/esp
cp -r $IDF_PATH/examples/get-started/hello_world .

Jump to the directory and using make command execute configuration utility menuconfig.

cd ~/esp/hello_world
make menuconfig
Main menu of menuconfig

Set serial port to correct one for your machine.

Serial port configuration in menuconfig utility

Now just build, flash and see if it works

make flash
Flashing binaries to serial port /dev/tty.usbserial-DN01DXXH (app at offset 0x10000)...
esptool.py v2.0-beta3
Connecting......
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0220
Compressed 15824 bytes to 9221...
Wrote 15824 bytes (9221 compressed) at 0x00001000 in 0.8 seconds (effective 152.2 kbit/s)...
Hash of data verified.
Compressed 355088 bytes to 167202...
Wrote 355088 bytes (167202 compressed) at 0x00010000 in 14.9 seconds (effective 191.1 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 82...
Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 1548.1 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting...

and if everything went fine

make monitor

I (1143) cpu_start: Starting scheduler on PRO CPU.
Hello world!
I (202) cpu_start: Starting scheduler on APP CPU.
This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 0, 4MB external flash
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...
Restarting in 6 seconds...
Restarting in 5 seconds...
Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...

What next

Definitely important reading is project and components structure. I will try and address it during next exercises.

Furthermore some more reading on memory and partitions and application entry points.

void app_main()
{
    printf("Hello world!\n");
    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);

It is app_main.

Coming back to reading, flashing and hopefully I’ll manage to explain it more and better.

/Rook