Faster DHCP IP

kilobyte_ch
Posts: 12
Joined: Wed Apr 19, 2017 2:17 pm

Faster DHCP IP

Postby kilobyte_ch » Fri Jul 20, 2018 10:21 pm

Hello

I'm currently trying to do a HTTP Request as fast as possible after a deep sleep (to consume a as low as possible energy to save battery life).

Thus I'm using deep sleep I'm fine with caching informations in RTC memory. Is there a general way to speed up the DHCP negotiation process? (That's currently the thing which needs "ages"). Or am I better of using static IP (basically caching the DHCP lease in RTC memory)?

User avatar
hassan789
Posts: 156
Joined: Thu Jun 29, 2017 2:15 am

Re: Faster DHCP IP

Postby hassan789 » Mon Jul 23, 2018 7:21 pm

Yes, the easiest way is to save the DHCP lease in RTC memory.
Every 20 connections renew DHCP...

The alternative is very complex, and I have a thread here that talks about this:
https://github.com/espressif/esp-idf/issues/799

kilobyte_ch
Posts: 12
Joined: Wed Apr 19, 2017 2:17 pm

Re: Faster DHCP IP

Postby kilobyte_ch » Tue Jul 24, 2018 5:49 pm

Thanks for reply. Ok, do you have any idea on how to do the DHCP caching in RTC memory? I have no idea on how to basically set an static IP on the ESP32 with the esp-idf :(

User avatar
hassan789
Posts: 156
Joined: Thu Jun 29, 2017 2:15 am

Re: Faster DHCP IP

Postby hassan789 » Wed Jul 25, 2018 1:55 am

You can use RTC_DATA_ATTR to save your struct in RTC ram.
After your first DHCP, save ip, mask, and gateway into RTC. On subsequent boots, use with static IP. Remeber to set your DNS to your gateway address.

dbaarda
Posts: 1
Joined: Thu Dec 07, 2023 10:27 pm

Re: Faster DHCP IP

Postby dbaarda » Thu Dec 07, 2023 11:40 pm

DHCP negotiation includes a bunch of info beyond just the IP assigned. It also includes the gateway IP to use, the DNS server IP to use, and the expiry time of the lease. IMHO you want to save and restore at least the IP assigned and IP gateway to use. There is also potentially useful things like the http proxy and time server to use. If you are using dnsmasq as your DNS and DHCP server you can add all sorts of standard or even arbitrary things to the DHCP reply, and even serve different answers to different clients, so you could even serve useful application specific settings info if you wanted.

The expiry time is also useful, but I'm not sure of the best way it could be used. The idea of only refreshing your DHCP every X number of wakeups is a simplistic approximation to DHCP lease expiry that could be very wrong, potentially causing problems with IP clashes in the worst case if it is too long, or refreshing too frequently and burning more battery if too short.

The ESP rtc clock is probably reliable enough to measure time even through deep sleeps, and if necessary you can use ntp (using the ntp server in the DHCP response) to keep it set and synchronised to the actual time. However, the DHCP lease times are provided in seconds, so you don't need to know the actual time, just the number of seconds since the lease was provided. To be on the safe side you should also refresh when between 50% to 90% of the lease time has expired.

After writing all this and digging around for more info, I found https://espressif-docs.readthedocs-host ... re-last-ip. So it looks like the IDF has grown support for this already.

Who is online

Users browsing this forum: alifepretend and 134 guests