kv2004 发表于 2021-3-2 15:28:03

为什么,电脑能ping通的ip地址,ESP8266 ping不通?

本帖最后由 kv2004 于 2021-3-2 15:33 编辑

比如
★用电脑ping:

都能ping通。
★以上地址用ESP8266就ping不通,
   ESP8266也不是都ping不通,
   像114.114.114.114 ,
      114.114.115.115 ,
      182.254.118.118 等等地址 就能够ping通。

这是为什么呢?

ESP8266是用的ESP-01 Relay模块:

编译环境是Arduino 1.8.13,选择的开发板是 Generic ESP8266 Module 用到的库是 ESP8266-ping 版本 2.0.1

我家的路由器有时抽风上不了网,重起一下就好,所以想做个监测能否上网的东西,监测到不能上网后,就断电,重新上电。所以就做了这个东西。
但是,发现,电脑都能ping通的ip地址,模块ping不通。
程序是在 ESP8266-ping 库的演示程序上修修改改得到的。
还有,发现ping www.baidu.com这样的也是ping不通的。
附上全部程序,帮忙看看。
/*****************************************************************************
Arduino library handling ping messages for the esp8266 platform

MIT License

Copyright (c) 2018 Alessio Leoncini

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/

#include <Pinger.h>
#include <ESP8266WiFi.h>
extern "C"
{
#include <lwip/icmp.h> // needed for icmp packet definitions
}

// Set global to avoid object removing after setup() routine
Pinger pinger;

#define WIFINAME "我的wifi,这里隐藏了"
#define WIFIPASS "我的WIFI密码,这里隐藏了"

intipTable[]={//来自:https://zhuanlan.zhihu.com/p/53958870
{8,8,8,8},
{8,8,4,4},
{199,85,126,10},
{199,85,127,10},
{199,85,126,20},
{199,85,127,20},
{199,85,126,30},
{199,85,126,30},
{208,67,222,222},
{208,67,220,220},
{208,67,222,123},
{208,67,220,123},
{84,200,69,80},
{84,200,70,40},
{8,26,56,26},
{8,20,247,20},
{64,6,64,6},
{64,6,65,6},
{192,95,54,3},
{192,95,54,1},
{81,218,119,11},
{209,88,198,133},
{1,1,1,1},
{114,114,114,114},
{114,114,115,115},
{119,29,29,29},
{182,254,118,118},
{223,5,5,5},
{223,6,6,6},
{180,76,76,76},
{1,2,4,8},
{210,2,4,8}
};

volatile bool stationConnected;//连上了wifi
volatile bool pingOK;//ping通过

/**
* 得到一个IP
* 参数:序号
*      ip
* 返回:序号
*/
int getIp(int ID, int ip)
{
int i;
i = sizeof(ipTable)/sizeof(int)/4;

//向后循环获取
ID++;
if(ID>=i) ID = ID%i;

ip = ipTable;
ip = ipTable;
ip = ipTable;
ip = ipTable;

return ID;
}

/**
* 给wifi断电上电
*/
void reStartWifi(void)
{
//断电
digitalWrite(0, LOW);
Serial.print("断电");

delay(2000);

//上电
Serial.print("上电");
digitalWrite(0, HIGH);
}

