Skip to content

Commit

Permalink
make W5500 detection GPIO configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Jan 1, 2023
1 parent 6f58ebd commit 3bbf9aa
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 18 deletions.
44 changes: 37 additions & 7 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Network* Network::_inst = nullptr;

RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];

Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences, const String& maintenancePathPrefix)
Network::Network(Preferences *preferences, const String& maintenancePathPrefix)
: _preferences(preferences)
{
_inst = this;
Expand All @@ -23,19 +23,44 @@ Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences
{
_maintenancePathPrefix[i] = maintenancePathPrefix.charAt(i);
}
setupDevice(networkDevice);
setupDevice();
}


void Network::setupDevice(NetworkDeviceType hardware)
void Network::setupDevice()
{

if(strcmp(WiFi_fallbackDetect, "wifi_fallback") == 0)
{
Log->println(F("Switching to WiFi device as fallabck."));
hardware = NetworkDeviceType::WiFi;
Log->println(F("Switching to WiFi device as fallback."));
_networkDeviceType = NetworkDeviceType::WiFi;
}
else
{
int hardwareDetect = _preferences->getInt(preference_network_hardware_detect);

switch(hardware)
if(hardwareDetect == 0)
{
hardwareDetect = 26;
_preferences->putInt(preference_network_hardware_detect, hardwareDetect);
}

if(hardwareDetect == -1)
{
Log->println(F("W5500 hardware is disable, using Wifi."));
_networkDeviceType = NetworkDeviceType::WiFi;
}
else
{
Log->print(F("Using PIN "));
Log->print(hardwareDetect);
Log->println(F(" for network device selection"));

pinMode(hardwareDetect, INPUT_PULLUP);
_networkDeviceType = digitalRead(hardwareDetect) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500;
}
}

switch(_networkDeviceType)
{
case NetworkDeviceType::W5500:
Log->println(F("Network device: W5500"));
Expand Down Expand Up @@ -775,3 +800,8 @@ void Network::publishPresenceDetection(char *csv)
{
_presenceCsv = csv;
}

const NetworkDeviceType Network::networkDeviceType()
{
return _networkDeviceType;
}
8 changes: 6 additions & 2 deletions Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum class NetworkDeviceType
class Network
{
public:
explicit Network(const NetworkDeviceType networkDevice, Preferences* preferences, const String& maintenancePathPrefix);
explicit Network(Preferences* preferences, const String& maintenancePathPrefix);

void initialize();
int update();
Expand Down Expand Up @@ -46,10 +46,12 @@ class Network
PubSubClient* mqttClient();
bool isMqttConnected();

const NetworkDeviceType networkDeviceType();

private:
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
void setupDevice(NetworkDeviceType hardware);
void setupDevice();
bool reconnect();

void buildMqttPath(const char* prefix, const char* path, char* outPath);
Expand Down Expand Up @@ -78,5 +80,7 @@ class Network
unsigned long _lastMaintenanceTs = 0;
unsigned long _lastRssiTs = 0;

NetworkDeviceType _networkDeviceType = (NetworkDeviceType)-1;

int8_t _lastRssi = 127;
};
1 change: 0 additions & 1 deletion Pins.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#define NETWORK_SELECT 26
#define ETHERNET_CS_PIN 5
#define ETHERNET_RESET_PIN 33
#define TRIGGER_LOCK_PIN 32
Expand Down
1 change: 1 addition & 0 deletions PreferencesKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define preference_mqtt_crt "mqttcrt"
#define preference_mqtt_key "mqttkey"
#define preference_mqtt_hass_discovery "hassdiscovery"
#define preference_network_hardware_detect "nwhwdt"
#define preference_hostname "hostname"
#define preference_network_timeout "nettmout"
#define preference_restart_on_disconnect "restdisc"
Expand Down
2 changes: 1 addition & 1 deletion Version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define nuki_hub_version "6.7"
#define nuki_hub_version "6.8"
52 changes: 52 additions & 0 deletions WebCfgServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ bool WebCfgServer::processArgs(String& message)
_preferences->putString(preference_mqtt_key, value);
configChanged = true;
}
else if(key == "NWHWDT")
{
_preferences->putInt(preference_network_hardware_detect, value.toInt());
configChanged = true;
}
else if(key == "HASSDISCOVERY")
{
if(_preferences->getString(preference_mqtt_hass_discovery) != value)
Expand Down Expand Up @@ -588,6 +593,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response)
printTextarea(response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE);
printTextarea(response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE);
printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE);
printDropDown(response, "NWHWDT", "Network hardward detection", String(_preferences->getInt(preference_network_hardware_detect)), getNetworkDetectionOptions());
printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5);
printCheckBox(response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect));
printInputField(response, "RSTTMR", "Restart timer (minutes; -1 to disable)", _preferences->getInt(preference_restart_timer), 10);
Expand Down Expand Up @@ -793,6 +799,36 @@ void WebCfgServer::printTextarea(String& response,
response.concat("</td></tr>");
}

