arduino上传温度和发送指令

作者:一声叹息 | 更新时间:2020-12-27 | 浏览量:1132

#include <aJSON.h>
#include <RSCG12864B.h>//引用库文件
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

//=============  此处必须修改============
String DEVICEID = "19567"; // 你的设备ID=======
String APIKEY = "1008154e0"; // 设备密码==
String INPUTID = "17560"; //接口ID==============
//=======================================

// 数据输出脚接开发板数字引脚2(温度传感器)
#define ONE_WIRE_BUS 2 
char Temp_Buffer[6];
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 float T_sum;

int incomedate = 0;
int relayPin = 8; //继电器引脚

int IN1 = 4;   // IN1 connected to pin 4
int IN2 = 5;   
int ENA = 3;  

int IN3 = 6;   // IN3 connected to pin6
int IN4 = 7;   
int ENB = 8;  
unsigned long time = 1200;  //delay time
int value = 100;   // the duty cycle(0-255)

unsigned long lastCheckStatusTime = 0; //记录上次报到时间
unsigned long lastUpdateTime = 0;//记录上次上传数据时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
const unsigned long updateInterval = 5000; // 数据上传间隔时间5秒
unsigned long checkoutTime = 0;//登出时间

void setup() {
  Serial.begin(115200);

  pinMode(relayPin, OUTPUT);

   RSCG12864B.begin();//串口初始化
   RSCG12864B.brightness(255);//屏幕亮度
  sensors.begin(); //初始化总线
  
  RSCG12864B.clear();

   pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
     pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);

  delay(5000);//等一会儿ESP8266

 
}
void loop() {

   // put your main code here, to run repeatedly:
   //当前温度为:B5 B1 C7 B0 CE C2 B6 C8 CE AA A3 BA   
   char chn[]={char(0xB5), char(0xB1),char(0xC7),char(0xB0),char(0xCE),char(0xC2),char(0xB6),char(0xC8),char(0xCE),char(0xAA),char(0xA3),char(0xBA),0,};//当前温度为:

  sensors.requestTemperatures(); // 发送命令获取温度,
  //向总线上所有设备发送温度转换请求,默认情况下该方法会阻塞
  dtostrf(sensors.getTempCByIndex(0),5,2,Temp_Buffer); // float 到 char
   RSCG12864B.print_string_12_xy(14,13,chn);//坐标16,15,显示chn里的内容(当前温度为:) 
   RSCG12864B.print_string_12(Temp_Buffer);//显示温度
  delay(500); 

  //每一定时间查询一次设备在线状态,同时替代心跳
  if (millis() - lastCheckStatusTime > postingInterval)
      {
        checkStatus();
      }
  //checkout 50ms 后 checkin
  if ( checkoutTime != 0 && millis() - checkoutTime > 50 )
      {
        checkIn();
        checkoutTime = 0;
      }
      
  //每隔一定时间上传一次数据
  if (millis() - lastUpdateTime > updateInterval)
      {
        update1(DEVICEID, INPUTID, sensors.getTempCByIndex(0)); //上传温度
        lastUpdateTime = millis();
      }
      
  //读取串口信息
  while (Serial.available()) {
    String inputString = Serial.readStringUntil('\n');
    //检测json数据是否完整
    int jsonBeginAt = inputString.indexOf("{");
    int jsonEndAt = inputString.lastIndexOf("}");
    if (jsonBeginAt != -1 && jsonEndAt != -1) {
      //净化json数据
      inputString = inputString.substring(jsonBeginAt, jsonEndAt + 1);
      int len = inputString.length() + 1;
      char jsonString[len];
      inputString.toCharArray(jsonString, len);
      aJsonObject *msg = aJson.parse(jsonString);
      processMessage(msg);
      aJson.deleteItem(msg);
    }
  }
}


//处理网络接收到到指令,执行相关动作
void processMessage(aJsonObject *msg) {
  aJsonObject* method = aJson.getObjectItem(msg, "M");
  if (!method) 
  {
    return;
  }
  String M = method->valuestring;
  if (M == "WELCOME TO BIGIOT") {
   // checkOut();
    checkoutTime = millis();
    return;
  }
  if (M == "connected") {
    checkIn();
  }
 if (M == "say") {
    aJsonObject* content = aJson.getObjectItem(msg, "C");
    aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
    String C = content->valuestring;
    String F_C_ID = client_id->valuestring;
    if (C == "startdc")     //发送指令 playdc  ,直流电机启动
    {
         digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
        analogWrite(ENA, value);
        delay(time);
      say(F_C_ID, "d-c generator start!");
    }
    if (C == "stopdc") //发送指令 stopdc  ,直流电机停止
    {
         digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(ENA, 0);
    delay(time);
      say(F_C_ID, "d-c generator stop!");
    }
    
    if (C == "waterin") {
         digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
        delay(time);
      say(F_C_ID, "正在抽水进来!");
    }
    if (C == "waterout") {
         digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
    delay(time);
      say(F_C_ID, "正在抽水出去!");
    }
    if (C == "waterstop")
    {
         digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW); 
    delay(time);
      say(F_C_ID, "抽水已经停止!");

    }
        if (C == "Templay")
     {
      digitalWrite(relayPin, LOW);
      say(F_C_ID, "relay 开始启动!");
   
    }
    if (C == "Temstop")
    {
     digitalWrite(relayPin, HIGH);
      say(F_C_ID, "relay Stop!");
    }
}
    
  }


//发送say指令,用于设备与用户、设备与设备间通讯
//{"M":"say","ID":"xx1","C":"xx2","SIGN":"xx3"}\n
void say(String ID, String c) {
  Serial.print("{\"M\":\"say\",\"ID\":\"");
  Serial.print(ID);
  Serial.print("\",\"C\":\"");
  Serial.print(c);
  Serial.print("\"}\r\n");
}
//设备登录
//{"M":"checkin","ID":"xx1","K":"xx2"}\n
void checkIn() {
  Serial.print("{\"M\":\"checkin\",\"ID\":\"");
  Serial.print(DEVICEID);
  Serial.print("\",\"K\":\"");
  Serial.print(APIKEY);
  Serial.print("\"}\n");
}
//强制设备下线,用消除设备掉线延时
//{"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");
}

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

//上传一个接口数据
//{"M":"update","ID":"2","V":{"2":"120"}}\n
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("\"}}");
}
//同时上传两个接口数据调用此函数
//{"M":"update","ID":"112","V":{"6":"1","36":"116"}}\n
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("\"}}");
  
}
 


评论:共1条

一声叹息 评论于:2020-12-27 00:39:13
可以上传温度,但是不能接收指令, 两部分分开可以实现功能,整合在一起就不行。有大神遇到过相似问题吗? 请指点一下我。 谢谢。 qq:1209337072,
返回顶部