How to save IPAddress to ESP32 Preferences?

Hi,

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

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

Where is IPAddress type is defined? see there - is it possible to represent it as integer type. I f so - you will be easily able to save it.

The local IPAddress is defined in the sketch. When I get the IPAddress from captive portal, I want local IPAddress to be overwritten with the new IP address. I am done till then. Now, I need to store that in the same format as IPAddress (xxx, xxx, xxx, xxx) format so that I can directly retrieve it. If there is another way, kindly suggest.

One solution is I can use delimiter and split it as integers and store as different pieces in memory, but I want it to be a single entry.

You didn't understand my question.
I asked where is IPAddress object type is defined - in which library. Inspecting the library source you can find, how to transform IP from the object to the simple variable - preferable an integer

Oh ok. It is in <WiFi.h> for ESP32.

To be more specific:

Thanks
See the later file - the casting operator .
You can transform the IPAddress type to the uint32_t integer as:

IPAddress _ip;
uint32_t ip_addr = (uint32_t) _ip;

now you can store it in the preferences as integer.

The reverse transformation is not more difficult

IPAddress _ip = ip_addr;

Well, Thank you for checking this for me.
Are you referring to this block?

IPAddress::IPAddress(uint32_t address)
{
    _address.dword = address;
}

So, I will convert to a 32bit int and store it in preferences file. Will program it and check the results. Now to retrieve it back to IPAddress format, any thoughts?

I wrote this small program to get the results:

#include "WiFi.h"
IPAddress server(192,168,0,1); // ip of ROS server
void setup() {
  Serial.begin(57600);
  uint32_t ip_addr = (uint32_t) server;
  Serial.println(ip_addr);
}

void loop() {

}

Result is : 16820416

Sorry, connection issues :frowning:
I have updated my answer above.

Thank you. It solved the issue almost albeit a small issue:

#include "WiFi.h"
IPAddress server(192,168,0,1); // ip of ROS server
IPAddress _ip;
void setup() {
  Serial.begin(57600);
  uint32_t ip_addr = (uint32_t) server;
  Serial.printf("IPAddress to Int is %d \n", ip_addr);
  _ip = ip_addr;
  Serial.print("String to IP Address is : ");
  Serial.println(_ip);
}

void loop() {

}

Result:
IPAddress to Int is 16820416
String to IP Address is : 192.168.0.1

The only small issue is, when I entered, the address has commas and the output has dots. Not sure if this is an issue in the program, but I will give it a try and check.

No
When you entered it, it is not an address. Instead you entered a list of four separate octets .
In the C the values on the list are separated by commas.

Yes. but when I retrieve it, it comes out as a series of numbers with dots in between. This is my main program.

byte local_ip[] = {192, 168, 0, 110};
IPAddress server(local_ip[0], local_ip[1], local_ip[2], local_ip[3]);

How do I get the four octets from the integer?

please see the methods of the IPAddress class in the IPaddress.h file
example:

byte local_ip[] = {192, 168, 0, 110};
IPAddress server(local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
uint8_t first_octet = server[0];

by the way, you can define the IP shorter:

byte local_ip[] = {192, 168, 0, 110};
IPAddress server(local_ip);

or even

IPAddress server(192,168,0,110);
1 Like

This is so Perfect solution! Thanks a lot. :grinning:

For others coming for a similar solution, below is an example program:

#include "WiFi.h"
IPAddress server(192,168,0,1); // ip of ROS server
IPAddress _ip;
void setup() {
  Serial.begin(57600);
  uint32_t ip_addr = (uint32_t) server;
  _ip = ip_addr;
  Serial.printf("IPAddress to Int is %d \n", ip_addr);
  Serial.println(String("IP address is : ")+_ip[0]+String(", ")+_ip[1]+String(", ")+_ip[2]+String(", ")+_ip[3]);
}

void loop() {

}

Result is:
IPAddress to Int is 16820416
IP address is : 192, 168, 0, 1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.