搜索
bottom↓
回复: 9

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

[复制链接]

出0入12汤圆

发表于 2021-3-2 15:28:03 | 显示全部楼层 |阅读模式
本帖最后由 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不通的。
附上全部程序,帮忙看看。
  1. /*****************************************************************************
  2. Arduino library handling ping messages for the esp8266 platform

  3. MIT License

  4. Copyright (c) 2018 Alessio Leoncini

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

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

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

  21. #include <Pinger.h>
  22. #include <ESP8266WiFi.h>
  23. extern "C"
  24. {
  25.   #include <lwip/icmp.h> // needed for icmp packet definitions
  26. }

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

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

  31. int  ipTable[][4]={//来自:https://zhuanlan.zhihu.com/p/53958870
  32.   {8,8,8,8},
  33.   {8,8,4,4},
  34.   {199,85,126,10},
  35.   {199,85,127,10},
  36.   {199,85,126,20},
  37.   {199,85,127,20},
  38.   {199,85,126,30},
  39.   {199,85,126,30},
  40.   {208,67,222,222},
  41.   {208,67,220,220},
  42.   {208,67,222,123},
  43.   {208,67,220,123},
  44.   {84,200,69,80},
  45.   {84,200,70,40},
  46.   {8,26,56,26},
  47.   {8,20,247,20},
  48.   {64,6,64,6},
  49.   {64,6,65,6},
  50.   {192,95,54,3},
  51.   {192,95,54,1},
  52.   {81,218,119,11},
  53.   {209,88,198,133},
  54.   {1,1,1,1},
  55.   {114,114,114,114},
  56.   {114,114,115,115},
  57.   {119,29,29,29},
  58.   {182,254,118,118},
  59.   {223,5,5,5},
  60.   {223,6,6,6},
  61.   {180,76,76,76},
  62.   {1,2,4,8},
  63.   {210,2,4,8}
  64. };

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

  67. /**
  68. * 得到一个IP
  69. * 参数:序号
  70. *      ip
  71. * 返回:序号
  72. */
  73. int getIp(int ID, int ip[4])
  74. {
  75.   int i;
  76.   i = sizeof(ipTable)/sizeof(int)/4;

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

  80.   ip[0] = ipTable[ID][0];
  81.   ip[1] = ipTable[ID][1];
  82.   ip[2] = ipTable[ID][2];
  83.   ip[3] = ipTable[ID][3];

  84.   return ID;
  85. }

  86. /**
  87. * 给wifi断电上电
  88. */
  89. void reStartWifi(void)
  90. {
  91.   //断电
  92.   digitalWrite(0, LOW);
  93.   Serial.print("断电");
  94.   
  95.   delay(2000);
  96.   
  97.   //上电
  98.   Serial.print("上电");
  99.   digitalWrite(0, HIGH);
  100. }

  101. void setup()
  102. {  
  103.   int i;
  104.   
  105.   pinMode(0,OUTPUT);
  106.   
  107.   // 状态输出
  108.   Serial.begin(115200);

  109. start:
  110.   //上电
  111.   digitalWrite(0, HIGH);
  112.         
  113.   i = 0;
  114.   do{
  115.     // 连接WIFI
  116.     stationConnected = WiFi.begin(WIFINAME,WIFIPASS);
  117.   
  118.     // 检查连接错误
  119.     if(!stationConnected)
  120.     {
  121.       Serial.print(i);
  122.       Serial.print("错误,连不上Wifi:");
  123.       Serial.print(WIFINAME);
  124.       Serial.print(" ");
  125.       Serial.println(WIFIPASS);

  126.       i++;
  127.       if(i>=5)
  128.       {
  129.         i = 0;
  130.         reStartWifi();
  131.       }
  132.       delay(10000);
  133.     }
  134.   }while(!stationConnected);
  135.   
  136.   // 等待连接完成
  137.   i = 0;
  138.   Serial.print("正在连路由器...");
  139.   while(WiFi.status() != WL_CONNECTED)
  140.   {
  141.     delay(500);
  142.     Serial.print(".");
  143.     if(i++>=120) goto start;
  144.   }
  145.   Serial.print("连上了\n");

  146.   pinger.OnReceive([](const PingerResponse& response)
  147.   {
  148.     if (response.ReceivedResponse)
  149.     {
  150.       Serial.printf(
  151.         "Reply from %s: bytes=%d time=%lums TTL=%d\n",
  152.         response.DestIPAddress.toString().c_str(),
  153.         response.EchoMessageSize - sizeof(struct icmp_echo_hdr),
  154.         response.ResponseTime,
  155.         response.TimeToLive);
  156.       pingOK  = true;
  157.     }
  158.     else
  159.     {
  160.       Serial.printf("Request timed out.\n");
  161.     }

  162.     // Return true to continue the ping sequence.
  163.     // If current event returns false, the ping sequence is interrupted.
  164.     return true;
  165.   });
  166.   
  167.   pinger.OnEnd([](const PingerResponse& response)
  168.   {
  169.     // Evaluate lost packet percentage
  170.     float loss = 100;
  171.     if(response.TotalReceivedResponses > 0)
  172.     {
  173.       loss = (response.TotalSentRequests - response.TotalReceivedResponses) * 100 / response.TotalSentRequests;
  174.     }
  175.    
  176.     // Print packet trip data
  177.     Serial.printf(
  178.       "Ping statistics for %s:\n",
  179.       response.DestIPAddress.toString().c_str());
  180.     Serial.printf(
  181.       "    Packets: Sent = %lu, Received = %lu, Lost = %lu (%.2f%% loss),\n",
  182.       response.TotalSentRequests,
  183.       response.TotalReceivedResponses,
  184.       response.TotalSentRequests - response.TotalReceivedResponses,
  185.       loss);

  186.     // Print time information
  187.     if(response.TotalReceivedResponses > 0)
  188.     {
  189.       Serial.printf("Approximate round trip times in milli-seconds:\n");
  190.       Serial.printf(
  191.         "    Minimum = %lums, Maximum = %lums, Average = %.2fms\n",
  192.         response.MinResponseTime,
  193.         response.MaxResponseTime,
  194.         response.AvgResponseTime);
  195.     }
  196.    
  197.     // Print host data
  198.     Serial.printf("Destination host data:\n");
  199.     Serial.printf(
  200.       "    IP address: %s\n",
  201.       response.DestIPAddress.toString().c_str());
  202.     if(response.DestMacAddress != nullptr)
  203.     {
  204.       Serial.printf(
  205.         "    MAC address: " MACSTR "\n",
  206.         MAC2STR(response.DestMacAddress->addr));
  207.     }
  208.     if(response.DestHostname != "")
  209.     {
  210.       Serial.printf(
  211.         "    DNS name: %s\n",
  212.         response.DestHostname.c_str());
  213.     }

  214.     return true;
  215.   });

  216.   /*
  217.   // Ping default gateway
  218.   Serial.printf(
  219.     "\n\nPinging default gateway with IP %s\n",
  220.     WiFi.gatewayIP().toString().c_str());
  221.   if(pinger.Ping(WiFi.gatewayIP()) == false)
  222.   {
  223.     Serial.println("Error during last ping command.");
  224.   }
  225.   
  226.   delay(10000);
  227.   
  228.   // Ping technologytourist.com
  229.   Serial.printf("\n\nPinging technologytourist.com\n");
  230.   if(pinger.Ping("technologytourist.com") == false)
  231.   {
  232.     Serial.println("Error during ping command.");
  233.   }

  234.   delay(10000);

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

  241.   delay(10000);

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

  250. void loop()
  251. {
  252.   static int  err=0;
  253.   static int  ipID;
  254.   int  ip[4];
  255.   
  256.   pingOK = false;

  257.   ipID = getIp(ipID,ip);
  258.   Serial.printf("\n\nPinging %d, ip='%d.%d.%d.%d'\n", ipID, ip[0], ip[1], ip[2], ip[3]);
  259.   if(pinger.Ping(IPAddress(ip[0],ip[1],ip[2],ip[3]),4,2000) == false)
  260.     {
  261.       Serial.println("ping过程中出错");
  262.     }

  263.   //每次2秒,共8次,再加上一点延时
  264.   delay(10000);
  265.   
  266.   if(pingOK == false)
  267.   {
  268.     err++;
  269.     Serial.printf("连续失败次数:%d\n",err);
  270.     if(err>30)
  271.     {
  272.       err=0;
  273.       reStartWifi();
  274.     }
  275.   }
  276.   else
  277.   {
  278.     err = 0;
  279.     //30秒以后再ping
  280.     Serial.println("等待30秒再ping");
  281.     delay(30000);
  282.   }
  283. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入12汤圆

 楼主| 发表于 2021-3-3 11:46:53 | 显示全部楼层
本帖最后由 kv2004 于 2021-3-3 11:49 编辑

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

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


  20. Pinging [url]www.baidu.com[/url]
  21. Request timed out.
  22. Request timed out.
  23. Request timed out.
  24. Request timed out.
  25. Ping statistics for 220.181.38.150:
  26.     Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
  27. Destination host data:
  28.     IP address: 220.181.38.150
  29.     DNS name: [url]www.baidu.com[/url]


  30. Pinging undefinedname
  31. Request timed out.
  32. Request timed out.
  33. Request timed out.
  34. Request timed out.
  35. Ping statistics for 255.255.255.255:
  36.     Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
  37. Destination host data:
  38.     IP address: 255.255.255.255
  39.     DNS name: undefinedname
复制代码



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

  8. Pinging default gateway with IP 172.28.135.1
  9. Request timed out.
  10. Request timed out.
  11. Request timed out.
  12. Request timed out.
  13. Ping statistics for 172.28.135.1:
  14.     Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
  15. Destination host data:
  16.     IP address: 172.28.135.1


  17. Pinging www.baidu.com
  18. Request timed out.
  19. Request timed out.
  20. Request timed out.
  21. Request timed out.
  22. Ping statistics for 255.255.255.255:
  23.     Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
  24. Destination host data:
  25.     IP address: 255.255.255.255
  26.     DNS name: www.baidu.com


  27. Pinging undefinedname
  28. Request timed out.
  29. Request timed out.
  30. Request timed out.
  31. Request timed out.
  32. Ping statistics for 255.255.255.255:
  33.     Packets: Sent = 4, Received = 0, Lost = 4 (100.00% loss),
  34. Destination host data:
  35.     IP address: 255.255.255.255
  36.     DNS name: undefinedname
复制代码



出10入23汤圆

发表于 2021-3-3 12:13:04 | 显示全部楼层
防火墙屏蔽了ICMP 报文?

出0入12汤圆

 楼主| 发表于 2021-3-3 12:36:17 | 显示全部楼层
zouzhichao 发表于 2021-3-3 12:13
防火墙屏蔽了ICMP 报文?

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

出0入12汤圆

 楼主| 发表于 2021-3-3 13:04:04 | 显示全部楼层
二楼的修改子网掩码和DNS后,还要重新WiFi.reconnect()一下,否则,什么IP都ping不通了。

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

出0入70汤圆

发表于 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); //开启继电器
      }
    }
}

出105入79汤圆

发表于 2021-3-3 13:43:00 | 显示全部楼层
可以试试连接NTP时间服务器. 获得时间就等于有网.

出0入12汤圆

 楼主| 发表于 2021-3-3 13:46:31 | 显示全部楼层
SkyGz 发表于 2021-3-3 13:10
用 这个方法试试, 我是这样用的
WiFiClient client;
HTTPClient http;

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

出0入12汤圆

 楼主| 发表于 2021-3-3 13:50:36 | 显示全部楼层
qwe2231695 发表于 2021-3-3 13:43
可以试试连接NTP时间服务器. 获得时间就等于有网.

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

出0入12汤圆

 楼主| 发表于 2021-3-3 15:16:21 | 显示全部楼层
似乎就是 路由把 ping 屏蔽了。
改成了 用url 的 客户端 测试是否网络通不通的方式,附上能用的程序。
测试中发现,www.google.com是一直失败,而 t66y 却能一直成功,但用 计算机的浏览器就是上不去,气人不。
  1. /*****************************************************************************
  2. Arduino library handling ping messages for the esp8266 platform

  3. MIT License

  4. Copyright (c) 2018 Alessio Leoncini

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

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

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

  21. #include <ESP8266WiFi.h>

  22. //#define WIFINAME "XXXXXX"
  23. #define WIFINAME "XXXXXX"
  24. #define WIFIPASS "XXXXXXXXX"

  25. char*  urlTable[]={
  26.   "www.baidu.com"
  27.   ,"www.163.com"
  28.   ,"www.sina.com.cn"
  29.   ,"www.sohu.com"
  30.   ,"www.t66y.com"
  31.   ,"www.163.net"
  32.   ,"www.360.cn"
  33.   ,"www.google.com"
  34.   ,"www.taobao.com"
  35.   ,"www.jd.com"
  36.   ,"weibo.com"
  37.   //,"www.amobbs.com" 封了IP就麻烦了
  38. };

  39. volatile bool stationConnected;//连上了wifi

  40. int getUrl(int ID, char** url)
  41. {
  42.   int i;
  43.   i=(sizeof(urlTable)/sizeof(char *));

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

  47.   *url = urlTable[ID];

  48.   return ID;
  49. }

  50. /**
  51. * 给wifi断电上电
  52. */
  53. void reStartWifi(void)
  54. {
  55.   //断电
  56.   digitalWrite(0, LOW);
  57.   Serial.print("断电");
  58.   
  59.   delay(2000);
  60.   
  61.   //上电
  62.   Serial.print("上电");
  63.   digitalWrite(0, HIGH);
  64. }

  65. void setup()
  66. {  
  67.   int i;
  68.   
  69.   pinMode(0,OUTPUT);
  70.   
  71.   // 状态输出
  72.   Serial.begin(115200);

  73. start:
  74.   //上电
  75.   digitalWrite(0, HIGH);
  76.         
  77.   i = 0;
  78.   do{
  79.     // 连接WIFI
  80.     stationConnected = WiFi.begin(WIFINAME,WIFIPASS);
  81.   
  82.     // 检查连接错误
  83.     if(!stationConnected)
  84.     {
  85.       Serial.print(i);
  86.       Serial.print("错误,连不上Wifi:");
  87.       Serial.print(WIFINAME);
  88.       Serial.print(" ");
  89.       Serial.println(WIFIPASS);

  90.       i++;
  91.       if(i>=5)
  92.       {
  93.         i = 0;
  94.         reStartWifi();
  95.       }
  96.       delay(10000);
  97.     }
  98.   }while(!stationConnected);
  99.   
  100.   // 等待连接完成
  101.   i = 0;
  102.   Serial.print("正在连路由器...");
  103.   while(WiFi.status() != WL_CONNECTED)
  104.   {
  105.     delay(500);
  106.     Serial.print(".");
  107.     if(i++>=120) goto start;
  108.   }
  109.   Serial.print("连上了\n");

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

  125.   //打印本机信息
  126.   Serial.print("当前工作模式:");     // 告知用户设备当前工作模式
  127.   Serial.println(WiFi.getMode());
  128.   Serial.print("连接到的接入点名字:");
  129.   Serial.println(WiFi.SSID());            // 告知用户连接到的接入点WiFi名
  130.   Serial.print("连接到的接入点密码:");
  131.   Serial.println(WiFi.psk());        // 告知用户连接到的接入点WiFi密码
  132.   Serial.print("当前无线终端静态IP地址: ");// 告知用户当前无线终端的IP地址(也就是我们设置的地址)
  133.   Serial.println(WiFi.localIP());
  134.   Serial.print("当前无线终端网关的IP地址: ");// 告知用户当前无线终端网关的IP地址
  135.   Serial.println(WiFi.gatewayIP());
  136.   Serial.print("当前无线终端的子网掩码: ");// 告知用户当前无线终端的子网掩码地址
  137.   Serial.println(WiFi.subnetMask());
  138.   Serial.print("DNS #1 IP为: ");//打印出获取的DNS地址
  139.   Serial.print(WiFi.dnsIP());
  140.   Serial.print(" ");
  141.   Serial.print("DNS #2 IP为: ");
  142.   Serial.print(WiFi.dnsIP(1));
  143. }

  144. bool checkNet(void)
  145. {
  146.   static int  urlID;
  147.   WiFiClient client;
  148.   char*  url;
  149.   urlID = getUrl(urlID,&url);
  150.   Serial.printf("\n\nURL=%s\n", url);
  151.   if(!client.connect(url,80))
  152.   {
  153.     Serial.println("失败");
  154.     return false;
  155.   }
  156.   else
  157.   {
  158.     client.stop();
  159.     Serial.println("成功");
  160.     return true;
  161.   }
  162. }

  163. void loop()
  164. {
  165.   static int  err=0;
  166.   
  167.   if(checkNet()==false)
  168.   {
  169.     err++;
  170.     Serial.printf("连续失败次数:%d\n",err);
  171.     if(err>30)
  172.     {
  173.       err=0;
  174.       reStartWifi();
  175.     }   
  176.   }
  177.   else
  178.   {
  179.     err = 0;
  180.     //30秒以后再ping
  181.     Serial.println("等待30秒再来");
  182.     delay(30000);
  183.   }
  184. }
复制代码


回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-26 03:17

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表