Demo entry 6607017

no

   

Submitted by XCR on Jun 05, 2017 at 12:25
Language: C. Code size: 7.9 kB.

#include <aJSON.h>
//=============  此处必须修改============
String DEVICEID = "1723"; // 你的设备ID=======
String APIKEY = "96386fb81"; // 设备密码==
String INPUTID = "1708"; //接口ID1==============
String INPUTID2 = "1927"; //接口ID2==============
String INPUTID3 = "1928"; //接口ID3==============
String INPUTID4 = "1929"; //接口ID4==============
//=======================================
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* parseJsoninit(char *jsonString);//定义aJson字符串

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2);
unsigned int PM1 = 0;
unsigned int PM2 = 0;
unsigned int PM10 = 0;
unsigned int HOCO = 0;
unsigned char gState_1 = 0;//通信模块
unsigned char RecieveData_1[50] = {0x00};//接收缓存
unsigned short Lib_1 = 0;//接收指针
unsigned char SerialBusy_1 = 0; //串口处理忙标志
unsigned char OperationFlag_1 = 0; //一帧数据串口接收完标志
unsigned char checkingflag = 0;
void setup()
{
  Serial.begin(9600);
  Serial1.begin(115200);
  lcd.();
  lcd.backlight();
  Serial2.begin(9600);
  delay(2000);
}
void loop()
{
  //if (millis() - lastCheckInTime > postingInterval ) {
  //Serial.println("begin check in");
  //checkIn();
  //}
  if ((millis() - lastUpdateTime > updateInterval)) {
    update4(DEVICEID, INPUTID, PM1, INPUTID2, PM2, INPUTID3, PM10, INPUTID4, HOCO);
    checkingflag++;
    if (checkingflag > 8)
    {
      checkingflag = 0;
      Serial.println("begin check in");
      checkIn();
    }
  }
  serialEvent1();
  if (stringComplete) {
    inputString.trim();
    //Serial.println(inputString);
    if (inputString == "CLOSED") {
      Serial1.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;
  }
  if (OperationFlag_1)//解析通信模块的指令
  {
    unsigned int sumcheck = 0;
    unsigned char sumcheckHigh = 0x00;
    unsigned char suncheckLow = 0x00;
    sumcheck = SUMCheck(RecieveData_1, 38);
    sumcheckHigh = sumcheck >> 8;
    suncheckLow = sumcheck;
    if (RecieveData_1[38] == sumcheckHigh && RecieveData_1[39] == suncheckLow)
    {
      PM1 = RecieveData_1[4] * 256 + RecieveData_1[5];
      PM2 = RecieveData_1[6] * 256 + RecieveData_1[7];
      PM10 = RecieveData_1[8] * 256 + RecieveData_1[9];
      HOCO = RecieveData_1[28] * 256 + RecieveData_1[29];
      FormatDisplay(PM1, PM2, PM10, HOCO);
    }
    OperationFlag_1 = 0;
    SerialBusy_1 = 0;
    gState_1 = 0;
    Lib_1 = 0;
    for (unsigned short i = 0; i < 50; i++)
    {
      RecieveData_1[i] = 0;
    }
  }


}
void FormatDisplay(unsigned int pm1, unsigned int pm2, unsigned int pm10, unsigned int hoco)
{
  lcd.setCursor(0, 0);
  lcd.print("1=");
  if (pm1 < 10)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(pm1);
  }
  else if (pm1 < 100)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(pm1);
  }
  else if (pm1 < 1000)
  {
    lcd.print(' ');
    lcd.print(pm1);
  }
  else
  {
    lcd.print(pm1);
  }
  lcd.setCursor(10, 0);
  lcd.print("2=");
  if (pm2 < 10)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(pm2);
  }
  else if (pm2 < 100)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(pm2);
  }
  else if (pm2 < 1000)
  {
    lcd.print(' ');
    lcd.print(pm2);
  }
  else
  {
    lcd.print(pm2);
  }
  lcd.setCursor(0, 1);
  lcd.print("3=");
  if (pm10 < 10)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(pm10);
  }
  else if (pm10 < 100)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(pm10);
  }
  else if (pm10 < 1000)
  {
    lcd.print(' ');
    lcd.print(pm10);
  }
  else
  {
    lcd.print(pm10);
  }
  lcd.setCursor(10, 1);
  lcd.print("4=");
  if (hoco < 10)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(hoco);
  }
  else if (hoco < 100)
  {
    lcd.print(' ');
    lcd.print(' ');
    lcd.print(hoco);
  }
  else if (hoco < 1000)
  {
    lcd.print(' ');
    lcd.print(hoco);
  }
  else
  {
    lcd.print(hoco);
  }
}
void serialEvent2()
{
  unsigned char inChar = Serial2.read();
  if (SerialBusy_1)
  {
    return ;
  }
  switch (gState_1)
  {
    case 0x00:
      {
        if (inChar == 0x42)
        {
          gState_1 = 0x01;
          Lib_1 = 0;
          RecieveData_1[0] = inChar;
        }
        else
        {
          Lib_1 = 0;
          gState_1 = 0x00;
        }
        break;
      }
    case 0x01:
      {
        if (inChar == 0x4D)
        {
          RecieveData_1[1] = inChar;
          gState_1 = 0x02;
          Lib_1 = 1;
        }
        else
        {
          Lib_1 = 0;
          gState_1 = 0x00;
        }
        break;
      }
    case 0x02:
      {
        RecieveData_1[++Lib_1] = inChar;
        if (Lib_1 > 38)
        {
          SerialBusy_1 = 1;
          OperationFlag_1 = 1;
        }
      }
  }
}

unsigned int SUMCheck(unsigned char *message, unsigned short len)
{
  unsigned int temp = 0x00;
  for (unsigned short i = 0; i < len; i++)
  {
    temp += (*(message + i));
  }
  return temp;
}
void checkIn() {
  if (!CONNECT) {
    Serial1.print("+++");
    delay(500);
    Serial1.print("\r\n");
    delay(1000);
    Serial1.print("AT+RST\r\n");
    delay(6000);
    CONNECT = true;
    //lastCheckInTime == 0;
    Serial.println("check in fail");
  }
  else {
    Serial1.print("{\"M\":\"checkin\",\"ID\":\"");
    Serial1.print(DEVICEID);
    Serial1.print("\",\"K\":\"");
    Serial1.print(APIKEY);
    Serial1.print("\"}\r\n");
    Serial.println("check in success");
  }
  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) {
  Serial1.print("{\"M\":\"update\",\"ID\":\"");
  Serial1.print(did);
  Serial1.print("\",\"V\":{\"");
  Serial1.print(inputid);
  Serial1.print("\":\"");
  Serial1.print(value);
  Serial1.println("\"}}");
  //lastCheckInTime = millis();
  lastUpdateTime = millis();
  Serial.println("begin upload");
}
void serialEvent1() {
  while (Serial1.available()) {
    char inChar = (char)Serial1.read();
    Serial.print(inChar);
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
//同时上传4个接口数据调用此函数
void update4(String did, String inputid1, float value1, String inputid2, float value2, String inputid3, float value3, String inputid4, float value4) {
  Serial1.print("{\"M\":\"update\",\"ID\":\"");
  Serial1.print(did);
  Serial1.print("\",\"V\":{\"");
  Serial1.print(inputid1);
  Serial1.print("\":\"");
  Serial1.print(value1);
  Serial1.print("\",\"");
  Serial1.print(inputid2);
  Serial1.print("\":\"");
  Serial1.print(value2);
  Serial1.print("\",\"");
  Serial1.print(inputid3);
  Serial1.print("\":\"");
  Serial1.print(value3);
  Serial1.print("\",\"");
  Serial1.print(inputid4);
  Serial1.print("\":\"");
  Serial1.print(value4);
  Serial1.println("\"}}");
  lastCheckInTime = millis();
  lastUpdateTime = millis();
  Serial.println("begin upload");
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).