Arduino Tutorials
Arduino Tutorials
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
www.keyestudio.com
www.keyestudio.com
2. Features
1. Elegant appearance
6. RFID function
www.keyestudio.com
www.keyestudio.com
3. Parameters
installation.
www.keyestudio.com
www.keyestudio.com
5. Installation of Structure
6. Projects
below:
www.keyestudio.com
www.keyestudio.com
1. Working Principle
2. Parameters
Power 0.1W
3. Control Pin
Yellow LED 12
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
1. Description
number of high level and low level in unit time, the more
time the high level occupies, the larger the PWM value,
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
Button 1 16
Button 2 27
1. Description
2. Test Code
#define btn1 16
#define btn2 27
void setup() {
Serial.begin(9600);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
}
void loop() {
boolean btn1_val = digitalRead(btn1);
boolean btn2_val = digitalRead(btn2);
www.keyestudio.com
www.keyestudio.com
Serial.print("button1 = ");
Serial.print(btn1_val);
Serial.print(" ");
Serial.print("button2 = ");
Serial.println(btn2_val);
delay(100);
}
3. Test Result
www.keyestudio.com
www.keyestudio.com
1. Description
2. Test Code
void setup() {
Serial.begin(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
www.keyestudio.com
www.keyestudio.com
Serial.println(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
www.keyestudio.com
www.keyestudio.com
1. Description
moving.
2. Control Pin
PIR motion 14
sensor
www.keyestudio.com
www.keyestudio.com
1. Test Code
#define pyroelectric 14
void setup() {
Serial.begin(9600);
pinMode(pyroelectric, INPUT);
}
void loop() {
boolean pyroelectric_val = digitalRead(pyroelectric);
Serial.print("pyroelectric value = ");
Serial.println(pyroelectric_val);
delay(200);
}
2. Test Result
www.keyestudio.com
www.keyestudio.com
up.
1. Test Code
#define pyroelectric 14
#define led_y 12 //Define the yellow led pin to 12
void setup() {
Serial.begin(9600);
pinMode(pyroelectric, INPUT);
pinMode(led_y, OUTPUT); //Set pin to output mode
}
void loop() {
boolean pyroelectric_val = digitalRead(pyroelectric);
Serial.print("pyroelectric value = ");
Serial.println(pyroelectric_val);
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
3. Control Pin
Passive Buzzer 25
1. Test Code
#include <ESP32Tone.h>
#define buzzer_pin 25
void setup() {
pinMode(buzzer_pin, OUTPUT);
birthday();
}
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
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() {
Serial.begin(9600);
pinMode(btn1, INPUT);
pinMode(25, OUTPUT);
// Music.tetris();
// Music.birthday();
// Music.Ode_to_Joy();
// Music.christmas();
// 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
www.keyestudio.com
www.keyestudio.com
{
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
Serial.println(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)
{Music.christmas();music_flag=0;} break;
case 3: if(music_flag == 1){Music.tetris();music_flag=0;}
break;
}
btn_state = 0; //The button is released and exits the loop
}
}
}
}
}
2. Test Result
www.keyestudio.com
www.keyestudio.com
1. Description
2. Component Knowledge
position detector.
www.keyestudio.com
www.keyestudio.com
is 0° --180 °.
www.keyestudio.com
www.keyestudio.com
3. Pin
www.keyestudio.com
www.keyestudio.com
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() {
myservo.attach(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
myservo.write(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
myservo.write(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
www.keyestudio.com
www.keyestudio.com
1. Description
2. Component Knowledge
3. Test Code
#include <ESP32_Servo.h>
Servo myservo;
#define servoPin 5
#define waterPin 34
void setup() {
Serial.begin(9600);
pinMode(waterPin, INPUT);
myservo.attach(servoPin);
myservo.write(176);
delay(200);
}
void loop() {
int water_val = analogRead(waterPin);
www.keyestudio.com
www.keyestudio.com
Serial.println(water_val);
if(water_val > 1500) {
myservo.write(0);
delay(200);
}
else {
myservo.write(176);
delay(200);
}
}
4. Test Result
1. Description
which can adjust the color to bring out the lamp effect of
www.keyestudio.com
www.keyestudio.com
other scenes.
2. Component Knowledge
how many they are, we can use a pin to control a RGB LED
highly consistent.
www.keyestudio.com
www.keyestudio.com
3. Pin
SK6812 26
1. Test Code
www.keyestudio.com
www.keyestudio.com
2. Test Result
1. Description
atmosphere lamp.
2. Test Code
shown below:
www.keyestudio.com
www.keyestudio.com
3. Test Result
Project 7: Fan
1. Description
2. Component Knowledge
blades. You can use PWM output to control the fan speed.
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
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() {
Serial.begin(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
{
www.keyestudio.com
www.keyestudio.com
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
Serial.println(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)
{
//Serial.println("on");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, speed_val);
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
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(){
mylcd.init();
mylcd.backlight();
}
void loop(){
mylcd.setCursor(0, 0);
mylcd.print("hello");
mylcd.setCursor(0, 1);
mylcd.print("keyestudio");
//mylcd.clear();
}
www.keyestudio.com
www.keyestudio.com
2. Test Result
The first line of the LCD1602 shows hello and the second
1. Description
2. Component Knowledge
this project .
www.keyestudio.com
www.keyestudio.com
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(){
Serial.begin(9600);
mylcd.init();
mylcd.backlight();
pinMode(buzPin, OUTPUT);
pinMode(gasPin, INPUT);
mylcd.setCursor(0, 0);
mylcd.print("safety");
}
void loop(){
boolean gasVal = digitalRead(gasPin); //Reads the value detected
by the gas sensor
Serial.println(gasVal);
if(gasVal == 0) //If the hazardous gas is detected , LCD displays
dangerous,the buzzer makes an alarm
{
while(i == 1)
{
mylcd.clear();
www.keyestudio.com
www.keyestudio.com
mylcd.setCursor(0, 0);
mylcd.print("dangerous");
i = 0;
j = 1;
}
digitalWrite(buzPin,HIGH);
delay(1);
digitalWrite(buzPin,LOW);
delay(1);
}
else{
digitalWrite(buzPin,LOW);
while(j == 1)
{
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("safety");
i = 1;
j = 0;
}
}
}
4. Test Result
"dangerous".
www.keyestudio.com
www.keyestudio.com
1. Component Knowledge
2. Control Pin
1. Test Code
//
*******************************************************************************
***
/*
* Filename : xht11
* Description : Read the temperature and humidity values of XHT11.
www.keyestudio.com
www.keyestudio.com
* Auther : http//www.keyestudio.coml
*/
#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() {
Serial.begin(9600);//Start the serial port monitor and set baud rate to 9600
mylcd.init();
mylcd.backlight();
}
void loop() {
if (xht.receive(dht)) { //Returns true when checked correctly
Serial.print("RH:");
Serial.print(dht[0]); //The integral part of humidity, DHT [1] is the fractional part
Serial.print("% ");
Serial.print("Temp:");
Serial.print(dht[2]); //The integral part of temperature, DHT [3] is the fractional part
Serial.println("C");
mylcd.setCursor(0, 0);
mylcd.print("T = ");
mylcd.print(dht[2]);
mylcd.setCursor(0, 1);
mylcd.print("H = ");
mylcd.print(dht[0]);
//mylcd.clear();
delay(200);
} else { //Read error
Serial.println("sensor error");
}
delay(1000); //It takes 1000ms to wait for the device to read
}
//
*******************************************************************************
***
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
2. Control Pins
SDA SDA
SCL SCL
1. Test Code
//
*********************************************************************
*************
/*
* Filename : RFID
* Description : RFID reader UID
* Auther : http//www.keyestudio.com
*/
#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
www.keyestudio.com
www.keyestudio.com
your address with i2c.
MFRC522 mfrc522(0x28); // create MFRC522.
#define servoPin 13
#define btnPin 16
boolean btnFlag = 0;
void setup() {
Serial.begin(115200); // initialize and PC's serial
communication
mylcd.init();
mylcd.backlight();
Wire.begin(); // initialize I2C
mfrc522.PCD_Init(); // initialize MFRC522
ShowReaderDetails(); // display PCD - MFRC522 read carder
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
myservo.attach(servoPin);
pinMode(btnPin, INPUT);
mylcd.setCursor(0, 0);
mylcd.print("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
{
Serial.println("close");
mylcd.setCursor(0, 0);
www.keyestudio.com
www.keyestudio.com
mylcd.print("close");
myservo.write(0);
btnFlag = 0;
}
}
return;
}
// save UID
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
Serial.print(mfrc522.uid.uidByte[i]);
password = password + String(mfrc522.uid.uidByte[i]);
}
if(password == "17121741227") //The card number is correct,
open the door
{
Serial.println("open");
mylcd.setCursor(0, 0);
mylcd.clear();
mylcd.print("open");
myservo.write(180);
password = "";
btnFlag = 1;
}
else //The card number is wrong,LCD displays error
{
password = "";
mylcd.setCursor(0, 0);
mylcd.print("error");
}
//Serial.println(password);
}
www.keyestudio.com
www.keyestudio.com
void ShowReaderDetails() {
// attain the MFRC522 software
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// when returning to 0x00 or 0xFF, may fail to transmit
communication signals
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the
MFRC522 properly connected?"));
}
}
//
*********************************************************************
*************
2. Test Result
door will turn and open, and LCD1602 shows "The door is
www.keyestudio.com
www.keyestudio.com
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
button2.attachLongPressStop(longPressStop2);
myservo.attach(servoPin);
mylcd.setCursor(0, 0);
mylcd.print("Enter password");
}
void loop() {
// keep watching the push buttons:
button1.tick();
button2.tick();
delay(10);
}
// This function will be called once, when the button1 is released after being pressed for a long
time.
void longPressStop1() {
Serial.print("-");
password = password + '-';
mylcd.setCursor(0, 1);
mylcd.print(password);
} // longPressStop1
www.keyestudio.com
www.keyestudio.com
else
{
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("error");
delay(2000);
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("input again");
}
password = "";
} // click2
void longPressStop2() {
//Serial.println("Button 2 longPress stop");
myservo.write(0); //Close the door
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("close");
} // longPressStop2
3. Test Result
www.keyestudio.com
www.keyestudio.com
easily.
1. Description
www.keyestudio.com
www.keyestudio.com
2. Test Code
#include <Arduino.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
www.keyestudio.com
www.keyestudio.com
const char* password = "ChinaNet@233";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("TCP server started");
MDNS.addService("http", "tcp", 80);
}
void loop() {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //The assigned IP address is printed
on the serial monitor
delay(200);
WiFiClient client = server.available();
if (!client) {
return;
}
while(client.connected() && !client.available()){
delay(1);
}
String req = client.readStringUntil('\r');
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
www.keyestudio.com
www.keyestudio.com
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
item=req;
Serial.println(item);
String s;
if (req == "/") //Browser accesses address can read the
information sent by the client.println(s);
{
IPAddress ip = WiFi.localIP();
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";
Serial.println("Sending 200");
client.println(s); //Send the string S, you can read the
information when visiting the address of E smart home using a
browser.
//client.print(s);
client.stop();
}
3. Test Result
www.keyestudio.com
www.keyestudio.com
1. Description
2. Test Code
www.keyestudio.com
www.keyestudio.com
#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() {
Serial.begin(115200);
mylcd.init();
mylcd.backlight();
pinMode(led_y, OUTPUT);
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
www.keyestudio.com
www.keyestudio.com
Serial.println("TCP server started");
MDNS.addService("http", "tcp", 80);
mylcd.setCursor(0, 0);
mylcd.print("ip:");
mylcd.setCursor(0, 1);
mylcd.print(WiFi.localIP()); //LCD displays the ip adress
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
while(client.connected() && !client.available()){
delay(1);
}
String req = client.readStringUntil('\r');
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
item=req;
Serial.println(item);
String s;
if (req == "/") //Browser accesses address can read the
information sent by the client.println(s);
{
IPAddress ip = WiFi.localIP();
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;
www.keyestudio.com
www.keyestudio.com
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
client.println(s); //Send the string S, you can read the
information when visiting the address of E smart home using a
browser.
//client.print(s);
}
if(req == "/led/on") //Browser accesses the ip address/led/on
{
client.println("turn on the LED");
digitalWrite(led_y, HIGH);
}
if(req == "/led/off") //Browser accesses the ip address/led/off
{
client.println("turn off the LED");
digitalWrite(led_y, LOW);
}
if(req == "/fan/on") //Browser accesses the ip address/fan/on
{
client.println("turn on the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 180);
}
if(req == "/fan/off") //Browser accesses the ip address/fan/off
{
client.println("turn off the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 0);
}
//client.print(s);
client.stop();
}
3. Test Result
www.keyestudio.com
www.keyestudio.com
be off.
of
www.keyestudio.com
www.keyestudio.com
Download APP
Android APP:
download it.
Icon:
APP Interface
www.keyestudio.com
www.keyestudio.com
download it.
1. Description
We will use APP to control the smart home LED lights and
fan switches.
2. Test Code
www.keyestudio.com
www.keyestudio.com
#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() {
Serial.begin(115200);
mylcd.init();
mylcd.backlight();
pinMode(led_y, OUTPUT);
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
www.keyestudio.com
www.keyestudio.com
Serial.println("TCP server started");
MDNS.addService("http", "tcp", 80);
mylcd.setCursor(0, 0);
mylcd.print("ip:");
mylcd.setCursor(0, 1);
mylcd.print(WiFi.localIP()); //LCD displays ip adress
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
while(client.connected() && !client.available()){
delay(1);
}
String req = client.readStringUntil('\r');
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
item=req;
Serial.println(item);
String s;
if (req == "/") //Browser accesses address can read the
information sent by the client.println(s);
{
IPAddress ip = WiFi.localIP();
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;
www.keyestudio.com
www.keyestudio.com
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
client.println(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
{
client.println("turn on the LED");
digitalWrite(led_y, HIGH);
}
if(req == "/led/off") //Browser accesses address ip address/led/off
{
client.println("turn off the LED");
digitalWrite(led_y, LOW);
}
if(req == "/fan/on") //Browser accesses address ip address/fan/on
{
client.println("turn on the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 180);
}
if(req == "/fan/off") //Browser accesses address ip address/fan/off
{
client.println("turn off the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 0);
}
//client.print(s);
client.stop();
}
3. Test Result
www.keyestudio.com
www.keyestudio.com
The mobile phone and the smart home must share the
www.keyestudio.com
www.keyestudio.com
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
www.keyestudio.com
www.keyestudio.com
WiFi, and the mobile phone used for operation should also
communication is
2. Test Code
3. Test Result
www.keyestudio.com
www.keyestudio.com
7. Resources
https://fs.keyestudio.com/KS5009
www.keyestudio.com
www.keyestudio.com
www.keyestudio.com