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?
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
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?
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.