From c93bdc0e28ee1b2ddead00e6e5e992843492e5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20O=2E=20Nordstrand?= Date: Sat, 12 Jun 2021 18:51:41 +0200 Subject: [PATCH] Add support for static IPv4 addressing --- CHANGELOG.md | 6 ++++++ src/config.default.h | 24 ++++++++++++++++++------ src/src.ino | 30 ++++++++++++++++++++++++++---- src/version.h | 2 +- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 037ceed..59c2559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security +## [1.1.0] - 2021-06-12 + +### Added + +- Add support for static IPv4 addressing. + ## [1.0.0] - 2020-06-29 Initial release. diff --git a/src/config.default.h b/src/config.default.h index 6fe542a..d0dbe8c 100644 --- a/src/config.default.h +++ b/src/config.default.h @@ -10,10 +10,6 @@ #define DHT_TYPE DHT11 // DHT pin #define DHT_PIN 14 -// HTTP server port -#define HTTP_SERVER_PORT 80 -// HTTP metrics endpoint -#define HTTP_METRICS_ENDPOINT "/metrics" // Temperature offset in degrees Celsius #define TEMPERATURE_CORRECTION_OFFSET 0 // Humidity offset in percent @@ -22,7 +18,23 @@ #define READ_INTERVAL 5000 // How many times to try to read the sensor before returning an error #define READ_TRY_COUNT 5 -// WiFi SSID (required) +// Wi-Fi SSID (required) #define WIFI_SSID "" -// WiFi password (required) +// Wi-Fi password (required) #define WIFI_PASSWORD "" +// Wi-Fi static IPv4 address enabled or disabled (disable for DHCPv4) +#define WIFI_IPV4_STATIC false +// Wi-Fi static IPv4 address +#define WIFI_IPV4_ADDRESS 192, 168, 1, 15 +// Wi-Fi static IPv4 gateway address +#define WIFI_IPV4_GATEWAY 192, 168, 1, 1 +// Wi-Fi static IPv4 subnet mask +#define WIFI_IPV4_SUBNET_MASK 255, 255, 255, 0 +// Wi-Fi static IPv4 primary DNS server +#define WIFI_IPV4_DNS_1 1, 1, 1, 1 +// Wi-Fi static IPv4 secondary DNS server +#define WIFI_IPV4_DNS_2 1, 0, 0, 1 +// HTTP server port +#define HTTP_SERVER_PORT 80 +// HTTP metrics endpoint +#define HTTP_METRICS_ENDPOINT "/metrics" diff --git a/src/src.ino b/src/src.ino index 20c6e8c..1bd045c 100644 --- a/src/src.ino +++ b/src/src.ino @@ -47,15 +47,37 @@ void setup_wifi() { char message[128]; snprintf(message, 128, "Wi-Fi SSID: %s", WIFI_SSID); log(message); + + if (WIFI_IPV4_STATIC) { + IPAddress address(WIFI_IPV4_ADDRESS); + IPAddress subnet(WIFI_IPV4_SUBNET_MASK); + IPAddress gateway(WIFI_IPV4_GATEWAY); + IPAddress dns1(WIFI_IPV4_DNS_1); + IPAddress dns2(WIFI_IPV4_DNS_2); + snprintf(message, 128, "Static IPv4 address: %d.%d.%d.%d", address[0], address[1], address[2], address[3]); + log(message); + snprintf(message, 128, "Static IPv4 subnet mask: %d.%d.%d.%d", subnet[0], subnet[1], subnet[2], subnet[3]); + log(message); + snprintf(message, 128, "Static IPv4 gateway: %d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]); + log(message); + snprintf(message, 128, "Static IPv4 primary DNS server: %d.%d.%d.%d", dns1[0], dns1[1], dns1[2], dns1[3]); + log(message); + snprintf(message, 128, "Static IPv4 secondary DNS server: %d.%d.%d.%d", dns2[0], dns2[1], dns2[2], dns2[3]); + log(message); + if (!WiFi.config(address, gateway, subnet, dns1, dns2)) { + log("Failed to configure Wi-Fi.", LogLevel::ERROR); + } + } + log("Wi-Fi connecting ..."); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { - log("Wi-Fi waiting ...", LogLevel::DEBUG); - delay(500); + log("Wi-Fi waiting ...", LogLevel::DEBUG); + delay(500); } - const IPAddress &ipaddr = WiFi.localIP(); + const IPAddress &address = WiFi.localIP(); log("Wi-Fi connected."); - snprintf(message, 128, "IPv4 address: %d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); + snprintf(message, 128, "IPv4 address: %d.%d.%d.%d", address[0], address[1], address[2], address[3]); log(message); } diff --git a/src/version.h b/src/version.h index 3aa8d7d..cb466f9 100644 --- a/src/version.h +++ b/src/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION "1.0.0" +#define VERSION "1.1.0"