作者:yjf | 更新時間:2020-12-03 | 瀏覽量:1323
程序:
#include #include
//============= 此處必須修該============
String DEVICEID="*****";//你的設備編號==改成你貝殼物聯里智能設備的ID
String APIKEY="*****";//設備密碼==改成你貝殼物聯里智能設備的密碼
const char* ssid="CMCC-hkws";//WiFi名稱 你的WIFI名稱
const char* password="147258369";//WiFi密碼 你的WIFI密碼
//=======================================
#define OUT 2//輸出控制繼電器端口
//=======================================
unsigned long lastCheckInTime=0;//記錄上次報到時間
const unsigned long postingInterval=40000;//每隔40秒向服務器報到一次
const char* host="www.eqytg.org";
const int httpPort=8181;
WiFiClient client;
void setup()
{
pinMode(OUT,OUTPUT);//設置輸出
digitalWrite(OUT,LOW);//上電打開輸出
pinMode(LED_BUILTIN,OUTPUT);//設備板載LED輸出模式
digitalWrite(LED_BUILTIN,HIGH);//關閉LED
Serial.begin(115200);
Serial.println();
Serial.println();
WiFi.begin(ssid,password);//連接WiFi
while(WiFi.status()!= WL_CONNECTED)//等待WiFi連接成功
{
delay(500);
Serial.print(".");
}
Serial.println();
}
void loop()
{
// Use WiFiClient class to create TCP connections
if(!client.connected())
{
if(!client.connect(host, httpPort))
{
Serial.println("connection failed");
delay(5000);
return;
}
}
if(millis()-lastCheckInTime>postingInterval||lastCheckInTime==0)
checkIn();
// Read all the lines of the reply from server and print them to Serial
if (client.available())
{
String inputString=client.readStringUntil('\n');
inputString.trim();
Serial.println(inputString);
int len=inputString.length()+1;
if(inputString.startsWith("{") && inputString.endsWith("}"))
{
char jsonString[len];
inputString.toCharArray(jsonString,len);
aJsonObject *msg = aJson.parse(jsonString);
processMessage(msg);
aJson.deleteItem(msg);
}
}
}
void processMessage(aJsonObject *msg)
{
aJsonObject* method=aJson.getObjectItem(msg,"M");
aJsonObject* content=aJson.getObjectItem(msg,"C");
aJsonObject* client_id=aJson.getObjectItem(msg,"ID");
if(!method)
return;
String M=method->valuestring;
if(M=="say")
{
String C=content->valuestring;
String F_C_ID=client_id->valuestring;
digitalWrite(LED_BUILTIN,LOW);//接收到指令就閃一下LED
delay(50);
digitalWrite(LED_BUILTIN,HIGH);
if(C=="stop") //接收到的是停止指令
{
digitalWrite(OUT,HIGH);//關閉
sayToClient(F_C_ID,"LED All off!");
}
if(C=="play") //接收到的是停止指令//接收到的不是停止指令
{
digitalWrite(OUT,LOW);//打開
sayToClient(F_C_ID,"LED All on!");
}
}
}
void checkIn()
{
String msg="{\"M\":\"checkin\",\"ID\":\"" + DEVICEID + "\",\"K\":\"" + APIKEY + "\"}\n";
client.print(msg);
lastCheckInTime=millis();
}
void sayToClient(String client_id, String content)
{
String msg="{\"M\":\"say\",\"ID\":\"" + client_id + "\",\"C\":\"" + content + "\"}\n";
client.print(msg);
lastCheckInTime=millis();
}
錯誤信息:
Arduino:1.8.12 (Mac OS X), 開發板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, All SSL ciphers (most compatible), 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
sketch_dec03c:64:21: error: variable or field 'processMessage' declared void
void processMessage(aJsonObject *msg)
^
sketch_dec03c:64:21: error: 'aJsonObject' was not declared in this scope
sketch_dec03c:64:34: error: 'msg' was not declared in this scope
void processMessage(aJsonObject *msg)
^
/Users/yyyy/Documents/Arduino/sketch_dec03c/sketch_dec03c.ino: In function 'void loop()':
sketch_dec03c:58:7: error: 'aJsonObject' was not declared in this scope
aJsonObject *msg = aJson.parse(jsonString);
^
sketch_dec03c:58:20: error: 'msg' was not declared in this scope
aJsonObject *msg = aJson.parse(jsonString);
^
sketch_dec03c:58:26: error: 'aJson' was not declared in this scope
aJsonObject *msg = aJson.parse(jsonString);
^
sketch_dec03c:59:25: error: 'processMessage' was not declared in this scope
processMessage(msg);
^
/Users/yuanjiafu/Documents/Arduino/sketch_dec03c/sketch_dec03c.ino: At global scope:
sketch_dec03c:64:21: error: variable or field 'processMessage' declared void
void processMessage(aJsonObject *msg)
^
sketch_dec03c:64:21: error: 'aJsonObject' was not declared in this scope
sketch_dec03c:64:34: error: 'msg' was not declared in this scope
void processMessage(aJsonObject *msg)
^
exit status 1
variable or field 'processMessage' declared void
在文件 -> 首選項開啟
“編譯過程中顯示詳細輸出”選項
這份報告會包含更多信息。