feat(iot): connection wifi and API *UNTESTED*

This commit is contained in:
Clement 2023-03-28 17:28:55 +02:00
parent 98e0342fe1
commit c57225c1f3
2 changed files with 53 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#define API_H
#include <Arduino_JSON.h>
#include <WiFiEsp.h>
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;
};

View File

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