下面贴上代码:
#include <aJSON.h> #include <SPI.h> #include <Wire.h> #include <dht11.h> #include <DS3231.h> //============= 此处必须修改============ String DEVICEID="1494"; // 你的设备编号 == String APIKEY="d6easd772"; // 设备密码== String INPUTID1="1583";//接口ID============== String INPUTID2="1588";//接口ID============== String INPUTID3="1589";//接口ID============== String INPUTID4="1590";//接口ID============== //======================================= unsigned long lastCheckInTime = 0; //记录上次报到时间 unsigned long lastUpdateTime = 0;//记录上次上传数据时间 const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次 const unsigned long updateInterval = 10000; // 数据上传间隔时间10秒 String inputString = "";//串口读取到的内容 boolean stringComplete = false;//串口是否读取完毕 boolean CONNECT = true; //连接状态 boolean isCheckIn = false; //是否已经登录服务器 char* parseJsonchar *jsonString);//定义aJson字符串 ///**************************************************** dht11 DHT11; #define DHT11PIN 2 /////////////PM2.5 int measurePin = 1; //Connect dust sensor to Arduino A1 pin int ledPower = 3; //Connect 3 led driver pins of dust sensor to Arduino D3 float voMeasured = 0; float calcVoltage = 0; float dustDensity = 0; int samplingTime = 280; int deltaTime = 40; int sleepTime = 9680; ///////////////////PM2.5 /////****************************************************** int val;//MQ_2 float valTemperature;//定义变量 float valHumidity;//定义变量 void update1String did, String inputid, float value); void setup) { Serial.begin115200);//ESP 8266 工作波特率 Wire.begin); int chk = DHT11.readDHT11PIN); pinModeledPower,OUTPUT); delay5000); } void loop) { ifmillis) - lastCheckInTime > postingInterval || lastCheckInTime==0) { checkIn); } val=analogRead0); valTemperature = float)DHT11.temperature; valHumidity = float)DHT11.humidity; digitalWriteledPower,LOW); delayMicrosecondssamplingTime); voMeasured = analogReadmeasurePin); // read the dust value delayMicrosecondsdeltaTime); digitalWriteledPower,HIGH); // turn the LED off delayMicrosecondssleepTime); calcVoltage = voMeasured * 5.0 / 1024.0); dustDensity = 0.17 * calcVoltage - 0.1; ifmillis) - lastUpdateTime >= updateInterval) && isCheckIn) { update1DEVICEID,INPUTID1,val);//MQ_2 update1DEVICEID,INPUTID2,valTemperature); update1DEVICEID,INPUTID3,valHumidity); update1DEVICEID,INPUTID4,dustDensity); ///************************ } serialEvent); if stringComplete) { inputString.trim); //Serial.printlninputString); ifinputString=="CLOSED"){ Serial.println"connect closed!"); CONNECT=false; }else{ int len = inputString.length)+1; ifinputString.startsWith"{") && inputString.endsWith"}")){ char jsonString[len]; inputString.toCharArrayjsonString,len); aJsonObject *msg = aJson.parsejsonString); processMessagemsg); aJson.deleteItemmsg); } } // clear the string: inputString = ""; stringComplete = false; } } void checkIn) {//定时签到,防止离线 if !CONNECT) { isCheckIn = false; Serial.print"+++"); delay500); Serial.print" "); delay1000); Serial.print"AT+RST "); delay6000); CONNECT=true; lastCheckInTime=0; } else{ Serial.print"{"M":"checkin","ID":""); Serial.printDEVICEID); Serial.print"","K":""); Serial.printAPIKEY); Serial.print""} "); lastCheckInTime = millis); isCheckIn = true; } } void processMessageaJsonObject *msg){ aJsonObject* method = aJson.getObjectItemmsg, "M"); aJsonObject* content = aJson.getObjectItemmsg, "C"); aJsonObject* client_id = aJson.getObjectItemmsg, "ID"); //char* st = aJson.printmsg); if !method) { return; } //Serial.printlnst); //freest); String M=method->valuestring; String C=content->valuestring; String F_C_ID=client_id->valuestring; ifM=="say"){ ifC=="play"){ // digitalWriteLED, HIGH); sayToClientF_C_ID,"LED on!"); } ifC=="stop"){ // digitalWriteLED, LOW); sayToClientF_C_ID,"LED off!"); } ifC=="wendu"){ sayToClientF_C_ID,StringvalTemperature)); } ifC=="shidu"){ sayToClientF_C_ID,StringvalHumidity)); } ifC=="mq2"){ sayToClientF_C_ID,Stringval)); } ifC=="pm"){ sayToClientF_C_ID,StringdustDensity)); } } } void sayToClientString client_id, String content){//命令回复 Serial.print"{"M":"say","ID":""); Serial.printclient_id); Serial.print"","C":""); Serial.printcontent); Serial.print""} "); lastCheckInTime = millis); } void serialEvent) { while Serial.available)) { char inChar = char)Serial.read); inputString += inChar; if inChar == ' ') { stringComplete = true; } } } //上报数据 拼接json void update1String did, String inputid, float value){ Serial.print"{"M":"update","ID":""); Serial.printdid); Serial.print"","V":{""); Serial.printinputid); Serial.print"":""); Serial.printvalue); Serial.println""}}"); lastCheckInTime = millis); lastUpdateTime= millis); } //同时上传两个接口数据调用此函数 void update2String did, String inputid1, float value1, String inputid2, float value2){ Serial.print"{"M":"update","ID":""); Serial.printdid); Serial.print"","V":{""); Serial.printinputid1); Serial.print"":""); Serial.printvalue1); Serial.print"",""); Serial.printinputid2); Serial.print"":""); Serial.printvalue2); Serial.println""}}"); lastCheckInTime = millis); lastUpdateTime= millis); }