Skip to content
This repository was archived by the owner on Jan 11, 2020. It is now read-only.
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
67 changes: 60 additions & 7 deletions Basecamp.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ bool Basecamp::isSetupModeWifiEncrypted(){
* Returns the SSID of the setup WiFi network
*/
String Basecamp::getSetupModeWifiName(){
return wifi.getAPName();
#ifndef BASECAMP_NOWIFI
return wifi.getAPName();
#else
return "";
#endif
}

/**
Expand All @@ -76,6 +80,7 @@ String Basecamp::getSetupModeWifiSecret(){
*/
bool Basecamp::begin(String fixedWiFiApEncryptionPassword)
{
#ifndef BASECAMP_NOWIFI
// Make sure we only accept valid passwords for ap
if (fixedWiFiApEncryptionPassword.length() != 0) {
if (fixedWiFiApEncryptionPassword.length() >= wifi.getMinimumSecretLength()) {
Expand All @@ -84,6 +89,7 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword)
Serial.println("Error: Given fixed ap secret is too short. Refusing.");
}
}
#endif

// Enable serial output
Serial.begin(115200);
Expand All @@ -108,7 +114,6 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword)
checkResetReason();

#ifndef BASECAMP_NOWIFI

// If there is no access point secret set yet, generate one and save it.
// It will survive the default config reset.
if (!configuration.isKeySet(ConfigurationKey::accessPointSecret) ||
Expand Down Expand Up @@ -140,6 +145,24 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword)
// Get WiFi MAC
mac = wifi.getSoftwareMacAddress(":");
#endif

#ifndef BASECAMP_NOETH
eth.begin(hostname);
if(!configuration.isKeySet(ConfigurationKey::ethConfigured))
if(configuration.get(ConfigurationKey::ethConfigured) != "false"){
const auto &ethIP = configuration.get("EthIP");
const auto &ethGateway = configuration.get("EthGateway");
const auto &ethSubnetMask = configuration.get("EthSubnetMask");
const auto &ethDNS1 = configuration.get("EthDNS1");
const auto &ethDNS2 = configuration.get("EthDNS2");

eth.config(ethIP, ethGateway, ethSubnetMask, ethDNS1, ethDNS2);
}


macEth = eth.getMac();
#endif

#ifndef BASECAMP_NOMQTT
// Check if MQTT has been disabled by the user
if (!configuration.get(ConfigurationKey::mqttActive).equalsIgnoreCase("false")) {
Expand Down Expand Up @@ -248,14 +271,28 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword)
web.setInterfaceElementAttribute("configform", "onsubmit", "collectConfiguration()");

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


#ifndef BASECAMP_NOWIFI
// 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

#ifndef BASECAMP_NOETH
// Add an input field for the eth data and link it to the corresponding configuration data
web.addInterfaceElement("EthIP", "input", "ETH IP:","#configform" , "EthIP");
web.addInterfaceElement("EthGateway", "input", "ETH Gateway:", "#configform", "EthGateway");
web.addInterfaceElement("EthSubnet", "input", "ETH Subnet:", "#configform", "EthSubnet");
web.addInterfaceElement("EthDNS1", "input", "ETH DNS 1:", "#configform", "EthDNS1");
web.addInterfaceElement("EthDNS2", "input", "ETH DNS 2:", "#configform", "EthDNS2");
web.addInterfaceElement("EthConfigured", "input", "", "#configform", "EthConfigured");
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")) {
Expand All @@ -272,9 +309,16 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword)
web.addInterfaceElement("saveform", "button", "Save","#configform");
web.setInterfaceElementAttribute("saveform", "type", "submit");

#ifndef BASECAMP_NOWIFI
// Show the devices MAC in the Webinterface
String infotext2 = "This device has the MAC-Address: " + mac;
String infotext2 = "This device has the WiFi MAC-Address: " + mac;
web.addInterfaceElement("infotext2", "p", infotext2,"#wrapper");
#endif

#ifndef BASECAMP_NOETH
String infotext3 = "This device has the ETH MAC-Address: " + macEth;
web.addInterfaceElement("infotext3", "p", infotext3,"#wrapper");
#endif

web.addInterfaceElement("footer", "footer", "Powered by ", "body");
web.addInterfaceElement("footerlink", "a", "Basecamp", "footer");
Expand Down Expand Up @@ -318,8 +362,11 @@ void Basecamp::handle (void)

bool Basecamp::shouldEnableConfigWebserver() const
{
return (configurationUi_ == ConfigurationUI::always ||
(configurationUi_ == ConfigurationUI::accessPoint && wifi.getOperationMode() == WifiControl::Mode::accessPoint));
return (configurationUi_ == ConfigurationUI::always
#ifndef BASECAMP_NOWIFI
|| (configurationUi_ == ConfigurationUI::accessPoint && wifi.getOperationMode() == WifiControl::Mode::accessPoint)
#endif
);
}

