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

Basic networking done, GET requests to shuttle server working

This commit is contained in:
zzzz 2024-05-04 16:37:22 +02:00
parent ea1374fca0
commit 91be852c99
8 changed files with 141 additions and 2 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
secrets.hpp

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}

View file

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

43
src/TeaNetworking.cpp Normal file
View file

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

15
src/TeaNetworking.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include <Arduino_JSON.h>
#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);
};

View file

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

View file

@ -1,4 +1,5 @@
#include <Arduino.h>
#include <Arduino_JSON.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>

33
src/rootCertificate.hpp Normal file
View file

@ -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/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn
O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