Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <WiFi.h>
- #include "time.h"
- // Replace with your network credentials
- // Set your Static IP address
- IPAddress local_IP(10, 0, 0, 110);
- // Set your Gateway IP address
- IPAddress gateway(10, 0, 0, 138);
- IPAddress subnet(255, 255, 255, 0);
- const char* ssid = "HOME-FRI";
- const char* password = "willy4711";
- const char* ntpServer = "pool.ntp.org";
- const long gmtOffset_sec = 3600;
- const int daylightOffset_sec = 3600;
- time_t now;
- 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");
- time(&now);
- }
- // Initialize WiFi
- void initWiFi() {
- WiFi.mode(WIFI_STA);
- WiFi.config(local_IP, gateway, subnet);
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WiFi ..");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(1000);
- }
- Serial.println(WiFi.localIP());
- }
- void setup()
- {
- Serial.begin(115200);
- //connect to WiFi
- initWiFi();
- //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();
- Serial.println(now);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement