Skip to content

Instantly share code, notes, and snippets.

@psycomp
Last active February 18, 2020 22:29
Show Gist options
  • Save psycomp/a8960cd8e588b28d1b865a57b05832a5 to your computer and use it in GitHub Desktop.
Save psycomp/a8960cd8e588b28d1b865a57b05832a5 to your computer and use it in GitHub Desktop.
Arduino code to power my FastLED Sunrise/Sunset Window
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESPTemplateProcessor.h>
#include <WiFiClient.h>
#include <ElegantOTA.h>
#include "WiFiManager.h"
#include "FastLED.h"
#define HOSTNAME "magicwindow"
#define NUM_LEDS 95
#define DATA_PIN 2
#define MAX_BRIGHTNESS 255
#define SUNRISE_LENGTH 30
#define RISE 0
#define SET 1
#define MIMETYPE "text/plain"
CRGB leds[NUM_LEDS];
ESP8266WebServer server(80);
int sunDirection = SET;
uint8_t heatIndex = MAX_BRIGHTNESS;
//const char* www_username = "up";
//const char* www_password = "load";
String htmlProcessor(const String& key) {
if (key == "MODE") return (sunDirection == RISE) ? F("Rising") : F("Setting") ;
else if (key == "HEAT") return String(heatIndex);
return "";
}
void handleRoot() {
if (!ESPTemplateProcessor(server).send(String("/index.html"), htmlProcessor)) {
server.send(200, MIMETYPE, F("page not found."));
}
}
void handleRise() {
sunDirection = RISE;
server.send(200, MIMETYPE, "");
}
void handleSet() {
sunDirection = SET;
server.send(200, MIMETYPE, "");
}
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(MAX_BRIGHTNESS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 10000);
fill_solid(leds, NUM_LEDS, CRGB::White);
WiFiManager wifiManager;
if (!wifiManager.autoConnect("ConfigureMagicWindow")) {
ESP.reset();
delay(1000);
}
if (!MDNS.begin(HOSTNAME)) {
ESP.reset();
delay(1000);
}
server.on("/rise", handleRise);
server.on("/set", handleSet);
server.on("/", handleRoot);
server.onNotFound(handleRoot);
ElegantOTA.begin(&server);
server.begin();
}
void sun() {
static const uint8_t interval = (SUNRISE_LENGTH * 60) / MAX_BRIGHTNESS;
EVERY_N_SECONDS(interval) {
if (sunDirection == RISE && heatIndex < MAX_BRIGHTNESS) {
heatIndex++;
}
else if (sunDirection == SET && heatIndex > 0) {
heatIndex--;
}
fill_solid(leds, NUM_LEDS, CRGB(heatIndex, heatIndex, heatIndex));
FastLED.show();
}
}
void loop() {
server.handleClient();
MDNS.update();
sun();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment