mirror of
				https://github.com/0xFEEDC0DE64/arduino-esp32.git
				synced 2025-10-28 04:31:42 +01:00 
			
		
		
		
	
		
			
	
	
		
			48 lines
		
	
	
		
			938 B
		
	
	
	
		
			Arduino
		
	
	
	
	
	
		
		
			
		
	
	
			48 lines
		
	
	
		
			938 B
		
	
	
	
		
			Arduino
		
	
	
	
	
	
|   | #include <WiFi.h>
 | ||
|  | #include "time.h"
 | ||
|  | 
 | ||
|  | const char* ssid       = "YOUR_SSID"; | ||
|  | const char* password   = "YOUR_PASS"; | ||
|  | 
 | ||
|  | const char* ntpServer = "pool.ntp.org"; | ||
|  | const long  gmtOffset_sec = 3600; | ||
|  | const int   daylightOffset_sec = 3600; | ||
|  | 
 | ||
|  | void printLocalTime() | ||
|  | { | ||
|  |   struct tm timeinfo; | ||
|  |   if(!getLocalTime(&timeinfo)){ | ||
|  |     Serial.println("Failed to obtain time"); | ||
|  |     return; | ||
|  |   } | ||
|  |   Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); | ||
|  | } | ||
|  | 
 | ||
|  | void setup() | ||
|  | { | ||
|  |   Serial.begin(115200); | ||
|  |    | ||
|  |   //connect to WiFi
 | ||
|  |   Serial.printf("Connecting to %s ", ssid); | ||
|  |   WiFi.begin(ssid, password); | ||
|  |   while (WiFi.status() != WL_CONNECTED) { | ||
|  |       delay(500); | ||
|  |       Serial.print("."); | ||
|  |   } | ||
|  |   Serial.println(" CONNECTED"); | ||
|  |    | ||
|  |   //init and get the time
 | ||
|  |   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); | ||
|  |   printLocalTime(); | ||
|  | 
 | ||
|  |   //disconnect WiFi as it's no longer needed
 | ||
|  |   WiFi.disconnect(true); | ||
|  |   WiFi.mode(WIFI_OFF); | ||
|  | } | ||
|  | 
 | ||
|  | void loop() | ||
|  | { | ||
|  |   delay(1000); | ||
|  |   printLocalTime(); | ||
|  | } |