ESP8266(NodeMcu Lua)学习 (2)WIFI控制led灯(WiFiHTTPSServer)

使用ESP8266(NodeMCU Lua)通过WiFi建立HTTP服务器,控制GPIO端口改变LED灯状态。通过访问特定URL(如:https://server_ip/?gpio=0或1)实现LED的开关。在同一局域网内,可以输入设备IP地址配合GPIO状态参数(0或1)来控制D4脚位的高低电平,进而点亮或熄灭LED。此外,生成对应URL的二维码可以方便地实现扫码控制灯光。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(二)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(
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值