First Commit

This commit is contained in:
OC01Dev_Pierre 2025-10-14 18:23:35 +02:00
commit 0b713a07dc
1 changed files with 84 additions and 0 deletions

84
main.cpp Normal file
View File

@ -0,0 +1,84 @@
#include <WiFi.h>
#include <WebSocketsClient.h>
// ⚙️ 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();
}