Compare commits

...

2 Commits

Author SHA1 Message Date
10e1c134e5 Add sleep mode 2025-03-25 22:18:32 +01:00
a0eb3ebaeb comments and code cleaning 2025-03-25 22:06:22 +01:00
2 changed files with 42 additions and 11 deletions

View File

@ -7,16 +7,21 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
// Activer le Wi-Fi
#define WIFI_ENABLE
#define WIFI_ENABLE //enable wifi // Initialiser le serveur web si le Wi-Fi est activé
#define SERVER_NAME "ticweb"
#ifdef WIFI_ENABLE #ifdef WIFI_ENABLE
ESP8266WebServer server(80); ESP8266WebServer server(HTTP_PORT);
#endif #endif
// Durée de sommeil en microsecondes (par exemple, 5 secondes)
const int sleepDuration = 5 * 1000000;
// Fonction pour configurer et connecter au réseau Wi-Fi
void setup_wifi() { void setup_wifi() {
delay(10); delay(10);
// We start by connecting to a WiFi network // Connexion au réseau Wi-Fi
DebugPort.println(); DebugPort.println();
DebugPort.print("Connecting to "); DebugPort.print("Connecting to ");
DebugPort.println(ssid); DebugPort.println(ssid);
@ -25,6 +30,7 @@ void setup_wifi() {
WiFi.begin(ssid, passPhrase); WiFi.begin(ssid, passPhrase);
int c = 0; int c = 0;
// Attendre la connexion Wi-Fi
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
DebugPort.print("."); DebugPort.print(".");
@ -35,42 +41,50 @@ void setup_wifi() {
DebugPort.println(WiFi.localIP()); DebugPort.println(WiFi.localIP());
} }
// Fonction pour obtenir les paramètres réseau et matériels
void getSettings() { void getSettings() {
String response = "{"; String response = "{";
// Ajouter les informations réseau à la réponse JSON
response += "\"ip\": \"" + WiFi.localIP().toString() + "\""; response += "\"ip\": \"" + WiFi.localIP().toString() + "\"";
response += ",\"gw\": \"" + WiFi.gatewayIP().toString() + "\""; response += ",\"gw\": \"" + WiFi.gatewayIP().toString() + "\"";
response += ",\"nm\": \"" + WiFi.subnetMask().toString() + "\""; response += ",\"nm\": \"" + WiFi.subnetMask().toString() + "\"";
// Ajouter l'intensité du signal Wi-Fi si demandé
if (server.arg("signalStrength") == "true") { if (server.arg("signalStrength") == "true") {
response += ",\"signalStrengh\": \"" + String(WiFi.RSSI()) + "\""; response += ",\"signalStrengh\": \"" + String(WiFi.RSSI()) + "\"";
} }
// Ajouter les informations matérielles si demandé
if (server.arg("chipInfo") == "true") { if (server.arg("chipInfo") == "true") {
response += ",\"chipId\": \"" + String(ESP.getChipId()) + "\""; response += ",\"chipId\": \"" + String(ESP.getChipId()) + "\"";
response += ",\"flashChipId\": \"" + String(ESP.getFlashChipId()) + "\""; response += ",\"flashChipId\": \"" + String(ESP.getFlashChipId()) + "\"";
response += ",\"flashChipSize\": \"" + String(ESP.getFlashChipSize()) + "\""; response += ",\"flashChipSize\": \"" + String(ESP.getFlashChipSize()) + "\"";
response += ",\"flashChipRealSize\": \"" + String(ESP.getFlashChipRealSize()) + "\""; response += ",\"flashChipRealSize\": \"" + String(ESP.getFlashChipRealSize()) + "\"";
} }
// Ajouter la mémoire libre disponible si demandé
if (server.arg("freeHeap") == "true") { if (server.arg("freeHeap") == "true") {
response += ",\"freeHeap\": \"" + String(ESP.getFreeHeap()) + "\""; response += ",\"freeHeap\": \"" + String(ESP.getFreeHeap()) + "\"";
} }
response += "}"; response += "}";
// Envoyer la réponse JSON au client
server.send(200, "text/json", response); server.send(200, "text/json", response);
} }
// Fonction pour obtenir les données TIC complètes
void getTicData() { void getTicData() {
String response = ticValuesAsJson(); String response = ticValuesAsJson();
server.send(200, "text/json", response.c_str()); server.send(200, "text/json", response.c_str());
} }
// Fonction pour obtenir les données TIC de base
void getTicBasicData() { void getTicBasicData() {
String response = ticBasicValuesAsJson(); String response = ticBasicValuesAsJson();
server.send(200, "text/json", response.c_str()); server.send(200, "text/json", response.c_str());
} }
// Define routing // Définir les routes du serveur REST
void restServerRouting() { void restServerRouting() {
server.on("/", HTTP_GET, []() { server.on("/", HTTP_GET, []() {
server.send(200, F("text/html"), server.send(200, F("text/html"),
@ -81,6 +95,7 @@ void restServerRouting() {
server.on(F("/ticbasic"), HTTP_GET, getTicBasicData); server.on(F("/ticbasic"), HTTP_GET, getTicBasicData);
} }
// Fonction pour configurer la communication série
void setup_serial() { void setup_serial() {
//debug interface //debug interface
//there is only RX on Serial 0 interface //there is only RX on Serial 0 interface
@ -88,34 +103,47 @@ void setup_serial() {
//thanks to swap. //thanks to swap.
//The speed is not appropriate for debug. //The speed is not appropriate for debug.
//Debug serial port is on Serial 1, TX only //Debug serial port is on Serial 1, TX only
DebugPort.begin(9600, SERIAL_7E1); DebugPort.begin(TIC_SPEED, SERIAL_7E1);
//data acquisition interface // Interface d'acquisition des données
#ifdef TIC #ifdef TIC
TicPort.begin(9600, SERIAL_7E1); TicPort.begin(TIC_SPEED, SERIAL_7E1);
Serial.swap(); Serial.swap();
#else #else
TicPort.begin(115200); TicPort.begin(DEBUG_SPEED);
#endif #endif
} }
// Fonction pour mettre l'ESP8266 en mode deep sleep
void goToDeepSleep() {
DebugPort.println("Going to deep sleep...");
ESP.deepSleep(sleepDuration);
}
// Fonction d'initialisation principale
void setup() { void setup() {
setup_serial(); setup_serial();
#ifdef WIFI_ENABLE #ifdef WIFI_ENABLE
setup_wifi(); setup_wifi();
// Set server routing // Configurer les routes du serveur
restServerRouting(); restServerRouting();
// Start server // Démarrer le serveur HTTP
DebugPort.println("Start HTTP server"); DebugPort.println("Start HTTP server");
server.begin(); server.begin();
DebugPort.println("HTTP server started"); DebugPort.println("HTTP server started");
#endif #endif
} }
// Boucle principale
void loop() { void loop() {
server.handleClient(); server.handleClient();
readTicPort(); readTicPort();
// Si aucune requête n'est en cours, mettre l'ESP8266 en mode deep sleep
if (server.client().available() == 0) {
goToDeepSleep();
}
} }

3
tic.h
View File

@ -7,6 +7,9 @@
#define TIC #define TIC
#define TIC_SPEED 9600 //9600 bps en mode standard ou 1500 bps pour le mode historique
#define DEBUG_SPEED 115200 //vitesse port débug
#define HTTP_PORT 80 //port
// Définition des constantes pour les délimiteurs de trame TIC // Définition des constantes pour les délimiteurs de trame TIC
//CF document ENEDIS //CF document ENEDIS