Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to config ip_route for pinging another esp? #17

Open
TridentTD opened this issue Feb 26, 2020 · 4 comments
Open

How to config ip_route for pinging another esp? #17

TridentTD opened this issue Feb 26, 2020 · 4 comments

Comments

@TridentTD
Copy link

TridentTD commented Feb 26, 2020

I have 2 esps as the following scenario

| ESP1-STA 192.168.43.10 | <-> |Home Router 192.168.43.1| <-> | 192.168.43.20 STA-ESP2 |

By this lwip-nat_arduino's liblwip_src.a, how to set any function
for I can ping ESP2 192.168.43.20 by ESP1 ?

I have tried to set ip_add_route( ip, mask, gw); but it's worked.
ESP1 can't see ESP2 by pinging.

Thank you.

@martin-ger
Copy link
Owner

All hosts are in the same subnet, they should "see" each other without any route.

ping 192.168.43.20

fails?

What kind of ping command do you use on the ESP?

@TridentTD
Copy link
Author

TridentTD commented Feb 29, 2020

Thanks for the answer and sorry for my late reply.

I change scenario to the following and with the code.
The result of pinging to the Notebook-STA (192.168.43.6) can't ping.
How to solve the issue?

Thank you.

scenario

| ESP1-STA 192.168.43.254 | <-> |Home Router 192.168.43.1| <-> | 192.168.43.6 Notebook-STA |

Environment

  • NodeMcu v3
  • lwip from this lwip_nat_arduino replace to the original of Arduino-ESP8266
    and select lwIP "v1.4 Higher Bandwidth" for compiling.

code for ESP1-STA

#include <ESP8266WiFi.h>
#include "lwip/lwip_napt.h"
#include "lwip/app/dhcpserver.h"
#include "lwip/ip_route.h"
#include "user_interface.h"
#include "ping.h"

#define  SSID         "-----------------"
#define  PASSWORD     "-----------------"

#define  AP_SSID      "-----------------"
#define  AP_PASSWORD  "-----------------"

#define NAPT 1000
#define NAPT_PORT 10

void esp_ping(IPAddress ip);

void setup() {
  Serial.begin(115200); Serial.println();
  WiFi.begin(SSID, PASSWORD);
  WiFi.softAP(AP_SSID, AP_PASSWORD);

  ip_napt_init(NAPT, NAPT_PORT);
  ip_napt_enable_no(SOFTAP_IF, 1);

  while(!WiFi.isConnected()) { delay(400); Serial.print("."); }
  Serial.println();
  Serial.print("STA IP : "); Serial.println(WiFi.localIP());
  Serial.print("AP  IP : "); Serial.println(WiFi.softAPIP());

  dhcps_set_DNS(WiFi.dnsIP(0));
  
  esp_ping(IPAddress(192,168,43,6));  // ping to IP of the Notebook-STA.
}

void loop() {
  // put your main code here, to run repeatedly:

}

void esp_ping(IPAddress ip){
  static struct ping_option  ping_opt;

  ping_opt.count        = 4;
  ping_opt.coarse_time  = 1;
  ping_opt.ip           = ip.v4();
  
  ping_regist_recv(&ping_opt, [](void* arg, void *pdata){
    struct ping_option* ping_opt = (struct ping_option*) arg;
    struct ping_resp *  ping_res = (struct ping_resp*) pdata;
      
    if(ping_res->ping_err == -1){
      Serial.printf("Reply from %s : offline\n", 
          IPAddress(ping_opt->ip).toString().c_str() );
    }else{
      Serial.printf("Reply from %s : bytes = %d time = %d ms\n", 
          IPAddress(ping_opt->ip).toString().c_str(), 
          ping_res->bytes, ping_res->resp_time);
    }
  });
  
  ping_start(&ping_opt);
}

Result

image

Notebook-STA by ipconfig

image

@martin-ger
Copy link
Owner

NAPT should not be involved here at all - it only comes into play, when you try to ping from an additional STA in the AP (192.168.4.x) network.

So this should work. Does it work with the original lwip? With an lwip 2 version? Without these calls?

ip_napt_init(NAPT, NAPT_PORT);
ip_napt_enable_no(SOFTAP_IF, 1);

@TridentTD
Copy link
Author

TridentTD commented Mar 1, 2020

Thanks again.

When I tried by the following code with lwip v2 Lower Memory,
The result can ping from the Notebook (192.168.43.6) to ESP (192.168.43.254).
but can't ping from ESP (192.168.43.254) to the Notebook (192.168.43.6).

Do you mind to guild how to ping from the esp-sta to the notebook-sta?

Code ( compiled with lwip v2 )

#include <ESP8266WiFi.h>
//#include "lwip/lwip_napt.h"
//#include "lwip/app/dhcpserver.h"
//#include "lwip/ip_route.h"
#include "user_interface.h"
#include "ping.h"

#define  SSID         "-----------------"
#define  PASSWORD     "-----------------"

#define  AP_SSID      "-----------------"
#define  AP_PASSWORD  "-----------------"

// #define NAPT 1000
// #define NAPT_PORT 10

void esp_ping(IPAddress ip);

void setup() {
  Serial.begin(115200); Serial.println();
  WiFi.begin(SSID, PASSWORD);
  WiFi.softAP(AP_SSID, AP_PASSWORD);

//  ip_napt_init(NAPT, NAPT_PORT);
//  ip_napt_enable_no(SOFTAP_IF, 1);

  while(!WiFi.isConnected()) { delay(400); Serial.print("."); }
  Serial.println();
  Serial.print("STA IP : "); Serial.println(WiFi.localIP());
  Serial.print("AP  IP : "); Serial.println(WiFi.softAPIP());
  Serial.print("DNS IP : "); Serial.println(WiFi.dnsIP(0));

//  dhcps_set_DNS(WiFi.dnsIP(0));

  esp_ping(IPAddress(192,168,43,6));  // ping to IP of the Notebook-STA.
}

void loop() {
  // put your main code here, to run repeatedly:

}

void esp_ping(IPAddress ip){
  static struct ping_option  ping_opt;

  ping_opt.count        = 4;
  ping_opt.coarse_time  = 1;
  ping_opt.ip           = ip.v4();
  
  ping_regist_recv(&ping_opt, [](void* arg, void *pdata){
    struct ping_option* ping_opt = (struct ping_option*) arg;
    struct ping_resp *  ping_res = (struct ping_resp*) pdata;
      
    if(ping_res->ping_err == -1){
      Serial.printf("Reply from %s : offline\n", 
          IPAddress(ping_opt->ip).toString().c_str() );
    }else{
      Serial.printf("Reply from %s : bytes = %d time = %d ms\n", 
          IPAddress(ping_opt->ip).toString().c_str(), 
          ping_res->bytes, ping_res->resp_time);
    }
  });
  
  ping_start(&ping_opt);
}

Result

image

Ping from the Notebook(192.168.43.6) to the ESP (192.168.43.254)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants