The code about my devices of keepwatch

作者:Yogi | 更新时间:2019-06-14

First, Thanks a lot for https://www.bigiot.net/help/2.html & https://www.bigiot.net/talk/21.html, They help me a lot, And I copy the code from there, My device name is "KeepWatch"

感謝BigIot,論壇的公開代碼對我來說幫助很大。以下代碼還是有很多不足,不過我會逐步改進

#include <ESP8266WiFi.h>
#include <aJSON.h>
#include <SimpleDHT.h>
WiFiClient client;
aJsonStream serial_stream(&client);
String DEVICEID = "DevicesID"; // 你的设备ID=======
String APIKEY = "APIKEY"; // 设备APIKEY==
String INPUTID1 = "InterfaceF"; //接口ID==============
String INPUTID2 = "InterfaceS";
unsigned long lastCheckInTime = 0; 
const char *ssid     = "MyWiFi";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
const char *password = "MyPassWord";//你要连接的wifi密码
const char *host = "www.bigiot.net";
unsigned long lastCheckStatusTime = 0; //记录上次报到时间
unsigned long lastUpdateTime = 0;//记录上次上传数据时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
const unsigned long updateInterval = 5000; // 数据上传间隔时间5秒
unsigned long checkoutTime = 0;//登出时间
const int pinDHT11 = 2;// DHT11 接 D4
byte temperature;
byte humidity;
int err = SimpleDHTErrSuccess;
SimpleDHT11 dht11(pinDHT11);
//IPAddress timeServer(120, 25, 108, 11); // time-a.timefreq.bldrdoc.gov
void setup() {
  Serial.begin(9600);
  delay(10);
  pinMode(4, OUTPUT);//GPIO4 D2
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  const int httpPort =8181;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  Serial.print("connecting to ");
  Serial.println(host);
  client.write("{\"M\":\"checkin\",\"ID\":\"******\",\"K\":\"******\"}\r\n");//登陆设备,修改成自己的ID和key
  delay(10);
}

void loop() {
  char valueread;
  int val;
  int temp;//定义变量
  int hum;//定义变量
  //每一定时间查询一次设备在线状态,同时替代心跳
  if (millis() - lastCheckStatusTime > postingInterval) {
     checkIn();
  }
  //每隔一定时间上传一次数据
  if (millis() - lastUpdateTime > updateInterval) {
    temp = (int)temperature; // 读取传感器的模拟值并赋值给temp
    hum = (int)humidity;
    if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
      Serial.print("Read DHT11 failed, err=");
      Serial.println(err);
      delay(1000);
     }
    Serial.print("temp=");
    Serial.println(temp);
    Serial.print("hum=");
    Serial.println(hum);
    if (temp <= 300&& hum <= 100){                                    
      Serial.println("temp <= 300");
      update2(DEVICEID,INPUTID1,hum,INPUTID2,temp);
      }
      lastUpdateTime = millis();
   }
  while(serial_stream.available()){
    Serial.println("------ available");
    aJsonObject *msg = aJson.parse(&serial_stream);
    processMessage(msg);
    aJson.deleteItem(msg);
  }
 // client.write("{\"M\":\"checkin\",\"ID\":\"******\",\"K\":\"******\"}\r\n");//登陆设备,修改成自己的ID和key
}

void processMessage(aJsonObject *msg){
  aJsonObject* Methods = aJson.getObjectItem(msg, "M");
  aJsonObject* Content = aJson.getObjectItem(msg, "C");     
  aJsonObject* Client_id = aJson.getObjectItem(msg, "ID");
  String C ="";  
  String F_C_ID = "";
  char* st = aJson.print(msg);        
  if (st != NULL) {
    Serial.println(Methods->valuestring);
    Serial.println(st); 
    free(st);
    String M=(Methods->valuestring);
    Serial.println(M);
    if(Client_id != NULL){
      F_C_ID=Client_id->valuestring;
      Serial.println(F_C_ID);
    }
    if(Content != NULL){
     C =Content->valuestring;
      Serial.println(C);
    }
    if(M=="say" ){
      if(C=="play"){
        sayToClient(F_C_ID,"switch on!"); 
        digitalWrite(4, HIGH);
      }
      if(C=="stop"){
        sayToClient(F_C_ID,"switch off!"); 
        digitalWrite(4, LOW);  
      }
      if (C == "left") {
      }
      if (C == "right") {
      }
    }
    
    if (M == "connected") {
      //checkIn();
    }
  } 
 else {
    Serial.println( "some thing was wrong");
  }
}


void sayToClient(String client_id, String content){
  client.print("{\"M\":\"say\",\"ID\":\"");
  client.print(client_id);
  client.print("\",\"C\":\"");
  client.print(content);
  client.println("\"}");
  lastCheckInTime = millis();
  Serial.print("say to ");    
  Serial.print(client_id);
  Serial.print(":");  
  Serial.println(content);    
}

//上传一个接口数据
//{"M":"update","ID":"2","V":{"2":"120"}}\n
void update1(String did, String inputid, float value) {
  client.print("{\"M\":\"update\",\"ID\":\"");
  client.print(did);
  client.print("\",\"V\":{\"");
  client.print(inputid);
  client.print("\":\"");
  client.print(value);
  client.println("\"}}");
}
//同时上传两个接口数据调用此函数
//{"M":"update","ID":"112","V":{"6":"1","36":"116"}}\n
void update2(String did, String inputid1, float value1, String inputid2, float value2) {
  client.print("{\"M\":\"update\",\"ID\":\"");
  client.print(did);
  client.print("\",\"V\":{\"");
  client.print(inputid1);
  client.print("\":\"");
  client.print(value1);
  client.print("\",\"");
  client.print(inputid2);
  client.print("\":\"");
  client.print(value2);
  client.println("\"}}");
}

//查询设备在线状态
//{"M":"status"}\n
void checkStatus() {
  Serial.print("{\"M\":\"status\"}\n");
  lastCheckStatusTime = millis();
}

//设备登录
//{"M":"checkin","ID":"xx1","K":"xx2"}\n
void checkIn() {
  client.print("{\"M\":\"checkin\",\"ID\":\"");
  client.print(DEVICEID);
  client.print("\",\"K\":\"");
  client.print(APIKEY);
  client.print("\"}\n");
  lastCheckStatusTime = millis();
}
//强制设备下线,用消除设备掉线延时
//{"M":"checkout","ID":"xx1","K":"xx2"}\n
void checkOut() {
  Serial.print("{\"M\":\"checkout\",\"ID\":\"");
  Serial.print(DEVICEID);
  Serial.print("\",\"K\":\"");
  Serial.print(APIKEY);
  Serial.print("\"}\n");
}


评论:共4条

Yogi 评论于:2019-06-14 10:42:20
我沒有用到 checkOut()&checkStatus()這兩個方法,而且在定時checkin方面也許可能再改進一下。DHT11在網上查閱後,據說精度不是太準,好像DHT22會好一些,DHT11庫我也試過其他的庫,還是這種方法比較準確(SimpleDHT.h)
dht11.read(&temperature, &humidity, NULL) 
贝壳物联 回复于:2019-06-15 22:11:32
回复 @Yogi: Thank you for your sharing.
Yogi 回复于:2019-06-19 16:21:48
回复 @贝壳物联: I need to thank you for setting up this website, It's really awesome, I want to buy something on your store, But didn't find what I want, sorry...
michaelcheng 评论于:2019-07-31 12:36:15
挺不错的代码,用到了WIFIClient。
评论支持使用[code][/code]标签添加代码
返回顶部