void setup()
{
int i;

pinMode(0,OUTPUT);

// 状态输出
Serial.begin(115200);

start:
//上电
digitalWrite(0, HIGH);
      
i = 0;
do{
    // 连接WIFI
    stationConnected = WiFi.begin(WIFINAME,WIFIPASS);

    // 检查连接错误
    if(!stationConnected)
    {
      Serial.print(i);
      Serial.print("错误,连不上Wifi:");
      Serial.print(WIFINAME);
      Serial.print(" ");
      Serial.println(WIFIPASS);

      i++;
      if(i>=5)
      {
      i = 0;
      reStartWifi();
      }
      delay(10000);
    }
}while(!stationConnected);

// 等待连接完成
i = 0;
Serial.print("正在连路由器...");
while(WiFi.status() != WL_CONNECTED)
{
    delay(500);
    Serial.print(".");
    if(i++>=120) goto start;
}
Serial.print("连上了\n");

pinger.OnReceive([](const PingerResponse& response)
{
    if (response.ReceivedResponse)
    {
      Serial.printf(
      "Reply from %s: bytes=%d time=%lums TTL=%d\n",
      response.DestIPAddress.toString().c_str(),
      response.EchoMessageSize - sizeof(struct icmp_echo_hdr),
      response.ResponseTime,
      response.TimeToLive);
      pingOK= true;
    }
    else
    {
      Serial.printf("Request timed out.\n");
    }

    // Return true to continue the ping sequence.
    // If current event returns false, the ping sequence is interrupted.
    return true;
});

pinger.OnEnd([](const PingerResponse& response)
{
    // Evaluate lost packet percentage
    float loss = 100;
    if(response.TotalReceivedResponses > 0)
    {
      loss = (response.TotalSentRequests - response.TotalReceivedResponses) * 100 / response.TotalSentRequests;
    }
   
    // Print packet trip data
    Serial.printf(
      "Ping statistics for %s:\n",
      response.DestIPAddress.toString().c_str());
    Serial.printf(
      "    Packets: Sent = %lu, Received = %lu, Lost = %lu (%.2f%% loss),\n",
      response.TotalSentRequests,
      response.TotalReceivedResponses,
      response.TotalSentRequests - response.TotalReceivedResponses,
      loss);

    // Print time information
    if(response.TotalReceivedResponses > 0)
    {
      Serial.printf("Approximate round trip times in milli-seconds:\n");
      Serial.printf(
      "    Minimum = %lums, Maximum = %lums, Average = %.2fms\n",
      response.MinResponseTime,
      response.MaxResponseTime,
      response.AvgResponseTime);
    }
   
    // Print host data
    Serial.printf("Destination host data:\n");
    Serial.printf(
      "    IP address: %s\n",
      response.DestIPAddress.toString().c_str());
    if(response.DestMacAddress != nullptr)
    {
      Serial.printf(
      "    MAC address: " MACSTR "\n",
      MAC2STR(response.DestMacAddress->addr));
    }
    if(response.DestHostname != "")
    {
      Serial.printf(
      "    DNS name: %s\n",
      response.DestHostname.c_str());
    }

    return true;
});

/*
// Ping default gateway
Serial.printf(
    "\n\nPinging default gateway with IP %s\n",
    WiFi.gatewayIP().toString().c_str());
if(pinger.Ping(WiFi.gatewayIP()) == false)
{
    Serial.println("Error during last ping command.");
}

delay(10000);

// Ping technologytourist.com
Serial.printf("\n\nPinging technologytourist.com\n");
if(pinger.Ping("technologytourist.com") == false)
{
    Serial.println("Error during ping command.");
}

delay(10000);

// Ping undefinedname
Serial.printf("\n\nPinging undefinedname\n");
if(pinger.Ping("undefinedname") == false)
{
    Serial.println("Error during ping command.");
}

delay(10000);

// Ping invalid ip
Serial.printf("\n\nPinging invalid ip 1.2.3.4\n");
if(pinger.Ping(IPAddress(1,2,3,4)) == false)
{
    Serial.println("Error during ping command.");
}
*/
}

void loop()
{
static interr=0;
static intipID;
intip;

pingOK = false;

ipID = getIp(ipID,ip);
Serial.printf("\n\nPinging %d, ip='%d.%d.%d.%d'\n", ipID, ip, ip, ip, ip);
if(pinger.Ping(IPAddress(ip,ip,ip,ip),4,2000) == false)
    {
      Serial.println("ping过程中出错");
    }

//每次2秒,共8次,再加上一点延时
delay(10000);

if(pingOK == false)
{
    err++;
    Serial.printf("连续失败次数:%d\n",err);
    if(err>30)
    {
      err=0;
      reStartWifi();
    }
}
else
{
    err = 0;
    //30秒以后再ping
    Serial.println("等待30秒再ping");
    delay(30000);
}
}


kv2004 发表于 2021-3-3 11:46:53

本帖最后由 kv2004 于 2021-3-3 11:49 编辑

★打印了一些信息,可以看到PING 百度时,还是能够得到IP地址的,但是还是PING不通:
当前工作模式:3
连接到的接入点名字:anan
连接到的接入点密码:藏起来了
当前无线终端静态IP地址: 172.28.135.7
当前无线终端网关的IP地址: 172.28.135.1
当前无线终端的子网掩码: 255.255.0.0
DNS #1 IP为: 172.28.135.1 DNS #2 IP为: 8.8.8.8

Pinging default gateway with IP 172.28.135.1
Request timed out.
Reply from 172.28.135.1: bytes=32 time=52ms TTL=128
Reply from 172.28.135.1: bytes=32 time=32ms TTL=128
Reply from 172.28.135.1: bytes=32 time=36ms TTL=128
Ping statistics for 172.28.135.1:
    Packets: Sent = 4, Received = 3, Lost = 1 (25.00% loss),
Approximate round trip times in milli-seconds:
    Minimum = 32ms, Maximum = 52ms, Average = 40.00ms
