Verified Commit 00d315e8 authored by Xesxen's avatar Xesxen
Browse files

Repo init with uncleaned project file

parents
// NeoPixelFunRandomChange
// This example will randomly select a number pixels and then
// start an animation to blend them from their current color to
// randomly selected a color
//
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NeoPixelBus.h>
#include <NeoPixelAnimator.h>
#include <ArduinoOTA.h>
const char* ssid = "hackalot";
const char* password = "zegikniet";
const char* mqtt_server = "mqtt.space.hackalot.nl";
bool SpaceOpen = 0;
WiFiClient espClient;
PubSubClient client(espClient);
const uint16_t PixelCount = 99; // make sure to set this to the number of pixels in your strip
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
NeoPixelBus<NeoGrbFeature, NeoEsp8266DmaWs2812xMethod> strip(PixelCount, PixelPin);
// For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.
// There are other Esp8266 alternative methods that provide more pin options, but also have
// other side effects.
// for details see wiki linked here https://github.com/Makuna/NeoPixelBus/wiki/ESP8266-NeoMethods
NeoPixelAnimator animations(PixelCount); // NeoPixel animation management object
// what is stored for state is specific to the need, in this case, the colors.
// Basically what ever you need inside the animation update function
struct MyAnimationState
{
RgbColor StartingColor;
RgbColor EndingColor;
};
// one entry per pixel to match the animation timing manager
MyAnimationState animationState[PixelCount];
RgbColor black(0,0,0);
void SetRandomSeed()
{
uint32_t seed;
// random works best with a seed that can use 31 bits
// analogRead on a unconnected pin tends toward less than four bits
seed = analogRead(0);
delay(1);
for (int shifts = 3; shifts < 31; shifts += 3)
{
seed ^= analogRead(0) << shifts;
delay(1);
}
// Serial.println(seed);
randomSeed(seed);
}
// simple blend function
void BlendAnimUpdate(const AnimationParam& param)
{
// this gets called for each animation on every time step
// progress will start at 0.0 and end at 1.0
// we use the blend function on the RgbColor to mix
// color based on the progress given to us in the animation
RgbColor updatedColor = RgbColor::LinearBlend(
animationState[param.index].StartingColor,
animationState[param.index].EndingColor,
param.progress);
// apply the color to the strip
strip.SetPixelColor(param.index, updatedColor);
if(param.progress == 1) {
animationState[param.index].StartingColor = animationState[param.index].EndingColor;
}
}
void PickRandom(float luminance)
{
// pick random count of pixels to animate
// uint16_t count = random(PixelCount);
uint16_t count = 13;
while (count > 0)
{
// pick a random pixel
uint16_t pixel = random(PixelCount);
// pick random time and random color
// we use HslColor object as it allows us to easily pick a color
// with the same saturation and luminance
uint16_t time = random(100, 400);
animationState[pixel].StartingColor = strip.GetPixelColor(pixel);
animationState[pixel].EndingColor = HslColor(random(360) / 360.0f, 1.0f, luminance);
animations.StopAnimation(pixel);
animations.StartAnimation(pixel, time, BlendAnimUpdate);
count--;
}
}
void FadeOut() {
for(int pixel = 0; pixel < PixelCount; pixel++) {
animationState[pixel].EndingColor = animationState[pixel].EndingColor;
animationState[pixel].EndingColor.Darken(64);
animations.StartAnimation(pixel, 400, BlendAnimUpdate);
}
}
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_kerstboom1");
// 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)
{
SpaceOpen = 1;
Serial.print("Space open!");
}
if (memcmp(payload, "closed", 4) == 0)
{
SpaceOpen = 0;
Serial.print("Space closed!");
}
}
}
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 setup()
{
strip.Begin();
strip.Show();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(MQTTcallback);
OTA();
SetRandomSeed();
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
ArduinoOTA.handle();
if (SpaceOpen) {
if (animations.IsAnimating())
{
// the normal loop just needs these two to run the active animations
animations.UpdateAnimations();
strip.Show();
}
else
{
// no animations runnning, start some
//
FadeOut();
PickRandom(0.30f); // 0.0 = black, 0.25 is normal, 0.5 is bright
}
}
else {
strip.ClearTo(black);
strip.Show();
}
}
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