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

(二)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 

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\r\n\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "\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
ESP8266(NodeMcu Lua)学习 (2)WIFI控制led灯(WiFiHTTPSServer)_第1张图片
此时网页显示led已点亮;

输入192.168.0.104/?gpio=0

ESP8266(NodeMcu Lua)学习 (2)WIFI控制led灯(WiFiHTTPSServer)_第2张图片
此时led熄灭!

不妨将网址生成二维码,这样就能实现扫码开灯了(要在同一局域网下)!

你可能感兴趣的:(arduino学习)