作者:白苹果 | 更新时间:2018-10-04 | 浏览量:2813
这个是代码,小白一窍不通。。谢谢各位大神指教
#include <aJSON.h>
//============= 此处必须修改============
String DEVICEID="xxxx"; // 你的设备ID=======
String APIKEY="xxxxxxxxx"; // 设备密码==
String INPUTID1="xxxx";//接口ID1==============
//=======================================
#define Moisture A0 //定义AO 引脚 为 IO-A0
#define DO 7 //定义DO 引脚 为 IO-7
unsigned long lastCheckInTime = 0; //记录上次报到时间
unsigned long lastUpdateTime = 0;//记录上次上传数据时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
const unsigned long updateInterval = 5000; // 数据上传间隔时间5秒
String inputString = "";//串口读取到的内容
boolean stringComplete = false;//串口是否读取完毕
boolean CONNECT = true; //连接状态
boolean isCheckIn = false; //是否已经登录服务器
char* parseJson(char *jsonString);//定义aJson字符串
#define Moisture A0 //定义AO 引脚 为 IO-A0
#define DO 7 //定义DO 引脚 为 IO-7
int humidity;
int temp=0;
void update1(String did, String inputid, float value);
void setup() {
pinMode(Moisture, INPUT);//定义A0为输入模式
pinMode(DO, INPUT);
Serial.begin(115200);
delay(1000);
}
void loop() {//修改部分
if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
if(millis() - lastUpdateTime > updateInterval)
{
float val;
int data;
val=analogRead(0);
//串口返回测量数据
Serial.print("Moisture=");
Serial.print(analogRead(Moisture));//读取AO的数值
Serial.print("|DO=");
Serial.println(digitalRead(DO));//读取DO的数值
delay(1000);
if((millis() - lastUpdateTime >= updateInterval) && isCheckIn)
{
update1(DEVICEID,INPUTID1,val);//MQ_2
humidity=analogRead(0);
}
serialEvent();
if (stringComplete) {
inputString.trim();
//Serial.println(inputString);
if(inputString=="CLOSED"){
Serial.println("connect closed!");
CONNECT=false;
}
else{
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);
}
}
}
}
// clear the string:
inputString = "";
stringComplete = false;
}
void checkIn() {
if (!CONNECT) {
Serial.print("+++");
delay(500);
Serial.print("\r\n");
delay(1000);
Serial.print("AT+RST\r\n");
delay(6000);
CONNECT=true;
lastCheckInTime==0;
}
else{
Serial.print("{\"M\":\"checkin\",\"ID\":\"");
Serial.print(DEVICEID);
Serial.print("\",\"K\":\"");
Serial.print(APIKEY);
Serial.print("\"}\r\n");
lastCheckInTime = millis();
}
}
void processMessage(aJsonObject *msg){
aJsonObject* method = aJson.getObjectItem(msg, "M");
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
//char* st = aJson.print(msg);
if (!method) {
return;
}
//Serial.println(st);
//free(st);
String M=method->valuestring;
String C=content->valuestring;
String F_C_ID=client_id->valuestring;
}
void sayToClient(String client_id, String content){
Serial.print("{\"M\":\"say\",\"ID\":\"");
Serial.print(client_id);
Serial.print("\",\"C\":\"");
Serial.print(content);
Serial.print("\"}\r\n");
lastCheckInTime = millis();
}
void update1(String did, String inputid, float value){
Serial.print("{\"M\":\"update\",\"ID\":\"");
Serial.print(did);
Serial.print("\",\"V\":{\"");
Serial.print(inputid);
Serial.print("\":\"");
Serial.print(value);
Serial.println("\"}}");
lastCheckInTime = millis();
lastUpdateTime= millis();
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}