Skip to content

Commit

Permalink
Merge pull request #3 from pvtex/Ethernet
Browse files Browse the repository at this point in the history
Ethernet preparations, and small fixes and adjustments
  • Loading branch information
pvtex authored Jun 18, 2024
2 parents 72fd549 + 2a41d5e commit c8d6d51
Show file tree
Hide file tree
Showing 18 changed files with 505 additions and 71 deletions.
15 changes: 15 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ monitor_speed = ${common.monitor_speed}
board_build.flash_mode = ${common.flash_mode}
board_build.partitions = ${common.partitions}

[env:ethernet]
board_build.f_cpu = ${common.f_cpu}
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board}
lib_deps = ${common.lib_deps}
extra_scripts = scripts/GENdeploy.py
build_flags = ${common.build_flags}
-DETHERNET
build_src_flags = ${common.build_src_flags}
upload_speed = ${common.upload_speed}
monitor_speed = ${common.monitor_speed}
board_build.flash_mode = ${common.flash_mode}
board_build.partitions = ${common.partitions}

; generic firmware for debugging purposes
[env:debug]
board_build.f_cpu = ${common.f_cpu}
Expand Down
42 changes: 41 additions & 1 deletion src/WiFiEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ esp_err_t WiFiEventHandler::eventHandler(void* ctx, system_event_t* event) {
break;
}

case SYSTEM_EVENT_ETH_START: {
rc = pWiFiEventHandler->EthStart();
break;
}
case SYSTEM_EVENT_ETH_CONNECTED: {
rc = pWiFiEventHandler->EthConnected();
break;
}
case SYSTEM_EVENT_ETH_GOT_IP: {
rc = pWiFiEventHandler->EthGotIp();
break;
}
case SYSTEM_EVENT_ETH_DISCONNECTED: {
rc = pWiFiEventHandler->EthDisconnected();
break;
}
case SYSTEM_EVENT_ETH_STOP: {
rc = pWiFiEventHandler->EthStop();
break;
}

default:
break;
}
Expand Down Expand Up @@ -139,6 +160,10 @@ esp_err_t WiFiEventHandler::staGotIp(system_event_sta_got_ip_t info) {
return ESP_OK;
} // staGotIp

esp_err_t WiFiEventHandler::EthGotIp() {
ESP_LOGD(LOG_TAG, "default EthGotIp");
return ESP_OK;
}

