作者:坐摇摇椅的老道 | 更新时间:2020-05-16 | 浏览量:1189
设备显示在线但是数据上传不上,是代码哪里有问题么?
#include <WiFi.h>
#include <aJSON.h>
const char* ssid = "Kkk";
const char* password = "95101010";
const char* host = "www.bigiot.net";
const int httpPort = 8181;
const int ya = 35;
int dat;
unsigned long lastCheckInTime = 0; //记录上次报到时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
const unsigned long updateInterval = 5000;
unsigned long lastUpdateTime = 0;
//============= 此处必须修该============
String inputid1="16103"; // 你的设备编号 ==
String DEVICEID="17982"; // 你的设备编号 ==
String APIKEY = "99942b8f4"; // 设备密码==
//=======================================
void setup()
{
Serial.begin(115200);
delay(10);
// mPython.begin();
// We start by connecting to a WiFi network
pinMode(ya,INPUT);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
delay(3000);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("connected: OK");
delay(1000);
}
WiFiClient client;
void loop()
{
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
// Use WiFiClient class to create TCP connections
if (!client.connected()) {
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
delay(5000);
return;
}
}
if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
if(millis() - lastUpdateTime > updateInterval) {
//get 18b20 data
// sensors.requestTemperatures();
dat = analogRead(ya);
update1(DEVICEID, inputid1, dat);
}
// Read all the lines of the reply from server and print them to Serial
if (client.available()) {
String inputString = client.readStringUntil('\n');
inputString.trim();
Serial.println(inputString);
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);
}
}
}
void processMessage(aJsonObject *msg){
aJsonObject* method = aJson.getObjectItem(msg, "M");
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
if (!method) {
return;
}
}
void checkIn() {
String msg = "{\"M\":\"checkin\",\"ID\":\"" + DEVICEID + "\",\"K\":\"" + APIKEY + "\"}\n";
client.print(msg);
lastCheckInTime = millis();
}
void update1(String did, String inputid1, float value1){
Serial.print("{\"M\":\"update\",\"ID\":\"");
Serial.print(did);
Serial.print("\",\"V\":{\"");
Serial.print(inputid1);
Serial.print("\":\"");
Serial.print(value1);
Serial.println("\"}}");
lastCheckInTime = millis();
lastUpdateTime= millis();
}