148 lines
4.3 KiB
C++
148 lines
4.3 KiB
C++
#include <WiFi.h>
|
|
#include <WebSocketsClient.h>
|
|
#include <HTTPClient.h>
|
|
#include <map>
|
|
|
|
//Association string -> fonction
|
|
typedef void (*CommandFunction)(String);
|
|
std::map<String, CommandFunction> commandMap;
|
|
|
|
//Infos WiFi
|
|
const char* ssid = "StCaillou";
|
|
const char* password = "02070500";
|
|
|
|
//Infos WebSocket
|
|
WebSocketsClient webSocket;
|
|
const char* websocket_host = "193.70.38.222";
|
|
const uint16_t websocket_port = 4000;
|
|
const char* websocket_path = "/ws";
|
|
|
|
|
|
String mac = WiFi.macAddress();
|
|
|
|
//-------------------------------------------------------------------------------
|
|
//Configuration des informations de connection sur l'api CANDLE
|
|
const String urlCANDLE = "https://auth.collineos.ovh"; //pseudo & mdp comme champ
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//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();
|
|
|
|
|
|
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
|
switch(type) {
|
|
case WStype_DISCONNECTED:
|
|
Serial.println("[DEBUG] Déconnecté !");
|
|
break;
|
|
case WStype_CONNECTED:
|
|
Serial.printf("[DEBUG] Connecté à: %s\n", payload);
|
|
login();
|
|
defineInterface();
|
|
break;
|
|
case WStype_TEXT: {
|
|
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;
|
|
}
|
|
case WStype_ERROR:
|
|
Serial.println("[DEBUG] Erreur !");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(500);
|
|
|
|
// Connexion au Wi-Fi
|
|
Serial.print("Connexion WiFi à ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("\n WiFi connecté");
|
|
Serial.print("IP locale: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
// Connexion au WebSocket
|
|
webSocket.begin(websocket_host, websocket_port, websocket_path);
|
|
webSocket.onEvent(webSocketEvent);
|
|
webSocket.setReconnectInterval(5000);
|
|
|
|
}
|
|
|
|
//----------------------------------------------------------------------
|
|
//Ici on définit les actions sous formes d'actions
|
|
//Pour le moment les parametre ne sont pas encore totalement développer
|
|
//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);
|
|
}
|
|
|
|
void saveFunc(String interfaceName, CommandFunction func, String params){
|
|
Serial.println("ENVOIE DE LINTERFACE AVEC PARAMS");
|
|
commandMap[interfaceName] = func;
|
|
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){
|
|
webSocket.sendTXT("VALUE$"+valueName+"$"+value);
|
|
}
|
|
|
|
//--------------------------------------------------------
|
|
|
|
//--------------------------------------------------------
|
|
//Le void loop ne doit pas être bloquant ( on utilisera millis )
|
|
//--------------------------------------------------------
|
|
void loop() {
|
|
|
|
webSocket.loop();
|
|
|
|
}
|