moved wifi stuff into its own file, cosmetic things wifi page

This commit is contained in:
Marcus 2024-10-21 19:54:20 +02:00
parent 825f2e8e19
commit 827c8cd184
5 changed files with 117 additions and 75 deletions

View file

@ -85,6 +85,7 @@
#include "include/CanGrow.h"
#include "include/CanGrow_Core.h"
#include "include/CanGrow_Wifi.h"
#include "include/CanGrow_LittleFS.h"
#include "include/CanGrow_Webserver.h"
@ -131,17 +132,9 @@ void setup() {
}
LFS_init();
LFS_Init();
LoadConfig();
if(strlen(configWifi.ssid) == 0) {
Serial.println(":: [SETUP] configWifi.ssid is empty, creating access point");
WifiAP();
} else {
Serial.printf(":: [SETUP] configWifi.ssid is set, connecting to ssid: %s\n", configWifi.ssid);
WifiConnect();
}
Wifi_Init();
SetupWebserver();
}

View file

@ -55,43 +55,4 @@ char* IP2Char(IPAddress ipaddr){
return buffer;
}
void WifiConnect() {
Serial.printf(":: [WiFi] connecting to ssid: %s\n", configWifi.ssid);
WiFi.begin(configWifi.ssid, configWifi.password);
if(configWifi.dhcp == false) {
Serial.println(":: [WiFi] using static ip configuration:");
Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip));
Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask));
Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(configWifi.gateway));
Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(configWifi.dns));
WiFi.config(configWifi.ip, configWifi.dns, configWifi.gateway, configWifi.netmask);
} else {
Serial.println(":: [WiFi] using DHCP for ip configuration");
}
Serial.print(":: [WiFi] ");
// wait until WiFi connection is established
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("CONNECTED!");
if(configWifi.dhcp == true) {
Serial.println(":: [WiFi] DHCP offered ip configuration:");
Serial.printf(":: [WiFi] IP : %s\n", IP2Char(WiFi.localIP()));
Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(WiFi.subnetMask()));
Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(WiFi.gatewayIP()));
Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(WiFi.dnsIP()));
}
}
void WifiAP() {
Serial.printf(":: [WiFi] create access point: %s\n", CANGROW_SSID);
WiFi.softAPConfig(configWifi.ip, configWifi.gateway, configWifi.netmask);
WiFi.softAP(CANGROW_SSID);
Serial.print(":: [WiFi] access point started with IP: ");
Serial.println(WiFi.softAPIP());
}

View file

@ -32,7 +32,7 @@
// LittleFS auto format
#define FORMAT_LITTLEFS_IF_FAILED true
void LFS_init() {
void LFS_Init() {
Serial.println(":: [LittleFS] initializing");
// ESP8266 crashes with first argument set
#ifdef ESP8266

View file

@ -0,0 +1,81 @@
/*
*
* include/CanGrow_Wifi.h - Wifi stuff header file
*
*
* MIT License
*
* Copyright (c) 2024 DeltaLima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
void Wifi_Connect() {
Serial.printf(":: [WiFi] connecting to ssid: %s\n", configWifi.ssid);
WiFi.begin(configWifi.ssid, configWifi.password);
if(configWifi.dhcp == false) {
Serial.println(":: [WiFi] using static ip configuration:");
Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip));
Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask));
Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(configWifi.gateway));
Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(configWifi.dns));
WiFi.config(configWifi.ip, configWifi.dns, configWifi.gateway, configWifi.netmask);
} else {
Serial.println(":: [WiFi] using DHCP for ip configuration");
}
Serial.print(":: [WiFi] ");
// wait until WiFi connection is established
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("CONNECTED!");
if(configWifi.dhcp == true) {
Serial.println(":: [WiFi] DHCP offered ip configuration:");
Serial.printf(":: [WiFi] IP : %s\n", IP2Char(WiFi.localIP()));
Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(WiFi.subnetMask()));
Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(WiFi.gatewayIP()));
Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(WiFi.dnsIP()));
}
}
void Wifi_AP() {
Serial.printf(":: [WiFi] create access point: %s\n", CANGROW_SSID);
WiFi.softAPConfig(configWifi.ip, configWifi.gateway, configWifi.netmask);
WiFi.softAP(CANGROW_SSID);
Serial.print(":: [WiFi] access point started with IP: ");
Serial.println(WiFi.softAPIP());
}
void Wifi_Init() {
Serial.println(":: [WiFi] initializing");
if(strlen(configWifi.ssid) == 0) {
Serial.println(":: [WiFi] no value found in configWifi.ssid, creating access point");
Wifi_AP();
} else {
Serial.printf(":: [SETUP] value found in configWifi.ssid, connecting to SSID: %s\n", configWifi.ssid);
Wifi_Connect();
}
}

View file

@ -36,48 +36,55 @@ const char* Page_wifi_HTML PROGMEM = R"(%HEADER%
<p>Select your wifi network from the SSID list.
<br>Reload the page, if your network is not listed.</p>
<form method='post' action='/wifiSettings/save'>
SSID: <select id='configWifi.ssid' name='configWifi.ssid' required>
<u>SSID</u>:<br>
<select id='configWifi.ssid' name='configWifi.ssid' required>
<option disabled value='' selected hidden>-Select your network-</option>
%WIFI_LIST%
</select><br>
Password: <input type='password' name='configWifi.password'><br>
<u>Password</u>:<br>
<input type='password' name='configWifi.password'><br>
IP: <input class='inputShort' type='number' min='0' max='255' name='configWifi.ip0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.ip1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.ip2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.ip3'><br>
Subnet mask: <input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask3'><br>
Gateway: <input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway3'><br>
DNS: <input class='inputShort' type='number' min='0' max='255' name='configWifi.dns0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.dns1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.dns2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.dns3'><br>
DHCP: <select name='configWifi.dhcp' required>
<u>DHCP</u>:<br>
<select name='configWifi.dhcp' required>
<option disabled value='' selected hidden>---</option>
<option value='1'>On</option>
<option value='0'>Off</option>
</select><br>
<u>IP</u>:<br>
<input class='inputShort' type='number' min='0' max='255' name='configWifi.ip0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.ip1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.ip2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.ip3'><br>
<u>Netmask</u>:<br>
<input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.netmask3'><br>
<u>Gateway</u>:<br>
<input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.gateway3'><br>
<u>DNS</u>:<br>
<input class='inputShort' type='number' min='0' max='255' name='configWifi.dns0'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.dns1'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.dns2'> .
<input class='inputShort' type='number' min='0' max='255' name='configWifi.dns3'><br>
<input type='submit' value='&#x1F4BE; Save settings'>
</form>
%FOOTER%)";
const char* Page_wifi_HTML_CURRENT_SETTINGS PROGMEM = R"(<u>Current Settings:</u><br>WiFi SSID: <b>%CONFIGWIFI_SSID%</b><br>
const char* Page_wifi_HTML_CURRENT_SETTINGS PROGMEM = R"(<b><u>Current Settings:</u></b><br>WiFi SSID: <b>%CONFIGWIFI_SSID%</b><br>
Use DHCP: <b>%CONFIGWIFI_DHCP%</b><br>
IP address: <b>%CONFIGWIFI_IP%</b><br>
Subnet mask: <b>%CONFIGWIFI_NETMASK%</b><br>