How to save IPAddress to ESP32 Preferences?

pravi.khm@gmail.com
Posts: 11
Joined: Sun Mar 31, 2019 1:31 am

How to save IPAddress to ESP32 Preferences?

Postby pravi.khm@gmail.com » Wed Sep 21, 2022 10:11 pm

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

lbernstone
Posts: 637
Joined: Mon Jul 22, 2019 3:20 pm

Re: How to save IPAddress to ESP32 Preferences?

Postby lbernstone » Fri Sep 23, 2022 3:12 am

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() {}

Who is online

Users browsing this forum: No registered users and 117 guests