#include <ESP8266WiFi.h>
WiFiClient client;
const int relay1 =D3;//#define relay1 D3(原来的代码不行,改了就可以控制了)//LED连接在8266的GPIO2上
const char *ssid = "*****";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
const char *password = "*******";//你要连接的wifi密码
const char *host = "121.42.180.30"; void setup() {
Serial.begin(115200);
delay(10);
pinMode(relay1,OUTPUT);
// We start by connecting to a 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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
const int httpPort =8181;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
Serial.print("connecting to ");
Serial.println(host);
client.write("{\"M\":\"checkin\",\"ID\":\"****\",\"K\":\"****\"}\r\n");//登陆设备,修改成自己的ID和key
delay(10);
} void loop() {
char valueread;
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
int i;
for(i=0;i<=150;i++)
{
if((line=='s') &&( line[i+1]=='t'))//这里需要修改为stop才可以控制
{
digitalWrite(relay1,HIGH);
client.write("{\"M\":\"say\",\"ID\":\"ALL\",\"C\":\"LED1 is on\",\"SIGN\":\"ok\"}\r\n");
} if((line=='p') && (line[i+1]=='l'))//这里需要修改为Play才可以控制
{
digitalWrite(relay1,LOW);
client.write("{\"M\":\"say\",\"ID\":\"ALL\",\"C\":\"LED1 is off\",\"SIGN\":\"ok\"}\r\n");
}
}
}
client.write("{\"M\":\"say\",\"ID\":\"*****\",\"C\":\"123456\"}\r\n");//修改自己的ID,向设备发送信息,防止掉线
delay(2000);
}
开始使用的代码一直不行,研究好久改了几个地方可以控制了,但是现在有个问题,控制延时5秒左右,不知道是什么情况,请大家参谋参谋。。。谢谢嘻嘻嘻
|