创作者:Tracis | 更新日期:2016-07-20 | 在线时长:4时
实时监测温湿度
实时监测温湿度
arduino r3 + ESP8266-01 + DHT11
链接:http://pan.baidu.com/s/1o8zresE 密码:494i
//需导入DHTstable库和aJson库
#include <aJSON.h>
#include <dht.h>
dht DHT;
#define DHT11_PIN 5 //定义DHT11的数据接入引脚
String DEVICEID="xxxx"; // 你的设备ID=======
String APIKEY="xxxxx"; // 设备密码==
String INPUTID1="zzzzz";
String INPUTID2="zzzzz";//接口ID==============
//=======================================
unsigned long lastCheckInTime = 0; //记录上次报到时间
unsigned long lastUpdateTime = 0;//记录上次上传数据时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
String inputString = "";//串口读取到的内容
boolean stringComplete = false;//串口是否读取完毕
boolean CONNECT = true; //连接状态
boolean isCheckIn = false; //是否已经登录服务器
char* parseJson(char *jsonString);//定义aJson字符串
void setup() {
Serial.begin(115200);
delay(10000);
//Serial.println("Type,\tstatus,\tTemperature (C)");
}
void loop() {
if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
// READ DATA
int chk = DHT.read11(DHT11_PIN);
if(chk==DHTLIB_OK){
double data1=DHT.temperature;//温度
double data2=DHT.humidity;//湿度
update1(DEVICEID, INPUTID1,data1);
delay(4000);
//update2(DEVICEID,INPUTID1,data1, INPUTID2,data2);
//delay(4000);
update1(DEVICEID, INPUTID2,data2);
delay(4000);
}
serialEvent();
if (stringComplete) {
inputString.trim();
//Serial.println(inputString);
if(inputString=="CLOSED"){
Serial.println("connect closed!");
CONNECT=false;
isCheckIn=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);//处理接收到的Json数据
aJson.deleteItem(msg);
}
}
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;
if(M=="checkinok"){
isCheckIn = true;
}
}
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;
}
}
}
//同时上传两个接口数据调用此函数
void update2(String did, String inputid1, float value1, String inputid2, float value2){
Serial.print("{\"M\":\"update\",\"ID\":\"");
Serial.print(did);
Serial.print("\",\"V\":{\"");
Serial.print(inputid1);
Serial.print("\":\"");
Serial.print(value1);
Serial.print("\",\"");
Serial.print(inputid2);
Serial.print("\":\"");
Serial.print(value2);
Serial.println("\"}}");
lastCheckInTime = millis();
lastUpdateTime= millis();
}