ayushsharma82 / EasyDDNS

Easy to Use ESP8266 & ESP32 DDNS Update Client Library.
MIT License
195 stars 59 forks source link

Not working when setting a static IP for port forwarding to expose Web server on internet #20

Closed ptareco1 closed 3 years ago

ptareco1 commented 3 years ago

Hi.

First let me thank you for your great library and support to it. It is really very useful.

My problem seems that setting a static IP on the app it does not let the EasyDDNS update the Duch DNS.

The issue I'm facing is that I cannot integrate it on my NodeMCU lolin v3 (ESP8266) application. I have running on oneNodeMCU a Web server. I need to access this from the internet. So I use a port forwarding on my Router from the outside to the static IP on the nodeMCU on port 80. Then I have a DuckDNS account that I use your library to get updated.

On my router I set: Application | Protocol | Source Port | Destination IP | Destination port myApp| TCP+UDP | 80 | 192.168.1.230 | 80

If I run another standalone app (no Web server) with your EasyDDNS on a second NodeMCU it runs perfectly and updates the public IP address on the Duck DNS without any problems. It works!

My problem seems that setting a static IP on the app it does not let the EasyDDNS update the Duch DNS. So I'm using 2 NodeMCUs. I would like to integrate all the code on one NodeMCU only. Any suggestions?

This is some of the code on my first NodeMCU

#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"

const char* ssid = "SSID";
const char* password = "PASSWORD";
IPAddress static_IP(192, 168, 1, 230); // Set your Static IP address
IPAddress gateway(192, 168, 1, 1);    // Set your Gateway IP address
IPAddress subnet(255, 255, 255, 0);   // Subnet Mask

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);
  // Connect to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.config(static_IP, gateway, subnet);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

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

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address on serial monitor
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");    //URL IP to be typed in mobile/desktop browser
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

This is the code on my second NodeMCU:

#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
#include <EasyDDNS.h>

const char * ssid = "SSID";
const char * password = "PASSWORD";

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("\r\n");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.localIP()); // Print the IP address
  server.begin();

  EasyDDNS.service("duckdns");

  EasyDDNS.client("myAPP.duckdns.org", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"); // Enter your DDNS Domain & Token

  // Get Notified when your IP changes
  EasyDDNS.onUpdate([&](const char* oldIP, const char* newIP){
    Serial.print("EasyDDNS - IP Change Detected: ");
    Serial.println(newIP);
  });
}

void loop() {
  // Check for new public IP every 10 seconds
  EasyDDNS.update(10000);
  }

Many thanks Regards Pedro

issue-label-bot[bot] commented 3 years ago

Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.

ayushsharma82 commented 3 years ago

My guess is that you are not getting internet access via your router when you set a static IP. EasyDDNS internally sends a HTTP request to a URL to fetch your public IP, if there is no internet access, it fails.

I'll suggest you to check if you are able to make a HTTP request in your static IP nodemcu to see if there is internet access or not, Use this code to check:

HTTPClient http;
      http.begin("http://ipv4bot.whatismyipaddress.com/");
      int httpCode = http.GET();
      if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK) {
          Serial.println(http.getString());
        } else {
          Serial.println("HTTP Request NOT OK: "+String(httpCode));
        }
      } else {
        http.end();
        Serial.println("HTTP Request Failed: "+String(httpCode));
        return;
      }
      http.end();

Send me the serial output to diagnose it.

Regards, Ayush

ptareco1 commented 3 years ago

Hi Ayush,

Many thanks for the reply.

It is working now!

You are right. The one with Static IP address although had internet access it did not have the DNS setup, so it did not work because it was not resolving the http://ipv4bot.whatismyipaddress.com/.

I've setup the DNS on it and it is working now.

Again many thanks and keep the good work!

Thanks BRs PT