Keyestudio ESP32 Smart Home Kit Guide
Keyestudio ESP32 Smart Home Kit Guide
com
1. Description.....................................................................3
2. Features......................................................................... 3
3. Parameters.....................................................................4
5. Installation of Structure..................................................5
6. Projects.......................................................................... 5
Project 7: Fan...............................................................30
7. Resources.....................................................................66
1. Description
[Link]
[Link]
2. Features
1. Elegant appearance
6. RFID function
[Link]
[Link]
3. Parameters
installation.
[Link]
[Link]
5. Installation of Structure
6. Projects
below:
[Link]
[Link]
1. Working Principle
2. Parameters
Power 0.1W
3. Control Pin
Yellow LED 12
[Link]
[Link]
1. Description
We can make the LED pin output high level and low level
2. Test Code
#define led_y 12 //Define the yellow led pin to 12
void setup() { //The code inside the setup function runs only once
pinMode(led_y, OUTPUT); //Set pin to output mode
}
void loop() { //The code inside the loop function will always run in
a loop
digitalWrite(led_y, HIGH); //Light up the LED
delay(200); //Delay statement, in ms
digitalWrite(led_y, LOW); //Close the LED
delay(200);
}
3. Test Result
After uploading the code , you can see white and yellow
[Link]
[Link]
1. Description
number of high level and low level in unit time, the more
time the high level occupies, the larger the PWM value,
[Link]
[Link]
2. Test Code
#include <analogWrite.h> //Import PWM output library files
#define led_y 12 //Define LED pins
void setup(){
pinMode(led_y, OUTPUT); //Set pin to output mode
}
void loop(){
for(int i=0; i<255; i++) //The for loop statement increments the
[Link]
[Link]
value of variable i until it exits the loop at 255
{
analogWrite(led_y, i); //PWM output, control LED brightness
delay(3);
}
for(int i=255; i>0; i--) //The for loop statement continues to
decrease the value of variable i until it exits the loop at 0
analogWrite(led_y, i);
delay(3);
}
}
3. Test Result
1. Description
which can control the light on and off pressing the button.
2. Button Principle
[Link]
[Link]
Button 1 16
Button 2 27
1. Description
2. Test Code
#define btn1 16
#define btn2 27
void setup() {
[Link](9600);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
}
void loop() {
boolean btn1_val = digitalRead(btn1);
boolean btn2_val = digitalRead(btn2);
[Link]
[Link]
[Link]("button1 = ");
[Link](btn1_val);
[Link](" ");
[Link]("button2 = ");
[Link](btn2_val);
delay(100);
}
3. Test Result
[Link]
[Link]
1. Description
2. Test Code
void setup() {
[Link](9600);
pinMode(btn1, INPUT);
pinMode(led_y, OUTPUT);
}
void loop() {
boolean btn1_val = digitalRead(btn1);
if(btn1_val == 0) //If the button is pressed
{
delay(10); //Delay 10ms to eliminate button jitter
if(btn1_val == 0) //Make sure the button is pressed again
{
boolean btn_state = 1;
while(btn_state == 1) //Loop indefinitely until the button is released
{
boolean btn_val = digitalRead(btn1);
if(btn_val == 1) //If the button is released
{
btn_count++; //Automatically increments by 1, account the clicked button times
[Link]
[Link]
[Link](btn_count);
btn_state = 0; //The button is released and exits the loop
}
}
}
boolean value = btn_count % 2; //Take the remainder of the value, you will get 0 or 1
if(value == 1)
{
digitalWrite(led_y, HIGH);
}
else{
digitalWrite(led_y, LOW);
}
}
}
3. Test Result
Open the serial monitor and print out the clicked button
times, then click the button once, the LED will be on, click
[Link]
[Link]
1. Description
moving.
2. Control Pin
PIR motion 14
sensor
[Link]
[Link]
1. Test Code
#define pyroelectric 14
void setup() {
[Link](9600);
pinMode(pyroelectric, INPUT);
}
void loop() {
boolean pyroelectric_val = digitalRead(pyroelectric);
[Link]("pyroelectric value = ");
[Link](pyroelectric_val);
delay(200);
}
2. Test Result
[Link]
[Link]
up.
1. Test Code
#define pyroelectric 14
#define led_y 12 //Define the yellow led pin to 12
void setup() {
[Link](9600);
pinMode(pyroelectric, INPUT);
pinMode(led_y, OUTPUT); //Set pin to output mode
}
void loop() {
boolean pyroelectric_val = digitalRead(pyroelectric);
[Link]("pyroelectric value = ");
[Link](pyroelectric_val);
[Link]
[Link]
delay(200);
if(pyroelectric_val == 1)
{
digitalWrite(led_y, HIGH);
}else{
digitalWrite(led_y, LOW);
}
}
2. Test Result
Move your hand in front of the sensor, the LED will turn
1. Description
using it.
2. Component Knowledge
[Link]
[Link]
3. Control Pin
Passive Buzzer 25
1. Test Code
#include <ESP32Tone.h>
#define buzzer_pin 25
void setup() {
pinMode(buzzer_pin, OUTPUT);
birthday();
}
[Link]
[Link]
void loop() {
void birthday()
{
tone(buzzer_pin,294,250,0); //The four parameters are pin,
frequency, delay and channel
tone(buzzer_pin,440,250,0);
tone(buzzer_pin,392,250,0);
tone(buzzer_pin,532,250,0);
tone(buzzer_pin,494,250,0);
tone(buzzer_pin,392,250,0);
tone(buzzer_pin,440,250,0);
tone(buzzer_pin,392,250,0);
tone(buzzer_pin,587,250,0);
tone(buzzer_pin,532,250,0);
tone(buzzer_pin,392,250,0);
tone(buzzer_pin,784,250,0);
tone(buzzer_pin,659,250,0);
tone(buzzer_pin,532,250,0);
tone(buzzer_pin,494,250,0);
tone(buzzer_pin,440,250,0);
tone(buzzer_pin,698,250,0);
tone(buzzer_pin,659,250,0);
tone(buzzer_pin,532,250,0);
tone(buzzer_pin,587,250,0);
tone(buzzer_pin,532,500,0);
noTone(buzzer_pin,0); //Close
}
2. Test Result
[Link]
[Link]
buttons.
1. Test Code
#include <ESP32Tone.h>
#include <musicESP32_home.h>
music Music(25);
#define btn1 16
int btn_count = 0; //Used to count the clicked button times
boolean music_flag = 0;
void setup() {
[Link](9600);
pinMode(btn1, INPUT);
pinMode(25, OUTPUT);
// [Link]();
// [Link]();
// Music.Ode_to_Joy();
// [Link]();
// Music.super_mario();
// Music.star_war_tone();
}
void loop() {
boolean btn1_val = digitalRead(btn1);
if(btn1_val == 0) //If the button is pressed
{
delay(10); //Delay 10ms to eliminate button jitter
if(btn1_val == 0) //Make sure the button is pressed again
[Link]
[Link]
{
boolean btn_state = 1;
while(btn_state == 1) //Loop indefinitely until the button is
released
{
boolean btn_val = digitalRead(btn1);
if(btn_val == 1) //If the button is released
{
music_flag = 1;
btn_count++; //Automatically increments by 1 to count the
number of times the button is clicked
[Link](btn_count);
if(btn_count == 4)
{
btn_count = 1;
}
switch(btn_count)
{
case 1: if(music_flag == 1)
{Music.Ode_to_Joy();music_flag=0;} break;
case 2: if(music_flag == 1)
{[Link]();music_flag=0;} break;
case 3: if(music_flag == 1){[Link]();music_flag=0;}
break;
}
btn_state = 0; //The button is released and exits the loop
}
}
}
}
}
2. Test Result
[Link]
[Link]
1. Description
2. Component Knowledge
position detector.
[Link]
[Link]
is 0° --180 °.
[Link]
[Link]
3. Pin
[Link]
[Link]
1. Test Code
#include <ESP32_Servo.h>
Servo myservo; // create servo object to control a servo
// 16 servo objects can be created on the ESP32
void setup() {
[Link](servoPin); // attaches the servo on pin 18 to the servo object
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
[Link](pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
[Link](pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
2. Test Result
The servo of the door turns with the door, back and forth
[Link]
[Link]
1. Description
2. Component Knowledge
3. Test Code
#include <ESP32_Servo.h>
Servo myservo;
#define servoPin 5
#define waterPin 34
void setup() {
[Link](9600);
pinMode(waterPin, INPUT);
[Link](servoPin);
[Link](176);
delay(200);
}
void loop() {
int water_val = analogRead(waterPin);
[Link]
[Link]
[Link](water_val);
if(water_val > 1500) {
[Link](0);
delay(200);
}
else {
[Link](176);
delay(200);
}
}
4. Test Result
1. Description
which can adjust the color to bring out the lamp effect of
[Link]
[Link]
other scenes.
2. Component Knowledge
how many they are, we can use a pin to control a RGB LED
highly consistent.
[Link]
[Link]
3. Pin
SK6812 26
1. Test Code
[Link]
[Link]
2. Test Result
1. Description
atmosphere lamp.
2. Test Code
shown below:
[Link]
[Link]
3. Test Result
Project 7: Fan
1. Description
2. Component Knowledge
blades. You can use PWM output to control the fan speed.
[Link]
[Link]
3. Control Method
Two pins are required to control the motor of the fan, one
for INA and two for INB. The PWM value range is 0~255.
When the PWM output of the two pins is different, the fan
can rotate.
4. Control Pins
INA 19
INB 18
[Link]
[Link]
1. Test Code
#include <analogWrite.h>
#define fanPin1 19
#define fanPin2 18
void setup() {
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
}
void loop() {
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 180);
delay(3000);
digitalWrite(fanPin1, LOW);
digitalWrite(fanPin2, LOW);
delay(1000);
digitalWrite(fanPin1, HIGH); //pwm = 255
analogWrite(fanPin2, 210);
delay(3000);
digitalWrite(fanPin1, LOW);
digitalWrite(fanPin2, LOW);
delay(1000);
}
2. Test Result
[Link]
[Link]
speeds.
1. Test Code
#include <analogWrite.h>
#define fanPin1 19
#define fanPin2 18
#define btn1 16
int btn_count = 0; //Used to count the clicked button times
#define btn2 27
int btn_count2 = 0;
int speed_val = 130; //Define the speed variables
void setup() {
[Link](9600);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
}
void loop() {
boolean btn1_val = digitalRead(btn1);
if(btn1_val == 0) //If the button is pressed
{
delay(10); //Delay 10ms to eliminate button jitter
if(btn1_val == 0) //Make sure the button is pressed again
{
boolean btn_state = 1;
while(btn_state == 1) //Loop indefinitely until the button is released
{
[Link]
[Link]
boolean btn_val = digitalRead(btn1);
if(btn_val == 1) //If the button is released
btn_count++; //Automatically increments by 1 to count the clicked button times
[Link](btn_count);
btn_state = 0; //The button is released and exits the loop
}
}
}
boolean value = btn_count % 2; //Take the remainder of the value, you will get 0 or 1
while(value == 1)
{
//[Link]("on");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, speed_val);
[Link]
[Link]
boolean btn1_val = digitalRead(btn1);
if(btn1_val == 0) //If the button is pressed
{
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 0);
value = 0; //Exit the loop
}
}
}
}
2. Test Result
1. Description
2. Component Knowledge
[Link]
[Link]
3. Control Pins
SDA SDA
SCL SCL
1. Test Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
void setup(){
[Link]();
[Link]();
}
void loop(){
[Link](0, 0);
[Link]("hello");
[Link](0, 1);
[Link]("keyestudio");
//[Link]();
}
[Link]
[Link]
2. Test Result
The first line of the LCD1602 shows hello and the second
1. Description
2. Component Knowledge
this project .
[Link]
[Link]
3. Test Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
#define gasPin 23
#define buzPin 25
boolean i = 1;
boolean j = 1;
void setup(){
[Link](9600);
[Link]();
[Link]();
pinMode(buzPin, OUTPUT);
pinMode(gasPin, INPUT);
[Link](0, 0);
[Link]("safety");
}
void loop(){
boolean gasVal = digitalRead(gasPin); //Reads the value detected
by the gas sensor
[Link](gasVal);
if(gasVal == 0) //If the hazardous gas is detected , LCD displays
dangerous,the buzzer makes an alarm
{
while(i == 1)
{
[Link]();
[Link]
[Link]
[Link](0, 0);
[Link]("dangerous");
i = 0;
j = 1;
}
digitalWrite(buzPin,HIGH);
delay(1);
digitalWrite(buzPin,LOW);
delay(1);
}
else{
digitalWrite(buzPin,LOW);
while(j == 1)
{
[Link]();
[Link](0, 0);
[Link]("safety");
i = 1;
j = 0;
}
}
}
4. Test Result
"dangerous".
[Link]
[Link]
1. Component Knowledge
2. Control Pin
1. Test Code
//
*******************************************************************************
***
/*
* Filename : xht11
* Description : Read the temperature and humidity values of XHT11.
[Link]
[Link]
* Auther : http//[Link]
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
#include "xht11.h"
xht11 xht(17);
unsigned char dht[4] = {0, 0, 0, 0};//Only the first 32 bits of data are received, not the parity bits
void setup() {
[Link](9600);//Start the serial port monitor and set baud rate to 9600
[Link]();
[Link]();
}
void loop() {
if ([Link](dht)) { //Returns true when checked correctly
[Link]("RH:");
[Link](dht[0]); //The integral part of humidity, DHT [1] is the fractional part
[Link]("% ");
[Link]("Temp:");
[Link](dht[2]); //The integral part of temperature, DHT [3] is the fractional part
[Link]("C");
[Link](0, 0);
[Link]("T = ");
[Link](dht[2]);
[Link](0, 1);
[Link]("H = ");
[Link](dht[0]);
//[Link]();
delay(200);
} else { //Read error
[Link]("sensor error");
}
delay(1000); //It takes 1000ms to wait for the device to read
}
//
*******************************************************************************
***
[Link]
[Link]
2. Test Result
1. Component Knowledge
To read the data in the tag, first put it into the reading
Lenz's law, then the RFID tag will supply power, thereby
[Link]
[Link]
2. Control Pins
SDA SDA
SCL SCL
1. Test Code
//
*********************************************************************
*************
/*
* Filename : RFID
* Description : RFID reader UID
* Auther : http//[Link]
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
#include <ESP32_Servo.h>
Servo myservo;
#include <Wire.h>
#include "MFRC522_I2C.h"
// IIC pins default to GPIO21 and GPIO22 of ESP32
// 0x28 is the i2c address of SDA, if doesn't match , please check
[Link]
[Link]
your address with i2c.
MFRC522 mfrc522(0x28); // create MFRC522.
#define servoPin 13
#define btnPin 16
boolean btnFlag = 0;
void setup() {
[Link](115200); // initialize and PC's serial
communication
[Link]();
[Link]();
[Link](); // initialize I2C
mfrc522.PCD_Init(); // initialize MFRC522
ShowReaderDetails(); // display PCD - MFRC522 read carder
[Link](F("Scan PICC to see UID, type, and data blocks..."));
[Link](servoPin);
pinMode(btnPin, INPUT);
[Link](0, 0);
[Link]("Card");
}
void loop() {
//
if ( ! mfrc522.PICC_IsNewCardPresent() || !
mfrc522.PICC_ReadCardSerial() ) {
delay(50);
password = "";
if(btnFlag == 1)
{
boolean btnVal = digitalRead(btnPin);
if(btnVal == 0) //Swipe the card to open the door and click
button 1 to close the door
{
[Link]("close");
[Link](0, 0);
[Link]
[Link]
[Link]("close");
[Link](0);
btnFlag = 0;
}
}
return;
}
// save UID
[Link](F("Card UID:"));
for (byte i = 0; i < [Link]; i++) {
[Link]([Link][i] < 0x10 ? " 0" : " ");
//[Link]([Link][i], HEX);
[Link]([Link][i]);
password = password + String([Link][i]);
}
if(password == "17121741227") //The card number is correct,
open the door
{
[Link]("open");
[Link](0, 0);
[Link]();
[Link]("open");
[Link](180);
password = "";
btnFlag = 1;
}
else //The card number is wrong,LCD displays error
{
password = "";
[Link](0, 0);
[Link]("error");
}
//[Link](password);
}
[Link]
[Link]
void ShowReaderDetails() {
// attain the MFRC522 software
byte v = mfrc522.PCD_ReadRegister([Link]);
[Link](F("MFRC522 Software Version: 0x"));
[Link](v, HEX);
if (v == 0x91)
[Link](F(" = v1.0"));
else if (v == 0x92)
[Link](F(" = v2.0"));
else
[Link](F(" (unknown)"));
[Link]("");
// when returning to 0x00 or 0xFF, may fail to transmit
communication signals
if ((v == 0x00) || (v == 0xFF)) {
[Link](F("WARNING: Communication failure, is the
MFRC522 properly connected?"));
}
}
//
*********************************************************************
*************
2. Test Result
door will turn and open, and LCD1602 shows "The door is
[Link]
[Link]
[Link]
[Link]
1. Description
release is “-”.
2. Test Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
#include "OneButton.h"
// Setup a new OneButton on pin 16.
OneButton button1(16, true);
// Setup a new OneButton on pin 27.
OneButton button2(27, true);
#include <ESP32_Servo.h>
Servo myservo;
int servoPin = 13;
String password = "";
String correct_p = "-.-"; //The correct password for the password door
[Link]
[Link]
[Link](longPressStop2);
[Link](servoPin);
[Link](0, 0);
[Link]("Enter password");
}
void loop() {
// keep watching the push buttons:
[Link]();
[Link]();
delay(10);
}
// This function will be called once, when the button1 is released after being pressed for a long
time.
void longPressStop1() {
[Link]("-");
password = password + '-';
[Link](0, 1);
[Link](password);
} // longPressStop1
[Link]
[Link]
else
{
[Link]();
[Link](0, 0);
[Link]("error");
delay(2000);
[Link]();
[Link](0, 0);
[Link]("input again");
}
password = "";
} // click2
void longPressStop2() {
//[Link]("Button 2 longPress stop");
[Link](0); //Close the door
[Link]();
[Link](0, 0);
[Link]("close");
} // longPressStop2
3. Test Result
[Link]
[Link]
easily.
1. Description
[Link]
[Link]
2. Test Code
#include <Arduino.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
[Link]
[Link]
const char* password = "ChinaNet@233";
WiFiServer server(80);
void setup() {
[Link](115200);
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]("");
[Link]("Connected to ");
[Link](ssid);
[Link]("IP address: ");
[Link]([Link]());
[Link]();
[Link]("TCP server started");
[Link]("http", "tcp", 80);
}
void loop() {
[Link]("Connected to ");
[Link](ssid);
[Link]("IP address: ");
[Link]([Link]()); //The assigned IP address is printed
on the serial monitor
delay(200);
WiFiClient client = [Link]();
if (!client) {
return;
}
while([Link]() && ![Link]()){
delay(1);
}
String req = [Link]('\r');
int addr_start = [Link](' ');
int addr_end = [Link](' ', addr_start + 1);
[Link]
[Link]
if (addr_start == -1 || addr_end == -1) {
[Link]("Invalid request: ");
[Link](req);
return;
}
req = [Link](addr_start + 1, addr_end);
item=req;
[Link](item);
String s;
if (req == "/") //Browser accesses address can read the
information sent by the [Link](s);
{
IPAddress ip = [Link]();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' +
String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!
DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
s += ipStr;
s += "</html>\r\n\r\n";
[Link]("Sending 200");
[Link](s); //Send the string S, you can read the
information when visiting the address of E smart home using a
browser.
//[Link](s);
[Link]();
}
3. Test Result
[Link]
[Link]
1. Description
2. Test Code
[Link]
[Link]
#include <Arduino.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
#include <analogWrite.h>
#define fanPin1 19
#define fanPin2 18
#define led_y 12 //Define the yellow led pin to 12
void setup() {
[Link](115200);
[Link]();
[Link]();
pinMode(led_y, OUTPUT);
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]("");
[Link]("Connected to ");
[Link](ssid);
[Link]("IP address: ");
[Link]([Link]());
[Link]();
[Link]
[Link]
[Link]("TCP server started");
[Link]("http", "tcp", 80);
[Link](0, 0);
[Link]("ip:");
[Link](0, 1);
[Link]([Link]()); //LCD displays the ip adress
}
void loop() {
WiFiClient client = [Link]();
if (!client) {
return;
}
while([Link]() && ![Link]()){
delay(1);
}
String req = [Link]('\r');
int addr_start = [Link](' ');
int addr_end = [Link](' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
[Link]("Invalid request: ");
[Link](req);
return;
}
req = [Link](addr_start + 1, addr_end);
item=req;
[Link](item);
String s;
if (req == "/") //Browser accesses address can read the
information sent by the [Link](s);
{
IPAddress ip = [Link]();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' +
String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!
DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
s += ipStr;
[Link]
[Link]
s += "</html>\r\n\r\n";
[Link]("Sending 200");
[Link](s); //Send the string S, you can read the
information when visiting the address of E smart home using a
browser.
//[Link](s);
}
if(req == "/led/on") //Browser accesses the ip address/led/on
{
[Link]("turn on the LED");
digitalWrite(led_y, HIGH);
}
if(req == "/led/off") //Browser accesses the ip address/led/off
{
[Link]("turn off the LED");
digitalWrite(led_y, LOW);
}
if(req == "/fan/on") //Browser accesses the ip address/fan/on
{
[Link]("turn on the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 180);
}
if(req == "/fan/off") //Browser accesses the ip address/fan/off
{
[Link]("turn off the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 0);
}
//[Link](s);
[Link]();
}
3. Test Result
[Link]
[Link]
be off.
of
[Link]
[Link]
Download APP
Android APP:
download it.
Icon:
APP Interface
[Link]
[Link]
download it.
1. Description
We will use APP to control the smart home LED lights and
fan switches.
2. Test Code
[Link]
[Link]
#include <Arduino.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
#include <analogWrite.h>
#define fanPin1 19
#define fanPin2 18
#define led_y 12 //Define the yellow led pin to 12
void setup() {
[Link](115200);
[Link]();
[Link]();
pinMode(led_y, OUTPUT);
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]("");
[Link]("Connected to ");
[Link](ssid);
[Link]("IP address: ");
[Link]([Link]());
[Link]();
[Link]
[Link]
[Link]("TCP server started");
[Link]("http", "tcp", 80);
[Link](0, 0);
[Link]("ip:");
[Link](0, 1);
[Link]([Link]()); //LCD displays ip adress
}
void loop() {
WiFiClient client = [Link]();
if (!client) {
return;
}
while([Link]() && ![Link]()){
delay(1);
}
String req = [Link]('\r');
int addr_start = [Link](' ');
int addr_end = [Link](' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
[Link]("Invalid request: ");
[Link](req);
return;
}
req = [Link](addr_start + 1, addr_end);
item=req;
[Link](item);
String s;
if (req == "/") //Browser accesses address can read the
information sent by the [Link](s);
{
IPAddress ip = [Link]();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' +
String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!
DOCTYPE HTML>\r\n<html>ESP32 ip:";
s += ipStr;
[Link]
[Link]
s += "</html>\r\n\r\n";
[Link]("Sending 200");
[Link](s); //Send the string S, then you can read the
information when visiting the address of E smart home using a
browser.
}
if(req == "/led/on") //Browser accesses address ip address/led/on
{
[Link]("turn on the LED");
digitalWrite(led_y, HIGH);
}
if(req == "/led/off") //Browser accesses address ip address/led/off
{
[Link]("turn off the LED");
digitalWrite(led_y, LOW);
}
if(req == "/fan/on") //Browser accesses address ip address/fan/on
{
[Link]("turn on the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 180);
}
if(req == "/fan/off") //Browser accesses address ip address/fan/off
{
[Link]("turn off the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 0);
}
//[Link](s);
[Link]();
}
3. Test Result
[Link]
[Link]
The mobile phone and the smart home must share the
[Link]
[Link]
Next, you can click the LED, then the smart home LED will
be turned on. Click the fan button and the fan will be
1. Description
[Link]
[Link]
WiFi, and the mobile phone used for operation should also
communication is
2. Test Code
3. Test Result
[Link]
[Link]
7. Resources
[Link]
[Link]
[Link]
[Link]