TA的每日心情 | 奋斗 2023-7-6 08:48 |
---|
签到天数: 169 天 连续签到: 1 天 [LV.7]常住居民III
|
我们的开发板可以连接到WIFI了,我们的云端服务器上也新建了设备了(发的帖子审核好慢,还没有通过)。
我们知道了apikey与deviceID了,那么就可以通过服务器上的设备对我们的开发板进行控制了。
下面是上一篇的LED设备的程序。
其中要填入自己的APIKEY与DEVICEID。
#include <esp8266wifi.h> #include <wstring.h> //the library are needed for autoconfig WiFi #include <dnsserver.h> #include <esp8266webserver.h> #include <wifimanager.h> // replace with your own API key and device ID, String apikey = " apikey 码"; const char* deviceID="设备ID"; const char* server = "<a href="https://www.linksprite.io" ;"="">www.linksprite.io";</a> WiFiClient client; void setup() { Serial.begin(115200); pinMode(BUILTIN_LED, OUTPUT); WiFiManager wifiManager; wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); wifiManager.autoConnect("LinkNodeAP"); Serial.print("WiFi Connected ...\n"); Serial.println("WiFi connected"); } void loop() { if (client.connect(server,80)) { String postStr ="{"; postStr +="\"action\":\"query\","; postStr +="\"apikey\":\""; postStr += apikey; postStr +="\","; postStr +="\"deviceid\":\""; postStr += deviceID; postStr +="\","; postStr += "\"params\":"; postStr += "["; postStr += "\"light\""; postStr +="]"; postStr +="}"; client.print("OST /api/http HTTP/1.1\n"); client.print("Host: "); client.print(server); client.print("\nContent-Type: application/json\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); } delay(1000); Serial.println("Store response..."); String request = ""; while (client.available()) { char c = client.read(); request +=c; } if (request!= NULL) { int index1 = request.indexOf(":{"); int index2 = request.indexOf("},"); String param = request.substring(index1, index2 + 1); Serial.print("The param is "); Serial.println(param); if(param.indexOf("off")>0){ digitalWrite(BUILTIN_LED, HIGH); Serial.println("OFF"); } else if(param.indexOf("on")>0){ digitalWrite(BUILTIN_LED, LOW); Serial.println("ON"); } client.stop(); Serial.println("Waiting..."); delay(2000); }}</wifimanager.h></esp8266webserver.h></dnsserver.h></wstring.h></esp8266wifi.h>点击如下的自己建的设备。
点击如下的“Light On”与“Light Off”可以控制开发板上的LED的亮灭。
|
|