I'm new to Arduino programming. I want to make my esp8266 get static IP based on gateway.
I mean if gateway is 192.168.0.1 then it should get 192.168.0.15 as static IP. And if gateway is 192.168.1.1 then it should get 192.168.1.15 as IP.
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
You can either set the router to allocate a static IP based on your esp8266's MAC address, or go the DHCP-way first, read the gateway IP address you got, and then do a WiFi.config(), something roughly like this (untested):
WiFi.begin(ssid, password); IPAddress ip = WiFi.gatewayIP(); IP = 15; IPAddress dns(8, 8, 8, 8); WiFi.config(ip, WiFi.gatewayIP(), dns, WiFi.subnetMask());
It is a bit of a trick, and not pretty, and you don't get to set the DNS as provided by the DHCP, because there is no getDNS(), for some silly reason, hence the use of Google DNS.
Note that when doing this, you run the risk of setting the esp8266's IP address to an address already taken by another device on the same router.