Microcontroller projects

Programming ESP's and the ESPBacker library

last updated: 27/07/19


Why this? There are cool books and tutorials on the net with code snippets to program ESP's. You can find e.g. a really useful guide from Pieter P on github.io.

I work often with both microcontroller from Espressif, the ESP8266 and the ESP32. And I'm a forgetful man, an often search quite a long time to find back pieces of code already written and used in my projects. So here I wrote an Arduino library to hold all those pieces of code and named it ESPBacker. The code is intended to work on ESP8266 and ESP32.

Boards used

I often use the LOLIN (WEMOS) D1 mini pro. The MHEtLive ESP32MiniKit is small and nearly (RxD and TxD are interchanged!!) pin compatible with the LOLIN board and comes handy if we need a more powerful controller.

flash memory

In the middle of the picture we see the prior version of the LOLIN board named Wemos D1 mini pro.

Other ESP8266 and ESP32 boards should also work with the following software.

Naming conventions

I was often confused and did not know what convention for naming identifiers (names given to a program element) to use in different languages, and so was sometimes not consistent in my own programs.

In last years I often use Python with a quite clear convention (PEP8) and as this convention does not conflict with C++ (Arduino) I will try to stick to that convention beginning today ;). Here my rules:

Logging and debugging

Let's start with the most important chapter :).

Debug with BUILTIN_LED

Most boards have one or more built in LEDs. When no serial interface or UDP connection is available, it's good to use the LED's to pass informations. In the library we have little helper methods to use the builtin LED.

For many boards LED_BUILTIN is already defined. If this is not the case or if you want to use another LED use the overloaded methode set_led_log(bool flag, byte led_pin) to change the pin number. The LED's on ESP8266 and ESP32 often use negative logic! If the logic on your board is negative use set_LED_pos_logic(false).

Here the example code using the library:

    /****** ESP_example_debug_with_builtin_LED.ino ****** www.weigu.lu ********/

    #include <ESPBacker.h>

    ESPBacker B;                         // create an ESPBacker object

    /****** SETUP *************************************************************/
    void setup() {
      //B.set_led_log(true,5);           // give alt. pin if LED_BUILTIN not defined
      B.set_led_pos_logic(false);        // default = true, positive logic
    /****** LOOP **************************************************************/

    void loop() {
      B.blink_led_x_times(3);            // default blink with 100 ms (f=5Hz)
      B.blink_led_x_times(3,500);        // blink with 500ms delay (f=1s)

There exists another LED function not connected to logging to initialise an LED:

Debug/Log with Serial

Logging per serial monitor helps a lot when programming and debugging. Unfortunately TxD0 and/or RxD0 from Serial0 (Serial) are also needed for programming most ESP boards or are needed for communication to a sensor or a device. ESP8266 has fortunately a second serial interface (look here for LOLIN board). ESP32 has even three serial interfaces. The second and the third serial interface are accessed through Serial1 ans Serial2.

With a serial to USB adapter cable (3.3V!! for ESP boards) and a terminal program like Cutecom we can log over Serial1 or Serial2.

The library contains the following methods for serial logging:

To debug or log messages we use the log() function. If we need a line feed we add '\n' to our message.

    /****** esp_example_log_serial.ino ****** www.weigu.lu ********************/

    #include <ESPBacker.h>

    ESPBacker B;

    /****** SETUP *************************************************************/

    void setup() {
      B.set_led_log(true);               // enable LED and Serial logging
      // overloaded method to choose Serial1 (1) or Serial2 (2, only ESP32)
    /****** LOOP **************************************************************/

    void loop() {
      B.blink_led_x_times(3);            // default blink with 100 ms

Debug/Log with UDP

Serial is cool over the Arduino serial monitor. But if this is not possible, it gets cumbersome because additional hardware is needed. The ESP's have Wi-Fi on board and allow us to use UDP to send messages to a computer. If we have a linux computer (e.g. raspberry pi) the simple netcat program allows us to listen to our log information. Open a terminal and type:

    nc -kulw 0 6666

I omitted some functions from above. For the whole program look at the end of the page (Downloads).

Wlan functions

Connect ESP to a router (Wifi station mode STA)

Using one network (SSID)