From 31dfa751a35dca055967ddf38b8b268cea35c3a8 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 27 Mar 2023 17:41:53 +0200 Subject: [PATCH 01/12] feat: add deps --- IOT/platformio.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/IOT/platformio.ini b/IOT/platformio.ini index 82e4637..c06a691 100644 --- a/IOT/platformio.ini +++ b/IOT/platformio.ini @@ -36,6 +36,8 @@ monitor_flags = ; librairies lib_deps = + bportaluri/WiFiEsp@^2.2.2 ; gestion des commande 'AT' de l'esp01 + arduino-libraries/Arduino_JSON@^0.2.0 ; gestion des json ; example: ; erropix/ESP32 AnalogWrite@^0.2 From e8ebe481284ce2afa7dd0867dac565d4a6afec9b Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 27 Mar 2023 17:42:17 +0200 Subject: [PATCH 02/12] feat: init api lib --- IOT/lib/API/include/API.h | 14 ++++++++++++++ IOT/lib/API/src/API.cpp | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 IOT/lib/API/include/API.h create mode 100644 IOT/lib/API/src/API.cpp diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h new file mode 100644 index 0000000..29b15c6 --- /dev/null +++ b/IOT/lib/API/include/API.h @@ -0,0 +1,14 @@ +#ifndef API_H +#define API_H + +#include + +class API { +public: + + API(); + JSONVar* connect(); +}; + + +#endif \ No newline at end of file diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp new file mode 100644 index 0000000..f921507 --- /dev/null +++ b/IOT/lib/API/src/API.cpp @@ -0,0 +1,12 @@ +#include "../include/API.h" + + +API::API(){ + //TODO: implement API constructor +} + +JSONVar* API::connect(){ + JSONVar* sortie = new JSONVar(); + //TODO: implement connect methods + return sortie; +} \ No newline at end of file From 98e0342fe1ec31a8451ef37d689689e258baa2fd Mon Sep 17 00:00:00 2001 From: Clement Date: Tue, 28 Mar 2023 16:05:36 +0200 Subject: [PATCH 03/12] feat(iot): constructeur API --- IOT/lib/API/include/API.h | 15 ++++++++++++++- IOT/lib/API/src/API.cpp | 8 ++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h index 29b15c6..d2823f1 100644 --- a/IOT/lib/API/include/API.h +++ b/IOT/lib/API/include/API.h @@ -6,8 +6,21 @@ class API { public: - API(); + + + API(String user, String password, String host, bool https = true); + + bool sendValute(String val, String poubelleID); + JSONVar* connect(); + +private: + + String user; + String password; + String serveurHost; + bool https; + String token; }; diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index f921507..7cefa69 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -1,8 +1,12 @@ #include "../include/API.h" -API::API(){ - //TODO: implement API constructor +API::API(String user, String password, String host, bool https = true){ + this->user = user; + this->password = password; + this->serveurHost = host; + this->https = https; + this->token = ""; } JSONVar* API::connect(){ From c57225c1f335475632f51c2eebfe74b4321a2e9a Mon Sep 17 00:00:00 2001 From: Clement Date: Tue, 28 Mar 2023 17:28:55 +0200 Subject: [PATCH 04/12] 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 From 89ecb3bf1722a120a40e706c5196494ebead0535 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 11:09:44 +0200 Subject: [PATCH 05/12] feat: commentaire --- IOT/lib/API/src/API.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index 4e7fa1c..1b9bee2 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -16,7 +16,7 @@ bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){ // check for the presence of the shield if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); + Serial.println("WiFi shield not present");//FIXME: rm debug return false; } From 3208c2ff6ba98829564dcec76189868f78373b76 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 11:10:12 +0200 Subject: [PATCH 06/12] feat(WIP): test main API lib --- IOT/src/main.cpp | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/IOT/src/main.cpp b/IOT/src/main.cpp index cff0213..a0a4e14 100644 --- a/IOT/src/main.cpp +++ b/IOT/src/main.cpp @@ -1,12 +1,32 @@ #include -#include "Program.h" +// #include "Program.h" -Program* program; +// Program* program; -void setup() { - program = new Program(); +// void setup() { +// program = new Program(); +// } + +// void loop() { +// program->loop(); +// } + +#include "API.h" +#include + +API* testAPI; + +void setup(){ + Serial.begin(MONITOR_SPEED); + Serial1.begin(MONITOR_SPEED); + + testAPI = new API("patrick","Patrick123","https://iot.epi.cb85.software"); + + testAPI->wifiBegin("Clement4G","Clement123",&Serial); + + Serial.print(JSONVar::stringify(testAPI->connect())); } -void loop() { - program->loop(); +void loop(){ + } \ No newline at end of file From 7861cfae51ba0fc357f76736bf62862cc28f855b Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 12:06:39 +0200 Subject: [PATCH 07/12] feat: connection OK --- IOT/lib/API/src/API.cpp | 30 +++++++++++++++++++++++++----- IOT/src/main.cpp | 4 ++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index 1b9bee2..36eda3b 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -31,7 +31,7 @@ bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){ } JSONVar* API::connect(){ - JSONVar* sortie = new JSONVar(); + JSONVar* sortie =nullptr; JSONVar data; @@ -43,9 +43,7 @@ JSONVar* API::connect(){ 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 + return sortie; } this->client->println("POST /api/collections/users/auth-with-password HTTP/1.1"); this->client->println("Host: iot.epi.cb85.software"); @@ -55,7 +53,29 @@ JSONVar* API::connect(){ this->client->println(StrData); this->client->println("Connection: close"); this->client->println(); + }else{ + //TODO: implement http (no 's') request } - //TODO: implement connect methods + + + while (!client->available()) { + + } + + String responce = ""; + while (client->available()) { + responce += (char)client->read(); + } + + String str = responce.substring(responce.indexOf("{"),responce.lastIndexOf("}")+1); + + sortie = new JSONVar(JSONVar::parse(str)); + + this->token = JSONVar::stringify((*sortie)["token"]); + + this->token = this->token.substring(1,this->token.length()-1); + + Serial.println(this->token); + return sortie; } \ No newline at end of file diff --git a/IOT/src/main.cpp b/IOT/src/main.cpp index a0a4e14..e5a6c3a 100644 --- a/IOT/src/main.cpp +++ b/IOT/src/main.cpp @@ -22,9 +22,9 @@ void setup(){ testAPI = new API("patrick","Patrick123","https://iot.epi.cb85.software"); - testAPI->wifiBegin("Clement4G","Clement123",&Serial); + testAPI->wifiBegin("Clement4G","Clement123",&Serial1); - Serial.print(JSONVar::stringify(testAPI->connect())); + Serial.print(JSONVar::stringify(*(testAPI->connect()))); } void loop(){ From 59728b9694227ae98caf1ff47bcfb930b7c89374 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 16:07:57 +0200 Subject: [PATCH 08/12] feat(iot): API Client OK --- IOT/lib/API/include/API.h | 3 +- IOT/lib/API/src/API.cpp | 75 +++++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h index bccd4d3..b4ff31c 100644 --- a/IOT/lib/API/include/API.h +++ b/IOT/lib/API/include/API.h @@ -13,7 +13,7 @@ public: bool wifiBegin(String wifiId, String wifiPass, Stream* espSerial); - bool sendValute(String val, String poubelleID); + bool sendValute(String val, String poubelleID, String unit, bool full); JSONVar* connect(); @@ -25,6 +25,7 @@ private: bool https; WiFiEspClient* client; + String token; diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index 36eda3b..f458068 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -1,7 +1,7 @@ #include "../include/API.h" -API::API(String user, String password, String host, bool https = true){ +API::API(String user, String password, String host, bool https){ this->user = user; this->password = password; this->serveurHost = host; @@ -16,21 +16,20 @@ bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){ // check for the presence of the shield if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present");//FIXME: rm debug + Serial.println("WiFi shield not present"); // FIXME: rm debug 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(){ + this->client->stop(); JSONVar* sortie =nullptr; JSONVar data; @@ -38,29 +37,30 @@ JSONVar* API::connect(){ data["identity"] = this->user; data["password"] = this->password; - String StrData = JSONVar::stringify(data); + 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; } - 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(); }else{ - //TODO: implement http (no 's') request + if (!this->client->connectSSL(("http://" + this->serveurHost).c_str(), 80)) { + Serial.println("NOT Connected to server"); + return sortie; + } } + this->client->println("POST /api/collections/users/auth-with-password HTTP/1.1"); + this->client->println("Host: " + this->serveurHost); + 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(); - while (!client->available()) { - - } + while (!client->available()) {} String responce = ""; while (client->available()) { @@ -75,7 +75,44 @@ JSONVar* API::connect(){ this->token = this->token.substring(1,this->token.length()-1); - Serial.println(this->token); - + this->client->stop(); + return sortie; +} + +bool API::sendValute(String val, String poubelleID, String valUnit, bool full){ + JSONVar data; + data["value"] = val; + data["trash_id"] = poubelleID; + data["unit"] = valUnit; + data["status"] = full; + + String strData = JSONVar::stringify(data); + + if(this->token == ""){ + this->connect(); + } + if(this->https){ + if (!this->client->connectSSL(("https://" + this->serveurHost).c_str(), 443)) { + Serial.println("NOT Connected to server"); + return false; + } + }else{ + if (!this->client->connectSSL(("http://" + this->serveurHost).c_str(), 80)) { + Serial.println("NOT Connected to server"); + return false; + } + } + this->client->println("POST /api/collections/data/records HTTP/1.1"); + this->client->println("Host: " + this->serveurHost); + this->client->println("Content-Type: application/json"); + this->client->println("Content-Length: " + String(strData.length())); + this->client->println("Authorization: Bearer " + this->token); + this->client->println(); + this->client->print(strData); + this->client->println("Connection: close"); + this->client->println(); + + this->token = "";//XXX: on rm le token a chaque fois car on sais pas si la requet a bien aboutie et donc si il y est encore bon + return true; } \ No newline at end of file From 789cab74a9958afeff372d19bf6961dd029f11af Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 16:19:32 +0200 Subject: [PATCH 09/12] refactor(iot): change type of connect on api --- IOT/lib/API/src/API.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index f458068..f5240a8 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -27,10 +27,9 @@ bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){ } return true; } - -JSONVar* API::connect(){ +bool API::connect(){ this->client->stop(); - JSONVar* sortie =nullptr; + bool sortie = false; JSONVar data; @@ -69,14 +68,16 @@ JSONVar* API::connect(){ String str = responce.substring(responce.indexOf("{"),responce.lastIndexOf("}")+1); - sortie = new JSONVar(JSONVar::parse(str)); + JSONVar jsonRes = new JSONVar(JSONVar::parse(str)); - this->token = JSONVar::stringify((*sortie)["token"]); + this->token = JSONVar::stringify(jsonRes["token"]); this->token = this->token.substring(1,this->token.length()-1); this->client->stop(); + sortie = true; + return sortie; } @@ -90,7 +91,8 @@ bool API::sendValute(String val, String poubelleID, String valUnit, bool full){ String strData = JSONVar::stringify(data); if(this->token == ""){ - this->connect(); + bool connected = this->connect(); + if(!connected)return false; } if(this->https){ if (!this->client->connectSSL(("https://" + this->serveurHost).c_str(), 443)) { From 97ae6c69771377f2e37f16e5ac2a977e179b1f6c Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 16:23:59 +0200 Subject: [PATCH 10/12] docs(iot): documentation de la lib api client --- IOT/lib/API/include/API.h | 62 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h index b4ff31c..c6df496 100644 --- a/IOT/lib/API/include/API.h +++ b/IOT/lib/API/include/API.h @@ -8,24 +8,82 @@ class API { public: - + /** + * @brief Construit un nouvelle objet API + * + * @param[in] user nom de l'utilisateur de l'API + * @param[in] password mot de passe de l'utilisateur + * @param[in] host address de l'api + * @param[in] https l'address de l'api est en https *(defaut `true`)* + */ API(String user, String password, String host, bool https = true); + /** + * @brief Initialise la connection Wifi de l'esp + * + * @param wifiId nom du wifi + * @param wifiPass mot de passe du wifi + * @param espSerial port serie de l'esp (hard ou soft) + * @return true la connexion a bien fonctionner + * @return false erreur a la connexion + */ bool wifiBegin(String wifiId, String wifiPass, Stream* espSerial); + /** + * @brief envoie la valeur d'un capteur de poubelle à l'api + * + * @param val valeur du capteur + * @param poubelleID ID de la poubelle + * @param unit uniter de mesure du capteur *(ex: g, cm, degree,...) + * @param full poubelle est considéré comme pleine + * @return true la valeur s'est bien envoyer + * @return false il y a une erreur durran l'envoie + */ bool sendValute(String val, String poubelleID, String unit, bool full); - JSONVar* connect(); private: + /** + * @brief connect l'utilisateur a l'api et met a jour le token + * + * @return true l'utilisateur est bien connecter + * @return false erreur lors de la connexion de l'utilisateur + */ + bool connect(); + /** + * @brief ID de l'utilisateur + * + */ String user; + /** + * @brief mdp de l'utilisateur + * + */ String password; + + /** + * @brief adresse du serveur API + * + */ String serveurHost; + + /** + * @brief true = serveur en https (443) + * + */ bool https; + /** + * @brief client http + * + */ WiFiEspClient* client; + /** + * @brief token de connexion du client (vide = deconnecter) + * + */ String token; From 60bb2911f4c0f9a5d4c4a03f3f7e474f4d0d3436 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 16:30:34 +0200 Subject: [PATCH 11/12] remove(iot): test in main programme --- IOT/src/main.cpp | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/IOT/src/main.cpp b/IOT/src/main.cpp index e5a6c3a..14f34bd 100644 --- a/IOT/src/main.cpp +++ b/IOT/src/main.cpp @@ -1,32 +1,12 @@ #include -// #include "Program.h" +#include "Program.h" -// Program* program; +Program* program; -// void setup() { -// program = new Program(); -// } - -// void loop() { -// program->loop(); -// } - -#include "API.h" -#include - -API* testAPI; - -void setup(){ - Serial.begin(MONITOR_SPEED); - Serial1.begin(MONITOR_SPEED); - - testAPI = new API("patrick","Patrick123","https://iot.epi.cb85.software"); - - testAPI->wifiBegin("Clement4G","Clement123",&Serial1); - - Serial.print(JSONVar::stringify(*(testAPI->connect()))); +void setup() { + program = new Program(); } -void loop(){ - -} \ No newline at end of file +void loop() { + program->loop(); +} From 4bde578c3596a3a1f302c2e4afc279ca45a0c929 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 16:30:44 +0200 Subject: [PATCH 12/12] update config --- IOT/README.md | 6 ++++++ IOT/config.ini | 3 ++- IOT/secrets.ini.example | 8 +++++++- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 IOT/README.md diff --git a/IOT/README.md b/IOT/README.md new file mode 100644 index 0000000..8942b26 --- /dev/null +++ b/IOT/README.md @@ -0,0 +1,6 @@ +#IOT: + + +## setup : + +rename `secrets.ini.example` to `secrets.ini` \ No newline at end of file diff --git a/IOT/config.ini b/IOT/config.ini index 3ffeff4..11acf53 100644 --- a/IOT/config.ini +++ b/IOT/config.ini @@ -14,6 +14,7 @@ build_flags = -D MONITOR_SPEED=${config.monitor_speed} ; DO NOT TOUCH --- END + -D API_HOST=\"iot.epi.cb85.software\" + -D EXAMPLE_NUMBER=69 - -D EXAMPLE_STRING=\"Pouet\" \ No newline at end of file diff --git a/IOT/secrets.ini.example b/IOT/secrets.ini.example index a3afda8..b517f84 100644 --- a/IOT/secrets.ini.example +++ b/IOT/secrets.ini.example @@ -3,4 +3,10 @@ ; To be able to reproduce it [secrets] -build_flags = \ No newline at end of file +build_flags = + + -D WIFI_SSID=\"...\" + -D WIFI_PASSWORD=\"...\"4 + + -D USER_NAME=\"...\" + -D USER_PASSWORD=\"...\" \ No newline at end of file