4

I'm using a sketch like this:

WiFi.config(IP, Gate, Subnet);
WiFi.begin (ssid, pass);

//-- somewhere below I want to drop the static IP configuration and obtain IP with DHCP.

WiFi.disconnect ();
WiFi.begin (ssid, pass). //ssid and pass are the same as above.

But IP is not changing. It seems that WiFi hasn't been reconnected. How to drop static IP configuration without restart? I'm using nodemcu v3 with esp8266 -12e.

cagdas
  • 1,634
  • 2
  • 14
  • 27
Alex Kirillov
  • 65
  • 1
  • 6

4 Answers4

3

You are right. There is a DHCP issue at Arduino firmware. When I did checked from the WiFiSTA firmware implementation, I saw that staticIP flag get set once when you call config() function and never get cleared. This flag guards the DHCP start/stop operations. So, here is a solution for you. Just add the ESP SDK header to your code like:

extern "C" {
  #include "user_interface.h"
}

Now you are able to call dhcp start function from firmware which was previously blocking by the flag. But, notice that you have to call it after WiFi.begin() with some delay. Use the code block below:

WiFi.disconnect(true);
delay(1000);
WiFi.begin(ssid, pass);
(void)wifi_station_dhcpc_start();
cagdas
  • 1,634
  • 2
  • 14
  • 27
  • You _can_ reset static without resorting to the C Espressif SDK calls, although it's not particularly obvious. You must call WiFi.config(0, 0, 0); to reset the static configuration back to DHCP. I am not sure if this was introduced in a later version than the current release (2.3.0), but, it's working in the newest Git. – Dawn Minion Oct 17 '16 at 16:47
  • 0,0,0 config is not also too obvious, both way need to check implementation to get notation. In lua firmware there is a dhcp starter method. Maybe it is also be introduced in later releases for arduino. Yes, as you said zero check is included in latest and 2.30. – cagdas Oct 17 '16 at 17:27
  • Using WiFi.config(0, 0, 0); i get error: conversion from 'int' to 'IPAddress' is ambiguous WiFi.config(0, 0, 0); – Alex Kirillov Oct 21 '16 at 12:17
  • You can use the method I've explained above. – cagdas Oct 21 '16 at 12:24
  • Yes it work well, but WiFi.config(0, 0, 0) a little bit easy. – Alex Kirillov Oct 21 '16 at 14:44
  • OK. So it looks like a type error. Maybe you can pass like that : `IPAddress addr(0, 0, 0, 0);` for each IP address. So : `WiFi.config(addr, addr, addr);` – cagdas Oct 21 '16 at 14:50
  • I tried IPAddress addr(0, 0, 0, 0) at first, but it didn't help too. Only (void)wifi_station_dhcpc_start() do the trick. – Alex Kirillov Nov 03 '16 at 12:41
3

This should really be a comment to the accepted answer but my reputation is not high enough... Anyways, as noted in that answer there is a built in option for resetting the staticIP flag, but the trick to making it work is that the arguments need to be unsigned. I.e. WiFi.config(0u, 0u, 0u);. Should the unsigned part be omitted, you get the "conversion from 'int' to 'IPAddress' is ambiguous" error mentioned in the accepted answer's comments.

Looking at the source for the Arduino firmware's station class one can see that WiFi.config(0u, 0u, 0u); actually changes a protected flag in addition to executing wifi_station_dhcpc_start(), so I would say that using WiFi.config(0u, 0u, 0u); is the way to go.

Anders
  • 31
  • 2
2

I tried to put the exact code you need as an edit to Anders response but the edit got rejected for some reason. Gotta love stack overflow sometimes. I tried for some time to get this working and it doesn't work if you just call WiFi.config(0u, 0u, 0u);. Lots of other combinations that I thought would work didn't. Credit goes to both Anders and Cagdas's as this is just their 2 answers combined. This is the exact code you need to get it working. Apologies if I'm breaking another of the many stack overflow rules :/

WiFi.disconnect(true);
delay(1000);
WiFi.begin(ssid, password);
WiFi.config(0U, 0U, 0U);
MikeKulls
  • 873
  • 1
  • 10
  • 22
0

got same problem w/ ESP12E
was solved using this command with AT firmware

AT+RESTORE

also googled variants (not tested) for other environment:
NodeMCU - node.restore(), Eclipse example (?) - system_restore()

Seriy_A
  • 31
  • 1
  • 8