Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 20 additions & 5 deletions src/MqttPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <string.h>
#include <sml/sml_file.h>

#ifdef ESP32_ETH
#include <ETH.h>
#endif

#define MQTT_RECONNECT_DELAY 5
#define MQTT_LWT_TOPIC "LWT"
#define MQTT_LWT_RETAIN true
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<MqttPublisher*>(MQTT_RECONNECT_DELAY, [](MqttPublisher* publisher) {
DEBUG(F("MQTT: Trying to reconnect, Wifi.isConnected = %d"), WiFi.isConnected());
if (WiFi.isConnected() || publisher->networkConnected) {
publisher->connect();
}
});
}, this);
});
}
};
Expand Down
20 changes: 15 additions & 5 deletions src/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ uint64_t millis64()
class SensorConfig
{
public:
HardwareSerial* uart;
const uint8_t pin;
const char *name;
const bool numeric_only;
Expand All @@ -55,10 +56,19 @@ class Sensor
this->config = config;
DEBUG("Initializing sensor %s...", this->config->name);
this->callback = callback;
this->serial = unique_ptr<SoftwareSerial>(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<SoftwareSerial>(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)
Expand All @@ -85,7 +95,7 @@ class Sensor
}

private:
unique_ptr<SoftwareSerial> serial;
unique_ptr<Stream> serial;
byte buffer[BUFFER_SIZE];
size_t position = 0;
unsigned long last_state_reset = 0;
Expand Down
31 changes: 27 additions & 4 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef DEBUG_H
#define DEBUG_H

#include "FormattingSerialDebug.h"
#define ARDUINO_ARCH_ESP8266
#include <FormattingSerialDebug.h>

#include <sml/sml_file.h>
#include <sml/sml_value.h>
#include "unit.h"
Expand Down
73 changes: 71 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
#include <IotWebConf.h>
#include "MqttPublisher.h"
#include "EEPROM.h"
#ifdef ESP32_ETH
#include <ETH.h>
#include <WebServer.h>
#include <HTTPUpdateServer.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#endif

std::list<Sensor *> *sensors = new std::list<Sensor *>();

Expand All @@ -17,7 +23,11 @@ void configSaved();

DNSServer dnsServer;
WebServer server(80);
#ifdef ESP32_ETH
HTTPUpdateServer httpUpdater;
#else
ESP8266HTTPUpdateServer httpUpdater;
#endif
WiFiClient net;

MqttConfig mqttConfig;
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -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(
Expand All @@ -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.");
Expand Down