diff --git a/src/TeaNetworking.cpp b/src/TeaNetworking.cpp index 5eddc05..ea79631 100644 --- a/src/TeaNetworking.cpp +++ b/src/TeaNetworking.cpp @@ -83,9 +83,11 @@ JSONVar TeaNetworking::httpsGETRequest(String serverName) return JSON.parse(payload); } -void TeaNetworking::httpsPOSTRequest(String serverName, String data) +JSONVar TeaNetworking::httpsPOSTRequest(String serverName, String data) { WiFiClientSecure client; + String payload = "{}"; + // wait for WiFi connection if ((WiFi.status() == WL_CONNECTED)) { @@ -105,6 +107,8 @@ void TeaNetworking::httpsPOSTRequest(String serverName, String data) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTPS] POST... code: %d\n", httpCode); + payload = http.getString(); + Serial.println(payload); } else { @@ -117,6 +121,7 @@ void TeaNetworking::httpsPOSTRequest(String serverName, String data) Serial.printf("[HTTPS] Unable to connect\n"); } } + return JSON.parse(payload); } String TeaNetworking::createTeaData( @@ -131,4 +136,14 @@ String TeaNetworking::createTeaData( teaData["watertemp"] = waterTemp; teaData["steepingseconds"] = steepingSeconds; return JSONVar::stringify(teaData); +} + +String TeaNetworking::createTeaData(TeaData tea) +{ + JSONVar teaData; + teaData["teaname"] = tea.m_teaName; + teaData["rfidcode"] = tea.m_rfidCode; + teaData["watertemp"] = tea.m_waterTemp; + teaData["steepingseconds"] = tea.m_steepingSeconds; + return JSONVar::stringify(teaData); } \ No newline at end of file diff --git a/src/TeaNetworking.hpp b/src/TeaNetworking.hpp index ba064d2..3febbd6 100644 --- a/src/TeaNetworking.hpp +++ b/src/TeaNetworking.hpp @@ -4,17 +4,19 @@ #include #include #include "rootCertificate.hpp" +#include "TeaData.hpp" class TeaNetworking { public: void init(const char *ssid, const char *password); JSONVar httpsGETRequest(String serverName); - void httpsPOSTRequest(String serverName, String data); + JSONVar httpsPOSTRequest(String serverName, String data); bool wifiConnected() { return WiFi.status() == WL_CONNECTED; } String createTeaData( String teaname, String rfidcode, int watertemp, int steepingseconds); + String createTeaData(TeaData tea); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0c1b86e..0475bdc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,7 +44,6 @@ SmartButton buttonRight(TACTILE_BTN_RIGHT); String shuttleServer = "https://ias-tea-axum.shuttleapp.rs"; TeaData currentTea("Teafault", "c0derfid", -1, 30); -String currentUid; enum State { @@ -134,7 +133,7 @@ void loop() String uid = smartRFID.readTag(5000); if (uid != "no_tag_detected") { - currentUid = uid; + currentTea.m_rfidCode = uid; state = PROCESS_RFID_SCAN; } break; @@ -142,7 +141,7 @@ void loop() case PROCESS_RFID_SCAN: { - display.println(currentUid); + display.println(currentTea.m_rfidCode); display.display(); delay(500); @@ -150,16 +149,15 @@ void loop() display.display(); delay(250); - JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + currentUid); + JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + currentTea.m_rfidCode); Serial.print("Result:"); Serial.println(result); if (JSONVar::stringify(result) == "null") { Serial.print("TEA DOES NOT EXIST YET!"); - String payload = teaNet.createTeaData("Unnamed Tea", currentUid, 90, 180); - teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload); - result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + currentUid); + String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, 90, 180); + result = teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload); state = State::DISPLAY_ADD_NEW_TEA_CONFIG; } else @@ -167,7 +165,7 @@ void loop() state = State::DISPLAY_SCANNED_TEA_CONFIG; } - currentTea = TeaData(result["teaname"], result["rfidcode"], result["watertemp"], result["steepingseconds"]); + currentTea = TeaData(result["teaname"], currentTea.m_rfidCode, result["watertemp"], result["steepingseconds"]); break; } case DISPLAY_SCANNED_TEA_CONFIG: @@ -176,7 +174,7 @@ void loop() String uid = smartRFID.readTag(50); // just enough time to detect tag if (uid != "no_tag_detected") { - currentUid = uid; + currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); state = PROCESS_RFID_SCAN; break; @@ -203,6 +201,10 @@ void loop() // change state if start pressed if (isBtnPressed()) { + String payload = teaNet.createTeaData(currentTea); + Serial.println(payload); + smartDisplay.printMsg("sync..."); + teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); teaTimer.beginSteeping(5000); state = State::STEEPING_IN_PROGRESS; } @@ -234,7 +236,7 @@ void loop() String uid = smartRFID.readTag(50); // just enough time to detect tag if (uid != "no_tag_detected") { - currentUid = uid; + currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); state = PROCESS_RFID_SCAN; break; @@ -263,7 +265,7 @@ void loop() String uid = smartRFID.readTag(50); // just enough time to detect tag if (uid != "no_tag_detected") { - currentUid = uid; + currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); state = PROCESS_RFID_SCAN; break; @@ -281,7 +283,9 @@ void loop() if (isBtnPressed()) { smartDisplay.printMsg("Accepted"); - delay(1000); + delay(500); + smartDisplay.printMsg("sync..."); + teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", teaNet.createTeaData(currentTea)); state = State::DISPLAY_SCANNED_TEA_CONFIG; }