Can't use DDNS to connect WEMOS to Local server

Hello,

Somebody knows why I can connect a wemos from WAN to my local server using Public IP, but not DDNS?

Init WiFi…Init WiFi Done
Init Blynk[9426] Connecting to VOO-022615
[9427] Connected to WiFi
[9427] IP: 192.168.0.108
[9427]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.5.4 on Arduino

[9436] Connecting to r.ddns.net:8080
[14437] Connecting to r.ddns.net:8080
[19438] Connecting to r.ddns.net:8080
[24439] Connecting to r.ddns.net:8080
[29440] Connecting to r.ddns.net:8080

PS: I deleted details here to better explanation below :wink:

Not sure what you are tying to do here… I use DDNS to connect my phone/tablets to my network, and thus to my Local Server… but all my hardware devices are INSIDE my network, along with my Local Server.

If for some reason you are utilizing a device outside of your network, then have you setup proper port forwarding of 8080 from WAN to LAN?

Hello Gumner,
Yes, the device is outside the LAN and the port forward is done.
What I want to explain is that the DDNS only works if I connect the DEVICE TO WiFi in DHCP.
If I connect the device to WiFi in IP static, I have to use the IP public of server instead of DDNS.

@Alx-I: The router that you connect to via WiFi needs to resolve the host name which it does not seem to do.
Either because DDNS is not set properly or because it does not provide the functionality to do it.

The DDNS works fine if I use DHCP of the router, not if I fix the IP of the device.
I put a more complete code below to be more understandable.
I use a non blocking method of connection, and try to use IP statis instead of DHCP to spare battery.
Once Blynk is connected and data are sent, the project goes in deepsleep mode for a while.

So I work in 2 steps:
1- connect Wifi in IP static => if fails, try in DHCP (non-blocking method)
2- connect to blynk “void Init_Blynk()” => the problem is in this part of sketch (non-blocking method)

void Connect_Blynk(){    //first try in IP Statique + if fails second try in DHCP

  if (!Blynk.connected() && Mode_WiFi == 0){         //first try quick connexion BLYNK in IP Static
  WiFi.forceSleepWake();                                       //wake from modem sleep (150mA au lieu de 15mA en mode modem-sleep)
  WiFi.hostname(My_Hostname);                                  //config hostname
  WiFi.persistent(false);                                      //memorise new connections credentials in flash only if they have changed
  
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);           //Disable DHCP-> win ~4sec for connexion
  Init_WiFi();
    
    if(WiFi.status() != WL_CONNECTED){Connect_Failure();}
    if(WiFi.status() == WL_CONNECTED){Init_Blynk();}
  }
    
  if (!Blynk.connected() && Mode_WiFi == 1){  //second try BLYNK in DHCP (+4sec average), if IP Static failed  
  WiFi.forceSleepWake();                                       //wake from modem sleep (150mA au lieu de 15mA en mode modem-sleep)
  WiFi.hostname(My_Hostname);                                  //config hostname
  WiFi.persistent(false);                                      // memorise new connections credentials in flash only if they have changed
  Init_WiFi();

    if(WiFi.status() != WL_CONNECTED){Connect_Failure();}
    if(WiFi.status() == WL_CONNECTED){Init_Blynk();}    
  }
}


void Connect_Failure(){
  if (Mode_WiFi==0) {Mode_WiFi=1;}     // flag to work in mode DHCP  
  else if (Mode_WiFi==1){Mode_WiFi=0;} // flag to work in mode STATIC IP 
ESP.deepSleep(interval * 1000000,WAKE_RF_DISABLED);}   //reboot
}


void Init_WiFi(){     //premier essai en IP Statique + deuxième essai en DHCP
int WiFi_counter = 0;   
WiFi_Seuil_counter=4000;   

WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str());         //advanced WiFi Management (non-blocking method)  
  while (WiFi.status() != WL_CONNECTED && (WiFi_counter <= 2 * WiFi_Seuil_counter)) {   //wait connection max 30sec, if fails abort to spare battery
  delay(5);
    if(WiFi_counter >= WiFi_Seuil_counter){break;}         
  WiFi_counter++;  
  }
}


void Init_Blynk(){      
int Blynk_counter = 0; 
Retries_Blynk=3;   //3 tries

Blynk.config(blynk_token, My_IP_Server, server_port);  //works fin with device in DHCP or in IP static mode
//Blynk.config(blynk_token, Server_DDNS, server_port);  //OK with device in DHCP, but KO in IP static mode ???????????????   <---- HERE IS MY PROBLEM, WHY IP STATIC + DDNS IS KO ????????

  while(!Blynk.connected() && WiFi.status() == WL_CONNECTED){
  Blynk.connect();
  Blynk_counter++; 
    if(Blynk_counter >= Retries_Blynk){break;}  
  delay(1);
  }
  if(Blynk.connected()){Serial.println("Blynk Server Connected");}
  else{Serial.println("Blynk Server Unreachable!");Connect_Failure();} 
}

