Reader small image

You're reading from  Developing IoT Projects with ESP32 - Second Edition

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803237688
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Vedat Ozan Oner
Vedat Ozan Oner
author image
Vedat Ozan Oner

Vedat Ozan Oner is an IoT product developer and software architect, with an excellent blend of technical knowledge and experience. During his career, he has contributed to several IoT projects in different roles, which allowed him to discover all key aspects of developing successful IoT products in highly competitive markets. Vedat has a bachelor's degree in METU/computer engineering and holds several industry-recognized credentials and qualifications, including PMP®, ITIL®, and AWS Certified Developer. Vedat started his limited company, Mevoo Ltd, in London in 2018 to provide consultancy services to his clients as well as develop his own IoT products. He still lives in London with his family.
Read more about Vedat Ozan Oner

Right arrow

Using Wi-Fi Communication for Connectivity

Wi-Fi (of the IEEE 802.11 family of standards) is the most prominent wireless standard in the industry, so Espressif has integrated this technology into all of its products except the ESP32-H2 series SoC, which combines IEEE 802.15.4 and Bluetooth LE wireless connectivity on the same hardware. In this chapter, we will learn how to develop applications in Wi-Fi environments. After connecting to a local Wi-Fi network, the huge world of IoT opens, which allows ESP32 to communicate with servers and other connected devices. ESP-IDF provides all the software support needed to develop Transmission Control Protocol/Internet Protocol (TCP/IP) applications.

This chapter contains practical examples of Wi-Fi connectivity basics as well as popular IoT protocols over TCP/IP, such as Message Queue Telemetry Transport (MQTT) and Representational State Transfer (REST) services. In the Further reading section, you can find more resources to learn about...

Technical requirements

As hardware, we will only use ESP32-C3-DevKitM-1 in the examples of this chapter. The Devkit has a button and LED on it, so it is enough for our purposes to try bi-directional communication for use cases with input/output. The code is located in the GitHub repository here: https://github.com/PacktPublishing/Developing-IoT-Projects-with-ESP32-2nd-edition/tree/main/ch6

There are also several third-party software tools that we need to test the final applications. They are:

  • curl: A command-line tool for communication over TCP/IP-based protocols. Its website for download and documentation is here: https://curl.se/
  • Mosquitto: An MQTT broker from the Eclipse Foundation. It also contains other utility tools to publish/subscribe to MQTT topics. You can download the broker and find the documentation here: https://mosquitto.org/
  • ESP SoftAP Provisioning: A mobile application from Espressif for provisioning. Available for both Android and iOS.
  • ...

Connecting to local Wi-Fi

Nodes in a Wi-Fi network form a star topology, which means a central hub exists and other nodes connect to the central hub to communicate within the Wi-Fi network. They can also talk to the outside world if the hub is connected to an Internet Service Provider (ISP) as a router. On a Wi-Fi network, we see two different modes of operation:

  • Access point (AP) mode
  • Station (STA) mode

We can configure ESP32 in both modes. In STA mode, ESP32 can connect to an access point and join a Wi-Fi network as a node. When it is in AP mode, other Wi-Fi-capable devices, such as mobile phones, can connect to the Wi-Fi network that ESP32 starts as the access point. The following figure shows both cases with two different Wi-Fi networks:

Figure 6.1: ESP32 in STA mode and AP mode

Let’s see an example of connecting ESP32 to a Wi-Fi network. In this project, we will develop a class with two callbacks for Wi-Fi-connected and Wi-Fi-disconnected...

Provisioning ESP32 on a Wi-Fi network

Provisioning is a standard feature in almost every IoT solution. It is usually the first thing to do during the installation of an IoT device, and for IoT devices, the easier the installation, the happier the user. The key issue here is how to share the credentials of a Wi-Fi network in a secure way. Luckily, ESP-IDF supports several methods for Wi-Fi provisioning:

  • Unified provisioning from Espressif: With this provisioning method, we can select BLE or Wi-Fi as the transport layer to share the credentials of the Wi-Fi network to be joined. If BLE is selected, the device to be provisioned runs a GATT server to receive the credentials. When Wi-Fi SoftAP is the transport scheme, the device starts a Soft Access Point (softAP) and HTTP server to receive the credentials. A client application, e.g., a mobile application, connects to the device and shares the credentials.
  • SmartConfig from Texas Instruments: This provisioning method is...

Communicating over MQTT

Message Queue Telemetry Transport (MQTT) is a many-to-many communication protocol with a message broker as the mediator. There are publishers that send messages to the topics on the broker, and there are subscribers that receive messages from the topics that they subscribe to. A node can be a publisher and subscriber at the same time:

Figure 6.6: MQTT communication model

