Skip to content

Commit

Permalink
CO2 Gadget Beta v0.12.078-development
Browse files Browse the repository at this point in the history
feat: Add support for setting fixed IP (fix - redo)
Remove obsolete defines: WIFI_SSID_CREDENTIALS and WIFI_PW_CREDENTIALS
  • Loading branch information
melkati committed May 20, 2024
1 parent 446ba30 commit 80157a1
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 54 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 Down Expand Up @@ -287,6 +296,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 +410,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 +506,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 +592,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 +698,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
19 changes: 18 additions & 1 deletion CO2_Gadget_WIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,12 @@ 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 @@ -1028,7 +1034,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
56 changes: 54 additions & 2 deletions data/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ <h1>CO2 Gadget Preferences</h1>
<!-- Networking Group -->
<fieldset>
<legend>Networking</legend>

<div id="wifiNetworks">
<fieldset>
<legend>WiFi Credentials</legend>
Expand All @@ -92,7 +91,6 @@ <h1>CO2 Gadget Preferences</h1>
</label>
<input type="password" id="wifiPass" name="wifiPass" value="" disabled>
</div>

</fieldset>

<fieldset>
Expand All @@ -106,8 +104,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 -->
<div id="mqttConfig">
<fieldset>
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.
56 changes: 55 additions & 1 deletion data/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@
<div class="status-item" id="hostNameItem">
<span class="status-label">Host Name:</span> <span id="hostName"></span>
</div>
<div class="status-item" id="useStaticIPItem">
<span class="status-label">Use Static IP:</span> <span id="useStaticIP"></span>
</div>
<div class="status-item" id="staticIPItem">
<span class="status-label">Static IP:</span> <span id="staticIP"></span>
</div>
<div class="status-item" id="gatewayItem">
<span class="status-label">Gateway:</span> <span id="gateway"></span>
</div>
<div class="status-item" id="subnetItem">
<span class="status-label">Subnet:</span> <span id="subnet"></span>
</div>
<div class="status-item" id="dns1Item">
<span class="status-label">DNS1:</span> <span id="dns1"></span>
</div>
<div class="status-item" id="dns2Item">
<span class="status-label">DNS2:</span> <span id="dns2"></span>
</div>
<div class="status-item" id="rootTopicItem">
<span class="status-label">Root Topic:</span> <span id="rootTopic"></span>
</div>
Expand Down Expand Up @@ -210,6 +228,42 @@
document.getElementById('hostNameItem').style.display = 'none';
}

if (data.useStaticIP !== undefined) {
document.getElementById('useStaticIP').textContent = data.useStaticIP;
} else {
document.getElementById('useStaticIPItem').style.display = 'none';
}

if (data.staticIP !== undefined) {
document.getElementById('staticIP').textContent = data.staticIP;
} else {
document.getElementById('staticIPItem').style.display = 'none';
}

if (data.gateway !== undefined) {
document.getElementById('gateway').textContent = data.gateway;
} else {
document.getElementById('gatewayItem').style.display = 'none';
}

if (data.subnet !== undefined) {
document.getElementById('subnet').textContent = data.subnet;
} else {
document.getElementById('subnetItem').style.display = 'none';
}

if (data.dns1 !== undefined) {
document.getElementById('dns1').textContent = data.dns1;
} else {
document.getElementById('dns1Item').style.display = 'none';
}

if (data.dns2 !== undefined) {
document.getElementById('dns2').textContent = data.dns2;
} else {
document.getElementById('dns2Item').style.display = 'none';
}

if (data.rootTopic !== undefined) {
document.getElementById('rootTopic').textContent = data.rootTopic;
} else {
Expand Down Expand Up @@ -340,7 +394,7 @@
hours = hours - (days * 24);
minutes = minutes - (days * 24 * 60) - (hours * 60);
seconds = seconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);


document.getElementById('uptime').textContent = data.uptime;
} else {
Expand Down
Binary file modified data/status.html.gz
Binary file not shown.

0 comments on commit 80157a1

Please sign in to comment.