Add support for static IPv4 addressing

This commit is contained in:
Håvard O. Nordstrand 2021-06-12 18:51:41 +02:00
parent b067b3f447
commit c93bdc0e28
4 changed files with 51 additions and 11 deletions

View File

@ -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.

View File

@ -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"

View File

@ -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);
}
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);
}

View File

@ -1,3 +1,3 @@
#pragma once
#define VERSION "1.0.0"
#define VERSION "1.1.0"