Skip to content
This repository has been archived by the owner on Jan 11, 2020. It is now read-only.

Add ethernet support #74

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions Basecamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,15 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword)

web.addInterfaceElement("DeviceName", "input", "Device name","#configform" , "DeviceName");

#ifndef BASECAMP_WIRED_NETWORK
// Add an input field for the WIFI data and link it to the corresponding configuration data
web.addInterfaceElement("WifiEssid", "input", "WIFI SSID:","#configform" , "WifiEssid");
web.addInterfaceElement("WifiPassword", "input", "WIFI Password:", "#configform", "WifiPassword");
web.setInterfaceElementAttribute("WifiPassword", "type", "password");
web.addInterfaceElement("WifiConfigured", "input", "", "#configform", "WifiConfigured");
web.setInterfaceElementAttribute("WifiConfigured", "type", "hidden");
web.setInterfaceElementAttribute("WifiConfigured", "value", "true");

#endif
// Add input fields for MQTT configurations if it hasn't been disabled
if (!configuration.get(ConfigurationKey::mqttActive).equalsIgnoreCase("false")) {
web.addInterfaceElement("MQTTHost", "input", "MQTT Host:","#configform" , "MQTTHost");
Expand Down Expand Up @@ -336,7 +337,7 @@ void Basecamp::connectToMqtt(TimerHandle_t xTimer)
{
AsyncMqttClient *mqtt = (AsyncMqttClient *) pvTimerGetTimerID(xTimer);

if (WiFi.status() == WL_CONNECTED) {
if (WifiControl::isConnected()) {
Serial.println("Trying to connect ...");
mqtt->connect(); // has no effect if already connected ( if (_connected) return;)
}
Expand Down
4 changes: 2 additions & 2 deletions Configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ class Configuration {

private:
static void CheckConfigStatus(void *);
// Set to true if configuration is memory-only
bool _memOnlyConfig;
String _jsonFile;
bool _configurationTainted = false;
String noResult_ = {};
// Set to true if configuration is memory-only
bool _memOnlyConfig;
};

#endif
3 changes: 1 addition & 2 deletions WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ namespace {
}

WebServer::WebServer()
: events("/events")
, server(80)
: server(80), events("/events")
{
server.addHandler(&events);
#ifdef BASECAMP_USEDNS
Expand Down
77 changes: 76 additions & 1 deletion WifiControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@
*/

#include "WifiControl.hpp"
#ifdef BASECAMP_WIRED_NETWORK
#include <ETH.h>
#endif

namespace {
// Minumum access point secret length to be generated (8 is min for ESP32)
const constexpr unsigned minApSecretLength = 8;
static bool eth_connected = false;
}

void WifiControl::begin(String essid, String password, String configured,
String hostname, String apSecret)
{
#ifdef BASECAMP_WIRED_NETWORK
DEBUG_PRINTLN("Connecting to Ethernet");
operationMode_ = Mode::client;
WiFi.onEvent(WiFiEvent);
ETH.begin() ;
ETH.setHostname(hostname.c_str());
DEBUG_PRINTLN ("Ethernet initialized") ;
DEBUG_PRINTLN ("Waiting for connection") ;
while (!eth_connected) {
DEBUG_PRINT (".") ;
delay(100) ;
}
#else
DEBUG_PRINTLN("Connecting to Wifi");

String _wifiConfigured = std::move(configured);
_wifiEssid = std::move(essid);
_wifiPassword = std::move(password);
Expand Down Expand Up @@ -49,6 +65,18 @@ void WifiControl::begin(String essid, String password, String configured,
WiFi.softAP(_wifiAPName.c_str());
}
}
#endif

}


bool WifiControl::isConnected()
{
#ifdef BASECAMP_WIRED_NETWORK
return eth_connected ;
#else
return WiFi.isConnected() ;
#endif
}

WifiControl::Mode WifiControl::getOperationMode() const
Expand All @@ -61,7 +89,11 @@ int WifiControl::status() {

}
IPAddress WifiControl::getIP() {
#ifdef BASECAMP_WIRED_NETWORK
return ETH.localIP() ;
#else
return WiFi.localIP();
#endif
}
IPAddress WifiControl::getSoftAPIP() {
return WiFi.softAPIP();
Expand All @@ -83,6 +115,39 @@ void WifiControl::WiFiEvent(WiFiEvent_t event)
// In case somebody wants to know this..
DEBUG_PRINTF("[WiFi-event] event. Bootcounter is %d\n", bootCounter);
DEBUG_PRINTF("[WiFi-event] event: %d\n", event);
#ifdef BASECAMP_WIRED_NETWORK
switch (event) {
case SYSTEM_EVENT_ETH_START:
DEBUG_PRINTLN("ETH Started");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
DEBUG_PRINTLN("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
DEBUG_PRINT("ETH MAC: ");
DEBUG_PRINT(ETH.macAddress());
DEBUG_PRINT(", IPv4: ");
DEBUG_PRINT(ETH.localIP());
if (ETH.fullDuplex()) {
DEBUG_PRINT(", FULL_DUPLEX");
}
DEBUG_PRINT(", ");
DEBUG_PRINT(ETH.linkSpeed());
DEBUG_PRINTLN("Mbps");
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
DEBUG_PRINTLN("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
DEBUG_PRINTLN("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
#else
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
DEBUG_PRINT("Wifi IP address: ");
Expand All @@ -97,6 +162,7 @@ void WifiControl::WiFiEvent(WiFiEvent_t event)
// INFO: Default = do nothing
break;
}
#endif
}

namespace {
Expand All @@ -120,16 +186,25 @@ namespace {
// See https://github.com/espressif/esp-idf/blob/master/components/esp32/include/esp_system.h
String WifiControl::getHardwareMacAddress(const String& delimiter)
{
#ifdef BASECAMP_WIRED_NETWORK
return ETH.macAddress() ;
#else
uint8_t rawMac[6];
esp_efuse_mac_get_default(rawMac);
return format6Bytes(rawMac, delimiter);
#endif
}

String WifiControl::getSoftwareMacAddress(const String& delimiter)
{
#ifdef BASECAMP_WIRED_NETWORK
return ETH.macAddress() ;
#else
uint8_t rawMac[6];
WiFi.macAddress(rawMac);
return format6Bytes(rawMac, delimiter);
#endif

}

unsigned WifiControl::getMinimumSecretLength() const
Expand Down
1 change: 1 addition & 0 deletions WifiControl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WifiControl {
WifiControl(){};
bool connect();
bool disconnect();
static bool isConnected() ;

Mode getOperationMode() const;

Expand Down