티스토리 뷰
#include <WiFiNINA.h>
#include <time.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <EasyNTPClient.h>
#include "BConstants.h"
#define PIN_Tx0 0
#define PIN_Rx0 1
#define PIN_UARTCONTROL 2
#define PIN_LED 2
#define PIN_IRRx0 3
#define PIN_IRTx0 4
#define PIN_SWITCHIN 5
#define PIN_RELAYCONTROL 6
#define PIN_DRIVER1 7
#define PIN_DRIVER2 8
#define PIN_DRIVER3 9
#define PIN_DRIVER5 10
#define PIN_Rx1 11
#define PIN_Tx1 12
#define PIN_ADC0 A0
#define PIN_ADC1 A1
#define PIN_ADC2 A2
#define NTP_PACKET_SIZE 48
const char WiFi_SSID[] = SECRET_SSID;
const char WiFi_PW[] = SECRET_WIFIPW;
const int TimeZone = 3;
int status = WL_IDLE_STATUS;
char server[] = "www.google.com";
byte NTPPacketBuffer[NTP_PACKET_SIZE];
int Count1 = 0;
WiFiClient client;
void setup(){
Serial.begin(9600);
while(!Serial);
if(WiFi.status() == WL_NO_MODULE){
Serial.println("[?] Communication with WiFi module failed");
while(true);
}
String tv = WiFi.firmwareVersion();
if(tv < WIFI_FIRMWARE_LATEST_VERSION){
Serial.println("Please upgrade the firmmware");
}
while(status != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(WiFi_SSID);
status = WiFi.begin(WiFi_SSID, WiFi_PW);
delay(1000);
}
Serial.println("Connected to WiFi");
printWiFiStatus();
Serial.println("\nStarting connection to server ...");
if(client.connect(server, 80)){
Serial.println("connected to server ");
client.println("GET /search?q=nano33 HTTP/1.1");
client.print("Host: ");
client.print(server);
client.println("Connection: close");
client.println();
}
//CheckCurrentTimefromNIST();
CheckCurrentTimefromNTP();
Serial.println("\n================================");
}
void loop(){
while(client.available()){
char c = client.read();
Serial.write(c);
}
if(!client.connected()){
Serial.println();
Serial.println("Disconnected from server.");
client.stop();
while(true);
}
}
void printWiFiStatus(){
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI): ");
Serial.print(rssi);
Serial.println(" dBm");
}
void CheckCurrentTimefromNIST(){
// Ref. : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=msyang59&logNo=220157017996
// Ref. : https://postpop.tistory.com/39
Serial.println("Checking Current Time ..... ");
//IPAddress timeServer(129, 6, 15, 28); //time.nist.gov
IPAddress timeServer(132, 163, 96, 1); // time-a-b.nist.gov NTP server
WiFiUDP wiFiUDP;
unsigned int localPort = 2390;
wiFiUDP.begin(localPort);
sendNTPpacket(wiFiUDP, timeServer);
delay(1000);
if(wiFiUDP.parsePacket()){
Serial.println("[!] NTP Packet received");
wiFiUDP.read(NTPPacketBuffer, NTP_PACKET_SIZE);
unsigned long highWord = word(NTPPacketBuffer[40], NTPPacketBuffer[41]);
unsigned long lowWord = word(NTPPacketBuffer[42], NTPPacketBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = ");
Serial.println(secsSince1900);
Serial.print("[Unix time] = ");
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;
Serial.println(epoch);
Serial.print("The UTC time is ");
Serial.print((epoch%86400L)/3600);
Serial.print(':');
if(((epoch%3600)/60) < 10){
Serial.print('0');
}
Serial.print((epoch%3600)/60);
Serial.print(':');
if((epoch%60) < 10){
Serial.print('0');
}
Serial.println(epoch%60);
}
Serial.println("-------------------------------");
wiFiUDP.stop();
}
void CheckCurrentTimefromNTP2(){
WiFiUDP wiFiUDP;
EasyNTPClient timeClient(wiFiUDP, "pool.ntp.org", 32400);
String unixTime = String(timeClient.getUnixTime());
Serial.print("[Unix Time] ");
Serial.println(unixTime); // --> [Unix Time] 1636025946
}
void CheckCurrentTimefromNTP(){
// Ref. : https://blog.naver.com/PostView.nhn?blogId=appti&logNo=221499699209&from=search&redirect=Log&widgetTypeCall=true&directAccess=false
// Ref. : https://m.blog.naver.com/myunyui/222082845393
WiFiUDP wiFiUDP;
NTPClient timeClient(wiFiUDP, "pool.ntp.org", 32400);
timeClient.begin();
delay(10);
timeClient.update();
time_t epochTime = timeClient.getEpochTime();
String formattedTime = timeClient.getFormattedTime();
Serial.print("[Formatted Time] ");
Serial.println(formattedTime);
//Serial.print("[EpochTime] ");
//Serial.println(epochTime);
// struct tm *ptm = gmtime((time_t *)&epochTime);
struct tm *ptm = localtime((time_t *)&epochTime);
long currentYear = ptm->tm_year+1900;
int currentMonth = ptm->tm_mon+1;
int monthDay = ptm->tm_mday;
int currentHour = ptm->tm_hour;
int currentMins = ptm->tm_min;
int currentSecs = ptm->tm_sec;
// int currentHour = timeClient.getHours();
// int currentMins = timeClient.getMinutes();
// int currentSecs = timeClient.getSeconds();
Serial.print("[Current DataTime] ");
Serial.print(String(currentYear));
Serial.print('-');
if(currentMonth < 10) Serial.print('0');
Serial.print(currentMonth);
Serial.print('-');
if(monthDay < 10) Serial.print('0');
Serial.print(monthDay);
Serial.print(' ');
if(currentHour<10) Serial.print('0');
Serial.print(currentHour);
Serial.print(':');
if(currentMins<10) Serial.print('0');
Serial.print(currentMins);
Serial.print(':');
if(currentSecs<10) Serial.print('0');
Serial.println(currentSecs);
timeClient.end();
}
void CheckCurrentTimefromGoogle(){
// Ref. : http://zerowin-coding.blogspot.com/2018/05/esp8266-esp-12e-uart-wifi-shield.html
}
/*
void CheckCurrentTimefromNTP2(){
Serial.println("Checking Current Time ..... ");
configTime(3*3600, 0, "pool.ntp.org");
//configTime(3*3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.println("\nWaiting for time ...");
while(!time(nullptr)){
Serial.print(".");
delay(300);
}
time_t now = time(nullptr);
struct tm *timeInfo;
timeInfo = localtime(&now);
Serial.print("[Current Time] ");
Serial.print(timeInfo->tm_year);
Serial.print('-');
Serial.print(timeInfo->tm_mon);
Serial.print('-');
Serial.print(timeInfo->tm_mday);
Serial.print(' ');
Serial.print(timeInfo->tm_hour);
Serial.print(':');
Serial.print(timeInfo->tm_min);
Serial.print(':');
Serial.print(timeInfo->tm_sec);
}
*/
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(WiFiUDP wiFiUDP, IPAddress& address) {
// set all bytes in the buffer to 0
memset(NTPPacketBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
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;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
wiFiUDP.beginPacket(address, 123); //NTP requests are to port 123
wiFiUDP.write(NTPPacketBuffer, NTP_PACKET_SIZE);
wiFiUDP.endPacket();
}
반응형
'SWDesk > Firmware' 카테고리의 다른 글
[Nano 33 IoT] Test33 (0) | 2021.11.17 |
---|---|
[Nano 33 IoT] Test32 (0) | 2021.11.17 |
[Nano 33 IoT] WiFi Client Example(2) (0) | 2021.11.13 |
[Nano 33 IoT] WiFi Client Example(1) (0) | 2021.11.12 |
[Nano 33 IoT] BLE Peripheral Example(2) - ScanResp (0) | 2021.11.11 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- badp
- 심심풀이치매방지기
- 허들
- 치매
- 둎
- 혁신과허들
- Innovation&Hurdles
- arduino
- 아두이노
- Decorator
- 전류
- 티스토리챌린지
- 혁신
- 오블완
- 치매방지
- Video
- 심심풀이
- Innovations
- ServantClock
- DYOV
- Innovations&Hurdles
- bilient
- 절연형
- 빌리언트
- image
- 전압
- 전압전류모니터링
- 배프
- Hurdles
- BSC
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함