Converting WiFi.IPaddress to string

I have looked through previous responses to this question and have not been successful in having them work. I am sure that it is because I know just enough to get into trouble. I put together a sketch for a ESP8266 that collects the temperature difference between the input and output temperature of an air-conditioner and the out side temperature in a string ever minute. Then sends that information as text in an email. Then starts over again. I use ESP.restart() to start the sketch over because that is the only way I have found to get the ESP to send multiple emails. The problem has to do with my having to connect to the internet through an AP. It seems unreliable at times and instead of connecting to the base router it seem I end up connecting to just the AP. Or loosing wifi connection while the sketch is running and it hangs up the sketch. I noticed that when thing are working I end up having an IP address of 192.168.68.220. When there is a problem the IP address is 192.168.1.100. My though is that the AP is messing up and when I connect to a network it may just be connecting to the AP. Naturally, it is random and the AP seems to correct itself, but by then the sketch has hung up and dead in the water. What I have been trying to do is add to the current script so the wifi connect function will run until it connects the router instead of hanging up when the AP starts to act up. I have the router set to fix the IP address for the ESP to a specific IP address based on its mac address. That way when it correctly connected I know what the address will be. It would be a simple thing to just have the wifi connect process loop until it gets the correct IP address, waiting out what ever issue is with the AP. For that to work I need to convert the WiFi.IPaddres into a string or at least the last 3 of the address into a string. I see part of sketches posted that seem to indicate the answer is there but I can't get them to work. Can someone please help by posting a short script, start to finish, that will connect to a wifi network and deliver the WiFi.IPaddress in the form of a String? Seeing the entire picture will help a great deal understanding what is involved.

I found your post very difficult to read due to it being a solid slab of text. I am sure that it would be easier to understand if it were broken up into paragraphs like this

I have looked through previous responses to this question and have not been successful in having them work. I am sure that it is because I know just enough to get into trouble.

I put together a sketch for a ESP8266 that collects the temperature difference between the input and output temperature of an air-conditioner and the out side temperature in a string ever minute. Then sends that information as text in an email. Then starts over again.

I use ESP.restart() to start the sketch over because that is the only way I have found to get the ESP to send multiple emails. The problem has to do with my having to connect to the internet through an AP. It seems unreliable at times and instead of connecting to the base router it seem I end up connecting to just the AP. Or loosing wifi connection while the sketch is running and it hangs up the sketch.

I noticed that when thing are working I end up having an IP address of 192.168.68.220. When there is a problem the IP address is 192.168.1.100. My though is that the AP is messing up and when I connect to a network it may just be connecting to the AP. Naturally, it is random and the AP seems to correct itself, but by then the sketch has hung up and dead in the water.

What I have been trying to do is add to the current script so the wifi connect function will run until it connects the router instead of hanging up when the AP starts to act up. I have the router set to fix the IP address for the ESP to a specific IP address based on its mac address. That way when it correctly connected I know what the address will be.

It would be a simple thing to just have the wifi connect process loop until it gets the correct IP address, waiting out what ever issue is with the AP. For that to work I need to convert the WiFi.IPaddres into a string or at least the last 3 of the address into a string. I see part of sketches posted that seem to indicate the answer is there but I can't get them to work.

Can someone please help by posting a short script, start to finish, that will connect to a wifi network and deliver the WiFi.IPaddress in the form of a String? Seeing the entire picture will help a great deal understanding what is involved

I hope that this may help

Why do you need t convert the IP Address to a string?

Or is it a String (rather than a string) that you need, and why ?

Which board are you using ?

As simple as adding something like this in your code

String myIP = WiFi.localIP().toString();

Regards

True, but I suspect an XY Problem which is it's been asked why @thetatek4 thinks it's necessary to convert the IP Address to a string in the first place.

I convert them into String usually because i want to add something to it and then display it, or i am using the String class already to display the webpage and i want to add the IP address. It is not such a strange thing to want to do.

It is given the problem that @thetatek4 stated he's trying to solve.

Thank you for splitting it up. Sorry it got away on me.

I am going through an access point and from time to time the access point can disconnect from the main router. When this happens the ESP seem to connect to the AP and the AP assigns an IP that is different from the one I fixed for ESP in the router.

