From c57225c1f335475632f51c2eebfe74b4321a2e9a Mon Sep 17 00:00:00 2001 From: Clement Date: Tue, 28 Mar 2023 17:28:55 +0200 Subject: [PATCH] feat(iot): connection wifi and API *UNTESTED* --- IOT/lib/API/include/API.h | 8 +++++++ IOT/lib/API/src/API.cpp | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h index d2823f1..bccd4d3 100644 --- a/IOT/lib/API/include/API.h +++ b/IOT/lib/API/include/API.h @@ -2,6 +2,7 @@ #define API_H #include +#include class API { public: @@ -10,6 +11,8 @@ public: API(String user, String password, String host, bool https = true); + bool wifiBegin(String wifiId, String wifiPass, Stream* espSerial); + bool sendValute(String val, String poubelleID); JSONVar* connect(); @@ -20,7 +23,12 @@ private: String password; String serveurHost; bool https; + + WiFiEspClient* client; String token; + + + }; diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index 7cefa69..4e7fa1c 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -7,10 +7,55 @@ API::API(String user, String password, String host, bool https = true){ this->serveurHost = host; this->https = https; this->token = ""; + this->client = new WiFiEspClient(); +} + + +bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){ + WiFi.init(espSerial); + + // check for the presence of the shield + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + return false; + } + + // attempt to connect to WiFi network + int status = WL_IDLE_STATUS; // the Wifi radio's status + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: ");//FIXME: rm debug + Serial.println(wifiId);//FIXME: rm debug + status = WiFi.begin(wifiId.c_str(), wifiPass.c_str()); + } + return true; } JSONVar* API::connect(){ JSONVar* sortie = new JSONVar(); + + JSONVar data; + + data["identity"] = this->user; + data["password"] = this->password; + + String StrData = JSONVar::stringify(data); + + if(this->https){ + if (!this->client->connectSSL(this->serveurHost.c_str(), 443)) { + Serial.println("NOT Connected to server"); + return sortie;//TODO: faire meuilleur gestion d'erreur + }else{ + //TODO: implement http (no 's') request + } + this->client->println("POST /api/collections/users/auth-with-password HTTP/1.1"); + this->client->println("Host: iot.epi.cb85.software"); + this->client->println("Content-Type: application/json"); + this->client->println("Content-Length: " + String(StrData.length())); + this->client->println(); + this->client->println(StrData); + this->client->println("Connection: close"); + this->client->println(); + } //TODO: implement connect methods return sortie; } \ No newline at end of file