/*************************************************************
Written by Titafubaki
You are Free to use, share and change this code like you want.
But do not sell it or parts of it and do not tell others you have
written it.
To compile this code You will need the additional Librarys:
- Adafruit_NeoPixel ( Search in the Library Manager or get it from GitHub: https://github.com/adafruit/Adafruit_NeoPixel )
- Blynk ( Search in the Library Manager or get it from there page: https://www.blynk.cc/ )
- BlynkESP8266_Lib ( Search in the Library Manager or get it from there page: https://www.blynk.cc/ )
Board: NodeMCU 1.0 (ESP-12 Module) -> Install ESP8266 via Board Manager
Upload Speed: 115200
For the Blynk App you can use this QR Code to Copy my Project: http://blynk-cloud.com/1d10593575434dd0acf4a2e92ee224fb/qr
If you want to use Google To Controll it you will need IFTTT
Each function and Color is one Applet. THis will take some time to configure but will be worth it. Trust me :D
First of all. Open an cmd and type in "blynk-cloud.com" it will return you an IP address.
You should use this address in the URL not the DNS name. For some reason this is not working
Create a new Applet in IFTTT and select Google Asistant (Should also work with Alexa but I dont own one)
Choose a trigger like "Say a simple phrase"
Than choos as Action "Webhooks" -> Make a web request
Here you tipe in the URL, Method is PUT, Content Type: "application/json" and then the Boddy.
Here is a List of some Commands for Google.
Turn On the Kristal:
URL: http://IPToBlynk/AuthToken/update/V0
Method: PUT
Content Type: application/json
Body: [ "1" ]
Turn Off the Kristal:
URL: http://IPToBlynk/AuthToken/update/V0
Method: PUT
Content Type: application/json
Body: [ "0" ]
For the colors you can use a color Picker from a Website and Change the Body it is in the Formate RGB: [ "R", "G", "B" ]
Change Color to Blue
URL: http://IPToBlynk/AuthToken/update/V3
Method: PUT
Content Type: application/json
Body: [ "0", "0", "255" ]
The Body contains the intex of the Mode (1-6)
Change Mode to ColorFade:
URL: http://IPToBlynk/AuthToken/update/V4
Method: PUT
Content Type: application/json
Body: ["5"]
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "e75a9243961a4e848fb0f5827c3ceb1e";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Anhalter42";
char pass[] = "Kauf dir Internet du Schnorrer!";
//GPIO used on the Board
#define PIN 14
//Count of LEDs
#define PIXELNUMBER 30
//The minimal brightness in Breath mode
#define BREATHMIN 30
//You should consider to change this Value depending on your power supply. (0 - 255)
#define OVERALLBRIGHTNESS 255
const int numReadings = 20;
//init of all needed Objects
BlynkTimer timer;
int prevMod = 0;
int selectedMod = 0;
int selectedColorR = 254;
int selectedColorG = 254;
int selectedColorB = 254;
bool step = 0;
int breathPercentage = 100;
int colorWheelCount = 255 / PIXELNUMBER;
int rainbowcolor = 0;
int speed = 50;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELNUMBER, PIN, NEO_GRB + NEO_KHZ800);
int ch1;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 1; // the average
//This Function is used to set the Color when you are in the Color Mode
void changeColor (){
if(selectedMod == 3 || selectedMod == 0){
for (int i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, selectedColorR,selectedColorG,selectedColorB);
}
strip.show();
}
}
//This Function will run once after the setup so you can see that the Crystal is ready. (It will fade the brightness to 100% and back to 0)
void setupCompleted(){
for (int i=0; i<100; i++){
int breathR = (selectedColorR / 100) * i;
int breathG = (selectedColorG / 100) * i;
int breathB = (selectedColorB / 100) * i;
for (int i2 = 0; i2 < strip.numPixels(); i2++){
strip.setPixelColor(i2, breathR,breathG,breathB);
}
strip.show();
delay(5);
}
for (int i=100; i>0; i--){
int breathR = (selectedColorR / 100) * i;
int breathG = (selectedColorG / 100) * i;
int breathB = (selectedColorB / 100) * i;
for (int i2 = 0; i2 < strip.numPixels(); i2++){
strip.setPixelColor(i2, breathR,breathG,breathB);
}
strip.show();
delay(5);
}
for (int i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0);
}
strip.show();
selectedMod = 6;
}
//This is the Wheel Funktion from Neopixel. I did not write it. It will give you a Color when you give it a value from 0 - 255
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
//This is one step in the Breath mode. It is as function so I am able to do more then one step in my timer without copy the hole code again and again :D
void stepBreath(){
if (step){
if (breathPercentage >= 100){
step = 0;
breathPercentage = 100;
}else{breathPercentage += +1;}
}
else
{
if (breathPercentage <= BREATHMIN){
step = 1;
breathPercentage = BREATHMIN;
}else{breathPercentage += -1;}
}
int breathR = (selectedColorR / 100) * breathPercentage;
int breathG = (selectedColorG / 100) * breathPercentage;
int breathB = (selectedColorB / 100) * breathPercentage;
for (int i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, breathR,breathG,breathB);
}
strip.show();
}
//This is one step in the RainbowWheel mode. It is as function so I am able to do more then one step in my timer without copy the hole code again and again :D
void stepColorWheel(){
if (rainbowcolor > 255){
rainbowcolor = 0;
}
for (int i=0; i<strip.numPixels(); i++) {
int myrainbowcolor = rainbowcolor + (colorWheelCount*i);
while (myrainbowcolor > 255){
myrainbowcolor+= -255;
}
strip.setPixelColor(i, Wheel(myrainbowcolor));
}
strip.show();
rainbowcolor--;
}
//This is one step in the ColorFade mode. It is as function so I am able to do more then one step in my timer without copy the hole code again and again :D
void stepColorFade (){
if (rainbowcolor > 255){
rainbowcolor = 0;
}
for (int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(rainbowcolor));
}
strip.show();
rainbowcolor++;
}
//This is the Main function the timer will work with. Each Case represent one selected mode
void playModus () {
ch1 = pulseIn(12, HIGH, 25000);
int speedHz = ch1;
if (speedHz > 1989){
speedHz = 1990;
}
if (speedHz < 1490){
speedHz = 1490;
}
speed = 200 - (((1991 - speedHz) * 2)/5);
if (speed > 199){
speed = 199;
}
if (speed < 20){
speed = 20;
}
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = speed;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
speed = average;
if (speed > 199){
speed = 199;
}
if (speed < 1){
speed = 1;
}
Serial.print("Channel 1:"); // Print the value of
Serial.println(speed);
switch (selectedMod){
case 1: {
for (int i = 0; i < strip.numPixels(); i++)
{
if (step){
strip.setPixelColor(i, selectedColorR,selectedColorG,selectedColorB);
step = 0;
}
else
{
strip.setPixelColor(i, 0);
step = 1;
}
}
strip.show();
if (step){step = 0;}else{step = 1;}
break;
}
case 2: {
stepBreath();
delay(50);
stepBreath();
delay(50);
stepBreath();
delay(50);
stepBreath();
break;
}
case 3: {
break;
}
case 4: {
for (int i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, random(254),random(254),random(254));
}
strip.show();
break;
}
case 5: {
stepColorFade();
delay(100);
stepColorFade();
break;
}
case 6: {
int mydelay = (200/speed);
for (int i = 0; i < speed -1; i++){
stepColorWheel();
delay(mydelay);
}
break;
}
}
}
//Here I get the selected mode from Blynk and store it in a variable
BLYNK_WRITE(V4)
{
switch (param.asInt()){
case 1: {
selectedMod = 1;
break;
}
case 2: {
selectedMod = 2;
break;
}
case 3: {
selectedMod = 3;
changeColor();
break;
}
case 4: {
selectedMod = 4;
break;
}
case 5: {
selectedMod = 5;
break;
}
case 6: {
selectedMod = 6;
break;
}
}
}
//Here I get the Color from Blynk
BLYNK_WRITE(V3)
{
selectedColorR = param[0].asInt();
selectedColorG = param[1].asInt();
selectedColorB = param[2].asInt();
changeColor();
}
//This is the Function to turn the lamp on and off
BLYNK_WRITE(V0)
{
if(param.asInt() == 0){
for (int i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0);
}
if (!selectedMod==0){
prevMod = selectedMod;
}
selectedMod = 0;
strip.show();
}
else {
if (prevMod==0){
prevMod = 3;
}
selectedMod = prevMod;
changeColor();
}
}
//Speed
BLYNK_WRITE(V1)
{
speed = param.asInt();
}
void setup()
{
pinMode(12, INPUT);
// Debug console
Serial.begin(9600);
randomSeed(analogRead(0));
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
//Turns off all LEDs at the beginn
strip.begin();
strip.setBrightness(OVERALLBRIGHTNESS);
strip.show();
//Init of all Variables used in this program
timer.setInterval(200L, playModus);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 1;
}
setupCompleted();
}
void loop()
{
Blynk.run();
timer.run();
}
Comments