When this happens the program hang up.

If I can convert the WiFi.IPaddress to a string, or some other workable variable , I can have the ESP continue to connect to the router using an if or when statement comparing the router/AP assigned IPs.

It is a String type that I am looking for. The board is a NodeMCU 0.9 (ESP-12 Module) part of the ESP8266 family. The problem has to do with getting the WiFi.IPaddress() into a format that I can deal with using more common actions.

I tried this and gfvalvo is right.

No it is not. Look in IPAddress.h. You'll see that the IPAddress class overides 'operator==()':

        bool operator==(const IPAddress& addr) const {
            return ip_addr_cmp(&_ip, &addr._ip);
        }

That means you can do direct comparison between objects of the IPAddress class. So, something like:

const IPAddress badIpAddress = {192, 168, 0, 100};

and later on:

  if (WiFi.localIP() == badIpAddress) {
    // Do something
  }

I considered using a static ID. What I have done is assign a static ID that the router will assign based on the ESP's mac address. I like you suggestion from the point that a static ID that is in the sketch and a when loop with an exit based on connecting with the static ID should work. I will have to try that out. Thanks.

I will also try this one one. Because my problem is random I am going to try to set thing up to fail by assigning a fixed ID in the Sketch and have the router assign a different ID based on the mac address. Interesting to see how the systems deal with it.

I tested it out and you solution allows me to make sure my ESP has the correct IP address. I fixed the ESP's IPaddress using its mac address to reserve an IPaddress on the router.

I tested you suggestion using the code below. At the start the badIPaddress is assigned an IPaddress that is not fixed in the router. That causes the IF statement to disconnect the WiFi and start it up again.

Before the IF statement the badIPaddress value is changed to the assigned IPaddress allowing the program to pass over the IF statement with the ESP connected using the correct IPaddress.

I used the mac address/router IP reservation method rather than assigning the ESP's address in the sketch to eliminate the change the router had previously assigned that address.

It was an XY problem after all. Thanks again.


//This is not the sketch I am working on it only purpose was to test gfvalvo suggestion. 
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#include Time.h
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#endif
#define WIFI_SSID "xxxx"                   // SSID name    CHANGE TO WHAT YOU USE  
#define WIFI_PASSWORD "xxxxx"       // SSID password     CHANGE TO WHAT YOU USE  
IPAddress badIpAddress = {192, 168, 68, 220};       //IP used cause the IF statement to activate YOU USE
IPAddress goodIpAddress = {192, 168, 68, 221};    //ESP's address reserved in my router  


void setup() { // put your setup code here, to run once:
 Serial.begin(115200);
  WiFi.mode(WIFI_STA);  // set the ESP8266 to station mode setting if set to  AP mode requires more work than one line in code and if in AP mode network like FaryLink_504A18 show's up you are in a different mode than station
  Serial.println(WiFi.macAddress());   //print the mac address of ESP8266 just for info. I used it to set my router to assign a specific IP address to my ESP when it connects
  delay(20); //delay after serial print
  Serial.println();
  delay(20); //delay after serial print


  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);  //use the SSID and PW you included when you changed the sketch to your information
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");
  delay(300);
  }


  Serial.print F("Connected with IP: ");
  Serial.println(WiFi.localIP());
        delay(20); 
  Serial.println();
        delay(20); 
  }

  void loop(){

    Serial.print F("badIpAddress = ");
    Serial.println(badIpAddress);
         delay(300);
           Serial.println();
        delay(20);

 if (WiFi.localIP() != badIpAddress) {
     WiFi.disconnect();
       Serial.println F("disconnect");
     delay(3000);
  Serial.println F("start   connect again");
        delay(20); 
  Serial.println();
      delay(20); 
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(300);
  Serial.print F("Connected with IP: ");
  Serial.println(WiFi.localIP());
        delay(20); 
}

// This allows the program to skip over the if statement next time
badIpAddress = goodIpAddress;  //change IP address to match what is reserved for device

        Serial.print(badIpAddress);  //for tracking only
         delay(20);
           Serial.println();
      delay(20);

  } 
}


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