diff --git a/src/main.cpp b/src/main.cpp index 10ec0be2..8711cae3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -95,6 +95,29 @@ void setup_wifi() delay(10); // We start by connecting to a WiFi network mqttSerial.printf("Connecting to %s\n", WIFI_SSID); + + #if defined(WIFI_IP) && defined(WIFI_GATEWAY) && defined(WIFI_SUBNET) + IPAddress local_IP(WIFI_IP); + IPAddress gateway(WIFI_GATEWAY); + IPAddress subnet(WIFI_SUBNET); + + #ifdef WIFI_PRIMARY_DNS + IPAddress primaryDNS(WIFI_PRIMARY_DNS); + #else + IPAddress primaryDNS(); + #endif + + #ifdef WIFI_SECONDARY_DNS + IPAddress secondaryDNS(WIFI_SECONDARY_DNS); + #else + IPAddress secondaryDNS(); + #endif + + if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { + mqttSerial.println("Failed to set static ip!"); + } + #endif + WiFi.begin(WIFI_SSID, WIFI_PWD); int i = 0; while (WiFi.status() != WL_CONNECTED) diff --git a/src/setup.h b/src/setup.h index 82c41766..c83cacd8 100644 --- a/src/setup.h +++ b/src/setup.h @@ -3,6 +3,13 @@ #define WIFI_SSID "SSID"//**Your SSID here** #define WIFI_PWD "password"//**Your password here** leave empty if open (bad!) +//Uncomment this to set a static IP instead of DHCP for the ESP (Separate by commas instead of dots) +//#define WIFI_IP 192, 168, 0, 5 +//#define WIFI_SUBNET 255, 255, 255, 0 +//#define WIFI_GATEWAY 192, 168, 0, 1 +//#define WIFI_PRIMARY_DNS 8, 8, 8, 8 //optional +//#define WIFI_SECONDARY_DNS 8, 8, 4, 4 //optional + #define MQTT_SERVER "192.168.1.4"//**IP address here of your MQTT server** #define MQTT_USERNAME ""//leave empty if not set (bad!) #define MQTT_PASSWORD ""//leave empty if not set (bad!)