Page 1 of 1

How to save IPAddress to ESP32 Preferences?

Posted: Wed Sep 21, 2022 10:11 pm
by pravi.khm@gmail.com
Hi,

I have a captive portal where IPAddress is typed in. The result is stored in a variable as:

Code: Select all

strcpy(server, custom_server.getValue());
IPAddress _ip;
_ip.fromString(server);
When I use a print statement, I can see that the IP address is saved and printed as 255,255,255,255, the same value I typed in the portal.

Now, how do I save this value (not sure if its a string as its stored in IPAddress format) in preferences memory of ESP32 and also how do I retrieve it in IPAddress format so that I can assign the IP on next reboot?

Kindly suggest. Thanks in advance

Re: How to save IPAddress to ESP32 Preferences?

Posted: Fri Sep 23, 2022 3:12 am
by lbernstone
An IP address is really just a 32-bit identifier.

Code: Select all

#include <Preferences.h>
void setup() {
  Preferences prefs;
  Serial.begin(115200);
  IPAddress ip1(192,168,0,1);
  prefs.begin("ip");
  prefs.putUInt("ip1", ip1);
  IPAddress ip2 = prefs.getUInt("ip1");
  Serial.println(ip2);
}
void loop() {}