作者:潇潇123 | 更新时间:2018-04-14 | 浏览量:1721
弄了挺久的,一直搞不定…求帮忙看看是为什么
串口助手一直显示这个:
{"M":"checkin","ID":"5438","K":"a3487a976"}
{"M":"update","ID":"5438","V":{"4888":"76.77"}}
{"M":"update","ID":"5438","V":{"4888":"106.55"}}
{"M":"update","ID":"5438","V":{"4888":"63.75"}}
{"M":"update","ID":"5438","V":{"4888":"72.59"}}
这是烧录在arduino的代码:
/*
*注意 :Arduino IDE版本请使用1.6.5-1.8.2
*/
#include <aJSON.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //设置模拟串口针脚(RX, TX)
//============= 此处必须修改============
String DEVICEID="5438"; // 你的设备ID=======
String APIKEY="a3487a976"; // 设备密码==
String INPUTID="4888";//接口ID==============
//=======================================
const int MQ2 = 0;// MQ-2 Vout 接 A0
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* parseJson(char *jsonString);//定义aJson字符串
void setup() {
Serial.begin(115200);
delay(10000);
}
void loop() {
if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
if(millis() - lastUpdateTime > updateInterval) {
float val;//定义变量
int dat;//定义变量
dat=analogRead( MQ2 );// 读取传感器的模拟值并赋值给dat
//val=(125*dat)>>8;//温度计算公式
val = dat * (4.76 / 1023.0*100);
update1(DEVICEID,INPUTID,val);
}
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();
}