//本程序是操作8266的GPIO 0 口 // 程序如下
#include <ESP8266WiFi.h> const char* ssid = "TP-11222"; //路由器账号 const char* password = "11000323"; //路由器密码
WiFiServer server(80); // 服务器端口号 void setup() { Serial.begin(115200); delay(10); pinMode(0, OUTPUT); //定义IO 0 输出
digitalWrite(0, 0); //IO 0 上点低电频
// Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); cStart the server server.begin(); Serial.println("Server started @ "); // Print the IP address & instructions Serial.println(WiFi.localIP()); //打印出IP地址,后期可以制作显示器来外部硬件显示ip Serial.println("To control GPIO, open your web browser."); Serial.println("To set GPIO 0 high, type:"); Serial.print(WiFi.localIP()); Serial.println("/gpio/1"); Serial.println("To set GPIO 0 low, type:"); Serial.print(WiFi.localIP()); Serial.println("/gpio/0"); Serial.println("To toggle GPIO 0, type:"); Serial.print(WiFi.localIP()); Serial.println("/gpio/4"); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { delay(100); return; } String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request int val; // 下面开始判断ip进行动作指令 if (req.indexOf("/gpio/off") != -1) digitalWrite(0, 1); else if (req.indexOf("/gpio/on") != -1) digitalWrite(0, 0);
else { Serial.println("invalid request"); client.print("HTTP/1.1 404\r\n"); client.stop(); return; } 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\nWelcome wenzheng space! "; s += "</html>\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); }
程序下载好后
打开串口监视器
复位8266
查看8622地址
记住这个地址 192.168.1.112 我的是这个地址 ,每个人的都不一样,自己记住
下面是ios捷径的配置(请用苹果内置浏览器打开):
开灯
https://www.icloud.com/shortcuts/738ab973d2024d6a97c2692638bbd809?from=singlemessage&isappinstalled=0
关灯
https://www.icloud.com/shortcuts/8e25019a47604bb794856e5317ca7a30?from=singlemessage&isappinstalled=0
进入快捷指令编辑
给这个地址改成自己的
点右上角的完成就可以了
体验siri控制灯吧
唯一缺点内网控制 不能用外网
|