Destination host data:
    IP address: 172.28.135.1
    MAC address: b0:d5:9d:43:93:35


Pinging www.baidu.com
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 220.181.38.150:
    Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
Destination host data:
    IP address: 220.181.38.150
    DNS name: www.baidu.com


Pinging undefinedname
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 255.255.255.255:
    Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
Destination host data:
    IP address: 255.255.255.255
    DNS name: undefinedname



★然后我修改了子网掩码和DNS后,什么也ping不到了:
当前工作模式:3
连接到的接入点名字:anan
连接到的接入点密码:也隐了
当前无线终端静态IP地址: 172.28.135.7
当前无线终端网关的IP地址: 172.28.135.1
当前无线终端的子网掩码: 255.255.255.0
DNS #1 IP为: 114.114.114.114 DNS #2 IP为: 114.114.115.115

Pinging default gateway with IP 172.28.135.1
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 172.28.135.1:
    Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
Destination host data:
    IP address: 172.28.135.1


Pinging www.baidu.com
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 255.255.255.255:
    Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
Destination host data:
    IP address: 255.255.255.255
    DNS name: www.baidu.com


Pinging undefinedname
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 255.255.255.255:
    Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
Destination host data:
    IP address: 255.255.255.255
    DNS name: undefinedname


zouzhichao 发表于 2021-3-3 12:13:04

防火墙屏蔽了ICMP 报文?

kv2004 发表于 2021-3-3 12:36:17

zouzhichao 发表于 2021-3-3 12:13
防火墙屏蔽了ICMP 报文?

{:handshake:}我对这些不懂,估计是吧。
我换了一个wifi就正常了。
以前那个WIFI是插在USB口的"360随身WIFI",手机连上它后,其中一个手机显示“已连接,但无法访问互联网”,但用手机浏览器能正常使用。另一个手机就完全正常。可能这个WIFI真的挡住了一些信息。

kv2004 发表于 2021-3-3 13:04:04

二楼的修改子网掩码和DNS后,还要重新WiFi.reconnect()一下,否则,什么IP都ping不通了。

针对ping不通,但还能上网的情况,我想,用“是否能ping通”来判断是否网络中断,这个想法可能还不好。

SkyGz 发表于 2021-3-3 13:10:37

用 这个方法试试, 我是这样用的
WiFiClient client;
HTTPClient http;


bool checknet(){
IPAddress ipaddr(sett.IPADDR);
if (!client.connect(ipaddr, sett.PORT)) {
    delay(2000);
    return false;
}

if (client.connected()) {
    client.stop();
    return true;
}

/*
http.begin(client, (String)sett.IPADDR, 80, "/");
auto httpCode = http.GET();
return (httpCode == HTTP_CODE_OK);
*/
}
void loop() {

    sec++;
    Serial.print(".");
    delay(500);
    if (sec >= 60){
      Serial.println("正在检查网络...");
      if (WiFi.status() == WL_CONNECTED) {
      if (!checknet()){
          fail++;
          Serial.print("连接失败...次数:");
          Serial.println((String)fail);
      } else {
          fail=0;
          Serial.println("网络正常!");
          }
      } else {
          fail++;
          Serial.print("连接失败...次数:");
          Serial.println((String)fail);
      }
      sec=0;
      
      if (fail>=10){ //连接失败大于或等于10次, 执行 断电 上电 复位路由
      fail=0;
      Serial.println("重启设备...");
      digitalWrite(GPIO_RELAY, HIGH); //关闭继电器
      delay(3000);
      digitalWrite(GPIO_RELAY, LOW); //开启继电器
      }
    }
}

qwe2231695 发表于 2021-3-3 13:43:00

可以试试连接NTP时间服务器. 获得时间就等于有网.

kv2004 发表于 2021-3-3 13:46:31

SkyGz 发表于 2021-3-3 13:10
用 这个方法试试, 我是这样用的
WiFiClient client;
HTTPClient http;


这个方式,可能我那个“360随身WIFI”就拦不住了。稍后我试试。

kv2004 发表于 2021-3-3 13:50:36

qwe2231695 发表于 2021-3-3 13:43
可以试试连接NTP时间服务器. 获得时间就等于有网.

我开始想的就是弄多个地址,不要老是薅一只羊的羊毛,“360随身wifi”可能屏蔽了一些ip的PING,但肯定不会屏蔽正常的浏览网页的需求。

kv2004 发表于 2021-3-3 15:16:21