For MQTT, TCP is the underlying transport protocol and TLS can be configured for communication security.

Let’s see how MQTT works with a simple example. The goal in this example is to publish sensor data to an MQTT broker running in the local network and control the sensor features remotely by connecting to the broker with an MQTT client running on another machine. As listed in Technical requirements, the broker is Mosquitto. We begin with installing the broker.

Installing the MQTT broker

The mosquitto installation package comes with an MQTT broker, a publisher...

Running a RESTful server on ESP32

Representational State Transfer (REST) is basically the client-server architecture for the web. It defines how a client and server communicate over a resource that the server exposes. In fact, we know it from the HTTP protocol as it implements the entire World Wide Web (WWW) document exchange services. A RESTful server publishes a REST API and clients consume it. A client sends a message, such as GET, POST, PUT, or DELETE, to the server for a resource, and the server replies to the client with an HTTP status code, such as 200 OK, 201 Created, or 404 Not Found. There exist many RESTful services on the internet; therefore, REST communication occupies an important place in IoT development. Mozilla provides many articles about HTTP here: https://developer.mozilla.org/en-US/docs/Web/HTTP

We can employ ESP32 as either a RESTful server or a client. When it is a server, we run an HTTP server on ESP32, define resources, and provide handlers for the HTTP...

Consuming RESTful services

In this example, our sensor will read its configuration from a RESTful server by connecting it as a client and publishing its state on the same server. As a server, we will run a simple Flask application in a virtual Python environment. Let’s prepare the server in steps:

  1. Copy the ch6/rest_client_ex/server directory from the GitHub repository and switch to this directory. Install the Python requirements in a virtual environment:
    $ python --version
    Python 3.6.8
    $ pyenv virtualenv 3.6.8 apptest
    $ pyenv local apptest
    (apptest) $ pip install -r requirements.txt
    
  2. Set the Flask application:
    (apptest) $ export FLASK_APP=./rest_server.py
    
  3. Start the server. The -h 0.0.0.0 option makes Flask serve on all network interfaces of the machine:
    (apptest) $ flask run -h 0.0.0.0
    * Serving Flask app './rest_server.py' (lazy loading)
    <logs removed>
    * Running on http://10.8.0.2:5000/ (Press CTRL...

Summary

In this chapter, we have seen examples of some popular IoT connectivity protocols. To connect a device to the internet, we need a networking infrastructure in the first place. Wi-Fi is the one for ESP32. We learned how to connect ESP32 to a Wi-Fi network and how to provision it if there is no Wi-Fi network configured. ESP-IDF provides several different ways to do this. After having a Wi-Fi connection, we can communicate with other devices on the network. MQTT is a many-to-many IoT communication protocol with a central broker that connects clients.

We installed Mosquitto as the MQTT broker on a machine in the local network and developed an application on ESP32 to subscribe and publish to the MQTT topics. REST is another common method for IoT communication. We can run a RESTful server on ESP32 or use ESP32 as a client that consumes a RESTful service. We developed applications for both use cases.

The upcoming chapter will explain how to develop secure applications on...

Questions

Here are some questions to review what we have learned in this chapter:

  1. Which one of the following statements is FALSE?
    1. ESP32 can start in Wi-Fi STA mode
    2. ESP32 can start in Wi-Fi AP mode
    3. In STA mode, a static IP is required
    4. Wi-Fi events are delivered over an event loop
  2. Which of the following is not a method provided by ESP-IDF?
    1. NFC provisioning
    2. Unified provisioning
    3. Smart Config
    4. Easy Connect
  3. Which protocol makes use of topics to publish and subscribe?
    1. HTTP
    2. WebSocket
    3. CoAP
    4. MQTT
  4. Which of the following is the correct HTTP method to request a resource from a RESTful server?
    1. PUT
    2. DELETE
    3. POST
    4. GET
  5. Which HTTP status code can we expect from a RESTful service when we try to create a resource by sending a POST request?
    1. 201...

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Developing IoT Projects with ESP32 - Second Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781803237688
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.
undefined
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 €14.99/month. Cancel anytime

Author (1)

author image
Vedat Ozan Oner

Vedat Ozan Oner is an IoT product developer and software architect, with an excellent blend of technical knowledge and experience. During his career, he has contributed to several IoT projects in different roles, which allowed him to discover all key aspects of developing successful IoT products in highly competitive markets. Vedat has a bachelor's degree in METU/computer engineering and holds several industry-recognized credentials and qualifications, including PMP®, ITIL®, and AWS Certified Developer. Vedat started his limited company, Mevoo Ltd, in London in 2018 to provide consultancy services to his clients as well as develop his own IoT products. He still lives in London with his family.
Read more about Vedat Ozan Oner