Skip to content

Instantly share code, notes, and snippets.

@MontienNgamkaew
Last active January 15, 2021 04:36
Show Gist options
  • Save MontienNgamkaew/fc916c05701ad452e2b026a1eb418f5f to your computer and use it in GitHub Desktop.
Save MontienNgamkaew/fc916c05701ad452e2b026a1eb418f5f to your computer and use it in GitHub Desktop.
ESP8266 ดึงเวลาจาก Internet มาใช้งาน
#include <ESP8266WiFi.h>
#include <time.h>
const char* ssid = "ssid"; //ใส่ชื่อ SSID Wifi
const char* password = "password"; //ใส่รหัสผ่าน
int timezone = 7 * 3600; //ตั้งค่า TimeZone ตามเวลาประเทศไทย
int dst = 0; //กำหนดค่า Date Swing Time
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(true);
WiFi.mode(WIFI_STA); //เชื่อมต่อ Wifi
WiFi.begin(ssid, password);
Serial.println("\nConnecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(",");
delay(1000);
}
configTime(timezone, dst, "pool.ntp.org", "time.nist.gov"); //ดึงเวลาจาก Server
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
Serial.println("");
}
void loop()
{
configTime(timezone, dst, "pool.ntp.org", "time.nist.gov"); //ดีงเวลาปัจจุบันจาก Server อีกครั้ง
time_t now = time(nullptr);
struct tm* p_tm = localtime(&now);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment