From f15c2559b4babd74e26fe5fda49fb85471e9f114 Mon Sep 17 00:00:00 2001 From: Jeff Lehman Date: Mon, 6 Mar 2023 15:07:19 -0600 Subject: [PATCH] Fix static IP string issue --- examples/0_MQTT_Gateway/fdrs_gateway_config.h | 4 +-- examples/1_UART_Gateway/fdrs_gateway_config.h | 4 +-- .../2_ESPNOW_Repeater/fdrs_gateway_config.h | 4 +-- .../3_LoRa_Repeater/fdrs_gateway_config.h | 4 +-- .../fdrs_gateway_config.h | 5 ++-- src/fdrs_gateway_wifi.h | 26 +++++++++++++++---- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/examples/0_MQTT_Gateway/fdrs_gateway_config.h b/examples/0_MQTT_Gateway/fdrs_gateway_config.h index d12540e..67bf001 100644 --- a/examples/0_MQTT_Gateway/fdrs_gateway_config.h +++ b/examples/0_MQTT_Gateway/fdrs_gateway_config.h @@ -59,14 +59,14 @@ // WiFi and MQTT Credentials -- These will override the global settings //#define WIFI_SSID "Your SSID" //#define WIFI_PASS "Your Password" -//#define DNS1_IPADDRESS "192.168.0.1" // Required for NTP even if not using static IP defined below // Use Static IP Address for WiFi connections // #define USE_STATIC_IPADDRESS // #define HOST_IPADDRESS "192.168.0.100" // #define GW_IPADDRESS "192.168.0.1" // #define SUBNET_ADDRESS "255.255.255.0" -// #define DNS2_IPADDRESS "192.168.0.2" // Optional. Only DNS1 is required to be able to use NTP +// #define DNS1_IPADDRESS "192.168.0.1" +// #define DNS2_IPADDRESS "192.168.0.2" //#define MQTT_ADDR "192.168.0.8" //#define MQTT_PORT 1883 // Default MQTT port is 1883 diff --git a/examples/1_UART_Gateway/fdrs_gateway_config.h b/examples/1_UART_Gateway/fdrs_gateway_config.h index 62a748a..2c5532b 100644 --- a/examples/1_UART_Gateway/fdrs_gateway_config.h +++ b/examples/1_UART_Gateway/fdrs_gateway_config.h @@ -59,14 +59,14 @@ // WiFi and MQTT Credentials -- These will override the global settings //#define WIFI_SSID "Your SSID" //#define WIFI_PASS "Your Password" -//#define DNS1_IPADDRESS "192.168.0.1" // Required for NTP even if not using static IP defined below // Use Static IP Address for WiFi connections // #define USE_STATIC_IPADDRESS // #define HOST_IPADDRESS "192.168.0.100" // #define GW_IPADDRESS "192.168.0.1" // #define SUBNET_ADDRESS "255.255.255.0" -// #define DNS2_IPADDRESS "192.168.0.2" // Optional. Only DNS1 is required to be able to use NTP +// #define DNS1_IPADDRESS "192.168.0.1" +// #define DNS2_IPADDRESS "192.168.0.2" //#define MQTT_ADDR "192.168.0.8" //#define MQTT_PORT 1883 // Default MQTT port is 1883 diff --git a/examples/2_ESPNOW_Repeater/fdrs_gateway_config.h b/examples/2_ESPNOW_Repeater/fdrs_gateway_config.h index 85956ae..bf407e2 100644 --- a/examples/2_ESPNOW_Repeater/fdrs_gateway_config.h +++ b/examples/2_ESPNOW_Repeater/fdrs_gateway_config.h @@ -59,14 +59,14 @@ // WiFi and MQTT Credentials -- These will override the global settings //#define WIFI_SSID "Your SSID" //#define WIFI_PASS "Your Password" -//#define DNS1_IPADDRESS "192.168.0.1" // Required for NTP even if not using static IP defined below // Use Static IP Address for WiFi connections // #define USE_STATIC_IPADDRESS // #define HOST_IPADDRESS "192.168.0.100" // #define GW_IPADDRESS "192.168.0.1" // #define SUBNET_ADDRESS "255.255.255.0" -// #define DNS2_IPADDRESS "192.168.0.2" // Optional. Only DNS1 is required to be able to use NTP +// #define DNS1_IPADDRESS "192.168.0.1" +// #define DNS2_IPADDRESS "192.168.0.2" //#define MQTT_ADDR "192.168.0.8" //#define MQTT_PORT 1883 // Default MQTT port is 1883 diff --git a/examples/3_LoRa_Repeater/fdrs_gateway_config.h b/examples/3_LoRa_Repeater/fdrs_gateway_config.h index 7c789e9..88ae4de 100644 --- a/examples/3_LoRa_Repeater/fdrs_gateway_config.h +++ b/examples/3_LoRa_Repeater/fdrs_gateway_config.h @@ -59,14 +59,14 @@ // WiFi and MQTT Credentials -- These will override the global settings //#define WIFI_SSID "Your SSID" //#define WIFI_PASS "Your Password" -//#define DNS1_IPADDRESS "192.168.0.1" // Required for NTP even if not using static IP defined below // Use Static IP Address for WiFi connections // #define USE_STATIC_IPADDRESS // #define HOST_IPADDRESS "192.168.0.100" // #define GW_IPADDRESS "192.168.0.1" // #define SUBNET_ADDRESS "255.255.255.0" -// #define DNS2_IPADDRESS "192.168.0.2" // Optional. Only DNS1 is required to be able to use NTP +// #define DNS1_IPADDRESS "192.168.0.1" +// #define DNS2_IPADDRESS "192.168.0.2" //#define MQTT_ADDR "192.168.0.8" //#define MQTT_PORT 1883 // Default MQTT port is 1883 diff --git a/examples/Gateway_Examples/1_MQTT_Gateway_Ethernet/fdrs_gateway_config.h b/examples/Gateway_Examples/1_MQTT_Gateway_Ethernet/fdrs_gateway_config.h index 5d88d22..2afca05 100644 --- a/examples/Gateway_Examples/1_MQTT_Gateway_Ethernet/fdrs_gateway_config.h +++ b/examples/Gateway_Examples/1_MQTT_Gateway_Ethernet/fdrs_gateway_config.h @@ -59,14 +59,15 @@ // WiFi and MQTT Credentials -- These will override the global settings //#define WIFI_SSID "Your SSID" //#define WIFI_PASS "Your Password" -//#define DNS1_IPADDRESS "192.168.0.1" // Required for NTP even if not using static IP defined below + // Use Static IP Address for WiFi connections // #define USE_STATIC_IPADDRESS // #define HOST_IPADDRESS "192.168.0.100" // #define GW_IPADDRESS "192.168.0.1" // #define SUBNET_ADDRESS "255.255.255.0" -// #define DNS2_IPADDRESS "192.168.0.2" // Optional. Only DNS1 is required to be able to use NTP +// #define DNS1_IPADDRESS "192.168.0.1" +// #define DNS2_IPADDRESS "192.168.0.2" #define MQTT_ADDR "192.168.2.3" //#define MQTT_PORT 1883 // Default MQTT port is 1883 diff --git a/src/fdrs_gateway_wifi.h b/src/fdrs_gateway_wifi.h index 12893d2..3f7d91c 100644 --- a/src/fdrs_gateway_wifi.h +++ b/src/fdrs_gateway_wifi.h @@ -117,9 +117,21 @@ void WiFiEvent(WiFiEvent_t event) const char *ssid = FDRS_WIFI_SSID; const char *password = FDRS_WIFI_PASS; #ifdef USE_STATIC_IPADDRESS - IPAddress hostIpAddress, gatewayAddress, subnetAddress, dns2Address; + uint8_t hostIpAddress[4], gatewayAddress[4], subnetAddress[4], dns2Address[4]; #endif -IPAddress dns1Address; +uint8_t dns1Address[4]; + +// Convert IP Addresses from strings to byte arrays of 4 bytes +void stringToByteArray(const char* str, char sep, byte* bytes, int maxBytes, int base) { + for (int i = 0; i < maxBytes; i++) { + bytes[i] = strtoul(str, NULL, base); // Convert byte + str = strchr(str, sep); // Find next separator + if (str == NULL || *str == '\0') { + break; // No more separators, exit + } + str++; // Point to next character after separator + } +} void begin_wifi() { @@ -134,9 +146,13 @@ void begin_wifi() } #else #ifdef USE_STATIC_IPADDRESS - WiFi.config(hostIpAddress.fromString(FDRS_HOST_IPADDRESS), gatewayAddress.fromString(FDRS_GW_IPADDRESS), subnetAddress.fromString(FDRS_SUBNET_ADDRESS), dns1Address.fromString(FDRS_DNS1_IPADDRESS), dns2Address.fromString(FDRS_DNS2_IPADDRESS)); -#else - WiFi.dnsIP(dns1Address.fromString(FDRS_DNS1_IPADDRESS)); + // Convert from String to byte array + stringToByteArray(FDRS_HOST_IPADDRESS, '.', hostIpAddress, 4, 10); + stringToByteArray(FDRS_GW_IPADDRESS, '.', gatewayAddress, 4, 10); + stringToByteArray(FDRS_SUBNET_ADDRESS, '.', subnetAddress, 4, 10); + stringToByteArray(FDRS_DNS1_IPADDRESS, '.', dns1Address, 4, 10); + stringToByteArray(FDRS_DNS2_IPADDRESS, '.', dns2Address, 4, 10); + WiFi.config(hostIpAddress, gatewayAddress, subnetAddress, dns1Address, dns2Address); #endif WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED)