diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index c16a9187..3024ef6a 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -1,5 +1,7 @@ #include "definitions.h" #include "version.h" + + // *************************************************************************** // Load libraries for: WebServer / WiFiManager / WebSockets // *************************************************************************** @@ -671,24 +673,7 @@ void setup() { }); server.on("/off", []() { - #ifdef ENABLE_LEGACY_ANIMATIONS - exit_func = true; - #endif - mode = OFF; - getArgs(); - getStatusJSON(); - #ifdef ENABLE_MQTT - mqtt_client.publish(mqtt_outtopic, String("OK =off").c_str()); - #endif - #ifdef ENABLE_AMQTT - amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =off").c_str()); - #endif - #ifdef ENABLE_HOMEASSISTANT - stateOn = false; - #endif - #ifdef ENABLE_STATE_SAVE_SPIFFS - if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState); - #endif + handleStripeOff(); //karo, moved to request_handlers.h }); server.on("/all", []() { @@ -903,6 +888,9 @@ void loop() { #ifdef ENABLE_BUTTON button(); #endif + #ifdef PIRSIGNAL + pirSignal(); + #endif server.handleClient(); webSocket.loop(); diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index e652a471..2e98d6bf 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -3,18 +3,29 @@ // Neopixel #define PIN D1 // PIN (14 / D5) where neopixel / WS2811 strip is attached -#define NUMLEDS 24 // Number of leds in the strip +#define NUMLEDS 180 // Number of leds in the strip #define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192 #define BUTTON 4 // Input pin (4 / D2) for switching the LED strip on / off, connect this PIN to ground to trigger button. - -const char HOSTNAME[] = "McLighting01"; // Friedly hostname +//#define PIRSIGNAL D8 // is pulldown on d1 mini, so just put to 3.3V or connect pir to get signal + // hmm, but I need to have low on PIN 8 if I want to flash esp again, so disconnect PIR if you want to do this + // and - better read before, if you start ESP and have D8 high (GPIO 15), the esp tries to start from + // sdcard (what ever this means), so D8 is not usable as simple as I thought :-( pir sends to fast a motion + // detection if there is something +#define PIRSIGNAL D2 //is no pull down, so I hope the pir sensor will do the pull down when switching from signal detected to off + //yes it works as expected, pir has output of low and high + +#ifdef PIRSIGNAL + #define PIR_MODE "STA| 1| 0|245|80|255|255|255" // Static white + // notused mode wsmode speed bright r g b +#endif +const char HOSTNAME[] = "McLightGarden"; // Friedly hostname #define HTTP_OTA // If defined, enable ESP8266HTTPUpdateServer OTA code. -//#define ENABLE_OTA // If defined, enable Arduino OTA code. -#define ENABLE_AMQTT // If defined, enable Async MQTT code, see: https://github.com/marvinroger/async-mqtt-client +#define ENABLE_OTA // If defined, enable Arduino OTA code. +//#define ENABLE_AMQTT // If defined, enable Async MQTT code, see: https://github.com/marvinroger/async-mqtt-client //#define ENABLE_MQTT // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API -#define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active -#define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control +//#define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active +//#define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control //#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select Tools -> IwIP Variant -> Higher Bandwidth #define ENABLE_LEGACY_ANIMATIONS @@ -28,7 +39,7 @@ const char HOSTNAME[] = "McLighting01"; // Friedly hostname #error "Cant have both PubSubClient and AsyncMQTT enabled. Choose either one." #endif #if ( (defined(ENABLE_HOMEASSISTANT) and !defined(ENABLE_MQTT)) and (defined(ENABLE_HOMEASSISTANT) and !defined(ENABLE_AMQTT)) ) -#error "To use HA, you have to either enable PubCubClient or AsyncMQTT" +#error "To use HA, you have to either enable PubCubClient or AsyncMQTT"N #endif #if ( !defined(ENABLE_HOMEASSISTANT) and defined(MQTT_HOME_ASSISTANT_SUPPORT) ) #error "To use HA support, you have to either enable Homeassistant component" diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index cbc7f4ce..57d72e64 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -1,6 +1,11 @@ // *************************************************************************** // Request handlers // *************************************************************************** +/* + * File is extended to handle PIR Signal + * moved parts from server.on("/off") from McLightning to function handleStripeOff + * */ +void getStatusJSON(); //forward declaration, karo, want to call function in handleStripeOff #ifdef ENABLE_HOMEASSISTANT void tickerSendState(){ new_ha_mqtt_msg = true; @@ -74,6 +79,29 @@ uint16_t convertSpeed(uint8_t mcl_speed) { // *************************************************************************** // Handler functions for WS and MQTT // *************************************************************************** +//karo: found no simple way to switch off, so build function wich is called +//from server.on("/off") and from my pir-sensor +void handleStripeOff() { + #ifdef ENABLE_LEGACY_ANIMATIONS + exit_func = true; + #endif + mode = OFF; + getArgs(); + getStatusJSON(); + #ifdef ENABLE_MQTT + mqtt_client.publish(mqtt_outtopic, String("OK =off").c_str()); + #endif + #ifdef ENABLE_AMQTT + amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =off").c_str()); + #endif + #ifdef ENABLE_HOMEASSISTANT + stateOn = false; + #endif + #ifdef ENABLE_STATE_SAVE_SPIFFS + if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState); + #endif +} + void handleSetMainColor(uint8_t * mypayload) { // decode rgb data uint32_t rgb = (uint32_t) strtol((const char *) &mypayload[1], NULL, 16); @@ -553,7 +581,6 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) { if (payload[0] == '=') { // we get mode data String str_mode = String((char *) &payload[0]); - handleSetNamedMode(str_mode); if (mqtt == true) { DBG_OUTPUT_PORT.print("MQTT: "); @@ -1175,7 +1202,39 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght } } #endif - +//pir signal karo +#ifdef PIRSIGNAL +void pirSignal() { + //Pir has integrated timer so I don't care about time control, + //will see if it is goud enough with the integrate one, yes it seems so + static byte previousState = LOW; + byte currState = digitalRead(PIRSIGNAL); + //int m = (int) strip.getMode();//mode number is not enough to detect off + //---so get information like in listStatusJSON + static bool needLight = false; + //------------------------------------------------- + if ( previousState == LOW && currState == HIGH) { + DBG_OUTPUT_PORT.println("PIR: Detect change from LOW to HIGH "); + DBG_OUTPUT_PORT.printf("main_color %i %i %i brightness %i \n",main_color.red,main_color.green,main_color.blue,brightness); + if ((uint8_t) mode == 2 //off + || (main_color.red == 0 && main_color.green == 0 && main_color.blue == 0 ) //all off + || brightness < 5 ) + needLight = true; + //set leds to white if LED is actually off + if (needLight) { + DBG_OUTPUT_PORT.println("ok, needlight is true, switch on"); + setModeByStateString(PIR_MODE); + } + } + else if (previousState == HIGH && currState == LOW && needLight) { + DBG_OUTPUT_PORT.println("PIR: Detect change from HIGH to LOW "); + //switch led to off + needLight = false; + handleStripeOff(); + } + previousState = currState; + } +#endif #ifdef ENABLE_STATE_SAVE_SPIFFS bool updateFS = false; #if defined(ENABLE_MQTT) or defined(ENABLE_AMQTT)