티스토리 뷰

To get current time in this region

 

 

 

#include <WiFiNINA.h>
#include <time.h>
#include <WiFiUdp.h>
#include <NTPClient.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();
}

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(){
  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();

}

// 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] Test32  (0) 2021.11.17
[Nano 33 IoT] WiFi Server Example(1)  (0) 2021.11.16
[Nano 33 IoT] WiFi Client Example(1)  (0) 2021.11.12
[Nano 33 IoT] BLE Peripheral Example(2) - ScanResp  (0) 2021.11.11
[Nano 33 IoT] BLE Central Example(1)  (0) 2021.11.09
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
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
글 보관함