linky_tic/linky_tic.ino

205 lines
5.3 KiB
C++

#include "serial.h"
#include "secret.h"
#include "tic.h"
#include "ota.h"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ElegantOTA.h>
#include <PubSubClient.h>
// Activer le Wi-Fi
#define WIFI_ENABLE
// Initialiser le serveur web si le Wi-Fi est activé
ESP8266WebServer server(HTTP_PORT);
WiFiClient espClient;
PubSubClient mqttclient(espClient);
unsigned long previousMillis = 0;
unsigned long previousForceMillis = 0;
const long interval = 1000; //interval in ms
void mqttConnect() {
// Loop until we're reconnected
while (!mqttclient.connected()) {
// Create a random client ID
String clientId = MQTT_CLIENTID;
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (mqttclient.connect(clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) {
// Once connected, publish an announcement...
mqttclient.publish(MQTT_TOPIC, "MQTT TIC interface online");
} else {
delay(1000);
}
}
}
// Fonction pour configurer et connecter au réseau Wi-Fi
void setup_wifi() {
delay(10);
// Connexion au réseau Wi-Fi
DebugPort.println();
DebugPort.print("Connecting to ");
DebugPort.println(STASSID);
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
int c = 0;
// Attendre la connexion Wi-Fi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
DebugPort.print(".");
}
DebugPort.println("");
DebugPort.println("WiFi connected");
DebugPort.println("IP address: ");
DebugPort.println(WiFi.localIP());
}
// Fonction pour obtenir les paramètres réseau et matériels
void getSettings() {
String response = "{";
// Ajouter les informations réseau à la réponse JSON
response += "\"ip\": \"" + WiFi.localIP().toString() + "\"";
response += ",\"gw\": \"" + WiFi.gatewayIP().toString() + "\"";
response += ",\"nm\": \"" + WiFi.subnetMask().toString() + "\"";
// Ajouter l'intensité du signal Wi-Fi si demandé
if (server.arg("signalStrength") == "true") {
response += ",\"signalStrengh\": \"" + String(WiFi.RSSI()) + "\"";
}
// Ajouter les informations matérielles si demandé
if (server.arg("chipInfo") == "true") {
response += ",\"chipId\": \"" + String(ESP.getChipId()) + "\"";
response += ",\"flashChipId\": \"" + String(ESP.getFlashChipId()) + "\"";
response += ",\"flashChipSize\": \"" + String(ESP.getFlashChipSize()) + "\"";
response += ",\"flashChipRealSize\": \"" + String(ESP.getFlashChipRealSize()) + "\"";
}
// Ajouter la mémoire libre disponible si demandé
if (server.arg("freeHeap") == "true") {
response += ",\"freeHeap\": \"" + String(ESP.getFreeHeap()) + "\"";
}
response += "}";
// Envoyer la réponse JSON au client
server.send(200, "text/json", response);
}
// Fonction pour obtenir les données TIC complètes
void getTicData() {
String response = ticValuesAsJson();
server.send(200, "text/json", response.c_str());
}
// Fonction pour obtenir les données TIC de base
void getTicBasicData() {
String response = ticBasicValuesAsJson();
server.send(200, "text/json", response.c_str());
}
// Définir les routes du serveur REST
void restServerRouting() {
server.on("/", HTTP_GET, []() {
server.send(200, F("text/html"),
F("Welcome to the REST Web Server"));
});
server.on(F("/settings"), HTTP_GET, getSettings);
server.on(F("/ticdata"), HTTP_GET, getTicData);
server.on(F("/ticbasic"), HTTP_GET, getTicBasicData);
}
// Fonction pour configurer la communication série
void setup_serial() {
//debug interface
//there is only RX on Serial 0 interface
//As the port is shared with usb debug, need to readress it
//thanks to swap.
//The speed is not appropriate for debug.
//Debug serial port is on Serial 1, TX only
DebugPort.begin(TIC_SPEED, SERIAL_7E1);
// Interface d'acquisition des données
#ifdef TIC
TicPort.begin(TIC_SPEED, SERIAL_7E1);
Serial.swap();
#else
TicPort.begin(DEBUG_SPEED);
#endif
}
// Fonction d'initialisation principale
void setup() {
setup_serial();
#ifdef WIFI_ENABLE
setup_wifi();
// Configurer les routes du serveur
restServerRouting();
ElegantOTA.begin(&server);
// ElegantOTA callbacks
ElegantOTA.onStart(onOTAStart);
ElegantOTA.onProgress(onOTAProgress);
ElegantOTA.onEnd(onOTAEnd);
// Démarrer le serveur HTTP
DebugPort.println("Start HTTP server");
server.begin();
DebugPort.println("HTTP server started");
//Démarrer le serveur MQTT
mqttclient.setServer(MQTT_SERVER, MQTT_PORT);
#endif
}
// Boucle principale
void loop() {
ElegantOTA.loop();
server.handleClient();
readTicPort();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
//Interface MQTT
if (!mqttclient.connected()) {
mqttConnect();
}
mqttPublish(&mqttclient);
}
if (currentMillis - previousForceMillis >= (30 * interval)) {
// save the last time you blinked the LED
previousForceMillis = currentMillis;
//Interface MQTT
if (!mqttclient.connected()) {
mqttConnect();
}
mqttForcePublish(&mqttclient);
}
mqttclient.loop();
/*
// Si aucune requête n'est en cours, mettre l'ESP8266 en mode deep sleep
if (server.client().available() == 0) {
goToDeepSleep();
}*/
}