diff --git a/platformio.ini b/platformio.ini index 2778e55..9b0dd77 100644 --- a/platformio.ini +++ b/platformio.ini @@ -51,3 +51,12 @@ build_flags = ${common.build_flags} -DSERIAL_DEBUG=true -DSERIAL_DEBUG_VERBOSE=f upload_port = /dev/ttyUSB0 monitor_port = /dev/ttyUSB0 monitor_speed = 115200 + +[env:esp32-poe] +platform = espressif32 +board = esp32-poe +framework = arduino +lib_deps = ${common.lib_deps} +lib_ldf_mode = ${common.lib_ldf_mode} +build_flags = ${common.build_flags} -DSERIAL_DEBUG=true -DSERIAL_DEBUG_VERBOSE=false +monitor_speed = 115200 \ No newline at end of file diff --git a/src/MqttPublisher.h b/src/MqttPublisher.h index 26eb6b1..c49e2c9 100644 --- a/src/MqttPublisher.h +++ b/src/MqttPublisher.h @@ -9,6 +9,10 @@ #include #include +#ifdef ESP32_ETH +#include +#endif + #define MQTT_RECONNECT_DELAY 5 #define MQTT_LWT_TOPIC "LWT" #define MQTT_LWT_RETAIN true @@ -148,8 +152,14 @@ class MqttPublisher this->reconnectTimer.detach(); } + void setNetworkConnected(bool connected) + { + networkConnected = connected; + } + private: bool connected = false; + bool networkConnected = false; MqttConfig config; AsyncMqttClient client; Ticker reconnectTimer; @@ -188,18 +198,23 @@ class MqttPublisher this->reconnectTimer.detach(); DEBUG(F("MQTT: Connection established.")); char message[64]; - snprintf(message, 64, "Hello from %08X, running SMLReader version %s.", ESP.getChipId(), VERSION); +#ifdef ESP32_ETH + snprintf(message, 64, "Hello from %s, running SMLReader version %s.", ETH.macAddress().c_str(), VERSION); +#else + snprintf(message, 64, "Hello from %08X, running SMLReader version %s.", ESP.getChip(), VERSION); +#endif info(message); publish(baseTopic + MQTT_LWT_TOPIC, MQTT_LWT_PAYLOAD_ONLINE, MQTT_LWT_QOS, MQTT_LWT_RETAIN); }); client.onDisconnect([this](AsyncMqttClientDisconnectReason reason) { this->connected = false; DEBUG(F("MQTT: Disconnected. Reason: %d."), reason); - reconnectTimer.attach(MQTT_RECONNECT_DELAY, [this]() { - if (WiFi.isConnected()) { - this->connect(); + reconnectTimer.attach(MQTT_RECONNECT_DELAY, [](MqttPublisher* publisher) { + DEBUG(F("MQTT: Trying to reconnect, Wifi.isConnected = %d"), WiFi.isConnected()); + if (WiFi.isConnected() || publisher->networkConnected) { + publisher->connect(); } - }); + }, this); }); } }; diff --git a/src/Sensor.h b/src/Sensor.h index 151877a..fa68507 100644 --- a/src/Sensor.h +++ b/src/Sensor.h @@ -37,6 +37,7 @@ uint64_t millis64() class SensorConfig { public: + HardwareSerial* uart; const uint8_t pin; const char *name; const bool numeric_only; @@ -55,10 +56,19 @@ class Sensor this->config = config; DEBUG("Initializing sensor %s...", this->config->name); this->callback = callback; - this->serial = unique_ptr(new SoftwareSerial()); - this->serial->begin(9600, SWSERIAL_8N1, this->config->pin, -1, false); - this->serial->enableTx(false); - this->serial->enableRx(true); + if (config->uart == nullptr) + { + auto newSerial = unique_ptr(new SoftwareSerial()); + newSerial->begin(9600, SWSERIAL_8N1, this->config->pin, -1, false); + newSerial->enableTx(false); + newSerial->enableRx(true); + this->serial = move(newSerial); + } + else + { + config->uart->begin(9600, SERIAL_8N1, config->pin, -1); + this->serial.reset(config->uart); + } DEBUG("Initialized sensor %s.", this->config->name); if (this->config->status_led_enabled) @@ -85,7 +95,7 @@ class Sensor } private: - unique_ptr serial; + unique_ptr serial; byte buffer[BUFFER_SIZE]; size_t position = 0; unsigned long last_state_reset = 0; diff --git a/src/config.h b/src/config.h index 4fba9a4..58822e3 100644 --- a/src/config.h +++ b/src/config.h @@ -13,14 +13,37 @@ const char *CONFIG_VERSION = "1.0.2"; const char *WIFI_AP_SSID = "SMLReader"; const char *WIFI_AP_DEFAULT_PASSWORD = ""; +#define ESP32_ETH + static const SensorConfig SENSOR_CONFIGS[] = { - {.pin = D2, + {.uart = &Serial1, + .pin = 2, .name = "1", .numeric_only = false, - .status_led_enabled = true, + .status_led_enabled = false, + .status_led_inverted = true, + .status_led_pin = 0, + .interval = 0}, + + {.uart = nullptr, + .pin = 36, + .name = "2", + .numeric_only = false, + .status_led_enabled = false, .status_led_inverted = true, - .status_led_pin = LED_BUILTIN, - .interval = 0}}; + .status_led_pin = 0, + .interval = 0}, + + {.uart = &Serial2, + .pin = 16, + .name = "3", + .numeric_only = false, + .status_led_enabled = false, + .status_led_inverted = true, + .status_led_pin = 0, + .interval = 0} + +}; const uint8_t NUM_OF_SENSORS = sizeof(SENSOR_CONFIGS) / sizeof(SensorConfig); diff --git a/src/debug.h b/src/debug.h index ef60daf..f89c905 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,7 +1,9 @@ #ifndef DEBUG_H #define DEBUG_H -#include "FormattingSerialDebug.h" +#define ARDUINO_ARCH_ESP8266 +#include + #include #include #include "unit.h" diff --git a/src/main.cpp b/src/main.cpp index c6d53c6..90b12bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,9 +6,15 @@ #include #include "MqttPublisher.h" #include "EEPROM.h" +#ifdef ESP32_ETH +#include +#include +#include +#else #include #include #include +#endif std::list *sensors = new std::list(); @@ -17,7 +23,11 @@ void configSaved(); DNSServer dnsServer; WebServer server(80); +#ifdef ESP32_ETH +HTTPUpdateServer httpUpdater; +#else ESP8266HTTPUpdateServer httpUpdater; +#endif WiFiClient net; MqttConfig mqttConfig; @@ -33,6 +43,58 @@ iotwebconf::TextParameter mqttTopicParam = iotwebconf::TextParameter("MQTT topic iotwebconf::ParameterGroup paramGroup = iotwebconf::ParameterGroup("MQTT Settings", ""); boolean needReset = false; +boolean validConfig = false; + +bool eth_connected = false; +void OnEthernetEvent(WiFiEvent_t event) +{ + switch (event) { + case ARDUINO_EVENT_ETH_START: + Serial.println("ETH Started"); + //set eth hostname here + ETH.setHostname("esp32-ethernet"); + break; + case ARDUINO_EVENT_ETH_CONNECTED: + Serial.println("ETH Connected"); + break; + case ARDUINO_EVENT_ETH_GOT_IP: + Serial.print("ETH MAC: "); + Serial.print(ETH.macAddress()); + Serial.print(", IPv4: "); + Serial.print(ETH.localIP()); + if (ETH.fullDuplex()) { + Serial.print(", FULL_DUPLEX"); + } + Serial.print(", "); + Serial.print(ETH.linkSpeed()); + Serial.println("Mbps"); + eth_connected = true; + if (validConfig) + { + publisher.connect(); + } + break; + case ARDUINO_EVENT_ETH_DISCONNECTED: + Serial.println("ETH Disconnected"); + eth_connected = false; + if (validConfig) + { + publisher.disconnect(); + } + break; + case ARDUINO_EVENT_ETH_STOP: + Serial.println("ETH Stopped"); + eth_connected = false; + if (validConfig) + { + publisher.disconnect(); + } + break; + default: + break; + } + publisher.setNetworkConnected(eth_connected); +} void process_message(byte *buffer, size_t len, Sensor *sensor) { @@ -57,6 +119,11 @@ void setup() delay(2000); #endif +#ifdef ESP32_ETH + WiFi.onEvent(OnEthernetEvent); + ETH.begin(); +#endif + // Setup reading heads DEBUG("Setting up %d configured sensors...", NUM_OF_SENSORS); const SensorConfig *config = SENSOR_CONFIGS; @@ -82,10 +149,12 @@ void setup() iotWebConf.setConfigSavedCallback(&configSaved); iotWebConf.setWifiConnectionCallback(&wifiConnected); - +#ifndef ESP32_ETH WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) { publisher.disconnect(); }); +#endif + // -- Define how to handle updateServer calls. iotWebConf.setupUpdateServer( @@ -94,7 +163,7 @@ void setup() [](const char *userName, char *password) { httpUpdater.updateCredentials(userName, password); }); - boolean validConfig = iotWebConf.init(); + validConfig = iotWebConf.init(); if (!validConfig) { DEBUG("Missing or invalid config. MQTT publisher disabled.");