Skip to content

Commit

Permalink
added ethernet support
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Jul 5, 2024
1 parent 232ee86 commit 886c76a
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Firmware/Bentuino/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@
"USE_WIFI",
"USE_OSC",
"BENTUINO_CREATORTAB",
"USE_ETHERNET=true",
"WIFI_DEFAULT_MODE=MODE_ETH",
"USE_POWER",
"POWER_KEEP_PIN=12",
"POWER_WAKEUP_BUTTON=34",
Expand Down
7 changes: 6 additions & 1 deletion Firmware/Bentuino/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"*.embeddedhtml": "html",
"typeinfo": "cpp",
"functional": "cpp",
"array": "cpp",
Expand Down Expand Up @@ -47,7 +48,11 @@
"regex": "cpp",
"ratio": "cpp",
"thread": "cpp",
"list": "cpp"
"list": "cpp",
"format": "cpp",
"xhash": "cpp",
"xstring": "cpp",
"xutility": "cpp"
},
"cmake.sourceDirectory": "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/creatorclub/FastAccelStepper"
}
2 changes: 2 additions & 0 deletions Firmware/Bentuino/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ board = esp32-poe
build_flags =
${bentuino.build_flags}
-D BENTUINO_CREATORTAB
-D USE_ETHERNET=true
-D WIFI_DEFAULT_MODE=MODE_ETH
-D USE_POWER
-D POWER_KEEP_PIN=12
-D POWER_WAKEUP_BUTTON=34
Expand Down
111 changes: 102 additions & 9 deletions Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ ImplementSingleton(WifiComponent)

AddStringParamConfig(ssid);
AddStringParamConfig(pass);
#ifdef USE_ETHERNET
AddIntParamConfig(mode);

WiFi.onEvent(std::bind(&WifiComponent::WiFiEvent, this, std::placeholders::_1));
#endif

connect();

Expand All @@ -18,7 +23,7 @@ ImplementSingleton(WifiComponent)

void WifiComponent::updateInternal()
{
#ifndef USE_ETHERNET
long curTime = millis();
if (curTime > lastConnectTime + timeBetweenTries)
{
Expand All @@ -33,7 +38,7 @@ void WifiComponent::updateInternal()
#endif
{
setState(Connected);
NDBG("Connected, local IP is " + getIP());

timeAtConnect = -1;
}

Expand Down Expand Up @@ -63,6 +68,7 @@ void WifiComponent::updateInternal()
break;
}
}
#endif
}

void WifiComponent::clearInternal()
Expand All @@ -76,6 +82,22 @@ void WifiComponent::setState(ConnectionState s)
return;

state = s;
switch (state)
{
case Connected:
#ifdef USE_ETHERNET
NDBG("Connected to ethernet with IP " + getIP());

#else
NDBG("Connected to wifi " + ssid + " : " + pass + " with IP " + getIP());
#endif
break;

case ConnectionError:
NDBG("Connection error");
break;
}

timeAtStateChange = millis();
sendEvent(ConnectionStateChanged);
}
Expand All @@ -88,17 +110,51 @@ void WifiComponent::connect()
if (state == Connected || state == Hotspot)
WiFi.disconnect();

WiFi.mode(WIFI_STA);
wifi_mode_t wMode = WIFI_STA;
switch (mode)
{
case MODE_STA:
wMode = WIFI_STA;
break;
case MODE_AP:
wMode = WIFI_AP;
break;
case MODE_AP_STA:
wMode = WIFI_AP_STA;
break;

#ifdef USE_ETHERNET
case MODE_ETH:
wMode = WIFI_OFF;
break;
case MODE_ETH_STA:
wMode = WIFI_STA;
break;
#endif
}

WiFi.mode(wMode);

#ifdef ESP32
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.setSleep(false);
WiFi.setTxPower(WIFI_POWER_19dBm);
#endif

