赞
踩
一.实验介绍
8266搭建网络服务器,通过同一wifi信号下的终端访问ESP8266IP地址,对开发板进行控制。
二.代码
1.esp8266_server.begin()
作用:启动网络服务,搭建网络服务器
2.esp8266_server.on("/",handleRoot)
作用:如果其他终端请求访问根目录,则执行handleRoot函数
3.esp8266_server.onNotFound(handleNotFound)
作用:如果访问过程中发生404错误,执行handleNotFound函数
4.esp8266_server.handleClient()
作用:检查http服务器访问
5.esp8266_server.send(200,"text/plain","Hello World")
作用:发送200指令,以文本形式显示Hello World
6.esp8266_server.send(404,"text/plain","404:Not found")
作用:发送404指令,以文本形式显示404:Not found
7.esp8266_server.on("/",HTTP_GET,handleRoot)
作用:以HTTP_GET方式访问根目录,执行handleRoot函数,在访问网站时输入网址回车默认就是HTTP_GET方式
8.esp8266_server.on("/LED",HTTP_POST,handleLED)
作用:以HTTP_POST方式访问/LED页面,执行handleLED
三.代码
- //搭建服务器并访问
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266WebServer.h>
- ESP8266WiFiMulti wifiMulti;
- ESP8266WebServer esp8266_server(80);
- void setup(void)
- {
- Serial.begin(9600);
- //添加wifi信息,自己选择最强信号连接
- wifiMulti.addAP("HUAWEI Mate 30","1234567890");
- int i=0;
- //等待连接
- while(wifiMulti.run()!=WL_CONNECTED)
- {
- delay(1000);
- Serial.print(i++);
- Serial.print(" ");
- }
- //连接成功,打印连接wifi的名称和IP地址
- Serial.println(" ");
- Serial.print("Connect:");
- Serial.println(WiFi.SSID());
- Serial.print("Address:");
- Serial.println(WiFi.localIP());
- //搭建网络服务器
- esp8266_server.begin();
- //其他设备请求访问根目录时,执行handleRoot函数
- esp8266_server.on("/",handleRoot);
- //访问根目录出现错误时,执行handleNotFound函数
- esp8266_server.onNotFound(handleNotFound);
- Serial.println("HTTP Started");
- }
- void loop(void)
- {
- //处理服务器访问,相当于一直判断有没有设备访问网站,有的话处理对应信息
- esp8266_server.handleClient();
- }
- void handleRoot(void)
- {
- //开发板发送指令200,将Hello World以文本形式显示
- esp8266_server.send(200,"text/plain","Hello World");
- }
- void handleNotFound(void)
- {
- //开发板发送指令200,将404:Not found以文本形式显示
- esp8266_server.send(404,"text/plain","404:Not found");
- }
在串口查看开发板IP地址
使用同一wifi下的终端访问此IP地址,试验成功
- //搭建网络服务器,通过服务器控制开发板板载LED实现亮灭
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266WebServer.h>
- ESP8266WiFiMulti wifiMulti;
- ESP8266WebServer esp8266_server(80);
- void setup(void)
- {
- int i=0;
- Serial.begin(9600);
- //设置LED引脚为输出
- pinMode(LED_BUILTIN,OUTPUT);
- //自动连接最强WiFi
- wifiMulti.addAP("HUAWEI Mate 30","1234567890");
- Serial.println("Connecting...");
- //等待连接
- while(wifiMulti.run()!=WL_CONNECTED)
- {
- delay(1000);
- Serial.print(i++);
- Serial.print(" ");
- }
- //连接成功,并打印WiFi名称和IP地址
- 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.on("/LED",HTTP_POST,handleLED);
- esp8266_server.onNotFound(handleBotFound);
- Serial.println("HTTP esp8266_server started");
- }
- void loop(void)
- {
- //处理服务器访问
- esp8266_server.handleClient();
- }
- void handleRoot(void)
- {
- //发送200指令,以html形式,后面为html代码创建一个按钮在/LED页面,以POST形式访问
- //按钮的名称为Toggle LED
- esp8266_server.send(200,"text/html","<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");
- }
- void handleLED(void)
- {
- //读取LED电平并翻转
- digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
- //跳转根目录
- esp8266_server.sendHeader("Location","/");
- esp8266_server.send(303);
- }
- void handleBotFound(void)
- {
- esp8266_server.send(404,"text/plain","404 Not Found");
- }
wifi连接成功
打开开发板IP地址,点击按钮,开发板LED实现亮灭
- #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();
- //时刻检查D3电平
- 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连接成功
在FLASH按键没按下时为高电平
FLASH按键按下后为低电平
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。