diff --git a/d1mini/D1Mini/D1Mini.ino b/d1mini/D1Mini/D1Mini.ino index 43140fa..9081ca3 100644 --- a/d1mini/D1Mini/D1Mini.ino +++ b/d1mini/D1Mini/D1Mini.ino @@ -8,24 +8,23 @@ const char* TOKEN = "mE7yG0kI"; char buffer[128]; uint8_t idx = 0; +/* ================= WIFI ================= */ void setup() { Serial.begin(115200); WiFiManager wm; - bool res = wm.autoConnect("ESP8266-Setup"); - - if (!res) { - Serial.println("WIFI_CONNECT_FAILED"); + if (!wm.autoConnect("ESP8266-Setup")) { + Serial.println("ERR;WIFI"); return; } - Serial.println("WIFI_CONNECTED"); - Serial.print("IP:"); + Serial.print("OK;WIFI;IP;"); Serial.println(WiFi.localIP()); http_get_root(); } +/* ================= LOOP ================= */ void loop() { while (Serial.available()) { char c = Serial.read(); @@ -34,120 +33,95 @@ void loop() { buffer[idx] = '\0'; handleLine(buffer); idx = 0; - } else { - if (idx < sizeof(buffer) - 1) { - buffer[idx++] = c; - } + } else if (idx < sizeof(buffer) - 1) { + buffer[idx++] = c; } } } +/* ================= SERIAL PARSER ================= */ void handleLine(char *line) { - Serial.print("RECV:"); - Serial.println(line); - - // check command prefix if (strncmp(line, "UPLOAD:", 7) == 0) { parseUpload(line + 7); } else { - Serial.println("UNKNOWN_CMD"); + Serial.println("ERR;CMD"); } } +/* ================= DATA PARSER ================= */ void parseUpload(char *data) { - double ph = 0; - double tds = 0; - double turbidity = 0; + double ph = 0, tds = 0, turbidity = 0; - char *token = strtok(data, ";"); + char *p = strtok(data, ";"); - while (token != NULL) { - char *equal = strchr(token, '='); + while (p) { + char *eq = strchr(p, '='); + if (eq) { + *eq = '\0'; + const char *key = p; + const char *val = eq + 1; - if (equal != NULL) { - *equal = '\0'; - char *key = token; - 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); - } + if (!strcmp(key, "ph")) ph = atof(val); + else if (!strcmp(key, "tds")) tds = atof(val); + else if (!strcmp(key, "turbidity")) turbidity = atof(val); } - - token = strtok(NULL, ";"); + p = strtok(NULL, ";"); } 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) { - Serial.println("WIFI_NOT_CONNECT"); - return; - } - - 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; + Serial.println("ERR;WIFI"); + return false; } WiFiClient client; 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); + int code = http.GET(); - int httpCode = http.GET(); - - if (httpCode > 0) { - Serial.print("HTTP_CODE_"); - Serial.println(httpCode); - - Serial.print("RESPONSE:"); - Serial.println(http.getString()); - } else { - Serial.print("HTTP_REQUEST_FAILED_"); - Serial.println(http.errorToString(httpCode)); + if (code <= 0) { + Serial.print("ERR;HTTP;"); + Serial.println(http.errorToString(code)); + http.end(); + return false; } + if (resp) *resp = http.getString(); + + Serial.print("OK;HTTP;"); + Serial.println(code); + 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); + } } \ No newline at end of file