Problem collecting IP Address using ESP8266

Hi, I am new to this and not a programmer so please bear with me. I have only taken a basic level programming course and that was a while ago.

I have a Dynamic IP Address and currently trying to use the ESP8266 to ‘update’ me by sending an email. In my old code, I was able to have it send me an email every 12 hours with the current IP Address, and is working well enough. Now, I would like it to check every 6 min and compare it to the original one, sending an email if they are different.

I test ran this code and when the IP changed, it was still showing the same IP as before, not the updated one. In my eyes this should be working but apparently not. Thanks in advance.

[The only thing I changed outside of the void loop was entering in wifi info and gmail info]

#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

    uint8_t connection_state = 0;
    uint16_t reconnect_interval = 10000;
    //smtp.email@
    EMailSender emailSend("SenderEmail@gmail.com", "PasswordForEmail");

uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
{
    static uint16_t attempt = 0;
    if(nSSID) {
        WiFi.begin(nSSID, nPassword);
    }

    uint8_t i = 0;
    while(WiFi.status()!= WL_CONNECTED && i++ < 50)
    {
        delay(200);
    }
    ++attempt;
    if(i == 51) {
        if(attempt % 2 == 0)
        return false;
    }
    return true;
}

void Awaits()
{
    uint32_t ts = millis();
    while(!connection_state)
    {
        delay(50);
        if(millis() > (ts + reconnect_interval) && !connection_state){
            connection_state = WiFiConnect();
            ts = millis();
        }
    }
}

//The setup function is called once at startup of the sketch
void setup()
{
    const char* ssid = "MyISP";
    const char* password = "PasswordForInternet";

    connection_state = WiFiConnect(ssid, password);
    if(!connection_state)  // if not connected to WIFI
        Awaits();          // constantly trying to connect

}


void loop()
{
// The below code is for capturing the IP address
    
     if (WiFi.status() == WL_CONNECTED) {                 //Check WiFi connection status
       HTTPClient http;                                   //Declare an object of class HTTPClient
       http.begin("http://api.ipify.org/?format=json");   //Specify request destination
       int httpCode = http.GET();                         //Send the request
       
       if (httpCode > 0) {                                //Check the returning code
         String payload1 = http.getString();              //Get the FIRST IP Address
         EMailSender::EMailMessage message;               //Email the IP
         message.subject = "ESP8622";  
         message.message = "New IP Address: " + payload1;
         EMailSender::Response resp = emailSend.send("ReceiverEmail@gmail.com", message);

         for (int i = 1; i > 0; i++) {                    //Loop until IP Addresses are different
            delay(360000);                                //Delay 6 min
            String payload2 = http.getString();           //Get the SECOND IP Address

            if (payload1.equals(payload2)) {              //Check if the IP Address are the same
                                                          //If so, Do Nothing
            }
            else {                                        //Otherwise, email new IP Address

              return;                                      //Terminate for loop and restart main loop
              
            }
         }
    }
    http.end();   //Close connection
  }
}

read https://forum.arduino.cc/index.php?topic=173960.0

.

Well, I looked at some old code and an easy way to get a current IP address is to have a client function call the below URL and then get the IP from what is returned.

checkip.dyndns.com

Thank you both for the feedback. I made a few adjustments and now it seems to be working correctly. I modified the URL to report a cleaner IP Address and found that the http connection needs to be closed and restarted to ‘get’ a new IP address, otherwise it keeps reporting the old one.

I know this code may not be written the best, but it’s the only thing I’m using on the WiFi module so storage is no issue for me. I will post the code below for anyone who may be interested in the working result.

#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

    uint8_t connection_state = 0;
    uint16_t reconnect_interval = 10000;
    //smtp.email@
    EMailSender emailSend("SenderEmail@gmail.com", "Password");

uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
{
    static uint16_t attempt = 0;
    if(nSSID) {
        WiFi.begin(nSSID, nPassword);
    }

    uint8_t i = 0;
    while(WiFi.status()!= WL_CONNECTED && i++ < 50)
    {
        delay(200);
    }
    ++attempt;
    if(i == 51) {
        if(attempt % 2 == 0)
        return false;
    }
    return true;
}

void Awaits()
{
    uint32_t ts = millis();
    while(!connection_state)
    {
        delay(50);
        if(millis() > (ts + reconnect_interval) && !connection_state){
            connection_state = WiFiConnect();
            ts = millis();
        }
    }
}

//The setup function is called once at startup of the sketch
void setup()
{
    const char* ssid = "SSID";
    const char* password = "Password";

    connection_state = WiFiConnect(ssid, password);
    if(!connection_state)  // if not connected to WIFI
        Awaits();          // constantly trying to connect
}


void loop()
{
// The below code is for capturing the IP address
    
     if (WiFi.status() == WL_CONNECTED) {                 //Check WiFi connection status
       HTTPClient http;                                   //Declare an object of class HTTPClient
       http.begin("http://api.ipify.org");                //Specify request destination
       int httpCode = http.GET();                         //Send the request
       
       if (httpCode > 0) {                                //Check the returning code
         String payload1 = http.getString();              //Get the FIRST IP Address

         EMailSender::EMailMessage message;               //Email the IP
         message.subject = "ESP8622";  
         message.message = payload1;
         EMailSender::Response resp0 = emailSend.send("ReceiverEmail@gmail.com", message);

         String payload2;
         for(int i=1; i>0; i++) {
          if (WiFi.status() == WL_CONNECTED) {            //Check WiFi connection status
            HTTPClient http;                              //Declare an object of class HTTPClient
            http.begin("http://api.ipify.org");           //Specify request destination
            int httpCode = http.GET();                    //Send the request
       
            if (httpCode > 0) {                           //Check the returning code
              String payload2 = http.getString();         //Get the SECOND IP Address
           
              if(payload1 != payload2) {                  //If Different, Break For Loop and Restart Void Loop
                delay(360000);                            //Delay 6 min
                break;
              }                                           //Else, test IP Address agin
              delay(360000);                              //Delay 6 min
            }
          http.end();   //Close connection
         }
        }
      }
    http.end();   //Close connection
  }
}