#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "fauxmoESP.h"
// Rename the credentials.sample.h file to credentials.h and
// edit it according to your router configuration
#define WIFI_SSID "TurmLAN"
#define WIFI_PASS "0070turm0070"
#define NUMPIXELS 27
#define ledPin ?
Adafruit_NeoPixel pixels(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
fauxmoESP fauxmo;
// -----------------------------------------------------------------------------
#define SERIAL_BAUDRATE 115200
#define espPin 2
#define espOut 16
#define mm0 "mm0 nur Routine"
#define mm1 "mm1 nur Routine"
#define gt0 "gt0 nur Routine"
#define gt1 "gt1 nur Routine"
int flagMM0 = 0;
int flagMM1 = 0;
int flagGT0 = 0;
int flagGT1 = 0;
int espPings = 0;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------
void wifiSetup() {
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
void setup() {
// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
Serial.println();
// LEDs
pinMode(espPin, OUTPUT);
digitalWrite(espPin, LOW);
pinMode(espOut, INPUT);
// Wifi
wifiSetup();
// By default, fauxmoESP creates it's own webserver on the defined port
// The TCP port must be 80 for gen3 devices (default is 1901)
// This has to be done before the call to enable()
fauxmo.createServer(true); // not needed, this is the default value
fauxmo.setPort(80); // This is required for gen3 devices
// You have to call enable(true) once you have a WiFi connection
// You can enable or disable the library at any moment
// Disabling it will prevent the devices from being discovered and switched
fauxmo.enable(true);
// You can use different ways to invoke alexa to modify the devices state:
// "Alexa, turn yellow lamp on"
// "Alexa, turn on yellow lamp
// "Alexa, set yellow lamp to fifty" (50 means 50% of brightness, note, this example does not use this functionality)
// Add virtual devices
fauxmo.addDevice(mm0);
fauxmo.addDevice(mm1);
fauxmo.addDevice(gt0);
fauxmo.addDevice(gt1);
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
// Callback when a command from Alexa is received.
// You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
// State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
// Just remember not to delay too much here, this is a callback, exit as soon as possible.
// If you have to do something more involved here set a flag and process it in your main loop.
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
// Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
// Otherwise comparing the device_name is safer.
if (strcmp(device_name, mm0)==0) {
flagMM0 = 1;
}
else if (strcmp(device_name, mm1)==0) {
flagMM1 = 1;
}
else if (strcmp(device_name, gt0)==0) {
flagGT0 = 1;
}
else if (strcmp(device_name, gt1)==0) {
flagGT1 = 1;
}
});
}
void loop() {
// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets
fauxmo.handle();
if(flagMM0 == 1){
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
flagMM0 = 0;
flagMM1 = 0;
flagGT0 = 0;
flagGT1 = 0;
}
if(flagMM1 == 1){
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
delay(90);
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
flagMM0 = 0;
flagMM1 = 0;
flagGT0 = 0;
flagGT1 = 0;
}
if(flagGT0 == 1){
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
delay(90);
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
delay(90);
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
flagMM0 = 0;
flagMM1 = 0;
flagGT0 = 0;
flagGT1 = 0;
}
if(flagGT1 == 1){
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
delay(90);
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
delay(90);
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
delay(90);
digitalWrite(espPin, HIGH);
delay(10);
digitalWrite(espPin, LOW);
flagMM0 = 0;
flagMM1 = 0;
flagGT0 = 0;
flagGT1 = 0;
}
if(digitalRead(espOut) == HIGH){
delay(50);
while(digitalRead(espOut)==LOW){
//Lichterkette Welle bis esp wieder high
//BLINK WITHOUT DELAY}
}
for(uint32_t espStartTime = millis(); (millis()-espStartTime)<500;){
if(digitalRead(espOut)==HIGH){
espPings ++;
delay(50);
}
}
if(espPings == 1){
//Blinken und dabei Farbe nach und nach ändern
}
if(espPings == 2){
//Rotes Warn Blinken
}
espPings = 0;
}
}