本帖最后由 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密码,这里隐藏了"
- int ipTable[][4]={//来自: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[4])
- {
- int i;
- i = sizeof(ipTable)/sizeof(int)/4;
- //向后循环获取
- ID++;
- if(ID>=i) ID = ID%i;
- ip[0] = ipTable[ID][0];
- ip[1] = ipTable[ID][1];
- ip[2] = ipTable[ID][2];
- ip[3] = ipTable[ID][3];
- 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 int err=0;
- static int ipID;
- int ip[4];
-
- pingOK = false;
- ipID = getIp(ipID,ip);
- Serial.printf("\n\nPinging %d, ip='%d.%d.%d.%d'\n", ipID, ip[0], ip[1], ip[2], ip[3]);
- if(pinger.Ping(IPAddress(ip[0],ip[1],ip[2],ip[3]),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);
- }
- }
复制代码
|