This doesn’t make sense to me… DHCP is for assigning an internal IP address to a device inside your network (LAN)… Not for use with a device connecting from outside (WAN).

For outside network connection, DDNS should redirect your device to your public IP and the port forwarding takes it from there.

Well, I try to explain better.

The device and the server are not in the same LAN like this ->
On the wemos side -> I connect the device in WiFi to a router (in DHCP, or better with fixed IP)
On the server side -> I connect the server to my home router (with its Public IP assigned to a DDNS, and with the setup of port forwarding done and tested).

The device connect to the server if the device is connected in DHCP to its router. Both work if I use Public IP or DDNS.
So far I’m sure my DDNS & port forwarding are OK.

But the device don’t connect to the server if the device is connected in static IP to its router and uses DDNS of my home router. If I use directly Public IP it’s OK, but not with DDNS. But as I don’t have a fixed IP Public, I want to use DDNS.
I could simply use the working method DHCP+DDNS, but I want to spare the battery and win some seconds on the connection…

In the code, the blocking method is this one ->

char Server_DDNS[] = "rx.ddns.net";
Blynk.config(blynk_token, Server_DDNS, server_port);  //OK with device in DHCP, but KO in IP static mode ???????????????   <---- HERE IS MY PROBLEM, WHY IP STATIC + DDNS IS KO ????????

OK, I see now… you are referencing two different routers, makes sense.

Well, this is not Blynk specific and involves more networking understanding then normal with this forum… but I suspect your issue is due to the fact that using the DHCP will not only assign an IP but handle all the gateway and DNS routing, whereas I believe setting a static IP tends to require you to handle the rest on your own as well. Thus If you don’t have the device-router DNS address properly set, then it will not pass through any internet requests… or so I think, I am not a router expert :stuck_out_tongue:

1 Like

Have you tried using WiFi.config with the extra gateway parameter:

WiFi.config(ip, dns, gateway, subnet);

Pete.

2 Likes

Hello,

In fact I’ve tested WiFi.config(ip, gateway, subnet, dns); … so dns at end? don(t know where I saw it… I’ll test & feedback you.

Thank you for suggestion!

Not better :frowning:
Trying to connect WiFi (IP Static) max 30s…Static IP address successed: 192.168.0.108
WM_IP_Wemos_IPAddress= 192.168.0.108
WM_GW_Wemos_IPAddress= 192.168.0.1
WM_Subnet_Wemos_IPAddress= 255.255.255.0
WM_DNS_Wemos_IPAddress= 192.168.0.1
WM_SSID_Char: VOO-xxxxxxxx
WM_PASS_Char: xxxxxxxx
WM_Blynk_Token_Char: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
WM_IP_Server_IPAddress: 109.88.xx.xxx
WM_Port_Server_Int: 8080
WM_DDNS_Server_String: rx.ddns.net

[737326]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.5.4 on Arduino

Trying to connect Server max 6 times
Tentative -> 1
[737345] Connecting to rx.ddns.net:8080
[742346] Connecting to rx.ddns.net:8080
Tentative -> 2
[747347] Connecting to rx.ddns.net:8080
[752348] Connecting to rx.ddns.net:8080
Tentative -> 3
[757349] Connecting to rx.ddns.net:8080
[762350] Connecting to rx.ddns.net:8080
Tentative -> 4
[767351] Connecting to rx.ddns.net:8080
[772352] Connecting to rx.ddns.net:8080
Tentative -> 5
[777353] Connecting to rx.ddns.net:8080
[782354] Connecting to rx.ddns.net:8080
Tentative -> 6
[787355] Connecting to rx.ddns.net:8080
[792356] Connecting to rx.ddns.net:8080
Blynk Server Unreachable!

Ok, this is a DNS problem as it seems that it can’t resolve the IP.

Syntax:

WiFi.config(ip);
WiFi.config(ip, dns);
WiFi.config(ip, dns, gateway);
WiFi.config(ip, dns, gateway, subnet);
Parameters

ip: the IP address of the device (array of 4 bytes)

dns: the address for a DNS server.

gateway: the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1

subnet: the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0

If everything works as expected I’d try to set the DNS to one of google:
8.8.8.8
8.8.4.4
Try and post back.
Rgds,
Erik

2 Likes

Hello,

I changed to the right order for parameters in WiFi.config(ip, dns, gateway, subnet); and changed to the DNS 8.8.8.8.
And that’s ok now :slight_smile:

Thanks guys!

1 Like

Glad to hear it. I’ll change this thread to solved.

Pete.