Skip to content

Instantly share code, notes, and snippets.

@jnewc
Created April 14, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnewc/b74758d46116f6e293564dfcc5bca144 to your computer and use it in GitHub Desktop.
Save jnewc/b74758d46116f6e293564dfcc5bca144 to your computer and use it in GitHub Desktop.
LEDTime.ino
#include <Time.h>
#include <TimeLib.h>
#include <NeoPixelBus.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
int dividerLEDs[] = { 4, 11 };
int dividerLEDsCount = 2;
int hoursLEDs[] = { 0, 1, 2, 3 };
int hoursLEDsCount = 4;
int minutesLEDs[] = { 5, 6, 7, 8, 9, 10 };
int minutesLEDsCount = 6;
int secondsLEDs[] = { 12, 13, 14, 15, 16, 17 };
int secondsLEDsCount = 6;
int totalLEDs = 18;
unsigned int localPort = 8888; // local port to listen for UDP packets
WiFiUDP Udp;
NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod> strip(totalLEDs, D7);
RgbwColor dividerColor(255, 0, 0, 0);
RgbwColor bgColor(250, 250, 250, 0);
RgbwColor timeColor(0, 0, 255, 0);
void setup() {
Serial.begin(115200);
delay(100);
setupWifi();
setupNtp();
strip.Begin();
}
void loop() {
// put your main code here, to run repeatedly:
setDividerLEDs();
time_t t = now();
//Serial.println((String)hour() + ":" + (String)minute() + ":" + (String)second());
setBinaryState(hoursLEDs, hoursLEDsCount, (hour() % 12) + 1);
setBinaryState(minutesLEDs, minutesLEDsCount, minute());
setBinaryState(secondsLEDs, secondsLEDsCount, second());
strip.Show();
delay(1000);
}
void setDividerLEDs() {
for (int i = 0; i < totalLEDs; i++) {
strip.SetPixelColor(i, bgColor);
}
for (int i = 0; i < dividerLEDsCount; i++) {
strip.SetPixelColor(dividerLEDs[i], dividerColor);
}
}
void setBinaryState(int leds[], int ledCount, int number) {
for (int i = 0; i < ledCount; i++) {
int complement = (int)pow(2, i);
int result = number & complement;
if (result > 0) {
strip.SetPixelColor(leds[i], timeColor);
}
}
}
const char* ssid = "PUT_YOUR_SSID_HERE";
const char* password = "PUT_YOUR_WIFI_PASSWORD_HERE";
void setupWifi() {
WiFi.mode(WIFI_STA);
WiFi.hostname("Office_LEDs");
WiFi.begin(ssid, password);
Serial.print("Awaiting wifi ");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.print(ssid);
Serial.print(" with IP address: ");
Serial.println(WiFi.localIP());
}
void setupNtp() {
Udp.begin(localPort);
Serial.println(Udp.localPort());
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
setSyncInterval(300);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment