|
| 1 | + |
| 2 | +#include <SSD1306.h> |
| 3 | + |
| 4 | +// simplestesp8266clock.ino |
| 5 | +// |
| 6 | +// Libraries needed: |
| 7 | +// Time.h & TimeLib.h: https://github.com/PaulStoffregen/Time |
| 8 | +// Timezone.h: https://github.com/JChristensen/Timezone |
| 9 | +// SSD1306.h & SSD1306Wire.h: https://github.com/squix78/esp8266-oled-ssd1306 |
| 10 | +// NTPClient.h: https://github.com/arduino-libraries/NTPClient |
| 11 | +// ESP8266WiFi.h & WifiUDP.h: https://github.com/ekstrand/ESP8266wifi |
| 12 | +// |
| 13 | +// 128x64 OLED pinout: |
| 14 | +// GND goes to ground |
| 15 | +// Vin goes to 3.3V |
| 16 | +// Data to I2C SDA (GPIO 0) |
| 17 | +// Clk to I2C SCL (GPIO 2) |
| 18 | + |
| 19 | +#include <ESP8266WiFi.h> |
| 20 | + |
| 21 | +#include <WifiUDP.h> |
| 22 | +#include <String.h> |
| 23 | +#include <Wire.h> |
| 24 | +#include <NTPClient.h> |
| 25 | +#include <Time.h> |
| 26 | +#include <TimeLib.h> |
| 27 | +#include <Timezone.h> |
| 28 | + |
| 29 | +// Define NTP properties |
| 30 | +#define NTP_OFFSET 60 * 60 // In seconds |
| 31 | +#define NTP_INTERVAL 60 * 1000 // In miliseconds |
| 32 | +#define NTP_ADDRESS "in.pool.ntp.org" // change this to whatever pool is closest (see ntp.org) |
| 33 | + |
| 34 | +// Set up the NTP UDP client |
| 35 | +WiFiUDP ntpUDP; |
| 36 | +NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL); |
| 37 | + |
| 38 | +// Create a display object |
| 39 | +SSD1306 display(0x3c, 5, 4); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED |
| 40 | + |
| 41 | +const char* ssid = ""; // insert your own ssid |
| 42 | +const char* password = ""; // and password |
| 43 | +String date; |
| 44 | +String t; |
| 45 | +const char * days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} ; |
| 46 | +const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"} ; |
| 47 | +const char * ampm[] = {"AM", "PM"} ; |
| 48 | + |
| 49 | +void setup () |
| 50 | +{ |
| 51 | + Serial.begin(115200); // most ESP-01's use 115200 but this could vary |
| 52 | + timeClient.begin(); // Start the NTP UDP client |
| 53 | + |
| 54 | + Wire.pins(5, 4); // Start the OLED with GPIO 0 and 2 on ESP-01 |
| 55 | + Wire.begin(5, 4); // 0=sda, 2=scl |
| 56 | + display.init(); |
| 57 | + display.flipScreenVertically(); |
| 58 | + |
| 59 | + // Connect to wifi |
| 60 | + Serial.println(""); |
| 61 | + Serial.print("Connecting to "); |
| 62 | + Serial.print(ssid); |
| 63 | + display.drawString(0, 10, "Connecting to Wifi..."); |
| 64 | + display.display(); |
| 65 | + WiFi.begin(ssid, password); |
| 66 | + while (WiFi.status() != WL_CONNECTED) |
| 67 | + { |
| 68 | + delay(500); |
| 69 | + Serial.print("."); |
| 70 | + } |
| 71 | + Serial.println(""); |
| 72 | + Serial.print("Connected to WiFi at "); |
| 73 | + Serial.print(WiFi.localIP()); |
| 74 | + Serial.println(""); |
| 75 | + display.drawString(0, 24, "Connected."); |
| 76 | + display.display(); |
| 77 | + delay(1000); |
| 78 | +} |
| 79 | + |
| 80 | +void loop() |
| 81 | +{ |
| 82 | + if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status |
| 83 | + { |
| 84 | + date = ""; // clear the variables |
| 85 | + t = ""; |
| 86 | + |
| 87 | + // update the NTP client and get the UNIX UTC timestamp |
| 88 | + timeClient.update(); |
| 89 | + unsigned long epochTime = timeClient.getEpochTime(); |
| 90 | + |
| 91 | + // convert received time stamp to time_t object |
| 92 | + time_t local, utc; |
| 93 | + utc = epochTime; |
| 94 | + |
| 95 | + // Then convert the UTC UNIX timestamp to local time |
| 96 | + |
| 97 | + TimeChangeRule IST = {"IST", First, Mon, Jan, 0, 270}; //UTC - 6 hours - change this as needed |
| 98 | + Timezone inST(IST, IST); |
| 99 | + local = inST.toLocal(utc); |
| 100 | + |
| 101 | + // now format the Time variables into strings with proper names for month, day etc |
| 102 | + date += days[weekday(local)-1]; |
| 103 | + date += ", "; |
| 104 | + date += months[month(local)-1]; |
| 105 | + date += " "; |
| 106 | + date += day(local); |
| 107 | + date += ", "; |
| 108 | + date += year(local); |
| 109 | + |
| 110 | + // format the time to 12-hour format with AM/PM and no seconds |
| 111 | + t += hourFormat12(local); |
| 112 | + t += ":"; |
| 113 | + if(minute(local) < 10) // add a zero if minute is under 10 |
| 114 | + t += "0"; |
| 115 | + t += minute(local); |
| 116 | + t += " "; |
| 117 | + t += ampm[isPM(local)]; |
| 118 | + |
| 119 | + // Display the date and time |
| 120 | + Serial.println(""); |
| 121 | + Serial.print("Local date: "); |
| 122 | + Serial.print(date); |
| 123 | + Serial.println(""); |
| 124 | + Serial.print("Local time: "); |
| 125 | + Serial.print(t); |
| 126 | + |
| 127 | + // print the date and time on the OLED |
| 128 | + display.clear(); |
| 129 | + display.setTextAlignment(TEXT_ALIGN_CENTER); |
| 130 | + display.setFont(ArialMT_Plain_24); |
| 131 | + display.drawStringMaxWidth(64, 10, 128, t); |
| 132 | + display.setFont(ArialMT_Plain_10); |
| 133 | + display.drawStringMaxWidth(64, 38, 128, date); |
| 134 | + display.display(); |
| 135 | + } |
| 136 | + else // attempt to connect to wifi again if disconnected |
| 137 | + { |
| 138 | + display.clear(); |
| 139 | + display.drawString(0, 10, "Connecting to Wifi..."); |
| 140 | + display.display(); |
| 141 | + WiFi.begin(ssid, password); |
| 142 | + display.drawString(0, 24, "Connected."); |
| 143 | + display.display(); |
| 144 | + delay(1000); |
| 145 | + } |
| 146 | + |
| 147 | + delay(10000); //Send a request to update every 10 sec (= 10,000 ms) |
| 148 | +} |
0 commit comments