// This is a task that is called if MQTT client has lost connection. After 2 seconds it automatically trys to reconnect.
Expand All @@ -336,7 +383,7 @@ void Basecamp::connectToMqtt(TimerHandle_t xTimer)
{
AsyncMqttClient *mqtt = (AsyncMqttClient *) pvTimerGetTimerID(xTimer);

if (WiFi.status() == WL_CONNECTED) {
if (WiFi.status() == WL_CONNECTED || EthControl::status() == ETH_GOT_IP ) {
Serial.println("Trying to connect ...");
mqtt->connect(); // has no effect if already connected ( if (_connected) return;)
}
Expand Down Expand Up @@ -431,8 +478,14 @@ void Basecamp::checkResetReason()
// TODO: expand infos
String Basecamp::showSystemInfo() {
std::ostringstream info;
#ifndef BASECAMP_NOWIFI
info << "MAC-Address: " << mac.c_str();
info << ", Hardware MAC: " << wifi.getHardwareMacAddress(":").c_str() << std::endl;
#endif

#ifndef BASECAMP_NOETH
info << "ETH MAC-Address:" << macEth.c_str() << std::endl;
#endif

if (configuration.isKeySet(ConfigurationKey::accessPointSecret)) {
info << "*******************************************" << std::endl;
Expand Down
9 changes: 9 additions & 0 deletions Basecamp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "WifiControl.hpp"
#endif

#ifndef BASECAMP_NOETH
#include "EthControl.hpp"
#endif

#ifndef BASECAMP_NOWEB
#ifdef BASECAMP_USEDNS
#include <DNSServer.h>
Expand Down Expand Up @@ -82,6 +86,11 @@ class Basecamp
WifiControl wifi;
#endif

#ifndef BASECAMP_NOETH
String macEth;
EthControl eth;
#endif

#ifndef BASECAMP_NOMQTT
AsyncMqttClient mqtt;
static TimerHandle_t mqttReconnectTimer;
Expand Down
4 changes: 3 additions & 1 deletion Configuration.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
enum class ConfigurationKey {
deviceName,
accessPointSecret,
ethConfigured,
wifiConfigured,
wifiEssid,
wifiPassword,
Expand All @@ -43,7 +44,8 @@ static const String getKeyName(ConfigurationKey key)

case ConfigurationKey::accessPointSecret:
return "APSecret";

case ConfigurationKey::ethConfigured:
return "EthConfigured";
case ConfigurationKey::wifiConfigured:
return "WifiConfigured";

Expand Down
76 changes: 76 additions & 0 deletions EthControl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "EthControl.hpp"


void EthControl::begin(String hostname) {
ETH.begin();
ETH.setHostname(hostname.c_str());
WiFi.onEvent(WiFiEvent);
}

bool EthControl::config(const String& ip, const String& gateway, const String& subnet, const String& dns1, const String& dns2 ) {
IPAddress mip, mgateway, msubnet, mdns1, mdns2;
mip.fromString(ip);
mgateway.fromString(gateway);
msubnet.fromString(subnet);
mdns1.fromString(dns1);
mdns2.fromString(dns2);

return config(mip, mgateway, msubnet, mdns1, mdns1);
}


bool EthControl::config(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2 )
{
return ETH.config(ip, gateway, subnet, dns1, dns2);
}


IPAddress EthControl::getIP() {
return ETH.localIP();
}

String EthControl::getMac() {
return ETH.macAddress();
}

eth_state_t EthControl::_state = ETH_STOPPED;

eth_state_t EthControl::status() {
return EthControl::_state;
}

void EthControl::WiFiEvent(WiFiEvent_t event){
switch (event) {
case SYSTEM_EVENT_ETH_START:
EthControl::_state = ETH_STARTED;
DEBUG_PRINTLN("ETH Started");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
EthControl::_state = ETH_CONNECTED;
DEBUG_PRINTLN("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
EthControl::_state = 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");
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
EthControl::_state = ETH_DISCONNECTED;
DEBUG_PRINTLN("ETH Disconnected");
break;
case SYSTEM_EVENT_ETH_STOP:
EthControl::_state = ETH_STOPPED;
DEBUG_PRINTLN("ETH Stopped");
break;
default:
break;
}
}
45 changes: 45 additions & 0 deletions EthControl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef EthControl_h
#define EthControl_h

// CONFIG FOR WAVESHARE LAN8720 WITH POWER EN MOD BY SAUTTER
//#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
//#define ETH_POWER_PIN 17
//#define ETH_TYPE ETH_PHY_LAN8720
//#define ETH_ADDR 1
//#define ETH_MDC_PIN 23
//#define ETH_MDIO_PIN 18

#include "debug.hpp"
#include <iomanip>
#include <sstream>
#include <ETH.h>
#include <WiFiClient.h>
#include <Preferences.h>
#include <IPAddress.h>

typedef enum eth_state_t {
ETH_STARTED,
ETH_CONNECTED,
ETH_GOT_IP,
ETH_DISCONNECTED,
ETH_STOPPED
};

class EthControl {
public:
EthControl(){};

void begin(String hostname = "BasecampDevice");
bool config(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2 );
bool config(const String& ip, const String& gateway, const String& subnet, const String& dns1, const String& dns2 );
bool connected();
IPAddress getIP();
String getMac();
static eth_state_t status();

static void WiFiEvent(WiFiEvent_t event);
private:
static eth_state_t _state;
};

#endif //EthControl_h
2 changes: 1 addition & 1 deletion external/ArduinoJson
Submodule ArduinoJson updated 188 files
2 changes: 1 addition & 1 deletion external/AsyncTCP