5.4.22 Project 13.1: Mobile Phone APP test

1. Download APP

Android APP:

Method One: The Android apk installation package is available in our resource pack, as shown below:

image64

You can transfer Android apk to your mobile phone via a USB cable, and then install it.

Method Two: Download from Google play:

Please search for keyes IoT home on Google play to download it.

image65

APP Interface

image66

Download iOS APP

Please search for keyes IoT home on APP Store to download it.

image67

APP Interface

image68

2. Description

We will use APP to control the smart home LED lights and fan switches.

3. Test Code

⚠️ \ ATTENTION:\ After opening the code file, you need to modify the WiFi name and passwords that the ESP32 development board needs to connect to. Replace ChinaNet-2.4G-0DF0 and ChinaNet@233 with your own WiFi name and password respectively. You must do this before uploading the code; otherwise, the ESP32 board will not be able to connect to the network.

const char* ssid = "ChinaNet-2.4G-0DF0";  // Enter your own WiFi name
const char* pwd = "ChinaNet@233"; // Enter your own WiFi passwords

⚠️ NOTE: Please ensure that the WiFi name and passwords in the code are the same as the network connected to your computer, mobile phone/tablet, ESP32 development board and router. They must be within the same local area network (WiFi).

⚠️ NOTE: The WiFi must be on a 2.4Ghz frequency; otherwise, the ESP32 cannot connect to WiFi.

#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif

#include <LiquidCrystal_I2C.h>

#define fanPin1 19 //IN+ pin
#define fanPin2 18 //IN- pin
#define led_y 12  //Define the yellow led pin as 12

const char* ssid = "ChinaNet-2.4G-0DF0";
const char* pwd = "ChinaNet@233";

#include <Wire.h>
//Initialize the LCD address, columns and rows
LiquidCrystal_I2C lcd(0x27, 16, 2);

WiFiServer server(80);  //Initialize the WiFi service

//Define the variable as the detected value
String request;

unsigned long prevTask = 0;

void setup() {
  Serial.begin(9600);
  //Connect to wifi
  WiFi.begin(ssid, pwd);
  //Determine whether it is connected
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  delay(1000);
  //The serial monitor will display the name and IP address of the wireless network
  Serial.println("Connected to WiFi");
  Serial.print("WiFi NAME:");
  Serial.println(ssid);
  Serial.print("IP:");
  Serial.println(WiFi.localIP());

  //Initialize the LCD
  lcd.init();
  //Turn on the LCD backlight
  lcd.backlight();
  //lcd.noBacklight();
  lcd.clear();
  //Set the position of the cursor
  lcd.setCursor(0, 0);
  //LCD printing
  lcd.print("IP:");
  //Set the position of the cursor
  lcd.setCursor(0, 1);
  //LCD printing
  lcd.print(WiFi.localIP());

  //Set pin modes
  pinMode(led_y, OUTPUT);
  pinMode(fanPin1, OUTPUT);
  pinMode(fanPin2, OUTPUT);
  //Start the service
  server.begin();
}

void loop() {
  //Check whether the client has been connected to the network server
  //When the client establishes a connection with the server, the "server.available()" function returns a WiFiClient object for client-side communication.
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New client connected");
    while (client.connected()) {
      //Determine whether the server sends data
      if (client.available()) {
        request = client.readStringUntil('s');
        Serial.print("Received message: ");
        Serial.println(request);
      }

      //LED
      if (request == "a") {
        digitalWrite(led_y, HIGH);
      } else if (request == "A") {
        digitalWrite(led_y, LOW);
      }

      //fan
      if (request == "f") {
        digitalWrite(fanPin1, LOW); //pwm = 0
        analogWrite(fanPin2, 100); //LEDC channel 5 is bound to the specified left motor output PWM value as 100.
      } else if (request == "F") {
        digitalWrite(fanPin1, LOW); //pwm = 0
        analogWrite(fanPin2, 0); //LEDC channel 5 is bound to the specified left motor output PWM value as 0.
      }

      request = "";
    }
    Serial.println("Client disconnected");
  }
}

4. Test Result

⚠️ Note: The mobile phone or tablet must be connected to the ESP32 development board via the same WiFi. Otherwise, it will not be able to access the control page. Also, when the ESP32 development board uses the WiFi function, it consumes a lot of power. An external DC power supply is required to meet its power demand for operation. If the power demand is not met, the ESP32 board will keep resetting, resulting in the code not running normally.

  1. Open the APP and select WiFi

image69

  1. APP controls LED and the fan

The mobile phone and the smart home must share the same WiFi, or the smart home connects to the hotspot of the mobile phone.

APP input IP address (LCD1602 displays the assigned IP address), then click connect, the connection is successful if ESP32 IP: 192.168.xx.xx is displayed.

Next, you can click the LED, then the smart home LED will be turned on. Click the fan button and the fan will be turned on, as shown below:

image70