You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
7.5 KiB

#include <Wire.h>
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <FastLED.h>
#include <async.h>
const boolean DEBUG = true;
const boolean LED_RUN = true;
boolean BUTTONSERVER_ACTIV = false;
boolean UPDATE_LEDS = false;
// Replace with your network credentials
const char *ssid = "FRITZ!BoxWZ";
const char *password = "40360548708873074408";
IPAddress ip(192, 168, 178, 12);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 178, 1);
const char *PARAM_INPUT_1 = "speed";
const char *PARAM_INPUT_2 = "modus";
// Create AsyncWebServer object on port 88
AsyncWebServer server(88);
Async asyncEngine = Async();
Async asyncEngineReset = Async();
/*
* define your ws2812fx presets
*/
#define LED_PIN_1 D7
#define LED_PIN_2 D8
// Fastled:
#define NUM_LEDS_1 38
#define NUM_LEDS_2 144 // Total of 216 LED's 24 x 9 Panels | 24, 48, 72, 96, 120, 144, 168, 192, 216
#define MILLI_AMPS 2400
#define LED_TYPE WS2812B
#define COLOR_ORDER_1 GRB
#define COLOR_ORDER_2 GRB
#define UPDATES_PER_SECOND 100
#define BRIGHTNESS 64
CRGB LEDsStatus[NUM_LEDS_1];
//CRGB LEDsPanel[NUM_LEDS_2];
#define COOLING 55
#define FRAMES_PER_SECOND 200
bool gReverseDirection = false;
bool variDemo = false;
int ledOn = 0;
void ledDemoApp(boolean start) {
variDemo = true;
}
void initWebService() {
// Send a GET request to <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2>
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request) {
String inputMessage1;
String inputMessage2;
// GET input1 value on <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2>
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
//digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
}
else {
inputMessage1 = "No message sent";
inputMessage2 = "No message sent";
}
/*
Serial.print("GPIO: ");
Serial.print(inputMessage1);
Serial.print(" - Set to: ");
Serial.println(inputMessage2);
*/
request->send(200, "text/plain", "OK");
});
// ledfxon
server.on("/ledfxon", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage1;
String inputMessage2;
// GET input1 value on <ESP_IP>/ledfxon?speed=<inputMessage1>&modus=<inputMessage2>
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
}
else {
inputMessage1 = 100;
inputMessage2 = "SCAN";
}
int speedled = 100;
String modus = "FX_MODE_SCAN";
speedled = inputMessage1.toInt();
inputMessage2.toUpperCase();
modus = inputMessage2;
//startFastLed(speedled, modus);
request->send(200, "text/plain", "OK");
});
server.on("/ledtest", HTTP_GET, [] (AsyncWebServerRequest *request) {
request->send(200, "text/plain", "OK");
});
server.on("/ledDemoOn", HTTP_GET, [] (AsyncWebServerRequest *request) {
ledDemoApp(true);
ledOn = 1;
request->send(200, "text/plain", "LED on");
});
server.on("/ledDemoOff", HTTP_GET, [] (AsyncWebServerRequest *request) {
ledDemoApp(false);
ledOn = 0;
stopLed();
request->send(200, "text/plain", "LED off");
});
server.on("/ledmodus", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage1;
// GET input1 value on <ESP_IP>/ledfxon?speed=<inputMessage1>&modus=<inputMessage2>
if (request->hasParam(PARAM_INPUT_2)) {
inputMessage1 = request->getParam(PARAM_INPUT_2)->value();
}
else {
inputMessage1 = "0";
}
int modus = 0;
modus = inputMessage1.toInt();
//changeModus(modus);
String text = "Start Modus " + inputMessage1 + " OK";
request->send(200, "text/plain", text);
});
server.on("/ledoff", HTTP_GET, [] (AsyncWebServerRequest *request) {
//stopFastLed();
request->send(200, "text/plain", "LED OFF");
});
// Start server
server.begin();
}
//The setup function is called once at startup of the sketch
void setup() {
Serial.begin(9600);
Serial.println("");
Wire.begin();
pinMode(D4, OUTPUT);
randomSeed(analogRead(0));
if (connectWifi()) {
// Init Panels
FastLed();
//initAllPanels();
initWebService();
} else {
digitalWrite(D4, LOW);
}
}
// ------------ FINISH -----------------
boolean connectWifi() {
boolean stat = true;
// Connect to Wi-Fi
WiFi.config(ip, gateway, subnet); // uncomment for dynamic IP
WiFi.begin(ssid, password);
int count = 0;
while (WiFi.status() != WL_CONNECTED) {
count++;
digitalWrite(D4, LOW);
delay(1000);
digitalWrite(D4, HIGH);
Serial.println("Connecting to WiFi..");
if (count == 10) {
stat = false;
break;
}
}
if (stat == true) {
digitalWrite(D4, LOW);
// Print ESP Local IP Address
Serial.println(WiFi.localIP());
}
return stat;
}
void FastLed() {
FastLED.addLeds<LED_TYPE, LED_PIN_1, COLOR_ORDER_1>(LEDsStatus, NUM_LEDS_1);
//FastLED.addLeds<LED_TYPE, LED_PIN_2, COLOR_ORDER_2>(LEDsPanel, NUM_LEDS_2);
FastLED.setBrightness(BRIGHTNESS);
}
// The loop function is called in an endless loop
void loop() {
if (variDemo == true && ledOn == 1) {
ledDemo();
}
//Fire2012(); // run simulation frame
//FastLED.show(); // display this frame
//FastLED.delay(1000 / FRAMES_PER_SECOND);
}
void stopLed() {
for (int panel = 0; panel < NUM_LEDS_1; panel++) {
LEDsStatus[panel] = CRGB::Black;
}
FastLED.show();
FastLED.clearData();
FastLED.show();
FastLED.clear();
FastLED.show();
}
void ledDemo() {
LEDsStatus[0] = CRGB::Red;
FastLED.show();
delay(500);
for (int panel = 0; panel < NUM_LEDS_1; panel++) {
LEDsStatus[panel] = CRGB::Yellow;
}
FastLED.show();
delay(500);
for (int panel = 0; panel < NUM_LEDS_1; panel++) {
LEDsStatus[panel] = CRGB::Green;
}
for (int panel = 0; panel < NUM_LEDS_1; panel++) {
LEDsStatus[panel] = CRGB::Green;
}
FastLED.show();
delay(500);
// Now turn the LED off, then pause
for (int panel = 0; panel < NUM_LEDS_1; panel++) {
LEDsStatus[panel] = CRGB::Black;
}
FastLED.show();
delay(500);
}
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120
void Fire2012()
{
// Array of temperature readings at each simulation cell
static uint8_t heat[LED_PIN_1];
// Step 1. Cool down every cell a little
for( int i = 0; i < LED_PIN_1; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / LED_PIN_1) + 2));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= LED_PIN_1 - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
int y = random8(7);
heat[y] = qadd8( heat[y], random8(160,255) );
}
// Step 4. Map from heat cells to LED colors
for( int j = 0; j < LED_PIN_1; j++) {
CRGB color = HeatColor( heat[j]);
int pixelnumber;
if( gReverseDirection ) {
pixelnumber = (LED_PIN_1-1) - j;
} else {
pixelnumber = j;
}
Serial.print("Panel: ");
Serial.println(pixelnumber);
LEDsStatus[pixelnumber] = color;
}
}