diff --git a/build_flags_mega2560-net.sh b/build_flags_mega2560-net.sh new file mode 100644 index 0000000..3f2c34c --- /dev/null +++ b/build_flags_mega2560-net.sh @@ -0,0 +1,30 @@ +#! /bin/bash +# usage: +# first make your own copy of template +# cp build_flags_template.sh build_flags_ENVNAME.sh +# then edit, change or comment something + export FLAGS="-DMY_CONFIG_SERVER=lazyhome.ru" + #export FLAGS="$FLAGS -DWATCH_DOG_TICKER_DISABLE" + #export FLAGS="$FLAGS -DUSE_1W_PIN=12" + #export FLAGS="$FLAGS -DSD_CARD_INSERTED" + export FLAGS="$FLAGS -DSERIAL_BAUD=115200" + #export FLAGS="$FLAGS -DWiz5500" + #export FLAGS="$FLAGS -DDISABLE_FREERAM_PRINT" + export FLAGS="$FLAGS -DCUSTOM_FIRMWARE_MAC=de:ad:be:ef:fe:fe" +# export FLAGS="$FLAGS -DDMX_DISABLE" +# export FLAGS="$FLAGS -DARTNET_ENABLE" +# export FLAGS="$FLAGS -DMODBUS_DISABLE" +# export FLAGS="$FLAGS -DOWIRE_DISABLE" +# export FLAGS="$FLAGS -DAVR_DMXOUT_PIN=18" +# export FLAGS="$FLAGS -DLAN_INIT_DELAY=2000" +# export FLAGS="$FLAGS -DCONTROLLINO" +# export FLAGS="$FLAGS -DESP_WIFI_AP=MYAP" +# export FLAGS="$FLAGS -DESP_WIFI_PWD=MYPWD" +# export FLAGS="$FLAGS -DWIFI_MANAGER_DISABLE" + export FLAGS="$FLAGS -DDHT_DISABLE" +# export FLAGS="$FLAGS -DRESET_PIN=5" +# export FLAGS="$FLAGS -DDHCP_RETRY_INTERVAL=60000" +# export FLAGS="$FLAGS -DRESTART_LAN_ON_MQTT_ERRORS" +# export FLAGS="$FLAGS -DW5500_CS_PIN=53" + export FLAGS="$FLAGS -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short) + echo $FLAGS diff --git a/build_flags_mega2560.sh b/build_flags_mega2560.sh new file mode 100644 index 0000000..ee99442 --- /dev/null +++ b/build_flags_mega2560.sh @@ -0,0 +1,30 @@ +#! /bin/bash +# usage: +# first make your own copy of template +# cp build_flags_template.sh build_flags_ENVNAME.sh +# then edit, change or comment something + export FLAGS="-DMY_CONFIG_SERVER=lazyhome.ru" + #export FLAGS="$FLAGS -DWATCH_DOG_TICKER_DISABLE" + #export FLAGS="$FLAGS -DUSE_1W_PIN=12" + #export FLAGS="$FLAGS -DSD_CARD_INSERTED" + export FLAGS="$FLAGS -DSERIAL_BAUD=115200" + #export FLAGS="$FLAGS -DWiz5500" + #export FLAGS="$FLAGS -DDISABLE_FREERAM_PRINT" + export FLAGS="$FLAGS -DCUSTOM_FIRMWARE_MAC=de:ad:be:ef:fe:ed" +# export FLAGS="$FLAGS -DDMX_DISABLE" +# export FLAGS="$FLAGS -DARTNET_ENABLE" +# export FLAGS="$FLAGS -DMODBUS_DISABLE" + export FLAGS="$FLAGS -DOWIRE_DISABLE" +# export FLAGS="$FLAGS -DAVR_DMXOUT_PIN=18" +# export FLAGS="$FLAGS -DLAN_INIT_DELAY=2000" +# export FLAGS="$FLAGS -DCONTROLLINO" +# export FLAGS="$FLAGS -DESP_WIFI_AP=MYAP" +# export FLAGS="$FLAGS -DESP_WIFI_PWD=MYPWD" +# export FLAGS="$FLAGS -DWIFI_MANAGER_DISABLE" + export FLAGS="$FLAGS -DDHT_DISABLE" +# export FLAGS="$FLAGS -DRESET_PIN=5" +# export FLAGS="$FLAGS -DDHCP_RETRY_INTERVAL=60000" +# export FLAGS="$FLAGS -DRESTART_LAN_ON_MQTT_ERRORS" +# export FLAGS="$FLAGS -DW5500_CS_PIN=53" + export FLAGS="$FLAGS -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short) + echo $FLAGS diff --git a/build_flags_template.sh b/build_flags_template.sh index 8c1c2ab..e66e237 100644 --- a/build_flags_template.sh +++ b/build_flags_template.sh @@ -10,7 +10,7 @@ export FLAGS="$FLAGS -DSERIAL_BAUD=115200" #export FLAGS="$FLAGS -DWiz5500" #export FLAGS="$FLAGS -DDISABLE_FREERAM_PRINT" - export FLAGS="$FLAGS -DCUSTOM_FIRMWARE_MAC=de:ad:be:ef:fe:00" + export FLAGS="$FLAGS -DCUSTOM_FIRMWARE_MAC=de:ad:be:ef:fe:ff" # export FLAGS="$FLAGS -DDMX_DISABLE" # export FLAGS="$FLAGS -DMODBUS_DISABLE" # export FLAGS="$FLAGS -DOWIRE_DISABLE" @@ -24,5 +24,6 @@ # export FLAGS="$FLAGS -DRESET_PIN=5" # export FLAGS="$FLAGS -DDHCP_RETRY_INTERVAL=60000" # export FLAGS="$FLAGS -DRESTART_LAN_ON_MQTT_ERRORS" +# export FLAGS="$FLAGS -DW5500_CS_PIN=53" export FLAGS="$FLAGS -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short) - echo $FLAGS \ No newline at end of file + echo $FLAGS diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 365e7df..fc5bec1 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -306,7 +306,15 @@ void Input::contactPoll() { if (store->bounce) store->bounce = store->bounce - 1; else //confirmed change { - onContactChanged(currentInputState); + if (inType & IN_PUSH_TOGGLE) { + if (currentInputState) { //react on leading edge only (change from 0 to 1) + store->logicState = !store->logicState; + onContactChanged(store->logicState); + } + } else { + store->logicState = currentInputState; + onContactChanged(currentInputState); + } store->currentValue = currentInputState; } } else // no change diff --git a/lighthub/inputs.h b/lighthub/inputs.h index 4895b51..94bbf32 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -20,12 +20,12 @@ e-mail anklimov@gmail.com #include -#define IN_ACTIVE_HIGH 2 // High level = PUSHED/ CLOSED/ ON othervise :Low Level +#define IN_ACTIVE_HIGH 2 // High level = PUSHED/ CLOSED/ ON othervise :Low Level. Use INPUT mode instead of INPUT_PULLUP for digital pin #define IN_ANALOG 64 // Analog input #define IN_RE 32 // Rotary Encoder (for further use) #define IN_PUSH_ON 0 // PUSH - ON, Release - OFF (ovverrided by pcmd/rcmd) - DEFAULT -#define IN_PUSH_TOGGLE 1 // Every physicall push toggle logical switch on/off +#define IN_PUSH_TOGGLE 1 // Used for push buttons. Every physicall push toggle logical switch on/off. Toggle on leading edge #define IN_DHT22 4 #define IN_COUNTER 8 #define IN_UPTIME 16 @@ -41,14 +41,14 @@ e-mail anklimov@gmail.com // //Normal (not button) Switch (toggled mode) -//"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "TOGGLE", "rcmd": "TOGGLE"} +//"pin": { "T":"0", "emit":"/light1", item:"light1", "scmd": "TOGGLE", "rcmd": "TOGGLE"} // or // "pin": { "T":"xx", "emit":"/light1", item:"light1"} -//Normal (not button) Switch -//"pin": { "T":"0", "emit":"/light1", item:"light1", "scmd": "ON", "rcmd": "OFF"} +//Use Button +//"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "ON", "rcmd": "OFF"} // or -// "pin": { "T":"0", "emit":"/light1", item:"light1"} +// "pin": { "T":"1", "emit":"/light1", item:"light1"} //or // "pin": { "emit":"/light1", item:"light1"} diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 3abbd2a..39d4820 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -55,6 +55,10 @@ int txt2cmd(char *payload) { else if (strcmp(payload, "REST") == 0) cmd = CMD_RESTORE; else if (strcmp(payload, "TOGGLE") == 0) cmd = CMD_TOGGLE; else if (strcmp(payload, "HALT") == 0) cmd = CMD_HALT; + else if (strcmp(payload, "XON") == 0) cmd = CMD_XON; + else if (strcmp(payload, "XOFF") == 0) cmd = CMD_XOFF; + else if (strcmp(payload, "INCREASE") == 0) cmd = CMD_UP; + else if (strcmp(payload, "DECREASE") == 0) cmd = CMD_DN; else if (*payload == '-' || (*payload >= '0' && *payload <= '9')) cmd = 0; else if (*payload == '{') cmd = -2; else if (*payload == '#') cmd = -3; @@ -102,10 +106,12 @@ Item::Item(char *name) //Constructor Parse(); } -uint8_t Item::getCmd() { +uint8_t Item::getCmd(bool ext) { aJsonObject *t = aJson.getArrayItem(itemArr, I_CMD); if (t) + if (ext) return t->valueint; + else return t->valueint & CMD_MASK; else return -1; } @@ -113,7 +119,11 @@ uint8_t Item::getCmd() { void Item::setCmd(uint8_t cmd) { aJsonObject *t = aJson.getArrayItem(itemArr, I_CMD); if (t) + { t->valueint = cmd; + Serial.print(F("SetCmd:")); + Serial.println(cmd); + } } int Item::getArg(short n) //Return arg int or first array element if Arg is array @@ -277,6 +287,21 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) { default: return -3; }//switch old cmd + break; + case CMD_XOFF: + if (itemType != CH_GROUP) //individual threating of channels. Ignore restore command for groups + switch (t = getCmd()) { + case CMD_XON: //previous command was CMD_XON ? + Serial.print(F("Turned off from:")); + Serial.println(t); + cmd = CMD_OFF; //turning Off + break; + default: + Serial.print(F("XOFF skipped. Prev cmd:")); + Serial.println(t); + return -3; + }//switch old cmd + break; } //switch cmd switch (cmd) { @@ -290,10 +315,21 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) { if (!Par[1]) itemType = CH_WHITE; case CH_GROUP: //Save for groups as well case CH_RGB: + if (n == 3) { // Full triplet passed st.h = Par[0]; st.s = Par[1]; st.v = Par[2]; setVal(st.aslong); + } else // Just volume passed + { + st.aslong = getVal(); // restore Colour + st.v = Par[0]; // override volume + setVal(st.aslong); // Save back + Par[0] = st.h; + Par[1] = st.s; + Par[2] = st.v; + n = 3; + } if (send) SendStatus(0,3,Par,true); // Send back triplet ? break; @@ -313,7 +349,19 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) { break; + case CMD_XON: + if (!isActive()) //if channel was'nt active before CMD_XON + { + Serial.println(F("Turning XON")); +// setCmd(cmd); + } + else + { //cmd = CMD_ON; + Serial.println(F("Already Active")); + if (itemType != CH_GROUP) return -3; + } case CMD_ON: + if (itemType==CH_RGBW && getCmd() == CMD_ON && getEnableCMD(500)) { Serial.println(F("Force White")); itemType = CH_WHITE; @@ -388,6 +436,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) { case CH_THERMO: Par[0] = 20; //20 degrees celsium - safe temperature params = 1; + setVal(20); SendStatus(0, params, Par); break; case CH_RGBW: @@ -396,16 +445,23 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) { Par[1] = 0; Par[2] = 100; params = 3; + // Store + st.h = Par[0]; + st.s = Par[1]; + st.v = Par[2]; + setVal(st.aslong); SendStatus(0, params, Par,true); break; case CH_RELAY: Par[0] = 100; params = 1; + setVal(100); if (send) SendStatus(CMD_ON); break; default: Par[0] = 100; params = 1; + setVal(100); SendStatus(0, params, Par); } } // default handler @@ -547,7 +603,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) { case CH_RELAY: { int k; pinMode(iaddr, OUTPUT); - digitalWrite(iaddr, k = ((cmd == CMD_ON) ? HIGH : LOW)); + digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? HIGH : LOW)); Serial.print(F("Pin:")); Serial.print(iaddr); Serial.print(F("=")); @@ -629,6 +685,7 @@ int Item::isActive() { // Simple check last command first switch (cmd) { case CMD_ON: + case CMD_XON: //Serial.println(" active"); return 1; case CMD_OFF: @@ -971,7 +1028,7 @@ int Item::checkFM() { } boolean Item::checkModbusRetry() { - int cmd = getCmd(); + int cmd = getCmd(true); if (cmd & CMD_RETRY) { // if last sending attempt of command was failed int val = getVal(); Serial.println(F("Retrying CMD")); @@ -1049,7 +1106,7 @@ int Item::checkModbusDimmer(int data) { if (d) { // Actually turned on if (cmd == CMD_OFF || cmd == CMD_HALT) SendStatus(CMD_ON); //update OH with ON if it was turned off before SendStatus(0, 1, &d); //update OH with value - setCmd(CMD_ON); //store command + if (cmd != CMD_XON && cmd != CMD_ON) setCmd(CMD_ON); //store command setVal(d); //store value } else { if (cmd != CMD_HALT && cmd != CMD_OFF) { @@ -1085,7 +1142,7 @@ int Item::Poll() { void Item::sendDelayedStatus(){ HSVstore st; - int cmd=getCmd(); + int cmd=getCmd(true); short params = 0; int Par[3]; if (cmd & CMD_REPORT) @@ -1127,9 +1184,9 @@ void Item::sendDelayedStatus(){ int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) { /// ToDo: relative patches, configuration - + int chancmd=getCmd(true); if (deffered) { - setCmd(cmd | CMD_REPORT); + setCmd(chancmd | CMD_REPORT); Serial.println(F("Status deffered")); // mqttClient.publish("/push", "1"); return 0; @@ -1146,6 +1203,7 @@ int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) { switch (cmd) { case CMD_ON: + case CMD_XON: strcpy(valstr, "ON"); break; case CMD_OFF: diff --git a/lighthub/item.h b/lighthub/item.h index bb777dc..7a854dd 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -33,11 +33,17 @@ e-mail anklimov@gmail.com #define CMD_ON 1 #define CMD_OFF 2 -#define CMD_HALT 5 -#define CMD_RESTORE 3 +#define CMD_RESTORE 3 //on only if was turned off by CMD_HALT #define CMD_TOGGLE 4 +#define CMD_HALT 5 //just Off +#define CMD_XON 6 //just on +#define CMD_XOFF 7 //off only if was previously turned on by CMD_XON +#define CMD_UP 8 //just on +#define CMD_DN 9 //off only if was previously turned on by CMD_XON +#define CMD_SET 0xe +#define CMD_MASK 0xf + #define CMD_CURTEMP 127 -#define CMD_SET 9 #define CMD_RETRY 64 #define CMD_REPORT 32 @@ -94,7 +100,7 @@ class Item boolean getEnableCMD(int delta); //int getVal(short n); //From VAL array. Negative if no array long int getVal(); //From int val OR array - uint8_t getCmd(); + uint8_t getCmd(bool ext = false); void setCmd(uint8_t cmd); //void setVal(uint8_t n, int par); void setVal(long int par); diff --git a/lighthub/main.cpp b/lighthub/main.cpp index d31a027..5d3fafe 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -493,6 +493,11 @@ void onInitialStateInitLAN() { #endif #if defined(__AVR__) || defined(__SAM3X8E__)||defined(ARDUINO_ARCH_STM32F1) +#ifdef W5500_CS_PIN + Ethernet.w5500_cspin = W5500_CS_PIN; + debugSerial.print(F("Use W5500 pin: ")); + debugSerial.println(Ethernet.w5500_cspin); +#endif IPAddress ip, dns, gw, mask; int res = 1; debugSerial.println(F("Starting lan")); diff --git a/lighthub/main.h b/lighthub/main.h index dc16718..5e4f782 100644 --- a/lighthub/main.h +++ b/lighthub/main.h @@ -37,15 +37,23 @@ //#define wdt_en() //#define wdt_dis() //#endif - -#if defined(WATCH_DOG_TICKER_DISABLE) && defined(__AVR__) +#if defined(__AVR__) +#if defined(WATCH_DOG_TICKER_DISABLE) #define wdt_en() wdt_disable() #define wdt_dis() wdt_disable() #define wdt_res() wdt_disable() +#else +#define wdt_en() wdt_enable(WDTO_8S) +#define wdt_dis() wdt_disable() +#define wdt_res() wdt_reset() +#endif +#endif + +#ifndef OWIRE_DISABLE +#include "DallasTemperature.h" #endif #include "Arduino.h" -#include "DallasTemperature.h" #include #include #include "utils.h" @@ -56,18 +64,18 @@ #include "stdarg.h" #include "item.h" #include "inputs.h" + #ifndef ARDUINO_ARCH_STM32F1 #include "FastLED.h" #endif + #include "Dns.h" //#include "hsv2rgb.h" #if defined(__SAM3X8E__) - #include #include #include - #endif #if defined(__AVR__) @@ -150,7 +158,9 @@ void restoreState(); lan_status lanLoop(); +#ifndef OWIRE_DISABLE void Changed(int i, DeviceAddress addr, int val); +#endif void modbusIdle(void); diff --git a/lighthub/owTerm.cpp b/lighthub/owTerm.cpp index dd4f101..adedd89 100644 --- a/lighthub/owTerm.cpp +++ b/lighthub/owTerm.cpp @@ -18,6 +18,7 @@ e-mail anklimov@gmail.com */ +#ifndef OWIRE_DISABLE #include "owTerm.h" #include @@ -204,3 +205,4 @@ void owAdd(DeviceAddress addr) { t_count++; #endif } +#endif diff --git a/lighthub/owTerm.h b/lighthub/owTerm.h index 951bc32..12588aa 100644 --- a/lighthub/owTerm.h +++ b/lighthub/owTerm.h @@ -32,16 +32,16 @@ e-mail anklimov@gmail.com #define recheck_interval 5 #define check_circle 2000/t_count -#define SW_FIND 1 +#define SW_FIND 1 #define SW_DOUBLECHECK 2 //Doublecheck required -#define SW_PULSE0 4 //Pulse Reset started -#define SW_PULSE1 8 //Pulse Reset stsrted +#define SW_PULSE0 4 //Pulse Reset started +#define SW_PULSE1 8 //Pulse Reset stsrted #define SW_PULSE_P0 0x10 //Pulse reset in process #define SW_PULSE_P1 0x20 //Pulse reset in process #define SW_CHANGED_P0 0x40 //Changes while pulse in progress #define SW_CHANGED_P1 0x80 //Changes while pulse in progress -#define SW_PULSE0_R 0x100 //Pulse Reset requested -#define SW_PULSE1_R 0x200 //Pulse Reset requested +#define SW_PULSE0_R 0x100 //Pulse Reset requested +#define SW_PULSE1_R 0x200 //Pulse Reset requested #define recheck_interval 5 @@ -50,6 +50,8 @@ e-mail anklimov@gmail.com #define t_max 20 //Maximum number of 1w devices #define TEMPERATURE_PRECISION 9 +#ifndef OWIRE_DISABLE + #ifndef ARDUINO_ARCH_STM32F1 #include #endif @@ -84,7 +86,8 @@ extern owChangedType owChanged; int owUpdate(); int owSetup(owChangedType owCh); void owLoop(); -void owIdle(void) ; +void owIdle(void) ; int owFind(DeviceAddress addr); void owAdd (DeviceAddress addr); +#endif diff --git a/platformio.ini b/platformio.ini index b33dc6f..c819fb2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,13 +10,13 @@ [platformio] src_dir = lighthub env_default = -; megaatmega2560 + megaatmega2560 ; megaatmega2560-net ; due ; esp8266 ; esp32 ; megaatmega2560-5500 - due-5500 +; due-5500 ; controllino ; stm32 @@ -99,12 +99,11 @@ lib_deps = [env:megaatmega2560] platform = atmelavr board = megaatmega2560 +;upload_port = net:192.168.88.31:23 framework = arduino ;lib_ldf_mode = chain+ build_flags = !sh build_flags_mega2560.sh lib_deps = - https://github.com/anklimov/Arduino-Temperature-Control-Library.git - https://github.com/anklimov/DS2482_OneWire https://github.com/anklimov/DmxSimple https://github.com/anklimov/httpClient https://github.com/anklimov/aJson @@ -112,7 +111,6 @@ lib_deps = https://github.com/anklimov/ModbusMaster https://github.com/anklimov/DMXSerial https://github.com/anklimov/Ethernet - https://github.com/anklimov/Ethernet2 https://github.com/PaulStoffregen/SPI.git https://github.com/knolleary/pubsubclient.git https://github.com/anklimov/Artnet.git @@ -120,6 +118,7 @@ lib_deps = EEPROM Adafruit Unified Sensor DHT sensor library + https://github.com/arcao/Syslog.git [env:esp8266] platform = espressif8266 @@ -168,6 +167,7 @@ lib_deps = Adafruit Unified Sensor DHT sensor library + [env:due-5500] platform = atmelsam framework = arduino @@ -211,7 +211,6 @@ lib_deps = https://github.com/anklimov/ModbusMaster https://github.com/anklimov/DMXSerial https://github.com/anklimov/Ethernet - https://github.com/anklimov/Ethernet2 https://github.com/PaulStoffregen/SPI.git https://github.com/knolleary/pubsubclient.git https://github.com/anklimov/Artnet.git