commit 0b713a07dcbace19c15a75d629c4f7a370fda11b Author: OC01Dev_Pierre Date: Tue Oct 14 18:23:35 2025 +0200 First Commit diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d8c115d --- /dev/null +++ b/main.cpp @@ -0,0 +1,84 @@ +#include +#include + +// ⚙️ Infos WiFi +const char* ssid = "StCaillou"; +const char* password = "02070500"; + +// ⚙️ Infos WebSocket +const char* websocket_host = "193.70.38.222"; // Remplace par ton IP publique si tu testes depuis un autre réseau +const uint16_t websocket_port = 4000; +const char* websocket_path = "/ws"; + +//Info sur l'identification du système +String mac = WiFi.macAddress(); + +int i = 0; + +WebSocketsClient webSocket; + +//Pour récuperer l'adress mac on utilise WiFi.macAddress(); + +void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { + switch(type) { + case WStype_DISCONNECTED: + Serial.println("[WSc] Déconnecté !"); + break; + case WStype_CONNECTED: + Serial.printf("[WSc] Connecté à: %s\n", payload); + webSocket.sendTXT(" Bonjour depuis l'ESP32 !"); + break; + case WStype_TEXT: + Serial.printf("[WSc] Message reçu: %s\n", payload); + break; + case WStype_ERROR: + Serial.println("[WSc] 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); // Essayer de se reconnecter toutes les 5s +} + +//Permet la connexion au serveur CANDLE pour récuperer un identifiant +//Cela nous permettra de s'assurer que nos appareils sont connu +void login(){ + //Connection pour récupération du token + webSocket.sendTXT("LOGIN$"+mac); +} + +void sendInterface(String interfaceName){ + //Comment spécifier le nombre de parametre ? + //On peut aussi faire une interface qui ne prends que un parametre pas un parametre + webSocket.sendTXT("REGISTER$"+mac+"$"+interfaceName); +} + +void sendValue(String valueName, String value){ + webSocket.sendTXT("VALUE$"+mac+"$"+valueName+"$"+value); +} + +void loop() { + webSocket.loop(); +}