Internet of Things Projects with ESP32
eBook - ePub

Internet of Things Projects with ESP32

Build exciting and powerful IoT projects using the all-new Espressif ESP32

  1. 252 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Internet of Things Projects with ESP32

Build exciting and powerful IoT projects using the all-new Espressif ESP32

Book details
Book preview
Table of contents
Citations

About This Book

Create and program Internet of Things projects using the Espressif ESP32.

Key Features

  • Getting to know the all new powerful EPS32 boards and build interesting Internet of Things projects
  • Configure your ESP32 to the cloud technologies and explore the networkable modules that will be utilised in your IoT projects
  • A step-by-step guide that teaches you the basic to advanced IoT concepts with ESP32

Book Description

ESP32 is a low-cost MCU with integrated Wi-Fi and BLE. Various modules and development boards-based on ESP32 are available for building IoT applications easily. Wi-Fi and BLE are a common network stack in the Internet of Things application. These network modules can leverage your business and projects needs for cost-effective benefits.

This book will serve as a fundamental guide for developing an ESP32 program. We will start with GPIO programming involving some sensor devices. Then we will study ESP32 development by building a number of IoT projects, such as weather stations, sensor loggers, smart homes, Wi-Fi cams and Wi-Fi wardriving. Lastly, we will enable ESP32 boards to execute interactions with mobile applications and cloud servers such as AWS.

By the end of this book, you will be up and running with various IoT project-based ESP32 chip.

What you will learn

  • Understand how to build a sensor monitoring logger
  • Create a weather station to sense temperature and humidity using ESP32
  • Build your own W-iFi wardriving with ESP32. Use BLE to make interactions between ESP32 and Android
  • Understand how to create connections to interact between ESP32 and mobile applications
  • Learn how to interact between ESP32 boards and cloud servers
  • Build an IoT Application-based ESP32 board

Who this book is for

This book is for those who want to build a powerful and inexpensive IoT projects using the ESP32.Also for those who are new to IoT, or those who already have experience with other platforms such as Arduino, ESP8266, and Raspberry Pi.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Internet of Things Projects with ESP32 by Agus Kurniawan in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Networking. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789953121
Edition
1

Controlling IoT Devices over the Internet

Internet of Things (IoT) is one of the most popular topics in the technological industry. IoT can be applied in various scenarios, such as monitoring and automation. In this chapter, we will learn how to implement IoT on an ESP32 board through a Wi-Fi network. Here, we will begin to connect to an existing Wi-Fi and then make our own smart home application over Wi-Fi.
We will cover the following topics in this book:
  • Connecting to an internet network via ESP32
  • Accessing data from a web server
  • Building a web server inside ESP32
  • Making a smart home application

Technical requirements

Before we begin, make sure you have the following things ready:
  • A computer with an OS installed, such as Windows, Linux, or macOS.
  • An ESP32 development board. We recommend the ESP-WROVER-KIT v4 board from Espressif.
  • A Wi-Fi network with internet access capability.

Introducing ESP32 Wi-Fi development

Wi-Fi, or a wireless network, is a communication model that makes it possible to interact with another system. With these, we can perform common network tasks over network protocols such as TCP/IP, UDP/IP, HTTP, or SMTP/POP3. Since the ESP32 chip has built-in Wi-Fi and a Bluetooth module, we connect our ESP32 board to an existing network.
To work with Wi-Fi on ESP32, we need the esp_wifi.h header file to be included in our project:
#include "esp_wifi.h"
Wi-Fi programming in ESP32 uses an event-based model. We call the Wi-Fi API from the board driver in order to access the Wi-Fi module on the ESP32 board. The ESP32 Wi-Fi API supports Wi-Fi security such as WPA, WPA2, and WEP. A list of Wi-Fi API functions can be found at https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/network/esp_wifi.html.
In this chapter, we will work with a Wi-Fi network stack in the ESP32 board. For demo purposes, I have used the ESP-WROVER-KIT v4 board.

Scanning Wi-Fi hotspot

The first demo is used to perform Wi-Fi scanning. We will scan for all existing Wi-Fi networks in the surrounding area in which we are present. First, we will create the ESP32 project, called wifiscan. Our main program is wifiscan.c.
Next, we load all libraries from header files into our program as follows:
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sys.h"
In the main entry app_main() function, we initialize storage for the Wi-Fi program by calling the esp_wifi_set_storage() function. We also need a function to listen to incoming events from the Wi-Fi API. For instance, we create a function named event_handler() and pass it into the esp_event_loop_init() function.
Next, we run the Wi-Fi service on ESP32 by calling the esp_wifi_start() function:
 esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );

tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
After the Wi-Fi service has started, we will perform Wi-Fi scanning using the esp_wifi_scan_start() function. To do this, we should pass the wifi_scan_config_t parameter into esp_wifi_scan_start(). For Wi-Fi scanning, we set NULL for the ssid and bssid parameters.
We perform looping in Wi-Fi scanning. After the Wi-Fi scanning process is done, we again call esp_wifi_scan_start():
 wifi_scan_config_t scanConf = {
.ssid = NULL,
.bssid = NULL,
.channel = 0,
.show_hidden = true
};

while(true){
ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf, true));
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
Now, we will implement the event_handler() function. Technically, we receive all events from the Wi-Fi service. You can read about Wi-Fi events in the ESP32 documentation at https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/wifi.html.
In this scenario, we will wait for the SYSTEM_EVENT_SCAN_DONE event. This event is raised after ESP32 performs the Wi-Fi scanning. To get the result of Wi-Fi scanning, we can call the esp_wifi_scan_get_ap_num() function.
Then, we will loop our program to retrieve Wi-Fi hotspot information by calling the esp_wifi_scan_get_ap_records() function. We will then print the Wi-Fi information into the Terminal as follows:
esp_err_t event_handler(void *ctx, system_event_t *event)
{
if (event->event_id == SYSTEM_EVENT_SCAN_DONE) {
uint16_t apCount = 0;
esp_wifi_scan_get_ap_num(&apCount);
printf("Wi-Fi found: %d\n",event->event_info.scan_done.number);
if (apCount == 0) {
return ESP_OK;
}
wifi_ap_record_t *wifi = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount);
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, wifi));
....
}
After we have obtained a list of wifi_ap_record_t items, we will set the authentication name based on the wifi_ap_record_t.authmode type, as follows:
 for (int i=0; i<apCount; i++) {
char *authmode;
switch(wifi[i].authmode) {
case WIFI_AUTH_OPEN:
authmode = "NO AUTH";
break;
case WIFI_AUTH_WEP:
authmode = "WEP";
break;
case WIFI_AUTH_WPA_PSK:
authmode = "WPA PSK";
break;
case WIFI_AUTH_WPA2_PSK:
authmode = "WPA2 PSK";
break;
case WIFI_AUTH_WPA_WPA2_PSK:
authm...

Table of contents

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