I noticed that if I start the task while the LAN cable is connected I get IP address, but when I start the program when the LAN cable is disconnected and after a while I re-connect it, I do not get IP address. Looking at LAN analyzer shows that no DHCP request are issued.
Any idea how can it be solved?
Yes. From your netif driver, call netif_set_link_up and netif_set_link_down to notify lwIP of the link state. When the lwIP internal link state changes from down to up, DHCP is restarted.