Files
gaoguangpu/src/gsmm_http.cpp
2026-01-15 15:00:21 +08:00

341 lines
10 KiB
C++

#include "gsmm_http.h"
// #define SerialAT Serial2 // 使用 UART1 连接 4G 模块
// void sendAT(String cmd, uint16_t delayMs = 500) {
// SerialAT.println(cmd);
// vTaskDelay(delayMs);
// while (SerialAT.available()) {
// Serial0.write(SerialAT.read()); // 打印模块返回
// }
// }
// void httpUpload(float temp, float hum)
// {
// sendAT("AT+HTTPINIT");
// sendAT("AT+HTTPPARA=\"CID\",1");
// sendAT("AT+HTTPPARA=\"URL\",\"http://tower.iris-rs.cn/HyperspectralV2/DataUpload.php\"");
// String json = "{\"temperature\":" + String(temp) + ",\"humidity\":" + String(hum) + "}";
// sendAT("AT+HTTPDATA=" + String(json.length()) + ",10000");
// SerialAT.print(json);
// SerialAT.write(0x1A);
// vTaskDelay(2000);
// sendAT("AT+HTTPACTION=1"); // 1 = POST
// vTaskDelay(3000);
// sendAT("AT+HTTPREAD");
// sendAT("AT+HTTPTERM");
// }
// HttpClient *http;
// // #ifdef GPS_4G
// // //初始化AIR780EG
// // http_gsmmanger = new GSMMannger(SIMUARTNUMER, SIMUART_RX, SIMUART_TX);
// // http = new HttpClient(*http_gsmmanger->client, "tower.iris-rs.cn");
// // String Date = getnetData();
// // write_log(log_path,"date is :"+Date,10);
// // gps_struct gps_structure;
// // String gpsbac = get_GPS(&gps_structure);
// // if (gpsbac != "-1")
// // {
// // // calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset);
// // }
// // else
// // {
// // write_log(log_path,"no gps",10);
// // }
// // #endif
// GSMMannger *http_gsmmanger;
// void gsmm_http_init()
// {
// http_gsmmanger = new GSMMannger(SIMUARTNUMER, SIMUART_RX, SIMUART_TX);
// http = new HttpClient(*http_gsmmanger->http_client, "tower.iris-rs.cn");
// String Date = getnetData();
// write_log(log_path,"date is :"+Date,10);
// gps_struct gps_structure;
// String gpsbac = get_GPS(&gps_structure);
// }
// String getnetData()
// {
// write_log(log_path,"start get net data",10);
// int err = http->get("/weather/php/Date.php");
// if (err != 0)
// {
// return "-1";
// }
// write_log(log_path,"responseStatusCode",10);
// int status = http->responseStatusCode();
// if (!status)
// {
// return "-1";
// }
// int times_try = 0;
// write_log(log_path,"headerAvailable",10);
// while (http->headerAvailable())
// {
// if(times_try > 100)
// {
// http->stop();
// return "-1";
// }
// write_log(log_path,"readHeaderName",10);
// String headerName = http->readHeaderName();
// write_log(log_path,"readHeaderValue",10);
// String headerValue = http->readHeaderValue();
// vTaskDelay(100);
// times_try++;
// }
// write_log(log_path,"isResponseChunked",10);
// if (http->isResponseChunked())
// {
// }
// write_log(log_path,"contentLength",10);
// String body = http->responseBody();
// http->stop();
// return body;
// }
// // /////////////////////////////////上传数据/////////////////////////////////////
// bool UpdateData(String path,uint8_t *data, size_t lenth, String Contenttype )
// {
// // http_flag = 1;
// // vTaskDelay(20);
// http->beginRequest();
// http->post(path);
// if (Contenttype != "")
// {
// http->sendHeader(HTTP_HEADER_CONTENT_TYPE, Contenttype);
// /* code */
// }
// http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, lenth);
// http->endRequest();
// int err = http->write((const byte *)data, lenth);
// vTaskDelay(1000);
// Serial0.print("send date size");
// Serial0.println(err);
// int status = http->responseStatusCode();
// int length = http->contentLength();
// String body = http->responseBody();
// Serial0.println("body:"+body);
// http->stop();
// vTaskDelay(10);
// // http_flag = 0;
// if((err == 0 )|| (!status) || (length == 0) || (body != "ok"))
// {
// return false;
// }
// return true;
// }
// void ReuploadData(String path, String webpath, String content = "")
// {
// vTaskDelay(1);
// Vector<String> files;
// String vec[20]; //每次处理20个
// files.setStorage(vec);
// while (!sdcard::ListDir(path.c_str(), files))
// {
// if (files.size() != 0)
// {
// Serial.println("find " + String(files.size()) + "file");
// for (size_t i = 0; i < files.size(); i++)
// {
// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb");
// size_t size = nowfile.size();
// char *arr = new char[size];
// nowfile.readBytes(arr, size);
// bool flagsucc = UpdateData(webpath, (uint8_t *)arr, size, content);
// if (!flagsucc)
// {
// return;
// /* code */
// }
// delete[] arr;
// sdcard::deleteFolderOrFile(files.at(i).c_str());
// vTaskDelay(1);
// /* code */
// }
// }
// vTaskDelay(1);
// }
// if (files.size() != 0)
// {
// Serial.println("find " + String(files.size()) + "file not enough 20");
// for (size_t i = 0; i < files.size(); i++)
// {
// vTaskDelay(1);
// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb");
// size_t size = nowfile.size();
// char *arr = new char[size];
// nowfile.readBytes(arr, size);
// Serial.println("run here now");
// bool flagsucc = UpdateData(webpath, (uint8_t *)arr, size, content);
// if (!flagsucc)
// {
// return;
// /* code */
// }
// delete[] arr;
// sdcard::deleteFolderOrFile(files.at(i).c_str());
// /* code */
// }
// }
// vTaskDelay(1);
// }
///////////////////////////////////////////////////////////////////////////////////////////////////
// String get_GPS(gps_struct *gps)
// {
// // write_log(log_path,"start get GPS ",10);
// // String gpsbac,Date,temp,Latitude,Longitude;
// // http_gsmmanger->modem->sendAT(GF("+CGNSPWR?"));
// // http_gsmmanger->modem->waitResponse("OK");
// // http_gsmmanger->modem->sendAT(GF("+CGNSPWR=1"));
// // http_gsmmanger->modem->waitResponse("OK");
// // http_gsmmanger->modem->sendAT(GF("+CGNSAID=31,1,1,1"));
// // http_gsmmanger->modem->waitResponse("OK");
// // http_gsmmanger->modem->sendAT(GF("+CGNSINF"));
// // //获取gps信息
// // http_gsmmanger->modem->waitResponse(5000,gpsbac);
// // write_log(log_path,"gpsbac is " + gpsbac,10);
// // //检测是否有效
// // temp = gpsbac.substring(16,17);
// // // write_log(log_path,"hahaha :" + temp,10);
// // if(temp != "1")
// // {
// // // write_log(log_path,"gps error,hahahaha",10);
// // return "-1";
// // }
// // //截取时间
// // Date = gpsbac.substring(18,32);
// // temp = Date.substring(0,4) + "-" + Date.substring(4,6) + "-" +Date.substring(6,8) + " " + Date.substring(8,10) + ":" +Date.substring(10,12) + ":" +Date.substring(12,-1);
// // gps->year = Date.substring(0,4).toInt();
// // gps->month = Date.substring(4,6).toInt();
// // gps->day = Date.substring(6,8).toInt();
// // gps->hour = Date.substring(8,10).toInt();
// // gps->minute = Date.substring(10,12).toInt();
// // gps->second = Date.substring(12,-1).toInt();
// // Date = temp;
// // // +CGNSINF: 1,1,20240430052525,40.040938,116.328013,51.900,0.26,0.00,3,,2.14,2.42,4.00,,10,8,,,34,,
// // // OK
// // ////////获取经纬度//////////
// // temp = gpsbac.substring(33);
// // // write_log(log_path,"Longitude is :" + temp,10);
// // int pos = temp.indexOf(",");
// // Latitude = temp.substring(0,pos);
// // gps->latitude = Latitude.toFloat();
// // // write_log(log_path,"Latitude ::::" + Latitude,10);
// // temp = temp.substring(pos+1);
// // // write_log(log_path,"Longitude is :" + temp,10);
// // pos = temp.indexOf(",");
// // Longitude = temp.substring(0,pos);
// // gps->longitude = Longitude.toFloat();
// // // write_log(log_path,"Longitude ::::" + Longitude,10);
// // if(Date.indexOf(",") != -1)
// // {
// // return "-1";
// // }
// // return Date;
// //////////////////////////////////////////////////////////////////////////////
// String gpsbac;
// write_log(log_path,"start get GPS ",10);
// http_gsmmanger->modem->sendAT(GF("+QGPS=1"));
// http_gsmmanger->modem->waitResponse("OK");
// http_gsmmanger->modem->sendAT(GF("+QGPSLOC=0"));
// // http_gsmmanger->modem->waitResponse("OK");
// //获取gps信息
// http_gsmmanger->modem->waitResponse(5000,gpsbac);
// write_log(log_path,"gpsbac is " + gpsbac,10);
// // //检测是否有效
// // temp = gpsbac.substring(16,17);
// // // write_log(log_path,"hahaha :" + temp,10);
// // if(temp != "1")
// // {
// // // write_log(log_path,"gps error,hahahaha",10);
// // return "-1";
// // }
// // //截取时间
// // Date = gpsbac.substring(18,32);
// // temp = Date.substring(0,4) + "-" + Date.substring(4,6) + "-" +Date.substring(6,8) + " " + Date.substring(8,10) + ":" +Date.substring(10,12) + ":" +Date.substring(12,-1);
// // gps->year = Date.substring(0,4).toInt();
// // gps->month = Date.substring(4,6).toInt();
// // gps->day = Date.substring(6,8).toInt();
// // gps->hour = Date.substring(8,10).toInt();
// // gps->minute = Date.substring(10,12).toInt();
// // gps->second = Date.substring(12,-1).toInt();
// // Date = temp;
// // // +CGNSINF: 1,1,20240430052525,40.040938,116.328013,51.900,0.26,0.00,3,,2.14,2.42,4.00,,10,8,,,34,,
// // // OK
// // ////////获取经纬度//////////
// // temp = gpsbac.substring(33);
// // // write_log(log_path,"Longitude is :" + temp,10);
// // int pos = temp.indexOf(",");
// // Latitude = temp.substring(0,pos);
// // gps->latitude = Latitude.toFloat();
// // // write_log(log_path,"Latitude ::::" + Latitude,10);
// // temp = temp.substring(pos+1);
// // // write_log(log_path,"Longitude is :" + temp,10);
// // pos = temp.indexOf(",");
// // Longitude = temp.substring(0,pos);
// // gps->longitude = Longitude.toFloat();
// // // write_log(log_path,"Longitude ::::" + Longitude,10);
// // if(Date.indexOf(",") != -1)
// // {
// // return "-1";
// // }
// return gpsbac;
// }