Get external IP (Public IP) address to Blynk.logEvent("notify",

Hi guys
I’m a new member, don’t know about programming, so I need your help.
I want to write code to get public ip, and have guessed this code.

void GetExternalIP(){
//GetExternalIP();
  Serial.println("  GetExternalIP(){  ");
  WiFiClient client;
  if (!client.connect("api.ipify.org", 80)) {
    Serial.println("Failed to connect  !");
  } else {
    int timeoutip = millis() + 5000;
    client.print("GET /?format=json HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
    while (client.available() == 0) {
      if (timeoutip - millis() < 0) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }
int size;
    while ((size = client.available()) > 0) {
      uint8_t* msg = (uint8_t*)malloc(size);
      size = client.read(msg, size);
      Serial.write(msg, size);
      free(msg);
    }
  }
  Serial.println("");
  //GetExternalIP();
}

and the result received on the serial screen is

12:02:06.002 ->   GetExternalIP(){  
12:02:06.606 -> HTTP/1.1 200 OK
12:02:06.606 -> Server: Cowboy
12:02:06.606 -> Connection: keep-alive
12:02:06.606 -> Content-Type: application/json
12:02:06.606 -> Vary: Origin
12:02:06.606 -> Date: Thu, 30 Jun 2022 05:02:06 GMT
12:02:06.606 -> Content-Length: 22
12:02:06.606 -> Via: 1.1 vegur
12:02:06.606 -> 
12:02:06.606 -> {"ip":"117.3.231.155"}

And now, I want to create a variable called ExternalIP, converted to String so that I can enter the code Blynk.notify() with an example like this

Blynk.logEvent("Blynkwrite", " <br>" + ssid_apnew + " restartcode = " + restartcode + " = " + restartcodetext + " <br>" + " SSID " + ssid + " ExternalIP " + ExternalIP + " IP " + WiFi.localIP().toString().c_str() + " MAC " + WiFi.macAddress().c_str() + " <br>" + " CHIP ID " + ESP.getChipId() + " <br> " + " PHIENBAN " + PHIENBAN );

Simply put, I want to get the Public IP value and attach it to Blynk.notify so that I can know the public ip of the device when I’m outside.

Thank you for your review.
Please write code for me.
Looking forward to your help.

@nlqt2022 please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

It’s also a good idea to add triple backticks to your serial monitor output too.

Pete.

Sorry, I’m a new member, I’ll try to fix it. Thanks.

You’ve edited your post, but used blockquotes instead of triple backticks.
Please format the code and serial monitor output correctly, otherwise it will be removed from your post.

Pete.

1 Like

thanks for your reminder, looks like i finished editing.

This is an esp32 sketch, but you can easily modify it to work with the esp8266

#define BLYNK_TEMPLATE_ID "your template ID"
#define BLYNK_DEVICE_NAME "your device name"
#define BLYNK_AUTH_TOKEN "your auth token "


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "your ssid";
char pass[] = "your password";

String payload;
const String url = "http://api.ipify.org/";

BlynkTimer timer;

void setup()
{
  // Debug console
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  timer.setInterval(5000L, getdata);
  }

 void loop()
 {
 Blynk.run();
 timer.run();
 }

void getdata()
{
 HTTPClient http;
 http.begin(url);

 int httpResponseCode = http.GET();
 if (httpResponseCode > 0) {
  Serial.print("HTTP ");
  Serial.println(httpResponseCode);
  payload = http.getString();
  Serial.println();
  Serial.println(payload);
  Blynk.virtualWrite(V10, payload);
 }
 else {
  Serial.print("Error code: ");
  Serial.println(httpResponseCode);
  Serial.println(":-(");
  http.end();
 }
}

This example will get the data from the url and send it to virtual pin number 10 (in my case), which is a string type datastream.

1 Like

Thank you for your code.

I had used this library for esp8266.

#include <ESP8266HTTPClient.h>

And, i get the result

22:29:37.176 -> HTTP 200
22:29:37.176 -> 
22:29:37.176 -> 117.3.231.155

I think this result is enough for my needs.
Thank you again.
Hope you guys later can refer to your code for their needs.

1 Like