Skip to content

Commit

Permalink
feat: Add support for setting fixed IP in WEB config
Browse files Browse the repository at this point in the history
  • Loading branch information
melkati committed May 20, 2024
1 parent 5819d5a commit 4330f82
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 22 deletions.
11 changes: 8 additions & 3 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ String mqttClientId = UNITHOSTNAME;
String mqttBroker = MQTT_BROKER_SERVER;
String mqttUser = "";
String mqttPass = "";
String wifiSSID = WIFI_SSID_CREDENTIALS;
String wifiPass = WIFI_PW_CREDENTIALS;
String wifiSSID = "";
String wifiPass = "";
String MACAddress = "Unset";
uint8_t peerESPNowAddress[] = ESPNOW_PEER_MAC_ADDRESS;

Expand All @@ -45,6 +45,12 @@ uint64_t timeToRetryTroubledWIFI = 300; // Time in seconds to retry WIFI connec
uint16_t WiFiConnectionRetries = 0;
uint16_t maxWiFiConnectionRetries = 10;
bool wifiChanged = false;
bool useStaticIP = false; // Set to true if you want to use a static IP
IPAddress staticIP(192, 168, 1, 199); // Change this to the desired IP
IPAddress gateway(192, 168, 1, 1); // Change this to your network's gateway
IPAddress subnet(255, 255, 255, 0); // Change this to your network's subnet mask
IPAddress dns1(8, 8, 8, 8); // Change this to your preferred DNS server
IPAddress dns2(8, 8, 4, 4); // Change this to your secondary DNS server

// MQTT options
bool activeMQTT = true;
Expand Down Expand Up @@ -130,7 +136,6 @@ bool captivePortalActive = false;
uint16_t timeToWaitForCaptivePortal = 60; // Time in seconds to wait for captive portal
#endif


#ifdef CUSTOM_I2C_SDA
#undef I2C_SDA
#define I2C_SDA CUSTOM_I2C_SDA
Expand Down
2 changes: 2 additions & 0 deletions CO2_Gadget_Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ MENU(wifiConfigMenu, "WIFI Config", doNothing, noEvent, wrapStyle
#ifdef SUPPORT_OTA
,SUBMENU(activeOTAMenu)
#endif
,OP("Set fixed IP", doNothing, noEvent)
,OP("in WEB config.", doNothing, noEvent)
,EXIT("<Back"));


