使用Arduino开发ESP32(二十六):WiFiClientEvents例程及从互联网上实时获取当前时间功能

前言

注:
官方的例程里偷偷列举了许多拓展用法的示例,差点错过

在看例程之前需要了解的一些函数

WiFi.hostByName

/**
 * Resolve the given hostname to an IP address.  
 * @param aHostname     Name to be resolved
 * @param aResult       IPAddress structure to store the returned IP address
 * @return 1 if aIPAddrString was successfully converted to an IP address,
 *          else error code
 */

功能:将给定的主机名解析为IP地址。
参数1 aHostname:需要被解析的主机名
参数2 aResult:一个IP类型的结构体以存放返回的IP值
返回值:成功则1,否则错误代码

memset
参考文章:C 库函数 - memset()

parsePacket()

int WiFiUDP::parsePacket(){
  if(rx_buffer)
    return 0;
  struct sockaddr_in si_other;
  int slen = sizeof(si_other) , len;
  char * buf = new char[1460];
  if(!buf){
    return 0;
  }
  if ((len = recvfrom(udp_server, buf, 1460, MSG_DONTWAIT, (struct sockaddr *) &si_other, (socklen_t *)&slen)) == -1){
    delete[] buf;
    if(errno == EWOULDBLOCK){
      return 0;
    }
    log_e("could not receive data: %d", errno);
    return 0;
  }
  remote_ip = IPAddress(si_other.sin_addr.s_addr);
  remote_port = ntohs(si_other.sin_port);
  if (len > 0) {
    rx_buffer = new cbuf(len);
    rx_buffer->write(buf, len);
  }
  delete[] buf;
  return len;
}

WiFiUDP.read(){

int WiFiUDP::read(){
  if(!rx_buffer) return -1;
  int out = rx_buffer->read();
  if(!rx_buffer->available()){
    cbuf *b = rx_buffer;
    rx_buffer = 0;
    delete b;
  }
  return out;
}

正文

代码:

#include "WiFi.h"

#define STA_SSID "Wifi名称"
#define STA_PASS "Wifi密码"
#define AP_SSID  "esp32-IPv6"

static volatile bool wifi_connected = false;

WiFiUDP ntpClient;

void setup(){
    Serial.begin(115200);
    WiFi.disconnect(true);
    WiFi.onEvent(WiFiEvent);    //设置wifi event回调函数
    WiFi.mode(WIFI_MODE_APSTA);           //调整模式为AP、STA并行工作
    WiFi.softAP(AP_SSID);                       //开启AP模式
    WiFi.begin(STA_SSID, STA_PASS);                   //连接wifi
}

void loop(){
    if(wifi_connected){
        wifiConnectedLoop();
    }
    while(Serial.available()) Serial.write(Serial.read());
}

void wifiOnConnect(){
    Serial.println("STA Connected");
    Serial.print("STA IPv4: ");
    Serial.println(WiFi.localIP());
    
    ntpClient.begin(2390);
}

void wifiOnDisconnect(){
    Serial.println("STA Disconnected");
    delay(1000);
    WiFi.begin(STA_SSID, STA_PASS);
}

void wifiConnectedLoop(){
  //lets check the time 查看时间
  const int NTP_PACKET_SIZE = 48;
  byte ntpPacketBuffer[NTP_PACKET_SIZE];

  IPAddress address;
  WiFi.hostByName("time.nist.gov", address);    

  //这一串代码,个人觉得是在写UDP通信协议报文
  memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);  //复制NTP_PACKET_SIZE到ntpPacketBuffer的第0位
  ntpPacketBuffer[0] = 0b11100011;   // LI, Version, Mode
  ntpPacketBuffer[1] = 0;     // Stratum, or type of clock
  ntpPacketBuffer[2] = 6;     // Polling Interval
  ntpPacketBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion                                   
  ntpPacketBuffer[12]  = 49;
  ntpPacketBuffer[13]  = 0x4E;
  ntpPacketBuffer[14]  = 49;
  ntpPacketBuffer[15]  = 52;
  
  ntpClient.beginPacket(address, 123); //NTP 请求 通过端口123发送
  ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE);//发送报文,参数1:报文内容,参数2:报文大小
  ntpClient.endPacket();   //结束发送报文

  delay(1000);
  
  int packetLength = ntpClient.parsePacket();           //接收缓冲区包长度
  if (packetLength){
    if(packetLength >= NTP_PACKET_SIZE){
      ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE);             //如果大于NTP_PACKET_SIZE,则读取NTP_PACKET_SIZE长度地数据至ntpPacketBuffer
    }
    ntpClient.flush();                         //清空缓冲区

    //这一部分为从接收到的数据中筛选出想要的数据(时间),并转换成特定格式
    uint32_t secsSince1900 = (uint32_t)ntpPacketBuffer[40] << 24 | (uint32_t)ntpPacketBuffer[41] << 16 | (uint32_t)ntpPacketBuffer[42] << 8 | ntpPacketBuffer[43];  
    //Serial.printf("Seconds since Jan 1 1900: %u\n", secsSince1900);
    uint32_t epoch = secsSince1900 - 2208988800UL;
    //Serial.printf("EPOCH: %u\n", epoch);
    uint8_t h = (epoch  % 86400L) / 3600;
    uint8_t m = (epoch  % 3600) / 60;
    uint8_t s = (epoch % 60);
    Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s);
  }

  delay(9000);
}

void WiFiEvent(WiFiEvent_t event){
    switch(event) {

        case SYSTEM_EVENT_AP_START:
            //can set ap hostname here
            WiFi.softAPsetHostname(AP_SSID);
            //enable ap ipv6 here
            WiFi.softAPenableIpV6();
            break;

        case SYSTEM_EVENT_STA_START:
            //set sta hostname here
            WiFi.setHostname(AP_SSID);
            break;
        case SYSTEM_EVENT_STA_CONNECTED:
            //enable sta ipv6 here
            WiFi.enableIpV6();
            break;
        case SYSTEM_EVENT_AP_STA_GOT_IP6:
            //both interfaces get the same event
            Serial.print("STA IPv6: ");
            Serial.println(WiFi.localIPv6());
            Serial.print("AP IPv6: ");
            Serial.println(WiFi.softAPIPv6());
            break;
        case SYSTEM_EVENT_STA_GOT_IP:
            wifiOnConnect();
            wifi_connected = true;
            break;
        case SYSTEM_EVENT_STA_DISCONNECTED:
            wifi_connected = false;
            wifiOnDisconnect();
            break;
        default:
            break;
    }
}


结果:
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值