Cours:Wt32-eth01 : Différence entre versions
| (Une révision intermédiaire par le même utilisateur non affichée) | |||
| Ligne 1 : | Ligne 1 : | ||
| + | =liens= | ||
| + | |||
| + | *https://github.com/egnor/wt32-eth01 | ||
| + | |||
| + | =programme minimum avec ota et HomeAssistant= | ||
<source lang=cpp> | <source lang=cpp> | ||
| − | + | #define DEBUG | |
| − | + | #define CFG_ON_SERIAL | |
| − | |||
| − | + | // Important to be defined BEFORE including ETH.h for ETH.begin() to work. | |
| + | // Example RMII LAN8720 (Olimex, etc.) | ||
#ifndef ETH_PHY_TYPE | #ifndef ETH_PHY_TYPE | ||
#define ETH_PHY_TYPE ETH_PHY_LAN8720 | #define ETH_PHY_TYPE ETH_PHY_LAN8720 | ||
| Ligne 17 : | Ligne 22 : | ||
#include <ETH.h> | #include <ETH.h> | ||
| − | #include < | + | // OTA |
| − | #include< | + | #include <WebServer.h> |
| − | + | #include <Update.h> | |
| + | WebServer server(80); | ||
| + | |||
| + | |||
#include <WiFiClient.h> | #include <WiFiClient.h> | ||
| + | #include "Arduino.h" | ||
| − | + | #include "HomeAssistantMQTT.h" | |
| − | |||
| − | // | + | ///////////////////////////////////////////////////////////////////////////////// |
| − | + | /// Settings | |
| − | + | ///////////////////////////////////////////////////////////////////////////////// | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | const char* mqtt_server = "192.168.x.x"; // MQTT Server URL | |
| − | + | const uint16_t mqtt_server_port = 1883; // MQTT Server port | |
| − | // | + | String _MQTTUSER = "mqttusername"; // MQTT Username |
| − | + | String _MQTTPASSWORD = "mqttpassword"; // MQTT Password | |
| − | |||
| − | |||
| − | |||
| + | ///////////////////////////////////////////////////////////////////////////////// | ||
| + | WiFiClient ethClient; | ||
| + | HomeAssistantMQTT mqtt; | ||
| + | bool _bMqttConfigPublished = false; | ||
static bool eth_connected = false; | static bool eth_connected = false; | ||
| + | |||
// WARNING: onEvent is called from a separate FreeRTOS task (thread)! | // WARNING: onEvent is called from a separate FreeRTOS task (thread)! | ||
| Ligne 76 : | Ligne 82 : | ||
} | } | ||
} | } | ||
| − | |||
| − | |||
| − | |||
void setup() { | void setup() { | ||
| − | + | #if defined(DEBUG) || defined(CFG_ON_SERIAL) | |
| − | + | Serial.begin(115200); | |
| + | delay(1000); | ||
| + | #endif | ||
| + | Network.onEvent(onEvent); | ||
#if ESP_ARDUINO_VERSION_MAJOR >= 3 | #if ESP_ARDUINO_VERSION_MAJOR >= 3 | ||
// The argument order changed in esp32-arduino v3+ | // The argument order changed in esp32-arduino v3+ | ||
| Ligne 89 : | Ligne 95 : | ||
ETH.begin(1, 16, 23, 18, ETH_PHY_LAN8720, ETH_CLOCK_GPIO0_IN); | ETH.begin(1, 16, 23, 18, ETH_PHY_LAN8720, ETH_CLOCK_GPIO0_IN); | ||
#endif | #endif | ||
| + | initOTA(); | ||
| + | #ifdef CFG_ON_SERIAL | ||
| + | Serial.println(""); | ||
| + | Serial.println("WiFi connected"); | ||
| + | Serial.print("IP address: "); | ||
| + | Serial.print("MAC: "); | ||
| + | #endif | ||
| − | + | mqtt.MqttUser = _MQTTUSER; | |
| + | mqtt.MqttPassword = _MQTTPASSWORD; | ||
| + | mqtt.Manufacturer = "iut"; | ||
| + | mqtt.Model = "1"; | ||
| + | mqtt.Version = "1"; | ||
| + | mqtt.Name = "geii"; | ||
| + | mqtt.DeviceName = "demo"; | ||
| + | mqtt.setCallback(HAMQTT_Callback); | ||
| + | |||
| + | mqtt.begin(ðClient, mqtt_server, mqtt_server_port); | ||
| + | } | ||
| − | + | void loop() { | |
| − | |||
| − | |||
| − | + | server.handleClient(); | |
| + | mqtt.loop(); | ||
| + | if (mqtt.connected()) | ||
| + | { | ||
| + | if (!_bMqttConfigPublished) | ||
| + | { | ||
| + | // Publish config upon boot | ||
| + | publishMqttConfig(); | ||
| + | // Try to read actual values from MQTT to restore previous state | ||
| + | //mqtt.readValues(); | ||
| + | _bMqttConfigPublished = true; | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| − | + | } | |
} | } | ||
| − | + | void publishMqttConfig() | |
| + | { | ||
| + | #ifdef DEBUG | ||
| + | Serial.println("Publishing config:"); | ||
| + | #endif | ||
| + | mqtt.publishConfigSwitch("", "enableCharge", "mdi:power-plug-battery", "true"); | ||
| + | |||
| + | } | ||
| − | + | void HAMQTT_Callback(String item, String payload, bool readFromMQTT) | |
| − | |||
| − | |||
| − | void | ||
| − | |||
{ | { | ||
| − | Serial. | + | #ifdef DEBUG |
| − | if( | + | Serial.print("Message arrived for item: '"); |
| − | + | Serial.print(item); | |
| + | Serial.print("' with payload: "); | ||
| + | Serial.print(payload); | ||
| + | if (readFromMQTT) | ||
| + | Serial.print(" (read from MQTT)"); | ||
| + | Serial.println(); | ||
| + | #endif | ||
| + | |||
| + | // PROCESS NEW DATA HERE | ||
| + | |||
| + | if (item != "Command") | ||
| + | { | ||
| + | if (item == "enableCharge") | ||
| + | { | ||
| + | mqtt.setValue(item, payload); | ||
| + | mqtt.sendValues(); | ||
} | } | ||
| + | } | ||
} | } | ||
| + | |||
</source> | </source> | ||
Version actuelle datée du 20 janvier 2026 à 22:44
liens
programme minimum avec ota et HomeAssistant
#define DEBUG
#define CFG_ON_SERIAL
// Important to be defined BEFORE including ETH.h for ETH.begin() to work.
// Example RMII LAN8720 (Olimex, etc.)
#ifndef ETH_PHY_TYPE
#define ETH_PHY_TYPE ETH_PHY_LAN8720
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC 23
#define ETH_PHY_MDIO 18
#define ETH_PHY_POWER -1
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
#endif
#include <ETH.h>
// OTA
#include <WebServer.h>
#include <Update.h>
WebServer server(80);
#include <WiFiClient.h>
#include "Arduino.h"
#include "HomeAssistantMQTT.h"
/////////////////////////////////////////////////////////////////////////////////
/// Settings
/////////////////////////////////////////////////////////////////////////////////
const char* mqtt_server = "192.168.x.x"; // MQTT Server URL
const uint16_t mqtt_server_port = 1883; // MQTT Server port
String _MQTTUSER = "mqttusername"; // MQTT Username
String _MQTTPASSWORD = "mqttpassword"; // MQTT Password
/////////////////////////////////////////////////////////////////////////////////
WiFiClient ethClient;
HomeAssistantMQTT mqtt;
bool _bMqttConfigPublished = false;
static bool eth_connected = false;
// WARNING: onEvent is called from a separate FreeRTOS task (thread)!
void onEvent(arduino_event_id_t event) {
switch (event) {
case ARDUINO_EVENT_ETH_START:
Serial.println("ETH Started");
// The hostname must be set after the interface is started, but needs
// to be set before DHCP, so set it from the event handler thread.
ETH.setHostname("esp32-ethernet");
break;
case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.println("ETH Got IP");
Serial.println(ETH);
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_LOST_IP:
Serial.println("ETH Lost IP");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default: break;
}
}
void setup() {
#if defined(DEBUG) || defined(CFG_ON_SERIAL)
Serial.begin(115200);
delay(1000);
#endif
Network.onEvent(onEvent);
#if ESP_ARDUINO_VERSION_MAJOR >= 3
// The argument order changed in esp32-arduino v3+
ETH.begin(ETH_PHY_LAN8720, 1, 23, 18, 16, ETH_CLOCK_GPIO0_IN);
#else
ETH.begin(1, 16, 23, 18, ETH_PHY_LAN8720, ETH_CLOCK_GPIO0_IN);
#endif
initOTA();
#ifdef CFG_ON_SERIAL
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.print("MAC: ");
#endif
mqtt.MqttUser = _MQTTUSER;
mqtt.MqttPassword = _MQTTPASSWORD;
mqtt.Manufacturer = "iut";
mqtt.Model = "1";
mqtt.Version = "1";
mqtt.Name = "geii";
mqtt.DeviceName = "demo";
mqtt.setCallback(HAMQTT_Callback);
mqtt.begin(ðClient, mqtt_server, mqtt_server_port);
}
void loop() {
server.handleClient();
mqtt.loop();
if (mqtt.connected())
{
if (!_bMqttConfigPublished)
{
// Publish config upon boot
publishMqttConfig();
// Try to read actual values from MQTT to restore previous state
//mqtt.readValues();
_bMqttConfigPublished = true;
}
}
}
void publishMqttConfig()
{
#ifdef DEBUG
Serial.println("Publishing config:");
#endif
mqtt.publishConfigSwitch("", "enableCharge", "mdi:power-plug-battery", "true");
}
void HAMQTT_Callback(String item, String payload, bool readFromMQTT)
{
#ifdef DEBUG
Serial.print("Message arrived for item: '");
Serial.print(item);
Serial.print("' with payload: ");
Serial.print(payload);
if (readFromMQTT)
Serial.print(" (read from MQTT)");
Serial.println();
#endif
// PROCESS NEW DATA HERE
if (item != "Command")
{
if (item == "enableCharge")
{
mqtt.setValue(item, payload);
mqtt.sendValues();
}
}
}