I have made a controller to irigate my garden (ESP8266 NodeMcu + flowswitch). It works perfectly. I use the ESP in server mode and i connect to it with just a navigator, through it's IP address. And that's my question.
In every example, we see that the authors uses the serial connection at initialization time to retrieve the IP address of the board to be able to access it. Right. I can do this also. Typically :
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
BUT ... what happend if my board fall out of current and restart later ? Nothing garantee that it will get the same IP address from the access point.
As it works alone in my garden, how can I connect to it if it receive another IP address ? That's my question.
I try to force a fixed IP address :
- First, i connect and retrieve the subnet mask, the gateway and IP.
- Then i impose ".63" instead of the ".69" that it originally receive
- I disconnect and try to reconnect with the following code :
// Try to connect with fixed IP Address
WiFi.disconnect();
WiFi.hostname(deviceName); // DHCP Hostname (useful for finding device for static lease)
ip[3] = 63; // Update IP to fixed IP
WiFi.config(ip, subnet, gateway, dns);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only
Serial.print("Connecting with ip: ");
Serial.println(ip);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
The connection succeed but not with the asked IP address.
I tried to impose a device name but it doesn't either work.
Note: I also tried to place the WiFi.config(ip, subnet, gateway, dns) after the WiFi.begin. Doesn't work.
Any idea ? Or any other solution to ensure that you can find it later ?
Best regards,
Rudy