NDBG("Connecting to " + ssid + " : " + pass + "...");
WiFi.begin(ssid.c_str(), pass.c_str());
#ifdef USE_ETHERNET
if (mode == MODE_ETH || mode == MODE_ETH_STA)
{
NDBG("Connecting to ethernet...");
ETH.begin();
}
#endif

if (wMode != WIFI_OFF)
{
NDBG("Connecting to wifi " + ssid + " : " + pass + "...");
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.setSleep(false);
WiFi.setTxPower(WIFI_POWER_19dBm);
WiFi.begin(ssid.c_str(), pass.c_str());
}

setState(Connecting);
}
Expand All @@ -115,10 +171,47 @@ void WifiComponent::disconnect()
setState(Off);
}

#ifdef USE_ETHERNET
void WifiComponent::WiFiEvent(WiFiEvent_t event)
{
switch (event)
{

case ARDUINO_EVENT_ETH_START:
ETH.setHostname(DeviceID.c_str());
setState(Connecting);
break;

case ARDUINO_EVENT_ETH_CONNECTED:
setState(Connected);
break;

case ARDUINO_EVENT_ETH_GOT_IP:
setState(Connected);
break;

case ARDUINO_EVENT_ETH_DISCONNECTED:
setState(ConnectionError);
break;

case ARDUINO_EVENT_ETH_STOP:
setState(ConnectionError);
break;

default:
break;
}
}
#endif

String WifiComponent::getIP() const
{
if (state == Connected)
#ifdef USE_ETHERNET
return StringHelpers::ipToString(ETH.localIP());
#else
return StringHelpers::ipToString(WiFi.localIP());
#endif

else if (state == Hotspot)
return StringHelpers::ipToString(WiFi.softAPIP());
Expand Down
46 changes: 46 additions & 0 deletions Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
#pragma once

#ifdef ESP32

#ifdef USE_ETHERNET

#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 12

#include <ETH.h>
#endif

#include <WiFi.h>

#elif defined ESP8266
#include <ESP8266WiFi.h>
#endif

#ifndef WIFI_DEFAULT_MODE
#define WIFI_DEFAULT_MODE WIFI_MODE_STA
#endif

DeclareComponentSingleton(Wifi, "wifi", )

enum ConnectionState { Off,
Expand All @@ -28,8 +42,27 @@ long timeAtStateChange;

ConnectionState state;

enum WifiMode
{
MODE_STA,
MODE_AP,
MODE_AP_STA,
#ifdef USE_ETHERNET
MODE_ETH,
MODE_ETH_STA,
#endif
MODE_MAX
};
const String wifiModeNames[MODE_MAX]{
"Wifi", "AP", "Wifi+AP",
#ifdef USE_ETHERNET
"Ethernet", "Wifi+Ethernet"
#endif
};

DeclareStringParam(ssid, "");
DeclareStringParam(pass, "");
DeclareIntParam(mode, WIFI_DEFAULT_MODE);

bool initInternal(JsonObject o) override;
void updateInternal() override;
Expand All @@ -42,21 +75,34 @@ void setAP();
void disable();
void setState(ConnectionState s);

#ifdef USE_ETHERNET
void WiFiEvent(WiFiEvent_t event);
#endif

String getIP() const;

HandleSetParamInternalStart
CheckAndSetParam(ssid);
CheckAndSetParam(pass);
#ifdef USE_ETHERNET
CheckAndSetParam(mode);
#endif
HandleSetParamInternalEnd;

FillSettingsInternalStart
FillSettingsParam(ssid);
FillSettingsParam(pass);
#ifdef USE_ETHERNET
FillSettingsParam(mode);
#endif
FillSettingsInternalEnd;

FillOSCQueryInternalStart
FillOSCQueryStringParam(ssid);
FillOSCQueryStringParam(pass);
#ifdef USE_ETHERNET
FillOSCQueryEnumParam(mode, wifiModeNames, WIFI_MODE_MAX);
#endif
FillOSCQueryInternalEnd;

DeclareComponentEventTypes(ConnectionStateChanged);
Expand Down

0 comments on commit 886c76a

Please sign in to comment.