diff --git a/src/TeaNetworking.cpp b/src/TeaNetworking.cpp index 4270122..5aaa8d5 100644 --- a/src/TeaNetworking.cpp +++ b/src/TeaNetworking.cpp @@ -1,5 +1,45 @@ #include "TeaNetworking.hpp" +void TeaNetworking::init(const char *ssid, const char *password) +{ + // networking init + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + Serial.println(""); + Serial.print("Connecting"); + int attempts = 0; + while (WiFi.status() != WL_CONNECTED) + { + if (attempts++ > 30) + { + Serial.println(""); + Serial.println("Failed to connect to network."); + return; + } + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.print("Connected to WiFi network with IP Address: "); + Serial.println(WiFi.localIP()); + + // Set time via NTP, as required for x.509 validation + configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); + Serial.print("Waiting for NTP time sync: "); + time_t now = time(nullptr); + while (now < 8 * 3600 * 2) + { + delay(500); + Serial.print("."); + now = time(nullptr); + } + Serial.println(""); + struct tm timeinfo; + gmtime_r(&now, &timeinfo); + Serial.print("Current time: "); + Serial.print(asctime(&timeinfo)); +} + String TeaNetworking::httpsGETRequest(const char *serverName) { WiFiClientSecure client; @@ -7,6 +47,8 @@ String TeaNetworking::httpsGETRequest(const char *serverName) // wait for WiFi connection if ((WiFi.status() == WL_CONNECTED)) { + // Create a list of certificates with the server certificate + X509List cert(IRG_Root_X1); client.setTrustAnchors(&cert); HTTPClient https; Serial.print("[HTTPS] begin...\n"); @@ -38,6 +80,6 @@ String TeaNetworking::httpsGETRequest(const char *serverName) Serial.printf("[HTTPS] Unable to connect\n"); } } - JSONVar jsonData = JSON.parse(data); + JSONVar jsonData = JSON.parse(payload); return jsonData; } \ No newline at end of file diff --git a/src/TeaNetworking.hpp b/src/TeaNetworking.hpp index 1358128..5de4a77 100644 --- a/src/TeaNetworking.hpp +++ b/src/TeaNetworking.hpp @@ -8,8 +8,7 @@ class TeaNetworking { public: + void init(const char *ssid, const char *password); String httpsGETRequest(const char *serverName); -private: - // Create a list of certificates with the server certificate - X509List cert(IRG_Root_X1); + bool wifiConnected(){return WiFi.status() == WL_CONNECTED;} }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5595f57..825cd44 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,7 @@ ContinuousServo contServo(SERVO_PIN); TeaTimer teaTimer(&contServo); SmartDial smartDial(POTENTIOMETER_PIN); SmartDisplay smartDisplay(&display); +TeaNetworking teaNet; // networking const char *shuttleServer = "https://ias-tea-axum.shuttleapp.rs/alltea"; @@ -38,35 +39,7 @@ void setup() { Serial.begin(9600); - // networking init----------------------- - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); - Serial.println("Connecting"); - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - Serial.print("."); - } - Serial.println(""); - Serial.print("Connected to WiFi network with IP Address: "); - Serial.println(WiFi.localIP()); - - // Set time via NTP, as required for x.509 validation - configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); - Serial.print("Waiting for NTP time sync: "); - time_t now = time(nullptr); - while (now < 8 * 3600 * 2) - { - delay(500); - Serial.print("."); - now = time(nullptr); - } - Serial.println(""); - struct tm timeinfo; - gmtime_r(&now, &timeinfo); - Serial.print("Current time: "); - Serial.print(asctime(&timeinfo)); - //--------------------------------------- + teaNet.init(ssid, password); Serial.println("Initializing SPI..."); SPI.begin(); @@ -101,10 +74,15 @@ void loop() { case NETWORKING_DEBUG: { - JSONVar myObject = httpsGETRequest(serverName); + if (!teaNet.wifiConnected()) + { + state = WAIT_FOR_RFID_SCAN; + break; + } + JSONVar myObject = teaNet.httpsGETRequest(shuttleServer); Serial.println(myObject); delay(2000); - state = DISPLAY_SCANNED_TEA_CONFIG; + state = WAIT_FOR_RFID_SCAN; break; } case WAIT_FOR_RFID_SCAN: @@ -166,7 +144,7 @@ void loop() printMsg("DONE"); if (!contServo.isTurning()) { - delay(3000); + delay(2000); state = State::WAIT_FOR_RFID_SCAN; } } diff --git a/src/main.hpp b/src/main.hpp index 993540c..50480b0 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -13,6 +13,7 @@ #include "ContinuousServo.hpp" #include "SmartDial.hpp" #include "SmartDisplay.hpp" +#include "TeaNetworking.hpp" bool toggle_led(void *); void printMsg(const String &s);