1
modification
(image) |
(ajouter le code pour envoyer les variables sur une page web servie par l' ESP D1) |
||
Ligne 122 : | Ligne 122 : | ||
delay(2000); // Pause de 2 secondes avant la prochaine lecture | delay(2000); // Pause de 2 secondes avant la prochaine lecture | ||
} | } | ||
</syntaxhighlight>Visualisation par serveur web<syntaxhighlight lang="c"> | |||
#include <ESP8266WiFi.h> | |||
#include <ESP8266WebServer.h> | |||
#include <ModbusMaster.h> | |||
const char* ssid = "Pixelm"; | |||
const char* password = "bienveillance"; | |||
ESP8266WebServer server(80); | |||
ModbusMaster node; | |||
// Variables pour stocker les données Modbus | |||
float voltage = 0; | |||
float current = 0; | |||
float power = 0; | |||
float energy = 0; | |||
// Constante pour le délai entre les lectures | |||
const unsigned long READ_INTERVAL = 2000; | |||
unsigned long lastReadTime = 0; | |||
void setup() { | |||
Serial.begin(9600); | |||
Serial.println("Initialisation Modbus..."); | |||
// Initialisation Modbus | |||
node.begin(1, Serial); // Adresse esclave 1, communication via le port série | |||
// Connexion Wi-Fi | |||
connectToWiFi(); | |||
// Initialisation serveur Web | |||
server.on("/", handleRoot); | |||
server.begin(); | |||
Serial.println("Serveur HTTP démarré."); | |||
} | |||
void loop() { | |||
server.handleClient(); | |||
// Lecture des données Modbus toutes les READ_INTERVAL millisecondes | |||
if (millis() - lastReadTime >= READ_INTERVAL) { | |||
lastReadTime = millis(); | |||
readModbusData(); | |||
} | |||
} | |||
void connectToWiFi() { | |||
WiFi.begin(ssid, password); | |||
Serial.println("Connexion au Wi-Fi..."); | |||
unsigned long startTime = millis(); | |||
while (WiFi.status() != WL_CONNECTED) { | |||
if (millis() - startTime > 30000) { // Limite de 30 secondes pour la connexion | |||
Serial.println("Erreur de connexion au Wi-Fi."); | |||
return; | |||
} | |||
delay(1000); | |||
Serial.print("."); | |||
} | |||
Serial.println(""); | |||
Serial.println("Connecté au Wi-Fi."); | |||
Serial.print("Adresse IP: "); | |||
Serial.println(WiFi.localIP()); | |||
} | |||
void readModbusData() { | |||
uint8_t result; | |||
uint16_t data[8]; | |||
// Lecture de 8 registres à partir de l'adresse 0x0000 | |||
result = node.readInputRegisters(0x0000, 8); | |||
if (result == node.ku8MBSuccess) { | |||
// Stocker les données dans le tableau `data` | |||
for (int i = 0; i < 8; i++) { | |||
data[i] = node.getResponseBuffer(i); | |||
} | |||
// Mise à jour des variables globales | |||
voltage = data[0] * 0.01; | |||
current = data[1] * 0.01; | |||
power = (data[3] << 16 | data[2]) * 0.1; | |||
energy = (data[5] << 16 | data[4]) * 1; | |||
// Affichage des valeurs lues sur le port série | |||
printModbusValues(); | |||
} else { | |||
Serial.print("Erreur lors de la lecture des registres: "); | |||
Serial.println(result, HEX); | |||
} | |||
} | |||
void printModbusValues() { | |||
Serial.print("Tension: "); | |||
Serial.print(voltage); | |||
Serial.println(" V"); | |||
Serial.print("Courant: "); | |||
Serial.print(current); | |||
Serial.println(" A"); | |||
Serial.print("Puissance: "); | |||
Serial.print(power); | |||
Serial.println(" W"); | |||
Serial.print("Energie: "); | |||
Serial.print(energy); | |||
Serial.println(" Wh"); | |||
} | |||
void handleRoot() { | |||
String html = "<html><head><title>Données Modbus</title></head><body>"; | |||
html += "<h1>Données Modbus</h1>"; | |||
html += "<p><strong>Tension: </strong>" + String(voltage) + " V</p>"; | |||
html += "<p><strong>Courant: </strong>" + String(current) + " A</p>"; | |||
html += "<p><strong>Puissance: </strong>" + String(power) + " W</p>"; | |||
html += "<p><strong>Energie: </strong>" + String(energy) + " Wh</p>"; | |||
html += "</body></html>"; | |||
server.send(200, "text/html", html); | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |
modification