Commit 87af56ed authored by boekenwuurm's avatar boekenwuurm
Browse files

initial commit

parent 2ce88619
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <FastLED.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
FASTLED_USING_NAMESPACE
const char* ssid = "hackalot";
const char* password = "zegikniet";
const char* mqtt_server = "mqtt.space.hackalot.nl";
bool SpaceOpen = 0;
bool Opening = 1;
bool Closing = 1;
bool Test = 0;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN D2
#define CLK_PIN D1
#define LED_TYPE APA102
#define COLOR_ORDER BRG
#define NUM_LEDS 150
CRGB leds[NUM_LEDS];
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 250
int FadeSpeed = 1; //higher is faster
int GenerateSpeed = 2; //lower is faster
int ChasingSpeed = 8; //Lower is slower
int counter = 0;
// List of all the functions here
void setup_wifi();
void OTA();
void MQTTcallback(char* topic, byte* payload, unsigned int length);
void reconnect();
void rainbow();
void confetti();
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void OTA() {
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("esp8266_windowLEDstrip");
// No authentication by default
ArduinoOTA.setPassword("HackalotOTA");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
ArduinoOTA.onStart([]() {
String type;
rainbow();
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void MQTTcallback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic, "hackalot/state") == 0) {
Serial.println("Spacestate changed");
// Handle spaceState message
if (memcmp(payload, "open", 4) == 0)
{
if (SpaceOpen == 0) {
Opening = 1;
}
SpaceOpen = 1;
Serial.print("Space open!");
}
if (memcmp(payload, "closed", 4) == 0)
{
if (SpaceOpen == 1) {
Closing = 1;
}
SpaceOpen = 0;
Serial.print("Space closed!");
}
}
if (strcmp(topic, "hackalot/test") == 0) {
Test = 1;
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("Alive", "WindowLEDstrip online");
// ... and resubscribe
client.subscribe("hackalot/state");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void rainbow() {
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
void confetti() {
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, FadeSpeed);
counter++;
if (counter % GenerateSpeed == 0) {
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255);
counter = 0;
}
}
void sinelon() {
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( ChasingSpeed, 0, NUM_LEDS - 1 , 74);
leds[pos] += CHSV( gHue, 255, 192);
}
void setup() {
Serial.begin(115200);
Serial.println("Booting");
//FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); //WS2812
FastLED.addLeds<LED_TYPE, DATA_PIN, CLK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); //APA102
FastLED.setBrightness(BRIGHTNESS); // set master brightness control
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(MQTTcallback);
OTA();
}
int i = 0;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
ArduinoOTA.handle();
if (SpaceOpen == 1 && Opening == 1) {
Closing = 0;
sinelon();
i++;
if (i > 800) {
i = 0;
Opening = 0;
}
}
else if (SpaceOpen == 1 && Opening == 0) {
confetti();
}
else if (SpaceOpen == 0 && Closing == 1) {
fadeToBlackBy( leds, NUM_LEDS, FadeSpeed);
Opening = 1;
}
else if (SpaceOpen == 1 && Test == 1) {
rainbow();
}
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000 / FRAMES_PER_SECOND);
EVERY_N_MILLISECONDS( 50 ) {
gHue++; // slowly cycle the "base color" through the rainbow
}
EVERY_N_SECONDS(60) {
Test = 0; //resets the test patern
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment