feat(IOT): LibAPI #21
6
IOT/README.md
Normal file
6
IOT/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#IOT:
|
||||||
|
|
||||||
|
|
||||||
|
## setup :
|
||||||
|
|
||||||
|
rename `secrets.ini.example` to `secrets.ini`
|
@ -14,6 +14,7 @@ build_flags =
|
|||||||
-D MONITOR_SPEED=${config.monitor_speed}
|
-D MONITOR_SPEED=${config.monitor_speed}
|
||||||
; DO NOT TOUCH --- END
|
; DO NOT TOUCH --- END
|
||||||
|
|
||||||
|
-D API_HOST=\"iot.epi.cb85.software\"
|
||||||
|
|
||||||
-D EXAMPLE_NUMBER=69
|
-D EXAMPLE_NUMBER=69
|
||||||
|
|
||||||
-D EXAMPLE_STRING=\"Pouet\"
|
|
94
IOT/lib/API/include/API.h
Normal file
94
IOT/lib/API/include/API.h
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
#ifndef API_H
|
||||||
|
#define API_H
|
||||||
|
|
||||||
|
#include <Arduino_JSON.h>
|
||||||
|
#include <WiFiEsp.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
120
IOT/lib/API/src/API.cpp
Normal file
120
IOT/lib/API/src/API.cpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#include "../include/API.h"
|
||||||
|
|
||||||
|
|
||||||
|
API::API(String user, String password, String host, bool https){
|
||||||
|
this->user = user;
|
||||||
|
this->password = password;
|
||||||
|
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"); // 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) {
|
||||||
|
status = WiFi.begin(wifiId.c_str(), wifiPass.c_str());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool API::connect(){
|
||||||
|
this->client->stop();
|
||||||
|
bool sortie = false;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
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()) {}
|
||||||
|
|
||||||
|
String responce = "";
|
||||||
|
while (client->available()) {
|
||||||
|
responce += (char)client->read();
|
||||||
|
}
|
||||||
|
|
||||||
|
String str = responce.substring(responce.indexOf("{"),responce.lastIndexOf("}")+1);
|
||||||
|
|
||||||
|
JSONVar jsonRes = new JSONVar(JSONVar::parse(str));
|
||||||
|
|
||||||
|
this->token = JSONVar::stringify(jsonRes["token"]);
|
||||||
|
|
||||||
|
this->token = this->token.substring(1,this->token.length()-1);
|
||||||
|
|
||||||
|
this->client->stop();
|
||||||
|
|
||||||
|
sortie = true;
|
||||||
|
|
||||||
|
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 == ""){
|
||||||
|
bool connected = this->connect();
|
||||||
|
if(!connected)return false;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
@ -36,6 +36,8 @@ monitor_flags =
|
|||||||
|
|
||||||
; librairies
|
; librairies
|
||||||
lib_deps =
|
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:
|
; example:
|
||||||
; erropix/ESP32 AnalogWrite@^0.2
|
; erropix/ESP32 AnalogWrite@^0.2
|
||||||
|
|
||||||
|
@ -4,3 +4,9 @@
|
|||||||
|
|
||||||
[secrets]
|
[secrets]
|
||||||
build_flags =
|
build_flags =
|
||||||
|
|
||||||
|
-D WIFI_SSID=\"...\"
|
||||||
|
-D WIFI_PASSWORD=\"...\"4
|
||||||
|
|
||||||
|
-D USER_NAME=\"...\"
|
||||||
|
-D USER_PASSWORD=\"...\"
|
Loading…
x
Reference in New Issue
Block a user