From 7861cfae51ba0fc357f76736bf62862cc28f855b Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 12:06:39 +0200 Subject: [PATCH] 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(){