4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

cleanup, added TeaNetworking class

This commit is contained in:
zzzz 2024-05-04 18:43:19 +02:00
parent 91be852c99
commit 811163e7a2
4 changed files with 56 additions and 36 deletions

View file

@ -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;
}

View file

@ -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;}
};

View file

@ -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;
}
}

View file

@ -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);