void WebCfgServer::printDropDown(String &response, const char *token, const char *description, const String preselectedValue, const std::vector<std::pair<String, String>> options)
{
response.concat("<tr><td>");
response.concat(description);
response.concat("</td><td>");

response.concat("<select name=\"");
response.concat(token);
response.concat("\">");

for(const auto option : options)
{
if(option.first == preselectedValue)
{
response.concat("<option selected=\"selected\" value=\"");
}
else
{
response.concat("<option value=\"");
}
response.concat(option.first);
response.concat("\">");
response.concat(option.second);
response.concat("</option>");
}

response.concat("</select>");
response.concat("</td></tr>");
}

void WebCfgServer::buildNavigationButton(String &response, const char *caption, const char *targetPath)
{
response.concat("<form method=\"get\" action=\"");
Expand Down Expand Up @@ -895,3 +931,19 @@ void WebCfgServer::sendFavicon()
{
_server.send(200, "image/png", (const char*)favicon_32x32, sizeof(favicon_32x32));
}

const std::vector<std::pair<String, String>> WebCfgServer::getNetworkDetectionOptions() const
{
std::vector<std::pair<String, String>> options;

options.push_back(std::make_pair("-1", "Disable W5500"));

for(int i=16; i <= 33; i++)
{
String txt = "Detect W5500 via GPIO ";
txt.concat(i);
options.push_back(std::make_pair(String(i), txt));
}

return options;
}
3 changes: 3 additions & 0 deletions WebCfgServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ class WebCfgServer
void printInputField(String& response, const char* token, const char* description, const int value, size_t maxLength);
void printCheckBox(String& response, const char* token, const char* description, const bool value);
void printTextarea(String& response, const char *token, const char *description, const char *value, const size_t maxLength);
void printDropDown(String &response, const char *token, const char *description, const String preselectedValue, std::vector<std::pair<String, String>> options);
void buildNavigationButton(String& response, const char* caption, const char* targetPath);

const std::vector<std::pair<String, String>> getNetworkDetectionOptions() const;

void printParameter(String& response, const char* description, const char* value);

String generateConfirmCode();
Expand Down
10 changes: 3 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ void initPreferences()

void setup()
{
pinMode(NETWORK_SELECT, INPUT_PULLUP);

Serial.begin(115200);
Log = &Serial;
Expand All @@ -171,11 +170,8 @@ void setup()
restartTs = preferences->getInt(preference_restart_timer) * 60 * 1000;
}

// const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi;
const NetworkDeviceType networkDevice = digitalRead(NETWORK_SELECT) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500;

const String mqttLockPath = preferences->getString(preference_mqtt_lock_path);
network = new Network(networkDevice, preferences, mqttLockPath);
network = new Network(preferences, mqttLockPath);
network->initialize();
networkLock = new NetworkLock(network, preferences);
networkLock->initialize();
Expand All @@ -189,7 +185,7 @@ void setup()
preferences->putUInt(preference_deviceId, deviceId);
}

initEthServer(networkDevice);
initEthServer(network->networkDeviceType());

bleScanner = new BleScanner::Scanner();
bleScanner->initialize("NukiHub");
Expand All @@ -216,7 +212,7 @@ void setup()
nukiOpener->initialize();
}

webCfgServer = new WebCfgServer(nuki, nukiOpener, network, ethServer, preferences, networkDevice == NetworkDeviceType::WiFi);
webCfgServer = new WebCfgServer(nuki, nukiOpener, network, ethServer, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi);
webCfgServer->initialize();

presenceDetection = new PresenceDetection(preferences, bleScanner, network);
Expand Down
Binary file modified webflash/nuki_hub.bin
Binary file not shown.

0 comments on commit 3bbf9aa

Please sign in to comment.