esp32之wifi-ipv6(转)

举报
zhangrelay 发表于 2021/10/03 22:03:23 2021/10/03
【摘要】 后续,会更新ipv6的ros2实现,转一下官方示例: #include "WiFi.h" #define STA_SSID "**********"#define STA_PASS "**********"#define AP_SSID "esp32-v6" static volatile bool wifi_connected =...

后续,会更新ipv6的ros2实现,转一下官方示例:


  
  1. #include "WiFi.h"
  2. #define STA_SSID "**********"
  3. #define STA_PASS "**********"
  4. #define AP_SSID "esp32-v6"
  5. static volatile bool wifi_connected = false;
  6. WiFiUDP ntpClient;
  7. void wifiOnConnect(){
  8. Serial.println("STA Connected");
  9. Serial.print("STA IPv4: ");
  10. Serial.println(WiFi.localIP());
  11. ntpClient.begin(2390);
  12. }
  13. void wifiOnDisconnect(){
  14. Serial.println("STA Disconnected");
  15. delay(1000);
  16. WiFi.begin(STA_SSID, STA_PASS);
  17. }
  18. void wifiConnectedLoop(){
  19. //lets check the time
  20. const int NTP_PACKET_SIZE = 48;
  21. byte ntpPacketBuffer[NTP_PACKET_SIZE];
  22. IPAddress address;
  23. WiFi.hostByName("time.nist.gov", address);
  24. memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
  25. ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
  26. ntpPacketBuffer[1] = 0; // Stratum, or type of clock
  27. ntpPacketBuffer[2] = 6; // Polling Interval
  28. ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision
  29. // 8 bytes of zero for Root Delay & Root Dispersion
  30. ntpPacketBuffer[12] = 49;
  31. ntpPacketBuffer[13] = 0x4E;
  32. ntpPacketBuffer[14] = 49;
  33. ntpPacketBuffer[15] = 52;
  34. ntpClient.beginPacket(address, 123); //NTP requests are to port 123
  35. ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE);
  36. ntpClient.endPacket();
  37. delay(1000);
  38. int packetLength = ntpClient.parsePacket();
  39. if (packetLength){
  40. if(packetLength >= NTP_PACKET_SIZE){
  41. ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE);
  42. }
  43. ntpClient.flush();
  44. uint32_t secsSince1900 = (uint32_t)ntpPacketBuffer[40] << 24 | (uint32_t)ntpPacketBuffer[41] << 16 | (uint32_t)ntpPacketBuffer[42] << 8 | ntpPacketBuffer[43];
  45. //Serial.printf("Seconds since Jan 1 1900: %u\n", secsSince1900);
  46. uint32_t epoch = secsSince1900 - 2208988800UL;
  47. //Serial.printf("EPOCH: %u\n", epoch);
  48. uint8_t h = (epoch % 86400L) / 3600;
  49. uint8_t m = (epoch % 3600) / 60;
  50. uint8_t s = (epoch % 60);
  51. Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s);
  52. }
  53. delay(9000);
  54. }
  55. void WiFiEvent(WiFiEvent_t event){
  56. switch(event) {
  57. case ARDUINO_EVENT_WIFI_AP_START:
  58. //can set ap hostname here
  59. WiFi.softAPsetHostname(AP_SSID);
  60. //enable ap ipv6 here
  61. WiFi.softAPenableIpV6();
  62. break;
  63. case ARDUINO_EVENT_WIFI_STA_START:
  64. //set sta hostname here
  65. WiFi.setHostname(AP_SSID);
  66. break;
  67. case ARDUINO_EVENT_WIFI_STA_CONNECTED:
  68. //enable sta ipv6 here
  69. WiFi.enableIpV6();
  70. break;
  71. case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
  72. Serial.print("STA IPv6: ");
  73. Serial.println(WiFi.localIPv6());
  74. break;
  75. case ARDUINO_EVENT_WIFI_AP_GOT_IP6:
  76. Serial.print("AP IPv6: ");
  77. Serial.println(WiFi.softAPIPv6());
  78. break;
  79. case ARDUINO_EVENT_WIFI_STA_GOT_IP:
  80. wifiOnConnect();
  81. wifi_connected = true;
  82. break;
  83. case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
  84. wifi_connected = false;
  85. wifiOnDisconnect();
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. void setup(){
  92. Serial.begin(115200);
  93. WiFi.disconnect(true);
  94. WiFi.onEvent(WiFiEvent);
  95. WiFi.mode(WIFI_MODE_APSTA);
  96. WiFi.softAP(AP_SSID);
  97. WiFi.begin(STA_SSID, STA_PASS);
  98. }
  99. void loop(){
  100. if(wifi_connected){
  101. wifiConnectedLoop();
  102. }
  103. while(Serial.available()) Serial.write(Serial.read());
  104. }

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/120589661

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。