/**
* @brief Handle the Access Point started event.
Expand All @@ -150,7 +175,6 @@ esp_err_t WiFiEventHandler::apStart() {
return ESP_OK;
} // apStart


/**
* @brief Handle the Access Point stop event.
* Handle an indication that the ESP32 has stopped being an access point.
Expand All @@ -173,12 +197,20 @@ esp_err_t WiFiEventHandler::staStart() {
return ESP_OK;
} // staStart

esp_err_t WiFiEventHandler::EthStart() {
ESP_LOGD(LOG_TAG, "default EthStart");
return ESP_OK;
}

esp_err_t WiFiEventHandler::staStop() {
ESP_LOGD(LOG_TAG, "default staStop");
return ESP_OK;
} // staStop

esp_err_t WiFiEventHandler::EthStop() {
ESP_LOGD(LOG_TAG, "default EthStop");
return ESP_OK;
}

/**
* @brief Handle the Station Connected event.
Expand All @@ -191,6 +223,10 @@ esp_err_t WiFiEventHandler::staConnected(system_event_sta_connected_t info) {
return ESP_OK;
} // staConnected

esp_err_t WiFiEventHandler::EthConnected() {
ESP_LOGD(LOG_TAG, "default staConnected");
return ESP_OK;
}

/**
* @brief Handle the Station Disconnected event.
Expand All @@ -203,6 +239,10 @@ esp_err_t WiFiEventHandler::staDisconnected(system_event_sta_disconnected_t info
return ESP_OK;
} // staDisconnected

esp_err_t WiFiEventHandler::EthDisconnected() {
ESP_LOGD(LOG_TAG, "default staDisconnected");
return ESP_OK;
}

/**
* @brief Handle a Station Connected to AP event.
Expand Down
5 changes: 5 additions & 0 deletions src/WiFiEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ class WiFiEventHandler {
virtual esp_err_t staStart();
virtual esp_err_t staStop();
virtual esp_err_t wifiReady();
virtual esp_err_t EthConnected();
virtual esp_err_t EthDisconnected();
virtual esp_err_t EthGotIp();
virtual esp_err_t EthStart();
virtual esp_err_t EthStop();

/**
* Get the next WiFi event handler in the chain, if there is one.
Expand Down
19 changes: 18 additions & 1 deletion src/config.esp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ bool ICACHE_FLASH_ATTR loadConfiguration(Config &config)
Serial.print(F("[ INFO ] config file: "));
Serial.println(configFile);
#endif
DynamicJsonDocument json(2048);
//DynamicJsonDocument json(2048);
JsonDocument json;
auto error = deserializeJson(json, buf.get(), size);
if (error)
{
Expand Down Expand Up @@ -243,18 +244,34 @@ bool ICACHE_FLASH_ATTR loadConfiguration(Config &config)
{
config.ipAddress.fromString(network["ip"].as<const char*>());
}
if (network.containsKey("ipeth"))
{
config.ipAddressEth.fromString(network["ipeth"].as<const char*>());
}
if (network.containsKey("subnet"))
{
config.subnetIp.fromString(network["subnet"].as<const char*>());
}
if (network.containsKey("subneteth"))
{
config.subnetIpEth.fromString(network["subneteth"].as<const char*>());
}
if (network.containsKey("gateway"))
{
config.gatewayIp.fromString(network["gateway"].as<const char*>());
}
if (network.containsKey("gatewayeth"))
{
config.gatewayIpEth.fromString(network["gatewayeth"].as<const char*>());
}
if (network.containsKey("dns"))
{
config.dnsIp.fromString(network["dns"].as<const char*>());
}
if (network.containsKey("dnseth"))
{
config.dnsIpEth.fromString(network["dnseth"].as<const char*>());
}

const char *apipch;
if (config.wifiApIp)
Expand Down
5 changes: 5 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ struct Config {
byte bssid[6] = {0, 0, 0, 0, 0, 0};
char *deviceHostname = NULL;
bool dhcpEnabled = true;
bool dhcpEnabledEth = true;
IPAddress dnsIp;
IPAddress dnsIpEth = (0,0,0,0);
uint8_t doorbellpin = 255;
char *doorName[MAX_NUM_RELAYS];
uint8_t doorstatpin = 255;
bool fallbackMode = false;
IPAddress gatewayIp;
IPAddress gatewayIpEth = (0,0,0,0);
char *httpPass = NULL;
IPAddress ipAddress;
IPAddress ipAddressEth = (192,168,5,1);
uint8_t ledwaitingpin = 255;
int lockType[MAX_NUM_RELAYS];
uint8_t maxOpenDoorTime = 0;
Expand Down Expand Up @@ -48,6 +52,7 @@ struct Config {
int relayType[MAX_NUM_RELAYS];
bool removeParityBits = true;
IPAddress subnetIp;
IPAddress subnetIpEth = (255,255,255,0);
const char *ssid;
char *tzInfo = (char *)"";
const char *wifiApIp = NULL;
Expand Down
133 changes: 133 additions & 0 deletions src/ethernet.esp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <ETH.h>


/* RMII */
#define GPIO_RMII_RXD0 25
#define GPIO_RMII_RXD1 26
#define GPIO_RMII_CRS 27
#define GPIO_RMII_TXD1 22
#define GPIO_RMII_TXD0 19
#define GPIO_RMII_TXEN 21
#define GPIO_RMII_CLK 17
#define GPIO_RMII_MDC 23
#define GPIO_RMII_MDIO 18
#define GPIO_RMII_PWR 0

// ethernet pin define
#define USE_ETHERNET // Add support for ethernet (+20k code)
// Dingtian Ethernet
#define ETH_PHY_TYPE ETH_PHY_JL1101
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC GPIO_RMII_MDC
#define ETH_PHY_MDIO GPIO_RMII_MDIO
#define ETH_PHY_POWER GPIO_RMII_PWR
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT

uint8_t mac[6] = {0xbc, 0x34, 0x00, 0x00, 0x00, 0x01};
//// eth event callback
int eth_connected = false;


