作者:yjf | 更新时间:2020-12-03 | 浏览量:1914
程序:
#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.bigiot.net";
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
在文件 -> 首选项开启
“编译过程中显示详细输出”选项
这份报告会包含更多信息。