Expand Down
55 changes: 55 additions & 0 deletions CO2_Gadget_Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ void printPreferences() {
Serial.println("-->[PREF] wifiPass:\t#" + wifiPass + "#");
#endif
Serial.println("-->[PREF] hostName:\t#" + hostName + "#");

// Fixed IP
Serial.println("-->[PREF] useStaticIP:\t #" + String(useStaticIP) + "#");
Serial.println("-->[PREF] staticIP:\t #" + staticIP.toString() + "#");
Serial.println("-->[PREF] gateway:\t #" + gateway.toString() + "#");
Serial.println("-->[PREF] subnet:\t #" + subnet.toString() + "#");
Serial.println("-->[PREF] dns1:\t #" + dns1.toString() + "#");
Serial.println("-->[PREF] dns2:\t #" + dns2.toString() + "#");

Serial.println("-->[PREF] selCO2Sensor:\t #" + String(selectedCO2Sensor) + "#");
Serial.println("-->[PREF] debugSensors is:\t#" + String(debugSensors ? "Enabled" : "Disabled") + "# (" + String(debugSensors) + ")");
Serial.println("-->[PREF] displayReverse is:\t#" + String(displayReverse ? "Reversed" : "Normal") + "# (" + String(displayReverse) + ")");
Expand All @@ -218,6 +227,7 @@ void printPreferences() {
Serial.println("-->[PREF] durationBuzzerBeep is:\t#" + String(durationBuzzerBeep) + "#");
Serial.println("-->[PREF] timeBetweenBuzzerBeeps is:\t#" + String(timeBetweenBuzzerBeeps) + "#");


Serial.println();
delay(50);
}
Expand Down Expand Up @@ -287,6 +297,15 @@ void initPreferences() {
wifiSSID = preferences.getString("wifiSSID", wifiSSID).c_str();
wifiPass = preferences.getString("wifiPass", wifiPass).c_str();
hostName = preferences.getString("hostName", hostName).c_str();

// Fixed IP
useStaticIP = preferences.getBool("useStaticIP", false);
staticIP.fromString(preferences.getString("staticIP", staticIP.toString()).c_str());
gateway.fromString(preferences.getString("gateway", gateway.toString()).c_str());
subnet.fromString(preferences.getString("subnet", subnet.toString()).c_str());
dns1.fromString(preferences.getString("dns1", dns1.toString()).c_str());
dns2.fromString(preferences.getString("dns2", dns2.toString()).c_str());

selectedCO2Sensor = preferences.getUInt("selCO2Sensor", 0);
debugSensors = preferences.getBool("debugSensors", false);
displayReverse = preferences.getBool("displayReverse", false);
Expand Down Expand Up @@ -392,6 +411,15 @@ void putPreferences() {
preferences.putString("wifiSSID", wifiSSID);
preferences.putString("wifiPass", wifiPass);
preferences.putString("hostName", hostName);

// Fixed IP
preferences.putBool("useStaticIP", useStaticIP);
preferences.putString("staticIP", staticIP.toString());
preferences.putString("gateway", gateway.toString());
preferences.putString("subnet", subnet.toString());
preferences.putString("dns1", dns1.toString());
preferences.putString("dns2", dns2.toString());

preferences.putUInt("selCO2Sensor", selectedCO2Sensor);
preferences.putBool("debugSensors", debugSensors);
preferences.putBool("displayReverse", displayReverse);
Expand Down Expand Up @@ -479,6 +507,15 @@ String getPreferencesAsJson() {
doc["wifiSSID"] = preferences.getString("wifiSSID", wifiSSID);
// doc["wifiPass"] = preferences.getString("wifiPass", wifiPass);
doc["hostName"] = preferences.getString("hostName", hostName);

// Fixed IP
doc["useStaticIP"] = preferences.getBool("useStaticIP", false);
doc["staticIP"] = preferences.getString("staticIP", staticIP.toString());
doc["gateway"] = preferences.getString("gateway", gateway.toString());
doc["subnet"] = preferences.getString("subnet", subnet.toString());
doc["dns1"] = preferences.getString("dns1", dns1.toString());
doc["dns2"] = preferences.getString("dns2", dns2.toString());

doc["selCO2Sensor"] = preferences.getInt("selCO2Sensor", 0);
doc["debugSensors"] = preferences.getBool("debugSensors", false);
doc["displayReverse"] = preferences.getBool("displayReverse", false);
Expand Down Expand Up @@ -556,6 +593,15 @@ String getActualSettingsAsJson(bool includePasswords = false) {
doc["wifiPass"] = wifiPass;
}
doc["hostName"] = hostName;

// Fixed IP
doc["useStaticIP"] = useStaticIP;
doc["staticIP"] = staticIP.toString();
doc["gateway"] = gateway.toString();
doc["subnet"] = subnet.toString();
doc["dns1"] = dns1.toString();
doc["dns2"] = dns2.toString();

doc["selCO2Sensor"] = selectedCO2Sensor;
doc["debugSensors"] = debugSensors;
doc["displayReverse"] = displayReverse;
Expand Down Expand Up @@ -653,6 +699,15 @@ bool handleSavePreferencesFromJSON(String jsonPreferences) {
displayOffOnExternalPower = JsonDocument["dispOffOnExP"];
wifiSSID = JsonDocument["wifiSSID"].as<String>().c_str();
hostName = JsonDocument["hostName"].as<String>().c_str();

// Fixed IP
useStaticIP = JsonDocument["useStaticIP"];
staticIP.fromString(JsonDocument["staticIP"].as<String>());
gateway.fromString(JsonDocument["gateway"].as<String>());
subnet.fromString(JsonDocument["subnet"].as<String>());
dns1.fromString(JsonDocument["dns1"].as<String>());
dns2.fromString(JsonDocument["dns2"].as<String>());

selectedCO2Sensor = JsonDocument["selCO2Sensor"];
debugSensors = JsonDocument["debugSensors"];
displayReverse = JsonDocument["displayReverse"];
Expand Down
25 changes: 19 additions & 6 deletions CO2_Gadget_WIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
/*****************************************************************************************************/
// clang-format on

#if !defined WIFI_SSID_CREDENTIALS || !defined WIFI_PW_CREDENTIALS
// If not using enviroment variables, you must have a credentials.h file
#include "credentials.h"
#endif

#ifdef SUPPORT_CAPTIVE_PORTAL
DNSServer dnsServer;
#endif
Expand Down Expand Up @@ -729,6 +724,13 @@ String getCO2GadgetStatusAsJson() {
doc["RSSI"] = WiFi.RSSI();
doc["MACAddress"] = MACAddress;
doc["hostName"] = hostName;
doc["useStaticIP"] = useStaticIP;
doc["staticIP"] = staticIP.toString();
doc["gateway"] = gateway.toString();
doc["subnet"] = subnet.toString();
doc["dns1"] = dns1.toString();
doc["dns2"] = dns2.toString();

#ifdef SUPPORT_MQTT
doc["rootTopic"] = rootTopic;
doc["discoveryTopic"] = discoveryTopic;
Expand Down Expand Up @@ -1033,7 +1035,18 @@ bool connectToWiFi() {
WiFi.disconnect(true); // disconnect form wifi to set new wifi connection
delay(100);
WiFi.mode(WIFI_STA);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);

if (useStaticIP) {
if (!WiFi.config(staticIP, gateway, subnet, dns1, dns2)) {
Serial.println("-->[WiFi] Failed to configure static IP and DNS");
return false;
}
Serial.print("-->[WiFi] Configuring static IP: ");
Serial.println(staticIP);
} else {
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // Use DHCP
}

WiFi.setHostname(hostName.c_str());
Serial.println("-->[WiFi] Setting hostname: " + hostName);
#ifdef WIFI_PRIVACY
Expand Down
Binary file modified data/index.html.gz
Binary file not shown.
Binary file modified data/ota.html.gz
Binary file not shown.
56 changes: 56 additions & 0 deletions data/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,62 @@ <h1>CO2 Gadget Preferences</h1>
</label>
</div>
</fieldset>
<fieldset>
<legend>Fixed IP</legend>

<div>
<label for="useStaticIP">Use Static IP:
<input type="checkbox" id="useStaticIP" name="useStaticIP">
<span class="tooltip-icon">ℹ️</span>
<span class="tooltip-text">Use a fixed IP address for the device. This is useful for
devices that need to be accessed by a fixed IP address.</span>
</label>

<div id="staticIPSettings">
<div>
<label for="staticIP">Static IP:
<span class="tooltip-icon">ℹ️</span>
<span class="tooltip-text">The fixed IP address for the device.</span>
<input type="text" id="staticIP" name="staticIP" value="">
</label>
</div>

<div>
<label for="gateway">Gateway:
<span class="tooltip-icon">ℹ️</span>
<span class="tooltip-text">The gateway IP address for the device.</span>
<input type="text" id="gateway" name="gateway" value="">
</label>
</div>

<div>
<label for="subnet">Subnet:
<span class="tooltip-icon">ℹ️</span>
<span class="tooltip-text">The subnet mask for the device.</span>
<input type="text" id="subnet" name="subnet" value="">
</label>
</div>

<div>
<label for="dns1">DNS 1:
<span class="tooltip-icon">ℹ️</span>
<span class="tooltip-text">The primary DNS server for the device.</span>
<input type="text" id="dns1" name="dns1" value="">
</label>
</div>

<div>
<label for="dns2">DNS 2:
<span class="tooltip-icon">ℹ️</span>
<span class="tooltip-text">The secondary DNS server for the device.</span>
<input type="text" id="dns2" name="dns2" value="">
</label>
</div>
</div>

</div>
</fieldset>

</div>
</fieldset>
<!-- MQTT Configuration Group -->
Expand Down
Binary file modified data/preferences.html.gz
Binary file not shown.
26 changes: 26 additions & 0 deletions data/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ function populateFormWithPreferences(preferences) {
if (getPreferencesDebug) console.log('Setting hostName to:', preferences.hostName);
document.getElementById("hostName").value = preferences.hostName;

if (getPreferencesDebug) console.log('Setting useStaticIP to:', preferences.useStaticIP);
document.getElementById("useStaticIP").checked = preferences.useStaticIP;

if (getPreferencesDebug) console.log('Setting staticIP to:', preferences.staticIP);
document.getElementById("staticIP").value = preferences.staticIP;

if (getPreferencesDebug) console.log('Setting gateway to:', preferences.gateway);
document.getElementById("gateway").value = preferences.gateway;

if (getPreferencesDebug) console.log('Setting subnet to:', preferences.subnet);
document.getElementById("subnet").value = preferences.subnet;

if (getPreferencesDebug) console.log('Setting dns1 to:', preferences.dns1);
document.getElementById("dns1").value = preferences.dns1;

if (getPreferencesDebug) console.log('Setting dns2 to:', preferences.dns2);
document.getElementById("dns2").value = preferences.dns2;

if (getPreferencesDebug) console.log('Setting selCO2Sensor to:', preferences.selCO2Sensor);
document.getElementById("selCO2Sensor").value = preferences.selCO2Sensor;

Expand Down Expand Up @@ -172,6 +190,7 @@ function populateFormWithPreferences(preferences) {
toggle('activeWIFI', 'wifiNetworks');
toggle('activeMQTT', 'mqttConfig');
toggle('activeESPNOW', 'espNowConfig');
toggle('useStaticIP', 'staticIPSettings');
}

function loadPreferencesFromServer() {
Expand Down Expand Up @@ -213,6 +232,12 @@ function collectPreferencesData() {
dispOffOnExP: document.getElementById("dispOffOnExP").checked,
wifiSSID: document.getElementById("wifiSSID").value,
hostName: document.getElementById("hostName").value,
useStaticIP: document.getElementById("useStaticIP").checked,
staticIP: document.getElementById("staticIP").value,
gateway: document.getElementById("gateway").value,
subnet: document.getElementById("subnet").value,
dns1: document.getElementById("dns1").value,
dns2: document.getElementById("dns2").value,
selCO2Sensor: document.getElementById("selCO2Sensor").value,
debugSensors: document.getElementById("debugSensors").checked,
displayReverse: document.getElementById("displayReverse").checked,
Expand Down Expand Up @@ -464,4 +489,5 @@ document.addEventListener("DOMContentLoaded", function () {
toggle('activeWIFI', 'wifiNetworks');
toggle('activeMQTT', 'mqttConfig');
toggle('activeESPNOW', 'espNowConfig');
toggle('useStaticIP', 'staticIPSettings');
});
Binary file modified data/preferences.js.gz
Binary file not shown.
Loading

0 comments on commit 4330f82

Please sign in to comment.