Controlling ESP32 from a mobile application

In this section, we will build a project to enable an ESP32 board to work with mobile devices. We can create Wi-Fi services on an ESP32 board to allow mobile devices to perform tasks on ESP32, such as turning on a lamp and turning off a motor. Our scenario is described in Figure 9.1. For a mobile device platform, we use the Android application.

The way in which a smart mobile project works can be seen in the following diagram:

Figure 9.1: A general design for a smart mobile project

We can control three lamps with an ESP32 board through relay modules as shown in the preceding diagram. For a simple demonstration, we only use three LEDs for lamp simulation. We will expose three HTTP services: /ping, /state, and /lamp:

  • The /ping HTTP request is used to perform an echo test.
  • The /state HTTP request is used to obtain all the LED states that could be on or off.
  • The /lamp HTTP request is used to control the LED-on or off state based on the input parameter provided by the user.

All /ping and /state request types are HTTP GET. We don't need to send parameters to perform these requests. However, a /lamp HTTP request is implemented as HTTP POST. We should specify an input parameter on the request body; we can define the following input parameters:

  • Input 1 is to turn on LED 1
  • Input 2 is to turn off LED 1
  • Input 3 is to turn on LED 2
  • Input 4 is to turn off LED 2
  • Input 5 is to turn on LED 3
  • Input 6 is to turn off LED 3

When ESP32 receives a /lamp HTTP request with input parameter 1, the ESP32 program will turn on LED 1. The Android application will perform HTTP requests to the ESP32 board with those request types.

Our program will run the Wi-Fi AP by naming SMART-MOBILE on the ESP32 board. Users who want to control the lamps should join the Wi-Fi SSID. For testing, I have used the ESP-WROVER-KIT v4 board as the ESP32 board sample.

Next, we will demonstrate hardware wiring.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.104.29