You are on page 1of 11

#include <ESP8266WiFi.

h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

#include <NTPClient.h>
#include <WiFiUdp.h>

#include <FastLED.h>

#define LED_PIN 5
#define NUM_LEDS 120
#define BRIGHTNESS 255
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

// Set web server port number to 80


ESP8266WebServer server(80);

long utcOffsetInSeconds = 7200;

// Define NTP Client to get time


WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org",
utcOffsetInSeconds);

// fast simulation
bool isSimOn = false;
bool isMarksOn = true;
bool isFadeOn = false;
int period = 100;

void setup() {
Serial.begin(115200);

// power-up safety delay


delay(3000);

// FastLED init
FastLED.addLeds<LED_TYPE, LED_PIN,
COLOR_ORDER>(leds,
NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
fill_solid(leds, NUM_LEDS, CHSV(255, 255, 0));

// WiFiManager
// Local initialization. Once its business is
done, there is no need to keep it around
WiFiManager wifiManager;

// Uncomment and run it once, if you want to


erase all the stored information
//wifiManager.resetSettings();

wifiManager.autoConnect("AutoConnectAP");
Serial.println("Connected.");

timeClient.begin();
server.on("/", handle_main);
server.on("/sim", handle_sim);
server.on("/time", handle_time);
server.on("/marks", handle_marks);
server.on("/fade", handle_fade);
server.begin();
Serial.println("HTTP server started");

timeClient.update();
Serial.println("NTP server updated");
// power-up safety delay
delay(2000);
}

void loop() {
server.handleClient();
handleClock();
FastLED.show();
if (isSimOn) simulateFast();
}

void simulateFast() {
EVERY_N_MILLISECONDS(period) {
utcOffsetInSeconds += 60;
timeClient.setTimeOffset(utcOffsetInSeconds);
Serial.println(timeClient.getFormattedTime());
}
}

void handleClock() {
timeClient.update();

if (isFadeOn) {
fadeToBlackBy(leds, NUM_LEDS, 1);
} else {
FastLED.clear();
}

if (isMarksOn) {
leds[29].setRGB(25, 25, 25);
leds[90].setRGB(25, 25, 25); //0
leds[44].setRGB(25, 25, 25);
leds[75].setRGB(25, 25, 25); //15
leds[59].setRGB(25, 25, 25);
leds[60].setRGB(25, 25, 25); //30
leds[14].setRGB(25, 25, 25);
leds[105].setRGB(25, 25, 25); //45
}

int secPos = timeClient.getSeconds() - 31;


secPos = secPos < 0 ? 60 + secPos : secPos;
int secMirPos = 60 - secPos + 59;
leds[secPos].setRGB(255, 0, 0);
leds[secMirPos].setRGB(255, 0, 0);

int minPos = timeClient.getMinutes() - 31;


minPos = minPos < 0 ? 60 + minPos : minPos;
leds[minPos].setRGB(0, 255, 0);

int hrPos = timeClient.getHours();


hrPos = hrPos > 11 ? hrPos - 12 : hrPos;
hrPos = (hrPos * 5) + (timeClient.getMinutes() /
12) - 31;
hrPos = hrPos < 0 ? 60 + hrPos : hrPos;
hrPos = 60 - hrPos + 59;

leds[hrPos].setRGB(0, 0, 255);
}

void handle_sim() {
isSimOn = !isSimOn;
if (!isSimOn) timeClient.setTimeOffset(10800);
server.send(200, "text/plain", isSimOn ? "Sim on"
: "Sim off");
}

void handle_marks() {
isMarksOn = !isMarksOn;
server.send(200, "text/plain", isMarksOn ? "Marks
on" : "Marks off");
}

void handle_fade() {
isFadeOn = !isFadeOn;
server.send(200, "text/plain", isFadeOn ? "Fade
on" : "Fade off");
}

void handle_main() {
server.send(200, "text/plain", "main");
}

void handle_time() {
timeClient.update();

String timeStr = (String)timeClient.getHours() +


":" + (String)timeClient.getMinutes() + ":" +
(String)timeClient.getSeconds();

server.send(200, "text/plain", timeStr);


}
GREEN - Minute

BLUE - Hour

RED - Second

You might also like