Last active
May 12, 2020 05:57
-
-
Save IdrisCytron/51625683d3007c5e069babc6f1568360 to your computer and use it in GitHub Desktop.
WS2812 Ring LED clock with NTP server using ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Project: WS2812 Ring LED clock with NTP server using ESP32 | |
Board: ESP32 Dev Module (Node32 Lite) | |
Connections: | |
ESP32 | OLED Strip | |
RAW - VCC | |
GND - GND | |
25 - DIN | |
External libraries: | |
- NeoPixelBus by Micheal C. Miller V2.5.7 (Manager) | |
- NTPClient by Fabrice Weinberg V3.1.0 (Zip) | |
https://github.com/taranais/NTPClient/archive/master.zip | |
*/ | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
const char ssid[] = "Your WiFi SSID"; // WiFi name | |
const char password[] = "Your WiFi Password"; // WiFi password | |
#include <NTPClient.h> | |
#include <WiFiUdp.h> | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP); | |
#include <NeoPixelBrightnessBus.h> | |
#define PIN 25 | |
#define NUMPIXELS 60 | |
NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod> strip(NUMPIXELS, PIN); | |
long currentMillis = 0; | |
long previousMillis = 0; | |
int interval = 1000; | |
String formattedDate; | |
int hour, minute, second; | |
int hour1, hour2, hour3; | |
void setup() | |
{ | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
strip.Begin(); | |
strip.SetBrightness(5); | |
clockSegment(); | |
strip.Show(); | |
timeClient.begin(); | |
timeClient.setTimeOffset(28800); // Set offset time in seconds, GMT+8 = 28800 | |
} | |
void loop() | |
{ | |
while (!timeClient.update()) { | |
timeClient.forceUpdate(); | |
} | |
currentMillis = millis(); | |
if (currentMillis - previousMillis > interval) { | |
previousMillis = millis(); | |
// Clear all leds | |
strip.SetPixelColor(hour, RgbColor(0, 0, 0)); | |
strip.SetPixelColor(hour+1, RgbColor(0, 0, 0)); | |
strip.SetPixelColor(hour+2, RgbColor(0, 0, 0)); | |
strip.SetPixelColor(hour+3, RgbColor(0, 0, 0)); | |
strip.SetPixelColor(hour+4, RgbColor(0, 0, 0)); | |
strip.SetPixelColor(minute, RgbColor(0, 0, 0)); | |
strip.SetPixelColor(second, RgbColor(0, 0, 0)); | |
strip.Show(); | |
// The formattedDate comes with the following format: | |
// 2018-05-28T16:00:13Z | |
formattedDate = timeClient.getFormattedDate(); | |
Serial.println(formattedDate); | |
// Extract time | |
hour = formattedDate.substring(11, 13).toInt(); | |
minute = formattedDate.substring(14, 16).toInt(); | |
second = formattedDate.substring(17, 19).toInt(); | |
if (hour > 11) { | |
hour = hour - 12; | |
} | |
hour = (hour * 5) + (minute / 12); | |
hour1 = hour; | |
if (hour == 59) { | |
hour2 = hour - 1; | |
hour3 = 0; | |
} | |
else if (hour == 0) { | |
hour2 = 59; | |
hour3 = hour + 1; | |
} | |
else { | |
hour2 = hour - 1; | |
hour3 = hour + 1; | |
} | |
clockSegment(); | |
strip.SetPixelColor(second, RgbColor(0, 0, 255)); | |
strip.SetPixelColor(hour1, RgbColor(255, 0, 0)); | |
strip.SetPixelColor(hour2, RgbColor(255, 0, 0)); | |
strip.SetPixelColor(hour3, RgbColor(255, 0, 0)); | |
strip.SetPixelColor(minute, RgbColor(0, 255, 0)); | |
strip.Show(); | |
} | |
} | |
RgbColor DimWhite(50, 50, 50); | |
void clockSegment() | |
{ | |
strip.SetPixelColor(0, DimWhite); | |
strip.SetPixelColor(5, DimWhite); | |
strip.SetPixelColor(10, DimWhite); | |
strip.SetPixelColor(15, DimWhite); | |
strip.SetPixelColor(20, DimWhite); | |
strip.SetPixelColor(25, DimWhite); | |
strip.SetPixelColor(30, DimWhite); | |
strip.SetPixelColor(35, DimWhite); | |
strip.SetPixelColor(40, DimWhite); | |
strip.SetPixelColor(45, DimWhite); | |
strip.SetPixelColor(50, DimWhite); | |
strip.SetPixelColor(55, DimWhite); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment