mirror of
https://github.com/xiaozhou233/RiverWaterQualityMonitoring.git
synced 2026-12-05 13:05:53 +08:00
优化WiFi连接和HTTP请求处理,改进数据上传逻辑
This commit is contained in:
1 parent
4591eba48d
commit
37f0adc483
1 file changed
+64
-90
+64
-90
@@ -8,24 +8,23 @@ const char* TOKEN = "mE7yG0kI";
|
|||||||
char buffer[128];
|
char buffer[128];
|
||||||
uint8_t idx = 0;
|
uint8_t idx = 0;
|
||||||
|
|
||||||
|
/* ================= WIFI ================= */
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
WiFiManager wm;
|
WiFiManager wm;
|
||||||
bool res = wm.autoConnect("ESP8266-Setup");
|
if (!wm.autoConnect("ESP8266-Setup")) {
|
||||||
|
Serial.println("ERR;WIFI");
|
||||||
if (!res) {
|
|
||||||
Serial.println("WIFI_CONNECT_FAILED");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("WIFI_CONNECTED");
|
Serial.print("OK;WIFI;IP;");
|
||||||
Serial.print("IP:");
|
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
http_get_root();
|
http_get_root();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================= LOOP ================= */
|
||||||
void loop() {
|
void loop() {
|
||||||
while (Serial.available()) {
|
while (Serial.available()) {
|
||||||
char c = Serial.read();
|
char c = Serial.read();
|
||||||
@@ -34,120 +33,95 @@ void loop() {
|
|||||||
buffer[idx] = '\0';
|
buffer[idx] = '\0';
|
||||||
handleLine(buffer);
|
handleLine(buffer);
|
||||||
idx = 0;
|
idx = 0;
|
||||||
} else {
|
} else if (idx < sizeof(buffer) - 1) {
|
||||||
if (idx < sizeof(buffer) - 1) {
|
buffer[idx++] = c;
|
||||||
buffer[idx++] = c;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================= SERIAL PARSER ================= */
|
||||||
void handleLine(char *line) {
|
void handleLine(char *line) {
|
||||||
Serial.print("RECV:");
|
|
||||||
Serial.println(line);
|
|
||||||
|
|
||||||
// check command prefix
|
|
||||||
if (strncmp(line, "UPLOAD:", 7) == 0) {
|
if (strncmp(line, "UPLOAD:", 7) == 0) {
|
||||||
parseUpload(line + 7);
|
parseUpload(line + 7);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("UNKNOWN_CMD");
|
Serial.println("ERR;CMD");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================= DATA PARSER ================= */
|
||||||
void parseUpload(char *data) {
|
void parseUpload(char *data) {
|
||||||
double ph = 0;
|
double ph = 0, tds = 0, turbidity = 0;
|
||||||
double tds = 0;
|
|
||||||
double turbidity = 0;
|
|
||||||
|
|
||||||
char *token = strtok(data, ";");
|
char *p = strtok(data, ";");
|
||||||
|
|
||||||
while (token != NULL) {
|
while (p) {
|
||||||
char *equal = strchr(token, '=');
|
char *eq = strchr(p, '=');
|
||||||
|
if (eq) {
|
||||||
|
*eq = '\0';
|
||||||
|
const char *key = p;
|
||||||
|
const char *val = eq + 1;
|
||||||
|
|
||||||
if (equal != NULL) {
|
if (!strcmp(key, "ph")) ph = atof(val);
|
||||||
*equal = '\0';
|
else if (!strcmp(key, "tds")) tds = atof(val);
|
||||||
char *key = token;
|
else if (!strcmp(key, "turbidity")) turbidity = atof(val);
|
||||||
char *value = equal + 1;
|
|
||||||
|
|
||||||
if (strcmp(key, "ph") == 0) {
|
|
||||||
ph = atof(value);
|
|
||||||
}
|
|
||||||
else if (strcmp(key, "tds") == 0) {
|
|
||||||
tds = atof(value);
|
|
||||||
}
|
|
||||||
else if (strcmp(key, "turbidity") == 0) {
|
|
||||||
turbidity = atof(value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
p = strtok(NULL, ";");
|
||||||
token = strtok(NULL, ";");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
upload_data(ph, tds, turbidity);
|
upload_data(ph, tds, turbidity);
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_get_root() {
|
/* ================= HTTP CORE ================= */
|
||||||
|
bool http_get(const String& url, String* resp = nullptr) {
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
Serial.println("WIFI_NOT_CONNECT");
|
Serial.println("ERR;WIFI");
|
||||||
return;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
HTTPClient http;
|
|
||||||
WiFiClient client;
|
|
||||||
|
|
||||||
Serial.println("SENDING_GET_REQUEST");
|
|
||||||
|
|
||||||
http.begin(client, serverUrl);
|
|
||||||
|
|
||||||
int httpCode = http.GET();
|
|
||||||
|
|
||||||
if (httpCode > 0) {
|
|
||||||
Serial.print("HTTP_CODE_");
|
|
||||||
Serial.println(httpCode);
|
|
||||||
|
|
||||||
String payload = http.getString();
|
|
||||||
Serial.print("RESPONSE:");
|
|
||||||
Serial.println(payload);
|
|
||||||
} else {
|
|
||||||
Serial.print("HTTP_REQUEST_FAILED_");
|
|
||||||
Serial.println(http.errorToString(httpCode));
|
|
||||||
}
|
|
||||||
|
|
||||||
http.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
void upload_data(double ph, double tds, double turbidity) {
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
Serial.println("WIFI_NOT_CONNECT");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
|
||||||
String url = String(serverUrl) + "data/upload?" +
|
|
||||||
"ph=" + String(ph) +
|
|
||||||
"&tds=" + String(tds) +
|
|
||||||
"&turbidity=" + String(turbidity) +
|
|
||||||
"&token=" + String(TOKEN);
|
|
||||||
|
|
||||||
Serial.println("UPLOADING_DATA");
|
|
||||||
Serial.println(url);
|
|
||||||
|
|
||||||
http.begin(client, url);
|
http.begin(client, url);
|
||||||
|
int code = http.GET();
|
||||||
|
|
||||||
int httpCode = http.GET();
|
if (code <= 0) {
|
||||||
|
Serial.print("ERR;HTTP;");
|
||||||
if (httpCode > 0) {
|
Serial.println(http.errorToString(code));
|
||||||
Serial.print("HTTP_CODE_");
|
http.end();
|
||||||
Serial.println(httpCode);
|
return false;
|
||||||
|
|
||||||
Serial.print("RESPONSE:");
|
|
||||||
Serial.println(http.getString());
|
|
||||||
} else {
|
|
||||||
Serial.print("HTTP_REQUEST_FAILED_");
|
|
||||||
Serial.println(http.errorToString(httpCode));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (resp) *resp = http.getString();
|
||||||
|
|
||||||
|
Serial.print("OK;HTTP;");
|
||||||
|
Serial.println(code);
|
||||||
|
|
||||||
http.end();
|
http.end();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================= ROOT REQUEST ================= */
|
||||||
|
void http_get_root() {
|
||||||
|
String resp;
|
||||||
|
http_get(String(serverUrl), &resp);
|
||||||
|
|
||||||
|
Serial.print("OK;ROOT;");
|
||||||
|
Serial.println(resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================= DATA UPLOAD ================= */
|
||||||
|
void upload_data(double ph, double tds, double turbidity) {
|
||||||
|
String url = String(serverUrl) + "data/upload?"
|
||||||
|
+ "ph=" + String(ph, 2)
|
||||||
|
+ "&tds=" + String(tds, 2)
|
||||||
|
+ "&turbidity=" + String(turbidity, 2)
|
||||||
|
+ "&token=" + TOKEN;
|
||||||
|
|
||||||
|
Serial.println("OK;UPLOAD");
|
||||||
|
|
||||||
|
String resp;
|
||||||
|
if (http_get(url, &resp)) {
|
||||||
|
Serial.print("OK;RESP;");
|
||||||
|
Serial.println(resp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user