void setEnableEth()
{
doEnableEth = true;
}

void onEthConnect(WiFiEvent_t event, WiFiEventInfo_t inf0)
{
#ifdef DEBUG
Serial.println(F("[ INFO ] Ethernet Connected"));
#endif
mqttReconnectTimer.detach();
}

void onEthDisconnect(WiFiEvent_t event, WiFiEventInfo_t inf0)
{
if ( !ETH.connected() )
{
return;
}
#ifdef DEBUG
Serial.println(F("[ INFO ] ETH Disconnected"));
#endif
mqttReconnectTimer.detach();
disconnectMqtt();
}

void onEthGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
{
#ifdef DEBUG
Serial.print("[ INFO ] Ethernet IP Connected: ");
Serial.println(ETH.localIP().toString());
#endif
connectToMqtt();
}

bool ICACHE_FLASH_ATTR connectETh()
{

if (!config.dhcpEnabled)
{
ETH.config(config.ipAddress, config.gatewayIp, config.subnetIp, config.dnsIp);
}
#ifdef DEBUG
Serial.print(F("[ INFO ] Trying to connect Ethernet: "));
#endif
unsigned long now = millis();
uint8_t timeout = 15; // define when to time out in seconds
do
{
ledWifiStatus();
delay(500);
#ifdef DEBUG
if (!ETH.isConnected())
Serial.print(F("."));
#endif
if (ETH.connected())
break;
} while (millis() - now < timeout * 1000);

// We now out of the while loop, either time is out or we connected. check what happened
if (ETH.connected())
{
String data = ETH.localIP().toString();
writeEvent("INFO", "ETH", "EThernet is connected", data);
#ifdef DEBUG
Serial.println(F("[ INFO ] Ethernet is connected"));
#endif
return true;
}
else
{
#ifdef DEBUG
Serial.println();
Serial.println(F("[ WARN ] Couldn't connect in time"));
#endif
return false;
}
}

void ICACHE_FLASH_ATTR disableEth()
{
#ifdef DEBUG
Serial.println(F("Turn Ethernet off."));
#endif
}

void ICACHE_FLASH_ATTR enableEthernet()
{

}

void setupEth(bool configured)
{
if (!configured)
{
//ETH.SetHostname(config.hostnm);
} else
{
//enableEth();
ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_POWER, ETH_CLK_MODE);
ETH.config(config.ipAddressEth, config.gatewayIpEth, config.subnetIpEth, config.dnsIpEth, config.dnsIpEth);
}
}
12 changes: 8 additions & 4 deletions src/log.esp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ void extern mqttPublishEvent(JsonDocument *root);

void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String data)
{
DynamicJsonDocument root(512);
//DynamicJsonDocument root(512);
JsonDocument root;
root["type"] = type;
root["src"] = src;
root["desc"] = desc;
Expand All @@ -28,7 +29,8 @@ void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String d

void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype, int access = ACCESS_GRANTED)
{
DynamicJsonDocument root(512);
//DynamicJsonDocument root(512);
JsonDocument root;
root["uid"] = uid;
root["username"] = username;
root["acctype"] = acctype;
Expand Down Expand Up @@ -56,7 +58,8 @@ void ICACHE_FLASH_ATTR sendLogFile(int page, String fileName, int logFileType, A
if (page == 1)
lastPos = 0;
float pages;
DynamicJsonDocument root(2048);
//DynamicJsonDocument root(2048);
JsonDocument root;
if (logFileType == LOGTYPE_EVENTLOG)
root["command"] = "eventlist";
if (logFileType == LOGTYPE_LATESTLOG)
Expand Down Expand Up @@ -227,7 +230,8 @@ void ICACHE_FLASH_ATTR logMaintenance(String action, String filename, AsyncWebSo
void ICACHE_FLASH_ATTR sendFileList(int page, AsyncWebSocketClient *client)
{

DynamicJsonDocument root(512);
//DynamicJsonDocument root(512);
JsonDocument root;
root["command"] = "listfiles";
root["page"] = page;
JsonArray items = root.createNestedArray("list");
Expand Down
Loading

0 comments on commit c8d6d51

Please sign in to comment.