当前位置:网站首页>Get to know esp8266 (II) -- build a network server to realize remote control
Get to know esp8266 (II) -- build a network server to realize remote control
2022-07-19 03:50:00 【C V engineering lion】
One . Experiment introduction
8266 Build a network server , By the same wifi Terminal access under signal ESP8266IP Address , Control the development board .
Two . Code
1.esp8266_server.begin()
effect : Start network service , Build a network server
2.esp8266_server.on("/",handleRoot)
effect : If other terminals request access to the root directory , execute handleRoot function
3.esp8266_server.onNotFound(handleNotFound)
effect : If something happens during the access 404 error , perform handleNotFound function
4.esp8266_server.handleClient()
effect : Check http Server access
5.esp8266_server.send(200,"text/plain","Hello World")
effect : send out 200 Instructions , Display as text Hello World
6.esp8266_server.send(404,"text/plain","404:Not found")
effect : send out 404 Instructions , Display as text 404:Not found
7.esp8266_server.on("/",HTTP_GET,handleRoot)
effect : With HTTP_GET To access the root directory , perform handleRoot function , When visiting the website, enter the URL and press enter. The default is HTTP_GET The way
8.esp8266_server.on("/LED",HTTP_POST,handleLED)
effect : With HTTP_POST Access to /LED page , perform handleLED
3、 ... and . Code
// Set up the server and access
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
ESP8266WiFiMulti wifiMulti;
ESP8266WebServer esp8266_server(80);
void setup(void)
{
Serial.begin(9600);
// add to wifi Information , Choose the strongest signal connection by yourself
wifiMulti.addAP("HUAWEI Mate 30","1234567890");
int i=0;
// Waiting for the connection
while(wifiMulti.run()!=WL_CONNECTED)
{
delay(1000);
Serial.print(i++);
Serial.print(" ");
}
// Successful connection , Print connection wifi Name and IP Address
Serial.println(" ");
Serial.print("Connect:");
Serial.println(WiFi.SSID());
Serial.print("Address:");
Serial.println(WiFi.localIP());
// Build a network server
esp8266_server.begin();
// When other devices request access to the root directory , perform handleRoot function
esp8266_server.on("/",handleRoot);
// When there is an error accessing the root directory , perform handleNotFound function
esp8266_server.onNotFound(handleNotFound);
Serial.println("HTTP Started");
}
void loop(void)
{
// Handle server access , It is equivalent to always judging whether there is a device to visit the website , If so, deal with the corresponding information
esp8266_server.handleClient();
}
void handleRoot(void)
{
// The development board sends instructions 200, take Hello World Display as text
esp8266_server.send(200,"text/plain","Hello World");
}
void handleNotFound(void)
{
// The development board sends instructions 200, take 404:Not found Display as text
esp8266_server.send(404,"text/plain","404:Not found");
}Check the development board on the serial port IP Address

Use the same wifi The terminal under accesses this IP Address , Successful test

// Build a network server , Control the development board through the server LED Realize on and off
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
ESP8266WiFiMulti wifiMulti;
ESP8266WebServer esp8266_server(80);
void setup(void)
{
int i=0;
Serial.begin(9600);
// Set up LED Pin is output
pinMode(LED_BUILTIN,OUTPUT);
// Automatic connection is the strongest WiFi
wifiMulti.addAP("HUAWEI Mate 30","1234567890");
Serial.println("Connecting...");
// Waiting for the connection
while(wifiMulti.run()!=WL_CONNECTED)
{
delay(1000);
Serial.print(i++);
Serial.print(" ");
}
// Successful connection , And print WiFi Name and IP Address
Serial.print("Connect OK:");
Serial.println(WiFi.SSID());
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
// Turn on the server
esp8266_server.begin();
// Peripheral access root directory
esp8266_server.on("/",HTTP_GET,handleRoot);
esp8266_server.on("/LED",HTTP_POST,handleLED);
esp8266_server.onNotFound(handleBotFound);
Serial.println("HTTP esp8266_server started");
}
void loop(void)
{
// Handle server access
esp8266_server.handleClient();
}
void handleRoot(void)
{
// send out 200 Instructions , With html form , The back is html The code creates a button in /LED page , With POST Form visit
// The name of the button is Toggle LED
esp8266_server.send(200,"text/html","<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");
}
void handleLED(void)
{
// Read LED Level and flip
digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
// Jump to the root directory
esp8266_server.sendHeader("Location","/");
esp8266_server.send(303);
}
void handleBotFound(void)
{
esp8266_server.send(404,"text/plain","404 Not Found");
}wifi Successful connection

Open the development board IP Address , Click button , Development board LED Realize on and off

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
ESP8266WiFiMulti WiFiMulti;
ESP8266WebServer esp8266_server(80);
#define buttonPin D3
bool PinState;
void setup(void)
{
int i=0;
Serial.begin(9600);
WiFiMulti.addAP("HUAWEI Mate 30","1234567890");
while(WiFiMulti.run()!=WL_CONNECTED)
{
delay(1000);
Serial.print(i++);
Serial.print(" ");
}
Serial.println(" ");
Serial.print("Connect OK:");
Serial.println(WiFi.SSID());
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
esp8266_server.begin();
esp8266_server.on("/",HTTP_GET,handleRoot);
esp8266_server.onNotFound(handleBotFound);
Serial.println("HTTP esp8266_server started");
}
void loop(void)
{
esp8266_server.handleClient();
// Always check D3 level
PinState=digitalRead(buttonPin);
}
void handleBotFound(void)
{
esp8266_server.send(404,"text/plain","404 NOT FOUND");
}
void handleRoot(void)
{
String str;
if(PinState==HIGH)
{
str="Button State: HIGH";
}
else
{
str="Button State: LOW";
}
esp8266_server.send(200,"text/plain",str);
}wifi Successful connection

stay FLASH When the key is not pressed, it is high

FLASH When the key is pressed, it is low level

边栏推荐
猜你喜欢
随机推荐
Analysis and processing of male and female voice signals based on MATLAB
【MySQL】在云服务器上安装配置mysql,并使用IDEA连接
Burpsuite2022.1 detailed installation steps include certificate installation
What happens when MySQL tables change from compressed tables to ordinary tables?
流水线技术
Penetration test-01 information collection
EfficientNet系列(1): EfficientNetV2网络详解
【LeetCode】745. Prefix and suffix search
剑指 Offer 60. n个骰子的点数
【论文摘要】记录一些感兴趣的摘要和特别领域论文的方法截图。
Thinkphp5.0 model operation uses page for paging
Bag of visual words (bovw) personal understanding of visual word bag
如何在自动化测试中使用MitmProxy获取数据返回?
电脑绘画软件哪个好用:试试Artweaver Plus吧,媲美sai绘画软件 | 最新版本的artweaver下载
动态管理内存的通讯录实现
Frequency school and Bayes school
Assembly line technology
GoogLeNet
Ouvrir le cvsharp d'ai pour trouver une petite image (version de cas)
[C language] 0 basic tutorial - file operation (to be continued)









