|
想請問各位前輩,以下的CODE哪裡有寫錯,以至於無法上傳到MCS
#include <LGPS.h>
#include "DHT.h"
#define DHTPIN 2 // 接在 D2 腳位
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); // 建立 DHT 物件
#include <HttpClient.h>
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <LDateTime.h>
#define WIFI_AP "889" //AP名稱
#define WIFI_PASSWORD "sf000093" //AP密碼
#define WIFI_AUTH LWIFI_WPA //加密方式為WPA
#define intervalBeat 60 //重新與MCS連線時間,單位為秒
#define intervalUpload 10 //上傳資料給MCS的時間,單位為秒
#define DEVICEID "Djzuvui2"
#define DEVICEKEY "Gb3pEfKMoYx5NJJv"
#define SITE_URL "api.mediatek.com"
gpsSentenceInfoStruct info; //GPS所有資訊
float h,t;
char buff[256], monobuf[20];
double latitude, longitude, altitude; //緯度,經度,高度
int satellite; //徫星數
char buffer_latitude[8], buffer_longitude[8], buffer_altitude[7],buffer_satellite[3];
LWiFiClient client;
LWiFiClient client2;
HttpClient http(client2);
unsigned int rtcBeat; //用於重新與MCS連線時間
unsigned int lrtcBeat;
unsigned int rtcUpload; //用於上傳資料給MCS的時間
unsigned int lrtcUpload;
char port[4]={0}; //埠號(字元陣列)
int portnum; //埠號(數值)
char connection_info[21]={0}; //連線資料
char ip[21]={0}; //IP位址
String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0"; //MCS裝置資料
void setup() {
LTask.begin();
LWiFi.begin();
LGPS.powerOn(); //啟動GPS
dht.begin(); // DHT 初始化
Serial.begin(9600);
Serial1.begin(9600);
while(!Serial) delay(1000); //除錯用
Serial.println("Connecting to AP");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { //每秒連線一次
delay(1000);
}
Serial.println("calling connection");
while (!client2.connect(SITE_URL, 80)) { //每秒連接MCS網站一次
Serial.println("Re-Connecting to WebSite");
delay(1000);
}
delay(100);
getconnectInfo(); //取得連線資訊
connectTCP(); //建立TCP連線
}
void getconnectInfo(){ //取得連線資訊
//以RESTful API方式建立連線
client2.print("GET /mcs/v2/devices/");
client2.print(DEVICEID);
client2.println("/connections.csv HTTP/1.1");
client2.print("Host: ");
client2.println(SITE_URL);
client2.print("deviceKey: ");
client2.println(DEVICEKEY);
client2.println("Connection: close");
client2.println();
delay(500);
int errorcount = 0; //計數器
while (!client2.available()) {
Serial.println("waiting HTTP response: ");
errorcount += 1;
if (errorcount > 10) { //超過2秒
Serial.println("HTTP not response!");
client2.stop();
return;
}
delay(200);
}
int err = http.skipResponseHeaders(); //忽略表頭部分
int bodyLen = http.contentLength(); //傳送資料長度
char ch;
int ipcount = 0;
int count = 0;
int separater = 0;
while(client2) { //讀取MCS傳送過來的資料:IP及PORT
int v = client2.read();
if (v != -1) {
ch = v;
Serial.print(ch);
connection_info[ipcount]=ch;
if(ch==',') //逗點前為IP,逗點後為PORT
separater=ipcount;
ipcount++;
} else {
Serial.println("no more content, disconnect");
client2.stop();
}
}
Serial.print("The connection info: ");
Serial.println(connection_info);
int i;
for(int i=0;i<separater;i++) { //取得IP
ip[i]=connection_info[i];
}
int j=0;
separater++;
for(i=separater;i<21 && j<5;i++) { //取得PORT
port[j]=connection_info[i];
j++;
}
Serial.print("IP: ");
Serial.println(ip);
Serial.print("Port: ");
Serial.println(port);
portnum = atoi(port);
}
void connectTCP(){ //建立TCP連線
client.stop(); //關閉原有連線
Serial.println("Connecting to TCP");
while (client.connect(ip, portnum) == 0) {
Serial.println("Re-Connecting to TCP");
delay(1000);
}
Serial.println("send TCP connect");
client.println(tcpdata); //傳送MCS裝置資料
client.println();
Serial.println("waiting TCP response:");
}
void loop() {
//每60秒重新連結MCS網頁一次
LDateTime.getRtc(&rtcBeat); //取得目前時間
if((rtcBeat - lrtcBeat) >= intervalBeat) { //大於等於設定時間
heartBeat();
lrtcBeat = rtcBeat; //記錄前次連線時間
}
//每10秒傳送資料給MCS一次
LDateTime.getRtc(&rtcUpload);
if ((rtcUpload - lrtcUpload) >= intervalUpload) {
LGPS.getData(&info); //取得GPS資訊
parseGPGGA((const char*)info.GPGGA); //解析GPS資料
uploadGPS(); //上傳資料給MCS
lrtcUpload = rtcUpload;
}
float t = 0.0;
float h = 0.0;
if(dht.readHT(&t, &h)) { // 讀取溫度、濕度
Serial.print("temperature = ");
Serial.println(t);
uploadGPS();
Serial.print("humidity = ");
Serial.println(h);
uploadGPS();
}
delay(10000);
}
void heartBeat(){ //連結MCS網頁
Serial.println("send TCP heartBeat");
client.println(tcpdata);
client.println();
}
void parseGPGGA(const char* GPGGAstr) { //解析GPS資料
int tmp;
if(GPGGAstr[0] == '$') { //第一個字元為「$」才是GPS資料
tmp = getComma(2, GPGGAstr); //取得第3個欄位資料:緯度
latitude = getDoubleNumber(&GPGGAstr[tmp])/100.0; //緯度整數部分
int latitude_int=floor(latitude);
double latitude_decimal=(latitude-latitude_int)*100.0/60.0; //緯度小數部分
latitude=latitude_int+latitude_decimal;
tmp = getComma(4, GPGGAstr); //取得第5個欄位資料:經度
longitude = getDoubleNumber(&GPGGAstr[tmp])/100.0; //經度整數部分
int longitude_int=floor(longitude);
double longitude_decimal=(longitude-longitude_int)*100.0/60.0; //經度小數部分
longitude=longitude_int+longitude_decimal;
tmp = getComma(9, GPGGAstr); //取得第10個欄位資料:高度值
altitude = getDoubleNumber(&GPGGAstr[tmp]);
tmp = getComma(7, GPGGAstr); //取得第8個欄位資料:衛星數
satellite = getIntNumber(&GPGGAstr[tmp]);
} else {
Serial.println("Not get data");
}
}
static unsigned char getComma(unsigned char num,const char *str) { //取得第num個逗點欄位起始位置
unsigned char i,j = 0;
int len=strlen(str);
for(i = 0;i < len;i ++) {
if(str[i] == ',') j++;
if(j == num) return i + 1;
}
return 0; //若無逗點則傳回0
}
static double getDoubleNumber(const char *s) { //傳回浮點數
getMonoValue(s);
return atof(monobuf);
}
static int getIntNumber(const char *s) { //傳回整數
getMonoValue(s);
return atoi(monobuf);
}
static void getString(const char *s) { //傳回字元陣列
getMonoValue(s);
}
void getMonoValue(const char *s) { //配合getComma取得單一欄資料
unsigned char i;
i=getComma(1, s);
i = i - 1;
strncpy(monobuf, s, i); //欄位資料置於整體變數monobuf
monobuf[i] = 0;
}
void uploadGPS() {
Serial.printf("latitude=%.4f\tlongitude=%.4f\n",latitude,longitude); //顯示經緯度
Serial.printf("Altitude : %.1f", altitude); //顯示高度
Serial.printf("satellites number : %d", satellite); //顯示徫星數
Serial.printf("temperature : %.2f", t);
Serial.printf("humidity : %.2f", h);
if(latitude>-90 && latitude<90 && longitude>0 && longitude<360) {
sprintf(buffer_latitude, "%.4f", latitude);
sprintf(buffer_longitude, "%.4f", longitude);
}
sprintf(buffer_satellite, "%d", satellite);
sprintf(buffer_altitude, "%.1f", altitude);
String upload_GPS = "GPS,," + String(buffer_latitude) + "," + String(buffer_longitude) + ",0\n"; //將GPS資料加入上傳字串
upload_GPS += "latitude,," + String(buffer_latitude) + "\n" + "longitude,," + String(buffer_longitude) + "\n"; //加入經緯度資料
upload_GPS += "satellite,," + String(buffer_satellite) + "\n" + "altitude,," + String(buffer_altitude)+ "\n"; //加入徫星數及高度
upload_GPS += "temperature,," + String(t) + "\n" + "humidity,," + String(h)+ "\n";
//上傳資料給MCS
int GPS_length = upload_GPS.length();
while (!client2.connect(SITE_URL, 80)) {
Serial.println("Re-Connecting to WebSite");
delay(1000);
}
delay(100);
client2.print("POST /mcs/v2/devices/");
client2.print(DEVICEID);
client2.println("/datapoints.csv HTTP/1.1");
client2.print("Host: ");
client2.println(SITE_URL);
client2.print("deviceKey: ");
client2.println(DEVICEKEY);
client2.print("Content-Length: ");
client2.println(GPS_length);
client2.println("Content-Type: text/csv");
client2.println("Connection: close");
client2.println();
client2.println(upload_GPS);
delay(500);
int errorcount = 0;
while (!client2.available()) {
Serial.print("waiting HTTP response: ");
errorcount += 1;
if (errorcount > 10) {
Serial.println("HTTP not response!");
client2.stop();
return;
}
delay(200);
}
} |
|