Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Internet of Things Projects with ESP32

You're reading from   Internet of Things Projects with ESP32 Build exciting and powerful IoT projects using the all-new Espressif ESP32

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher Packt
ISBN-13 9781789956870
Length 252 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Agus Kurniawan Agus Kurniawan
Author Profile Icon Agus Kurniawan
Agus Kurniawan
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with ESP32 2. Making Visual Data and Animation on an LCD FREE CHAPTER 3. Building a Simple Game with an Embedded ESP32 Board 4. Building a Sensor Monitoring Logger 5. Controlling IoT Devices over the Internet 6. Building an IoT Weather Station 7. Making Your Own Wi-Fi Wardriving 8. Building Your Own Wi-Fi Cam 9. Making IoT Devices Interact with Mobile Applications 10. Building IoT Monitoring with Cloud Technology 11. Other Books You May Enjoy

Demo 1 – building your first ESP32 program

In this section, we will write a simple program for the ESP32 board. We need three LEDs including cable jumpers for this. We will turn on one LED from LED 1 to LED 3. For implementation, I use the ESP-WROVER-KIT board.

Let's begin.

Wiring

We connect three LEDs on the ESP32 board GPIO. We then do the following wiring:

  • LED 1 is connected to IO12
  • LED 1 is connected to IO14
  • LED 1 is connected to IO26
  • All LED GND pins are connected to the ESP32 board GND

You can see a wiring scheme in the following diagram:

Wiring on LED demo

Next, we create a project.

Creating a project

In general, there is no project template for the ESP32 program with SDK. However, we can create a project with the project structure as shown in this screenshot:

A project structure

Each project has the following files:

  • Makefile on your project root
  • main folder
  • Program file (*.c)
  • component.mk file inside the main folder

In this demo, we create a project by creating a folder called blinking. Then, we create a Makefile file. We also create a main folder. Inside the main folder, we create the blinking.c and component.mk files.

We will write code for those files in the next section.

Writing the program

Now, we write scripts and codes on the Makefile, component.mk, and blinking.c files:

  1. In the Makefile file, we declare our project name. This should be the same name as the project folder. The following are Makefile scripts:
   PROJECT_NAME := blinking

include $(IDF_PATH)/make/project.mk

  1. component.mk is required for compiling purposes. You should create a component file with this exact name. The content of the component.mk file is empty:
    #
# "main" pseudo-component makefile.
#
# (Uses default behavior of compiling all source files in
directory, adding 'include' to include path.)
  1. Now, we write code for our main program, blinking.c. Firstly, we declare our required library headers as follows:
    #include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
  1. We define our three LEDs on ESP32 GPIO. We use IO12, IO14, and IO26 pins from ESP32 GPIO:
    #define LED1 12
#define LED2 14
#define LED3 26
  1. A main entry of the program is app_main(). For this, we create a task and pass a function, called blinking_task:
    void app_main()
{
xTaskCreate(&blinking_task, "blinking_task",
configMINIMAL_STACK_SIZE, NULL, 5, NULL);
}
  1. The blinking_task() function performs GPIO initialization by calling gpio_pad_select_gpio(). Then, we set the GPIO pin as output using the gpio_set_direction() function. In the main loop, we turn on the LEDs one by one. We call the turn_on_led() function to perform this task:
void blinking_task(void *pvParameter)
{
// set gpio and its direction
gpio_pad_select_gpio(LED1);
gpio_set_direction(LED1, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(LED2);
gpio_set_direction(LED2, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(LED3);
gpio_set_direction(LED3, GPIO_MODE_OUTPUT);

int current_led = 1;
while(1) {
turn_on_led(current_led);
vTaskDelay(1000 / portTICK_PERIOD_MS);

current_led++;
if(current_led>3)
current_led = 1;
}
}
  1. To turn on/off LEDs, we call gpio_set_level() with 1 or 0 parameters. If we pass 1 on gpio_set_level(), it means we set a power voltage on that GPIO:
void turn_on_led(int led)
{
// turn off all leds
gpio_set_level(LED1, 0);
gpio_set_level(LED2, 0);
gpio_set_level(LED3, 0);

switch(led)
{
case 1:
gpio_set_level(LED1, 1);
break;
case 2:
gpio_set_level(LED2, 1);
break;
case 3:
gpio_set_level(LED3, 1);
break;
}
}
  1. Now, save all programs.

Next, we configure the project before flashing on the ESP32 board.

Configuring the project

Now, we should configure our project using menuconfig. This tool is a part of the ESP32 toolchain that you have configured previously on your platform.

Open Terminal and navigate to your project directory. Then, you can type this command:

$ make menuconfig

You should get the dialog shown in the following screenshot:

Espressif project config

We configure our ESP32 serial port and then select the Serial flasher config menu. From here, fill your serial port of the ESP32 board. You can see my ESP32 serial port from the ESP-WROVER-KIT board here:

Setting a serial port for the ESP32 board

If complete, click the Save button. The menuconfig program will save your project configuration. The program output can be seen in the following screenshot. You should see that this tool generates the sdkconfig file into your current project directory:

A result of configuring a project

Now, your program is ready for compiling and flashing.

Compiling and flashing

After we configured our project, we can flash our program into the ESP32 board. We can type this command on the current project directory from the following terminal:

$ make flash

This command performs compiling and flashing. If the make command is not found, you should install make for your platform.

If you have configured a serial port of the ESP32 board, the program will be flashed. Otherwise, you will get a timeout because the tool cannot find the ESP32 serial port. You can see my program output on the flashing process in the following screenshot:

Flashing a program into the ESP32 board

If this succeeds, you should see lighting on LED 1, LED2, and LED 3.

Next, we develop the ESP32 program using Arduino Sketch.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime