arduino上传完程序后不在线怎么解决??串口有数据流

作者:奋斗し | 更新时间:2019-04-20 | 浏览量:1822

#include <aJSON.h>
#include <SPI.h>
#include <Wire.h>
#include <dht11.h>

//=============  此处必须修改============
String DEVICEID = "10509"; // 你的设备编号   ==
String  APIKEY = "1adb68719"; // 设备密码==
//String INPUTID1 = "9398"; //接口ID==============
String INPUTID2 = "9398"; //接口ID==============
String INPUTID3 = "9399"; //接口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* parseJson(char *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 update1(String did, String inputid, float value);
void setup() {
  Serial.begin(115200);//ESP 8266 工作波特率
  Wire.begin();
  int chk = DHT11.read(DHT11PIN);
  pinMode(ledPower, OUTPUT);
  delay(5000);
}
void loop() {
  if (millis() - lastCheckInTime > postingInterval || lastCheckInTime == 0) {
    checkIn();
  }
  val = analogRead(0);
  valTemperature = (float)DHT11.temperature;
  valHumidity = (float)DHT11.humidity;
  digitalWrite(ledPower, LOW);
  delayMicroseconds(samplingTime);
  voMeasured = analogRead(measurePin); // read the dust value
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower, HIGH); // turn the LED off
  delayMicroseconds(sleepTime);

  calcVoltage = voMeasured * (5.0 / 1024.0);

  dustDensity = 0.17 * calcVoltage - 0.1;
  if ((millis() - lastUpdateTime >= updateInterval) && isCheckIn)
  {
    //update1(DEVICEID, INPUTID1, val); //MQ_2
    update1(DEVICEID, INPUTID2, valTemperature);
    update1(DEVICEID, INPUTID3, valHumidity);
   // update1(DEVICEID, INPUTID4, dustDensity);
    ///************************
  }
  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) {
    isCheckIn = false;
    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();
    isCheckIn = true;
  }
}
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;
  if (M == "say") {
    if (C == "play") {
      // digitalWrite(LED, HIGH);
      sayToClient(F_C_ID, "LED on!");
    }
    if (C == "stop") {
      //  digitalWrite(LED, LOW);
      sayToClient(F_C_ID, "LED off!");
    }
    if (C == "wendu") {
      sayToClient(F_C_ID, String(valTemperature));
    }
    if (C == "shidu") {
      sayToClient(F_C_ID, String(valHumidity));
    }
    if (C == "mq2") {
      sayToClient(F_C_ID, String(val));
    }
    if (C == "pm") {
      sayToClient(F_C_ID, String(dustDensity));
    }
  }
}
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 serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
//上报数据  拼接json
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 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();
}


评论:共6条

799243768 评论于:2019-04-20 15:47:14
硬件连接图是怎么样的
奋斗し 回复于:2019-04-21 01:33:51
回复 @799243768:就是0.1Wi-Fi 2dht11用的uno板,其他没接
799243768 回复于:2019-04-21 13:08:17
回复 @奋斗し:WiFi模块有配置好连上路由器并设置为cilent吗
奋斗し 评论于:2019-04-21 21:07:53
我发现每次重连必须设置一遍透传
alilong1200 评论于:2019-04-24 17:47:40
能加下您的QQ吗,我目前也做这个,不过用的esp8266
奋斗し 评论于:2019-04-24 23:59:27
用这个代码就好了
返回顶部