khoih-prog / DDNS_Generic

A simple library that implements an automatic DDNS Update Client for SAM DUE, nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, RP2040-based RASPBERRY_PI_PICO, Portenta_H7, etc. besides ESP8266/ESP32, using ESP8266-AT/ESP32-AT WiFi, WiFiNINA, Ethernet W5x00, ENC28J60 or LAN8742A. It is designed to be light-weight and currently supports DuckDNS, No-ip, DynDNS, Dynu, enom, all-inkl, selfhost.de, dyndns.it, strato, freemyip, afraid.org.
MIT License
11 stars 2 forks source link

Possible to get current public IP? #1

Closed mikekgr closed 3 years ago

mikekgr commented 3 years ago

Dear Mr. Hoang, I am trying your very good DDNS_Generic library using no-ip provider having a classic ESP8266 board (WeMos D1 mini). What I want and I haven't find the way to do, is to get somehow my current public IP. Do you support this through your library? I have seen that in your DDNS_Generic.h at DDNSGenericClass you have as private the publicIPRequest but it is uncertain to me if and how can call it. Please instruct me how can achieve that.

Thanks and Best Regards, Mike Kranidis

khoih-prog commented 3 years ago

Dear Mike,

You can currently use the onUpdateCallback() function and get the current Public IP from newIP. The Public IP got by this way will reflect in real-time (whenever a public IP change is detected). Just copy to your local/global var and use.

This code snippet is in every example:

void onUpdateCallback(const char* oldIP, const char* newIP)
{
  Serial.print("DDNSGeneric - IP Change Detected: ");
  Serial.println(newIP);
}

Best Regards,

mikekgr commented 3 years ago

Dear Mr. Hoang, thanks for your fast reply. I am not sure how to get the result that I want. Let me explain better: I need, inside my sketch, to print the current public IP in a particular momment, not when the IP is changed. So, how can get my current public IP? I am not sure how can use onUpdateCallback on demand and having my requested result. Sorry for the newbie question but something I missed probably from your reply.

Thanks and Best Regards, Mike Kranidis

khoih-prog commented 3 years ago

Dear Mike,

When you run an example, for example SAMD_WiFiNINA_DuckDNS_Client on Nano-33-IoT, you'll see in the terminal

Start SAMD_WiFiNINA_DuckDNS_Client on SAMD_NANO_33_IOT with WiFiNINA using WiFiNINA_Generic Library
Connecting to WiFi SSID: HueNet1
HTTP WebServer is @ IP : 192.168.2.38
[DDNS] Access whatismyipaddress
Connected
[DDNS] httpCode = 200
HttpClient::responseBody => bodyLength =15
[DDNS] Current Public IP = 206.248.aaa.bbb
[DDNS] response = 206.248.aaa.bbb
Connected
[DDNS] Sending HTTP_GET to duckdns
[DDNS] HTTP_GET = http://www.duckdns.org/update?domains=account.duckdns.org&token=12345678-1234-1234-1234-123456789012&ip=206.248.aaa.bbb
DDNSGeneric - IP Change Detected: 206.248.aaa.bbb    <=======  This is auto-generated from onUpdateCallback()
[DDNS] Updated IP = 206.248.aaa.bbb

The following line is the print-out from onUpdateCallback()

DDNSGeneric - IP Change Detected: 206.248.aaa.bbb    <=======  This is auto-generated from onUpdateCallback()
void onUpdateCallback(const char* oldIP, const char* newIP)
{
  Serial.print("DDNSGeneric - IP Change Detected: ");
  Serial.println(newIP);
}

You just copy the newIP to your global/local and use it



For example, your global var is now myPublicIP, and will be printed out every 20s

#include "defines.h"

int status = WL_IDLE_STATUS;     // the Wifi radio's status

String myPublicIP = "";

void printPublicIP()
{
  static unsigned long printPublicIP_timeout = 0;

  //KH
#define PRINT_INTERVAL    20000L
  // Print hearbeat every PRINT_INTERVAL (20) seconds.
  if ((millis() > printPublicIP_timeout))
  {
    if (myPublicIP != "")
    {
      Serial.println("==========================================");
      Serial.print("DDNSGeneric - Public IP : ");
      Serial.println(myPublicIP);
      Serial.println("==========================================");
    }

    printPublicIP_timeout = millis() + PRINT_INTERVAL;
  }
}

void onUpdateCallback(const char* oldIP, const char* newIP)
{
  myPublicIP = String(newIP); 
  Serial.print("DDNSGeneric - IP Change Detected: ");
  Serial.println(newIP); 
}

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  Serial.print("\nStart SAMD_WiFiNINA_DuckDNS_Client on " + String(BOARD_NAME));
  Serial.println(" with " + String(SHIELD_TYPE));

#if USE_WIFI_NINA
  if (WiFi.status() == WL_NO_MODULE)
#else
  if (WiFi.status() == WL_NO_SHIELD)
#endif  
  {
    Serial.println(F("WiFi shield not present"));
    // don't continue
    while (true);
  }

#if USE_WIFI_NINA
  String fv = WiFi.firmwareVersion();

  if (fv < WIFI_FIRMWARE_LATEST_VERSION)
  {
    Serial.println("Please upgrade the firmware");
  }
#endif

  WiFi.begin(ssid, password);

  Serial.println("Connecting to WiFi SSID: " + String(ssid));

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  Serial.print(F("\nHTTP WebServer is @ IP : "));
  Serial.println(WiFi.localIP());

  server.begin();

  DDNSGeneric.service("duckdns");    // Enter your DDNS Service Name - "duckdns" / "noip"

  /*
    For DDNS Providers where you get a token:
    DDNSGeneric.client("domain", "token");

    For DDNS Providers where you get username and password: ( Leave the password field empty "" if not required )
    DDNSGeneric.client("domain", "username", "password");
  */
  DDNSGeneric.client("account.duckdns.org", "12345678-1234-1234-1234-123456789012");

  DDNSGeneric.onUpdate(onUpdateCallback);
}

void loop() 
{
  // Check for New Ip Every 10 mins.
  DDNSGeneric.update(600000);
  printPublicIP();
}

and terminal output


Start SAMD_WiFiNINA_DuckDNS_Client on SAMD_NANO_33_IOT with WiFiNINA using WiFiNINA_Generic Library
Connecting to WiFi SSID: HueNet1

HTTP WebServer is @ IP : 192.168.2.38
[DDNS] Access whatismyipaddress
Connected
[DDNS] httpCode = 200
HttpClient::responseBody => bodyLength =15
[DDNS] Current Public IP = 206.248.aaa.bbb
[DDNS] response = 206.248.aaa.bbb
Connected
[DDNS] Sending HTTP_GET to duckdns
[DDNS] HTTP_GET = http://www.duckdns.org/update?domains=account.duckdns.org&token=12345678-1234-1234-1234-123456789012&ip=206.248.178.112
DDNSGeneric - IP Change Detected: 206.248.aaa.bbb
[DDNS] Updated IP = 206.248.aaa.bbb
==========================================
DDNSGeneric - Public IP : 206.248.aaa.bbb                                  <== printed from new global variable you can use
==========================================
mikekgr commented 3 years ago

Dear Mr. Hoang, many thanks for your kindness to write the sketch for me, I always appreciate what you give us so generously. I will study your sketch and I will try to implement to mine. By the way, as I wrote I am using ESP8266 Wemos d1 mini board.

Again many thanks and Best Regards, Mike Kranidis