Skip to content

Commit

Permalink
Merge pull request #106 from munzili/main
Browse files Browse the repository at this point in the history
implemented feature to set static ip instead of dhcp
  • Loading branch information
raomin committed May 8, 2022
2 parents 1531b16 + ce75bbc commit d995394
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down

0 comments on commit d995394

Please sign in to comment.