似乎就是 路由把 ping 屏蔽了。
改成了 用url 的 客户端 测试是否网络通不通的方式,附上能用的程序。
测试中发现,www.google.com是一直失败,而 t66y 却能一直成功,但用 计算机的浏览器就是上不去,气人不。
/*****************************************************************************
Arduino library handling ping messages for the esp8266 platform

MIT License

Copyright (c) 2018 Alessio Leoncini

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/

#include <ESP8266WiFi.h>

//#define WIFINAME "XXXXXX"
#define WIFINAME "XXXXXX"
#define WIFIPASS "XXXXXXXXX"

char*urlTable[]={
"www.baidu.com"
,"www.163.com"
,"www.sina.com.cn"
,"www.sohu.com"
,"www.t66y.com"
,"www.163.net"
,"www.360.cn"
,"www.google.com"
,"www.taobao.com"
,"www.jd.com"
,"weibo.com"
//,"www.amobbs.com" 封了IP就麻烦了
};

volatile bool stationConnected;//连上了wifi

int getUrl(int ID, char** url)
{
int i;
i=(sizeof(urlTable)/sizeof(char *));

//向后循环获取
ID++;
if(ID>=i) ID = ID%i;

*url = urlTable;

return ID;
}

/**
* 给wifi断电上电
*/
void reStartWifi(void)
{
//断电
digitalWrite(0, LOW);
Serial.print("断电");

delay(2000);

//上电
Serial.print("上电");
digitalWrite(0, HIGH);
}

void setup()
{
int i;

pinMode(0,OUTPUT);

// 状态输出
Serial.begin(115200);

start:
//上电
digitalWrite(0, HIGH);
      
i = 0;
do{
    // 连接WIFI
    stationConnected = WiFi.begin(WIFINAME,WIFIPASS);

    // 检查连接错误
    if(!stationConnected)
    {
      Serial.print(i);
      Serial.print("错误,连不上Wifi:");
      Serial.print(WIFINAME);
      Serial.print(" ");
      Serial.println(WIFIPASS);

      i++;
      if(i>=5)
      {
      i = 0;
      reStartWifi();
      }
      delay(10000);
    }
}while(!stationConnected);

// 等待连接完成
i = 0;
Serial.print("正在连路由器...");
while(WiFi.status() != WL_CONNECTED)
{
    delay(500);
    Serial.print(".");
    if(i++>=120) goto start;
}
Serial.print("连上了\n");

/* 如果修改了参数,就要重新连接
WiFi.config(WiFi.localIP(),WiFi.gatewayIP(),IPAddress(255,255,255,0),IPAddress(114,114,114,114),IPAddress(114,114,115,115));
WiFi.reconnect();
   
// 等待连接完成
i = 0;
Serial.print("正在连路由器...");
while(WiFi.status() != WL_CONNECTED)
{
    delay(500);
    Serial.print(".");
    if(i++>=120) goto start;
}
Serial.print("连上了\n");
*/

//打印本机信息
Serial.print("当前工作模式:");   // 告知用户设备当前工作模式
Serial.println(WiFi.getMode());
Serial.print("连接到的接入点名字:");
Serial.println(WiFi.SSID());            // 告知用户连接到的接入点WiFi名
Serial.print("连接到的接入点密码:");
Serial.println(WiFi.psk());      // 告知用户连接到的接入点WiFi密码
Serial.print("当前无线终端静态IP地址: ");// 告知用户当前无线终端的IP地址(也就是我们设置的地址)
Serial.println(WiFi.localIP());
Serial.print("当前无线终端网关的IP地址: ");// 告知用户当前无线终端网关的IP地址
Serial.println(WiFi.gatewayIP());
Serial.print("当前无线终端的子网掩码: ");// 告知用户当前无线终端的子网掩码地址
Serial.println(WiFi.subnetMask());
Serial.print("DNS #1 IP为: ");//打印出获取的DNS地址
Serial.print(WiFi.dnsIP());
Serial.print(" ");
Serial.print("DNS #2 IP为: ");
Serial.print(WiFi.dnsIP(1));
}

bool checkNet(void)
{
static inturlID;
WiFiClient client;
char*url;
urlID = getUrl(urlID,&url);
Serial.printf("\n\nURL=%s\n", url);
if(!client.connect(url,80))
{
    Serial.println("失败");
    return false;
}
else
{
    client.stop();
    Serial.println("成功");
    return true;
}
}

void loop()
{
static interr=0;

if(checkNet()==false)
{
    err++;
    Serial.printf("连续失败次数:%d\n",err);
    if(err>30)
    {
      err=0;
      reStartWifi();
    }   
}
else
{
    err = 0;
    //30秒以后再ping
    Serial.println("等待30秒再来");
    delay(30000);
}
}


页: [1]
查看完整版本: 为什么,电脑能ping通的ip地址,ESP8266 ping不通?