diff --git a/.gitignore b/.gitignore index 89cc49c..2181750 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +secrets.hpp \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..70e34ec --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index b56bdc1..9e84adc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,3 +15,4 @@ framework = arduino lib_deps = adafruit/Adafruit SSD1306@^2.5.9 miguelbalboa/MFRC522@^1.4.11 + arduino-libraries/Arduino_JSON@^0.2.0 diff --git a/src/TeaNetworking.cpp b/src/TeaNetworking.cpp new file mode 100644 index 0000000..4270122 --- /dev/null +++ b/src/TeaNetworking.cpp @@ -0,0 +1,43 @@ +#include "TeaNetworking.hpp" + +String TeaNetworking::httpsGETRequest(const char *serverName) +{ + WiFiClientSecure client; + String payload = "{}"; + // wait for WiFi connection + if ((WiFi.status() == WL_CONNECTED)) + { + client.setTrustAnchors(&cert); + HTTPClient https; + Serial.print("[HTTPS] begin...\n"); + if (https.begin(client, serverName)) + { // HTTPS + Serial.print("[HTTPS] GET...\n"); + // start connection and send HTTP header + int httpCode = https.GET(); + // httpCode will be negative on error + if (httpCode > 0) + { + // HTTP header has been send and Server response header has been handled + Serial.printf("[HTTPS] GET... code: %d\n", httpCode); + // file found at server + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) + { + payload = https.getString(); + Serial.println(payload); + } + } + else + { + Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); + } + https.end(); + } + else + { + Serial.printf("[HTTPS] Unable to connect\n"); + } + } + JSONVar jsonData = JSON.parse(data); + return jsonData; +} \ No newline at end of file diff --git a/src/TeaNetworking.hpp b/src/TeaNetworking.hpp new file mode 100644 index 0000000..1358128 --- /dev/null +++ b/src/TeaNetworking.hpp @@ -0,0 +1,15 @@ +#pragma once +#include +#include +#include +#include +#include "rootCertificate.hpp" + +class TeaNetworking +{ +public: + String httpsGETRequest(const char *serverName); +private: + // Create a list of certificates with the server certificate + X509List cert(IRG_Root_X1); +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7a4b4d1..5595f57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "main.hpp" +#include "secrets.hpp" #define WIRE Wire @@ -9,7 +10,6 @@ // RFID #define RST_PIN 2 // D4 -uint8_t scanDelay = 5; // 5 second delay between reads #define SS_PIN 15 // D8 MFRC522 mfrc522(SS_PIN, RST_PIN); // create RFID instance MFRC522::MIFARE_Key key; @@ -21,19 +21,53 @@ TeaTimer teaTimer(&contServo); SmartDial smartDial(POTENTIOMETER_PIN); SmartDisplay smartDisplay(&display); +// networking +const char *shuttleServer = "https://ias-tea-axum.shuttleapp.rs/alltea"; + enum State { WAIT_FOR_RFID_SCAN, DISPLAY_SCANNED_TEA_CONFIG, ADD_NEW_TEA_CONFIG, STEEPING_IN_PROGRESS, + NETWORKING_DEBUG, }; -State state = State::DISPLAY_SCANNED_TEA_CONFIG; +State state = State::NETWORKING_DEBUG; 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)); + //--------------------------------------- + Serial.println("Initializing SPI..."); SPI.begin(); @@ -65,6 +99,14 @@ void loop() switch (state) { + case NETWORKING_DEBUG: + { + JSONVar myObject = httpsGETRequest(serverName); + Serial.println(myObject); + delay(2000); + state = DISPLAY_SCANNED_TEA_CONFIG; + break; + } case WAIT_FOR_RFID_SCAN: { smartDisplay.printWaitForRFIDScreen(); diff --git a/src/main.hpp b/src/main.hpp index 5425e6f..993540c 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/src/rootCertificate.hpp b/src/rootCertificate.hpp new file mode 100644 index 0000000..9646bf0 --- /dev/null +++ b/src/rootCertificate.hpp @@ -0,0 +1,33 @@ +const char IRG_Root_X1[] PROGMEM = R"CERT( +-----BEGIN CERTIFICATE----- +MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh +cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4 +WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu +ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY +MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc +h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+ +0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U +A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW +T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ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