0% found this document useful (0 votes)
12 views11 pages

Io T

This document contains code for an ESP8266-based clock that displays the time on RGB LED strips using FastLED. It initializes WiFi and NTP time synchronization. Buttons are provided to simulate fast time changes, and toggle fading/marks effects and LEDs. The clock displays seconds in red, minutes in green, and hours in blue on the LED strip in real-time.

Uploaded by

agilan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Io T

This document contains code for an ESP8266-based clock that displays the time on RGB LED strips using FastLED. It initializes WiFi and NTP time synchronization. Buttons are provided to simulate fast time changes, and toggle fading/marks effects and LEDs. The clock displays seconds in red, minutes in green, and hours in blue on the LED strip in real-time.

Uploaded by

agilan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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