From 827c8cd184972d152124218d250894ece745b04d Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 21 Oct 2024 19:54:20 +0200 Subject: [PATCH] moved wifi stuff into its own file, cosmetic things wifi page --- Arduino/CanGrow/CanGrow.ino | 13 +-- Arduino/CanGrow/include/CanGrow_Core.h | 39 --------- Arduino/CanGrow/include/CanGrow_LittleFS.h | 2 +- Arduino/CanGrow/include/CanGrow_Wifi.h | 81 +++++++++++++++++++ .../include/Webserver/Page_wifi_HTML.h | 57 +++++++------ 5 files changed, 117 insertions(+), 75 deletions(-) create mode 100644 Arduino/CanGrow/include/CanGrow_Wifi.h diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 9f58e55..fe1a454 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -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(); } diff --git a/Arduino/CanGrow/include/CanGrow_Core.h b/Arduino/CanGrow/include/CanGrow_Core.h index 8aca97a..6793550 100644 --- a/Arduino/CanGrow/include/CanGrow_Core.h +++ b/Arduino/CanGrow/include/CanGrow_Core.h @@ -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()); -} diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 8a79cbb..82642be 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -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 diff --git a/Arduino/CanGrow/include/CanGrow_Wifi.h b/Arduino/CanGrow/include/CanGrow_Wifi.h new file mode 100644 index 0000000..50fde1c --- /dev/null +++ b/Arduino/CanGrow/include/CanGrow_Wifi.h @@ -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(); + } +} diff --git a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h index 0856f31..8921f41 100644 --- a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h @@ -36,48 +36,55 @@ const char* Page_wifi_HTML PROGMEM = R"(%HEADER%

Select your wifi network from the SSID list.
Reload the page, if your network is not listed.

-SSID: %WIFI_LIST%
-Password:
+Password:
+
-IP: . - . - . -
- -Subnet mask: . - . - . -
- -Gateway: . - . - . -
- -DNS: . - . - . -
- - -DHCP:
+IP:
+ . + . + . +
+ +Netmask:
+ . + . + . +
+ +Gateway:
+ . + . + . +
+ +DNS:
+ . + . + . +
+
%FOOTER%)"; -const char* Page_wifi_HTML_CURRENT_SETTINGS PROGMEM = R"(Current Settings:
WiFi SSID: %CONFIGWIFI_SSID%
+const char* Page_wifi_HTML_CURRENT_SETTINGS PROGMEM = R"(Current Settings:
WiFi SSID: %CONFIGWIFI_SSID%
Use DHCP: %CONFIGWIFI_DHCP%
IP address: %CONFIGWIFI_IP%
Subnet mask: %CONFIGWIFI_NETMASK%