Store ipddress in eeprom

I need to store an ipaddress in the eeprom. how can i save the ip address as char array or similar. i want to get the ipaddress from udp.remoteipaddres() then save it in the eeprom

Here's a function to do it:

// convert IPAddress to char array and put it in the passed buffer
void IPtoa(const IPAddress & IP, char IPcharBuffer[]) {
#if defined(__ARDUINO_X86__)
  sprintf (IPcharBuffer, "%u", IP[0]);
#else  //defined(__ARDUINO_X86__)
  utoa(IP[0], IPcharBuffer, 10);  //convert the first octet
#endif  //defined(__ARDUINO_X86__)
  for (byte octetCount = 1; octetCount < 4; octetCount++) {  //convert the other 3 octets
    strcat(IPcharBuffer, ".");
    char octetChar[3 + 1];  //3 digit byte + null terminator
#if defined(__ARDUINO_X86__)
    sprintf (octetChar, "%u", IP[octetCount]);
#else  //defined(__ARDUINO_X86__)
    utoa(IP[octetCount], octetChar, 10);  //convert the octet
#endif  //defined(__ARDUINO_X86__)
    strcat(IPcharBuffer, octetChar);
  }
}

pert:
Here's a function to do it:

// convert IPAddress to char array and put it in the passed buffer

void IPtoa(const IPAddress & IP, char IPcharBuffer[]) {
#if defined(ARDUINO_X86)
  sprintf (IPcharBuffer, "%u", IP[0]);
#else  //defined(ARDUINO_X86)
  utoa(IP[0], IPcharBuffer, 10);  //convert the first octet
#endif  //defined(ARDUINO_X86)
  for (byte octetCount = 1; octetCount < 4; octetCount++) {  //convert the other 3 octets
    strcat(IPcharBuffer, ".");
    char octetChar[3 + 1];  //3 digit byte + null terminator
#if defined(ARDUINO_X86)
    sprintf (octetChar, "%u", IP[octetCount]);
#else  //defined(ARDUINO_X86)
    utoa(IP[octetCount], octetChar, 10);  //convert the octet
#endif  //defined(ARDUINO_X86)
    strcat(IPcharBuffer, octetChar);
  }
}

i dont even know how to begin to comprehend that. i see sprintf writing into ipcahrbuffer. but how do i use this function

You need to create a buffer of the appropriate size, then pass the IPAddress and the buffer to the function:

byte IPAddressLengthMax = 3 + 1 + 3 + 1 + 3 + 1 + 3;  //4 x octet + 3 x dot
char IPstringBuffer[IPAddressLengthMax + 1];  // Make sure to leave room in the buffer for the terminator
...
IPtoa(someIPAddress, IPstringBuffer);

After the IPtoa() function returns, IPstringBuffer will contain the string representation of someIPAddress.

pert:
You need to create a buffer of the appropriate size, then pass the IPAddress and the buffer to the function:

byte IPAddressLengthMax = 3 + 1 + 3 + 1 + 3 + 1 + 3;  //4 x octet + 3 x dot

char IPstringBuffer[IPAddressLengthMax + 1];  // Make sure to leave room in the buffer for the terminator
...
IPtoa(someIPAddress, IPstringBuffer);



After the IPtoa() function returns, IPstringBuffer will contain the string representation of someIPAddress.

Hi thanks. im trying to understand this. my compiler complains about declaring void function.

void IPtoa(someIPAddress, IPstringBuffer) {
  byte IPAddressLengthMax = 3 + 1 + 3 + 1 + 3 + 1 + 3;  //4 x octet + 3 x dot
  char IPstringBuffer[IPAddressLengthMax + 1];  // Make sure to leave room in the buffer for the terminator
  return
}

my compiler complains about declaring void function

You misunderstand. @pert has shown you how to use the function he provided in his previous reply.

You don't declare a new function, you just call IPtoa() as he has shown.

notsolowki:
Hi thanks. im trying to understand this. my compiler complains about declaring void function.

void IPtoa(someIPAddress, IPstringBuffer) {

byte IPAddressLengthMax = 3 + 1 + 3 + 1 + 3 + 1 + 3;  //4 x octet + 3 x dot
  char IPstringBuffer[IPAddressLengthMax + 1];  // Make sure to leave room in the buffer for the terminator
  return
}

return without a semi-colon.

cattledog:
You misunderstand. @pert has shown you how to use the function he provided in his previous reply.

You don't declare a new function, you just call IPtoa() as he has shown.

I have missunderstood.

when i try to compile this i get error

array bound is not an integer constant before ']' token
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

void IPtoa(const IPAddress & IP, char IPcharBuffer[]) {
#if defined(__ARDUINO_X86__)
  sprintf (IPcharBuffer, "%u", IP[0]);
#else  //defined(__ARDUINO_X86__)
  utoa(IP[0], IPcharBuffer, 10);  //convert the first octet
#endif  //defined(__ARDUINO_X86__)
  for (byte octetCount = 1; octetCount < 4; octetCount++) {  //convert the other 3 octets
    strcat(IPcharBuffer, ".");
    char octetChar[3 + 1];  //3 digit byte + null terminator
#if defined(__ARDUINO_X86__)
    sprintf (octetChar, "%u", IP[octetCount]);
#else  //defined(__ARDUINO_X86__)
    utoa(IP[octetCount], octetChar, 10);  //convert the octet
#endif  //defined(__ARDUINO_X86__)
    strcat(IPcharBuffer, octetChar);
  }
}
byte IPAddressLengthMax = 3 + 1 + 3 + 1 + 3 + 1 + 3;  //4 x octet + 3 x dot
char IPstringBuffer[IPAddressLengthMax + 1];  // Make sure to leave room in the buffer for the terminator



void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
IPtoa(someIPAddress, IPstringBuffer);
}

Nope im still getting ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ from the serial.println of _EEPROM.UV_IP after IPtoa(WiFi.localIP(), _EEPROM.UV_IP);

will someone explain the behaviour. why when i print _EEPROM.IV_IP after reboot it prints all question marks.
even though its printing all question marks i can still use it as an ipaddress with udp.beginPacket.

wow it was so easy to do,

String str = Udp.remoteIP().toString();
str.toCharArray(_EEPROM.UV_IP, 15);

how come no one said this

Because you don't give people the time to react :wink:

And you can't store Strings in EEPROM. It will store the pointer to the actual data but not the actual data itself. After a reboot of your board, the data where that pointer points to might have been moved (e.g. when you change your sketch) or the data might have been wiped.