SD read ip

I can't read an IP address from my SD card and i would create a web server at that address... how can i do it? :frowning:

I can't read an IP address from my SD card

Why not? What format is the data in? ASCII or binary?

and i would create a web server at that address...

Well, OK.

how can i do it?

That depends on how the data is stored and what issues you are having doing something as simple as reading a few bytes of data from a file with a fixed name on the SD card.

in my SD card i've a txt file called address and for example i've write the ip "192.168.5.4".
I would like use that address for my web server and for example if i change in the txt file the address the server change the address.

I would like use that address for my web server and for example if i change in the txt file the address the server change the address.

Fine by me. Go ahead.

yes but i can't do it.... can you help me?

DanRiccardo:
yes but i can't do it.... can you help me?

Why can't you? There are plenty of examples for reading from an SD card. What you do with the data read from the card is completely separate from reading the data.

then ...
I read the data and check for a string then convert to int .
finally steps 4 int values ​​the creation of variable IPAddress but when I view with localip me ip 52.0.0.0

if(modalita == "REMOTE" || modalita == "Remote" || modalita == "remote"){
      //faccio in remoto su ip della funzione url
      File url;
  String IPgen;
  int numIP1, numIP2, numIP3, numIP4, tmp1 = 0;;
  char tm;
  
  url = SD.open("url.txt", FILE_READ);
  if(sd){
      while ((tm = url.read()) > 0){
        if((char)tm != '.')
          IPgen += (char)tm;
        if((char)tm == '.'){
          tmp1++;
          if(tmp1 == 1){
            numIP1 = IPgen.toInt();
            IPgen = "";
          }
          if(tmp1 == 2){
            numIP2 = IPgen.toInt();
            IPgen = "";
          }
          if(tmp1 == 3){
            numIP3 = IPgen.toInt();
            IPgen = "";
          }
          if(tmp1 == 4){
            numIP4 = IPgen.toInt();
            IPgen = "";
          }
        }
        else
          IPgen += (char)tm;

      }
      }
      IPAddress ip = (numIP1, numIP2, numIP3, numIP4);
      Intervallo();
      Ethernet.begin(mac, ip);
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
     }

in the file i've write "10.0.0.30"

        if((char)tm != '.')
          IPgen += (char)tm;

It's pointless to cast a char to a char.

There is a maximum length for a IP4 IP address. Shitcan the String class.

It looks to me like you are writing everything that is not a . to the String instance twice.

      IPAddress ip = (numIP1, numIP2, numIP3, numIP4);

That is NOT how to define an IPAddress. Look at the examples. An IPAddress is an array of bytes. You are assigning numIP4 to ip.

for you how can i do it? because i don't understand what i'm doing wrong

DanRiccardo:
for you how can i do it? because i don't understand what i'm doing wrong

For one thing:

      IPAddress ip = (numIP1, numIP2, numIP3, numIP4);

should be

      IPAddress ip = {numIP1, numIP2, numIP3, numIP4};

For another:

while ((tm = url.read()) > 0)
{
  if((char)tm != '.')
    IPgen += (char)tm;
    
  if((char)tm == '.')
  {
// snipped some stuff
  }
  else
    IPgen += (char)tm;
}

If the character is not a ., add it to the String. Then, if the character is a ., do something. Otherwise, add the character to the String. What happens when the first character is a 1? What is in IPgen after the 1st character is read?

Why don't you know?

I can't use that --> { because ipaddress type accept only this--> (

I've tryed in many ways but i can't do it.... i don't konw why

if the character is '.' i assign the number at the first variable but it isn't '.' i assign at the string the number thet i read.... for me is right but it don't work...

I can't use that --> { because ipaddress type accept only this--> (

Oops, my bad.

for me is right but it don't work...

If it was right, it would work. I'll make it plainer. After:

while ((tm = url.read()) > 0)
{

put:

   Serial.print("I just read a >");
   Serial.print(tm);
   Serial.print("<");

After:

  else
    IPgen += (char)tm;

put:

   Serial.print("IPgen: [");
   Serial.print(IPGen);
   Serial.print("]");

Show your output.

this is my output:

just read a >1<IPgen: [11]I just read a >0<IPgen: [1100]I just read a >.<IPgen: []I just read a >0<IPgen: [00]I just read a >.<IPgen: []I just read a >0<IPgen: [00]I just read a >.<IPgen: []I just read a >3<IPgen: [33]I just read a >0<IPgen: [3300] server is at 30.0.0.0

I can't do it because i don't understand the error code... :frowning: for me the code is right because it follow my argument... can you help me please?

So, you read a '1' from the file, and IPgen becomes "11".
Then, you read a '0', and IPgen becomes "1100".

And, you still want to argue that you are not adding each character to IPgen twice?

and then what i'll do for fix it?

i've resolve the problem... but now i've a problem with the function IPAddress ip = (numIP1, numIP2, numIP3, numIP4); because i print in the serial monitor the value of the four variables but after that function when i print my local ip i see 30.0.0.0 insead of 10.0.0.30. what type of variables IPAddress accept?? i've tryed to pass it int or byte variables and the output is the same.

what type of variables IPAddress accept??

You have the source code. Have a peek.

class IPAddress : public Printable {
private:
    uint8_t _address[4];  // IPv4 address
    // Access the raw byte array containing the address.  Because this returns a pointer
    // to the internal structure rather than a copy of the address this function should only
    // be used when you know that the usage of the returned uint8_t* will be transient and not
    // stored.
    uint8_t* raw_address() { return _address; };

public:
    // Constructors
    IPAddress();
    IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);

i've resolve the problem

Which, obviously, required some code changes. And, yet, no code...Again!