作者:GoastTokyo | 更新时间:2021-11-25 | 浏览量:843
#include <aJSON.h>
#include <Servo.h>
#include <SoftwareSerial.h>
//============= 此处必须修该============
String DEVICEID = "xxx"; // 设备ID ==
String APIKEY = "xxx"; //设备APIKEY ==
//=======================================
SoftwareSerial mySerial(10, 11); // RX, TX
unsigned long lastCheckStatusTime = 0; //记录上次报到时间
const unsigned long postingInterval = 30000; // 每隔40秒向服务器报到一次
unsigned long checkoutTime = 0;//登出时间
// DC hobby servo
Servo myservo;
int servospd=240;
void setup() {
//舵机初始化
myservo.attach(9);
//串口初始化
Serial.begin(115200);
//esp8266串口初始化
mySerial.begin(115200);
//esp8266网络设置初始化
//退出可能未退出的CIPSEND模式
mySerial.write("+++");
delay(1000);
mySerial.write("\r\n");
printmessage();
delay(1000);
//检验是否退出CIPSEND模式
mySerial.write("AT\r\n");
printmessage();
delay(1000);
mySerial.write("AT+CWMODE?\r\n");
printmessage();
delay(1000);
mySerial.write("AT+CWMODE=3\r\n");
printmessage();
delay(1000);
mySerial.write("AT+CWMODE?\r\n");
printmessage();
delay(1000);
mySerial.write("AT+CWJAP=\"xxx\",\"xxx\"\r\n");
printmessage();
delay(10000);
//检验透传模式
mySerial.write("AT+CIPMODE?\r\n");
printmessage();
delay(1000);
//刷新更改透传模式为1(开启)
mySerial.write("AT+CIPMODE=1\r\n");
printmessage();
delay(1000);
//检验透传模式是否更改成功
mySerial.write("AT+CIPMODE?\r\n");
printmessage();
delay(1000);
//连接贝壳物联
mySerial.write("AT+CIPSTART=\"TCP\",\"www.bigiot.net\",8181\r\n");
printmessage();
delay(5000);
//开始对话
mySerial.write("AT+CIPSEND\r\n");
printmessage();
delay(1000);
//设备登录
mySerial.write("{\"M\":\"checkin\",\"ID\":\"xxx\",\"K\":\"xxx\"}\r\n");
printmessage();
delay(10000);
//退出对话
mySerial.write("+++");
printmessage();
delay(1000);
mySerial.write("\r\n");
printmessage();
delay(1000);
}
void loop() {
//每一定时间查询一次设备在线状态,同时替代心跳
if (millis() - lastCheckStatusTime > postingInterval) {
mySerial.write("AT+CIPSEND\r\n");
delay(1000);
mySerial.write("{\"M\":\"status\"}\r\n");
lastCheckStatusTime = millis();
delay(5000);
mySerial.write("+++");
delay(1000);
mySerial.write("\r\n");
delay(1000);
}
#处理来自贝壳物联的信息
while(mySerial.available()){
String inputmsg=mySerial.readStringUntil("\n");
int jsonBeginAt=inputmsg.indexOf("{");
int jsonEndAt=inputmsg.lastIndexOf("}");
if(jsonBeginAt!=-1 && jsonEndAt!=-1){
inputmsg=inputmsg.substring(jsonBeginAt,jsonEndAt+1);
int len = inputmsg.length()+1;
char jsonString[len];
inputmsg.toCharArray(jsonString,len);
aJsonObject *msg=aJson.parse(jsonString);
aJsonObject* method=aJson.getObjectItem(msg, "M");
if (!method){
return;
}
String M = method->valuestring;
if(M == "say"){
aJsonObject* content=aJson.getObjectItem(msg, "C");
aJsonObject* web_id= aJson.getObjectItem(msg, "ID");
String C = content->valuestring;
String ID = web_id->valuestring;
if(C == "unlock"){
myservo.write(servospd);
mySerial.write("AT+CIPSEND\r\n");
mySerial.write("{\"M\":\"say\",\"ID\":\"U17110\",\"C\":\"Servo forword!\",\"SIGN\":\"esp8266\"}\r\n");
Serial.println("Unlock!");
delay(1000);
mySerial.write("+++");
mySerial.write("\r\n");
}
if(C == "lock"){
myservo.write(servospd*(-1));
mySerial.write("AT+CIPSEND\r\n");
mySerial.write("{\"M\":\"say\",\"ID\":\"U17110\",\"C\":\"Servo reverse!\",\"SIGN\":\"esp8266\"}\r\n");
delay(1000);
mySerial.write("+++");
mySerial.write("\r\n");
}
}
aJson.deleteItem(msg);
}
}
}
void printmessage(){
if (mySerial.available())
Serial.println(mySerial.readStringUntil("\n"));
if (Serial.available())
mySerial.write(Serial.read());
}