ESP8266(NodeMcu Lua)学习 (2)WIFI控制led灯(WiFiHTTPSServer)
2019-02-17 20:54
2136 查看
(二)WiFi控制led灯
1、打开示例->ESP8266WIFI->WiFiwebServer;
没有的话,直接复制下面的代码吧!
2、代码如下:
/* * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * **http://server_ip/?gpio=0** will set the GPIO2 low, * **http://server_ip/?gpio=1** will set the GPIO2 high * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. */ #include <ESP8266WiFi.h> const char* ssid = "your wifi"; const char* password = "123456"; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80);//开启板子的port 80 void setup() { Serial.begin(115200);//开启端口,速度为115200 delay(10); // prepare GPIO2 pinMode(2, OUTPUT);//2号脚位定为输出,即D4脚位 digitalWrite(2, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password);//使用WiFi开始连线 while (WiFi.status() != WL_CONNECTED) {//连线成功后停止跳点 delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req);//打印出收到的讯息 client.flush();//流刷新,如果不flush,那么可能就堵塞了 // 比对收到的讯息,确定执行什么操作 int val; if (req.indexOf("/?gpio=0") != -1) val = 0; else if (req.indexOf("/?gpio=1") != -1) val = 1; else { Serial.println("invalid request"); client.stop(); return; } // Set GPIO2 according to the request digitalWrite(2, val); client.flush();//刷新 // Prepare the response String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "; s += (val)?"high":"low"; s += "</html>\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed }
烧录上去,打开串口监听窗口,记录分配到的IP地址(我的是192.168.0.104);
The server will set a GPIO pin depending on the request
https://server_ip/?gpio=0 will set the GPIO2 low,
https://server_ip/?gpio=1 will set the GPIO2 high
在同一局域网下的手机电脑,浏览器中输入https://server_ip/?gpio=0 致D4脚位为低电平;’
输入https://server_ip/?gpio=1则使D4脚位为高电平,点亮led;server_ip就是串口监听器的打印的IP地址(例如我的就是:https://192.168.0.105/?gpio=1 点亮led)!
输入192.168.0.104/?gpio=1
此时网页显示led已点亮;
输入192.168.0.104/?gpio=0
此时led熄灭!
不妨将网址生成二维码,这样就能实现扫码开灯了(要在同一局域网下)!
相关文章推荐
- ESP8266-01之NodeMCU(lua)实现远程控制LED灯
- Esp8266学习之旅13 动手做个8266毕设小案例,smartConfig + MQTT协议轻松实现远程控制一盏LED。(附带demo)
- Esp8266学习之旅④ 了解PWM,更为深入地用PWM控制一盏LED的亮度变化。(附带demo)
- Nodemcu freertos 学习之路 -- 利用中断 控制led开关
- STM32F103VET6_ ESP8266-12F 手机或笔记本电脑WIFI控制LED的实现
- ESP8266(NodeMcu Lua)学习 (3)建立路由(ESP8266-01学习)并测试该路由
- ESP8266(NodeMcu Lua)学习 (附件)nodeMcu API说明
- Nodemcu freertos 学习之路 -- GPIO口控制LED灯开关
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- 4.lua学习笔记:控制语句
- 嵌入式Linux学习入门:控制LED灯
- lua学习笔记之浅淡控制结构
- 树莓派学习笔记——yeelink 远程控制LED
- 树莓派linux驱动学习之LED控制
- CC2530学习路线-基础实验-GPIO 控制LED灯亮灭(1)
- Lua学习之8:控制语句
- 树莓派linux驱动学习之LED控制
- 用C#控制TQ2440开发板上的LED小灯(C# GPIO学习笔记)
- linux字符驱动学习实践1(简单控制LED灯)
- 0基础lua学习(六)控制语句