DFrobot 的 FireBeetle 的 SNTP Client 实现-Arduino中文社区 - Powered by Discuz! Archiver

Zoologist 发表于 2019-2-5 12:29

DFrobot 的 FireBeetle 的 SNTP Client 实现

来自 https://github.com/arduino-libraries/NTPClient 的库,经过试验可以在 FireBeetle(ESP32) 上使用。

/*
*This sketch sends random data over UDP on a ESP32 device
*
*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include "NTPClient.h"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

// WiFi network name and password:
const char * networkName = "ChinaNet-73";
const char * networkPswd = "adminp1988";

//Are we currently connected?
boolean connected = false;

//The udp library class
WiFiUDP udp;

void setup(){
// Initilize hardware serial:
Serial.begin(115200);

//Connect to the WiFi network
connectToWiFi(networkName, networkPswd);


}

void loop(){
//only send data when connected
if(connected){
      timeClient.update();
      Serial.println(timeClient.getFormattedTime());
}
//Wait for 1 second
delay(1000);
}

void connectToWiFi(const char * ssid, const char * pwd){
Serial.println("Connecting to WiFi network: " + String(ssid));

// delete old config
WiFi.disconnect(true);
//register event handler
WiFi.onEvent(WiFiEvent);

//Initiate connection
WiFi.begin(ssid, pwd);

Serial.println("Waiting for WIFI connection...");
}

//wifi event handler
void WiFiEvent(WiFiEvent_t event){
    switch(event) {
      case SYSTEM_EVENT_STA_GOT_IP:
          //When connected set
          Serial.print("WiFi connected! IP address: ");
          Serial.println(WiFi.localIP());
          connected = true;
          timeClient.begin();
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          Serial.println("WiFi lost connection");
          connected = false;
          break;
    }
}

测试使用的是国内的 sntp 服务器,工作正常。




lbq691477940 发表于 2019-6-23 18:00

请教楼主ntp返回来的时间还让转化为当天的总天数

lbq691477940 发表于 2019-6-23 18:13

楼主请问怎样将ntp服务器返回来的日戳时间转换为当天的总天数,不是月日分离哟,谢谢

Zoologist 发表于 2019-6-24 08:30

获得时间后,手工计算一下

例如:计算任意两个日期之间的天数【附源码】

https://blog.csdn.net/Coder_Yafeng/article/details/88533549
页: [1]
查看完整版本: DFrobot 的 FireBeetle 的 SNTP Client 实现