Ajout de la possibilité d'envoyer des actions sur l'appareil connecté. Attention la gestion des paramètres n'est pas encore totalement décidé. TODO : RAJOUTER LE LOGIN SUR L API ET RAJOUTER UNE INTERFACE WEB SIMPLISTE

This commit is contained in:
OC01Dev_Pierre 2025-10-18 17:47:33 +02:00
parent 83cb90a7d7
commit f5766a2817
1 changed files with 84 additions and 28 deletions

112
main.cpp
View File

@ -1,44 +1,77 @@
#include <WiFi.h> #include <WiFi.h>
#include <WebSocketsClient.h> #include <WebSocketsClient.h>
#include <HTTPClient.h>
#include <map>
// ⚙️ Infos WiFi //Association string -> fonction
typedef void (*CommandFunction)(String);
std::map<String, CommandFunction> commandMap;
//Infos WiFi
const char* ssid = "StCaillou"; const char* ssid = "StCaillou";
const char* password = "02070500"; const char* password = "02070500";
// ⚙️ Infos WebSocket //Infos WebSocket
const char* websocket_host = "193.70.38.222"; // Remplace par ton IP publique si tu testes depuis un autre réseau WebSocketsClient webSocket;
const char* websocket_host = "193.70.38.222";
const uint16_t websocket_port = 4000; const uint16_t websocket_port = 4000;
const char* websocket_path = "/ws"; const char* websocket_path = "/ws";
//Info sur l'identification du système
String mac = WiFi.macAddress(); String mac = WiFi.macAddress();
int i = 0; //-------------------------------------------------------------------------------
//Configuration des informations de connection sur l'api CANDLE
const String urlCANDLE = "https://auth.collineos.ovh"; //pseudo & mdp comme champ
//-------------------------------------------------------------------------------
WebSocketsClient webSocket; //Déclaration de prototypes pour éviter les non définitions au sein du fichier
void login();
void saveFunc(String interfaceName, CommandFunction func);
void saveFunc(String interfaceName, CommandFunction func, String params);
void defineInterface();
//Pour récuperer l'adress mac on utilise WiFi.macAddress();
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) { switch(type) {
case WStype_DISCONNECTED: case WStype_DISCONNECTED:
Serial.println("[WSc] Déconnecté !"); Serial.println("[DEBUG] Déconnecté !");
break; break;
case WStype_CONNECTED: case WStype_CONNECTED:
Serial.printf("[WSc] Connecté à: %s\n", payload); Serial.printf("[DEBUG] Connecté à: %s\n", payload);
webSocket.sendTXT(" Bonjour depuis l'ESP32 !"); login();
defineInterface();
break; break;
case WStype_TEXT: case WStype_TEXT: {
Serial.printf("[WSc] Message reçu: %s\n", payload); String message = (char*)payload;
Serial.printf("[DEBUG] Message reçu: %s\n", message.c_str());
// Parse le message : "ACTION$COMMAND$PARAM"
int firstIndex = message.indexOf('$');
int secondIndex = message.indexOf('$', firstIndex + 1);
if (firstIndex != -1 && message.substring(0, firstIndex) == "ACTION") {
String command = message.substring(firstIndex + 1, secondIndex);
String param = (secondIndex != -1) ? message.substring(secondIndex + 1) : "";
if (commandMap.find(command) != commandMap.end()) {
CommandFunction func = commandMap[command];
func(param);
} else {
Serial.println("Commande inconnue: " + command);
}
}
break; break;
}
case WStype_ERROR: case WStype_ERROR:
Serial.println("[WSc] Erreur !"); Serial.println("[DEBUG] Erreur !");
break; break;
default: default:
break; break;
} }
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(500); delay(500);
@ -59,33 +92,56 @@ void setup() {
// Connexion au WebSocket // Connexion au WebSocket
webSocket.begin(websocket_host, websocket_port, websocket_path); webSocket.begin(websocket_host, websocket_port, websocket_path);
webSocket.onEvent(webSocketEvent); webSocket.onEvent(webSocketEvent);
webSocket.setReconnectInterval(5000); // Essayer de se reconnecter toutes les 5s webSocket.setReconnectInterval(5000);
} }
//Permet la connexion au serveur CANDLE pour récuperer un identifiant //----------------------------------------------------------------------
//Cela nous permettra de s'assurer que nos appareils sont connu //Ici on définit les actions sous formes d'actions
void login(){ //Pour le moment les parametre ne sont pas encore totalement développer
//Connection pour récupération du token //Correction prochaine
//----------------------------------------------------------------------
void ping(String param){
Serial.println("PONG!");
}
void defineInterface(){
//Permet de définir les actions qu'un utilisateur peut entreprendre
saveFunc("PING", ping);
}
//----------------------------------------------------------------------
//Ces fonctions permettent le fonctionnement général de la communication
//----------------------------------------------------------------------
void login(){//Il faut rajouter le token
webSocket.sendTXT("LOGIN$"+mac); webSocket.sendTXT("LOGIN$"+mac);
} }
void sendInterface(String interfaceName){ void saveFunc(String interfaceName, CommandFunction func, String params){
//Comment spécifier le nombre de parametre ? Serial.println("ENVOIE DE LINTERFACE AVEC PARAMS");
//On peut aussi faire une interface qui ne prends que un parametre pas un parametre commandMap[interfaceName] = func;
webSocket.sendTXT("REGISTER$"+mac+"$"+interfaceName); webSocket.sendTXT("REGISTER$"+interfaceName + "$" + params);
}
void saveFunc(String interfaceName, CommandFunction func){
Serial.println("ENVOIE DE LINTERFACE SANS PARAMS");
commandMap[interfaceName] = func;
webSocket.sendTXT("REGISTER$"+interfaceName + "$");
} }
void sendValue(String valueName, String value){ void sendValue(String valueName, String value){
webSocket.sendTXT("VALUE$"+mac+"$"+valueName+"$"+value); webSocket.sendTXT("VALUE$"+valueName+"$"+value);
} }
//--------------------------------------------------------
//--------------------------------------------------------
//Le void loop ne doit pas être bloquant ( on utilisera millis )
//--------------------------------------------------------
void loop() { void loop() {
webSocket.loop(); webSocket.loop();
i++;
if(i > 100000){
i = 0;
sendValue("temp", "temp");
}
} }