From 994081430c3fa7bd4346b60b233259b24d771567 Mon Sep 17 00:00:00 2001 From: device111 <48546979+device111@users.noreply.github.com> Date: Thu, 3 Sep 2020 09:37:10 +0200 Subject: [PATCH 1/2] AS3935 Refactor - Fix Autodisturber - Fix Event IRQ Flicker when calibrating and on Start up - Add command `power` on/off - Add command `noirqevent` (suppress IRQ with no event) - Add Info Log when no Pin defined - Add Info Log when SCO and TCO Calibration Fails - optimize Autotune Caps - new init procedure (Reset before Start) - save 128 Bytes of Ram - update language files --- tasmota/language/bg_BG.h | 2 + tasmota/language/cs_CZ.h | 2 + tasmota/language/de_DE.h | 4 +- tasmota/language/el_GR.h | 2 + tasmota/language/en_GB.h | 2 + tasmota/language/es_ES.h | 2 + tasmota/language/fr_FR.h | 2 + tasmota/language/he_HE.h | 2 + tasmota/language/hu_HU.h | 2 + tasmota/language/it_IT.h | 2 + tasmota/language/ko_KO.h | 2 + tasmota/language/nl_NL.h | 2 + tasmota/language/pl_PL.h | 2 + tasmota/language/pt_BR.h | 2 + tasmota/language/pt_PT.h | 2 + tasmota/language/ro_RO.h | 2 + tasmota/language/ru_RU.h | 2 + tasmota/language/sk_SK.h | 2 + tasmota/language/sv_SE.h | 2 + tasmota/language/tr_TR.h | 2 + tasmota/language/uk_UA.h | 2 + tasmota/language/zh_CN.h | 2 + tasmota/language/zh_TW.h | 2 + tasmota/my_user_config.h | 2 +- tasmota/settings.h | 4 +- tasmota/tasmota_template.h | 4 +- tasmota/tasmota_template_ESP32.h | 4 +- tasmota/xsns_67_as3935.ino | 674 ++++++++++++++++--------------- 28 files changed, 413 insertions(+), 323 deletions(-) diff --git a/tasmota/language/bg_BG.h b/tasmota/language/bg_BG.h index ec637b220..e0543019c 100644 --- a/tasmota/language/bg_BG.h +++ b/tasmota/language/bg_BG.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "открит шум" #define D_AS3935_DISTDET "открито смущение" #define D_AS3935_INTNOEV "Прекъсване без Събитие!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "слушане..." #define D_AS3935_ON "Вкл." #define D_AS3935_OFF "Изкл." diff --git a/tasmota/language/cs_CZ.h b/tasmota/language/cs_CZ.h index 1d591b610..1200ceab5 100644 --- a/tasmota/language/cs_CZ.h +++ b/tasmota/language/cs_CZ.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/de_DE.h b/tasmota/language/de_DE.h index 4798c625e..abef862c9 100644 --- a/tasmota/language/de_DE.h +++ b/tasmota/language/de_DE.h @@ -812,7 +812,7 @@ #define D_AS3935_GAIN "Rauschpegel:" #define D_AS3935_ENERGY "Energie:" #define D_AS3935_DISTANCE "Entfernung:" -#define D_AS3935_DISTURBER "Störsingal:" +#define D_AS3935_DISTURBER "Enstörer:" #define D_AS3935_VRMS "µVrms:" #define D_AS3935_APRX "ca.:" #define D_AS3935_AWAY "entfernt" @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "Rauschen entdeckt" #define D_AS3935_DISTDET "Störer entdeckt" #define D_AS3935_INTNOEV "Interrupt ohne Grund!" +#define D_AS3935_FLICKER "IRQ Pin flackerd!" +#define D_AS3935_POWEROFF "Ausgeschaltet" #define D_AS3935_NOMESS "lausche..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/el_GR.h b/tasmota/language/el_GR.h index 8e3653957..74a7b625f 100644 --- a/tasmota/language/el_GR.h +++ b/tasmota/language/el_GR.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/en_GB.h b/tasmota/language/en_GB.h index c077dcb81..18d655ce3 100644 --- a/tasmota/language/en_GB.h +++ b/tasmota/language/en_GB.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ Pin flicker!" +#define D_AS3935_POWEROFF "Powerd Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/es_ES.h b/tasmota/language/es_ES.h index 10c0b1ac4..674b38eab 100644 --- a/tasmota/language/es_ES.h +++ b/tasmota/language/es_ES.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "Ruido detectado" #define D_AS3935_DISTDET "Perturbancia detectada" #define D_AS3935_INTNOEV "Interrupción sin evento!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "Escuchando..." #define D_AS3935_ON "Encendido" #define D_AS3935_OFF "Apagado" diff --git a/tasmota/language/fr_FR.h b/tasmota/language/fr_FR.h index 28310cc52..be1fd4f93 100644 --- a/tasmota/language/fr_FR.h +++ b/tasmota/language/fr_FR.h @@ -814,6 +814,8 @@ #define D_AS3935_NOISE "bruit détecté" #define D_AS3935_DISTDET "interférence détectée" #define D_AS3935_INTNOEV "Interruption sans évenement!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "en écoute..." #define D_AS3935_ON "Actif" #define D_AS3935_OFF "Inactif" diff --git a/tasmota/language/he_HE.h b/tasmota/language/he_HE.h index dd0f2fbee..0de84e609 100644 --- a/tasmota/language/he_HE.h +++ b/tasmota/language/he_HE.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/hu_HU.h b/tasmota/language/hu_HU.h index 79d7c40aa..23a7be121 100644 --- a/tasmota/language/hu_HU.h +++ b/tasmota/language/hu_HU.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/it_IT.h b/tasmota/language/it_IT.h index 34f5a1696..60792b77f 100644 --- a/tasmota/language/it_IT.h +++ b/tasmota/language/it_IT.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "rilevato rumore" #define D_AS3935_DISTDET "rilevato disturbatore" #define D_AS3935_INTNOEV "Interrupt senza evento!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "in ascolto..." #define D_AS3935_ON "ON" #define D_AS3935_OFF "OFF" diff --git a/tasmota/language/ko_KO.h b/tasmota/language/ko_KO.h index ef1f781b2..d08aea216 100644 --- a/tasmota/language/ko_KO.h +++ b/tasmota/language/ko_KO.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/nl_NL.h b/tasmota/language/nl_NL.h index a3a5821fd..6a40607c9 100644 --- a/tasmota/language/nl_NL.h +++ b/tasmota/language/nl_NL.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/pl_PL.h b/tasmota/language/pl_PL.h index 929774738..7e9d25e5c 100644 --- a/tasmota/language/pl_PL.h +++ b/tasmota/language/pl_PL.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/pt_BR.h b/tasmota/language/pt_BR.h index 990b308cb..88f1d0c86 100644 --- a/tasmota/language/pt_BR.h +++ b/tasmota/language/pt_BR.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/pt_PT.h b/tasmota/language/pt_PT.h index a976811c3..83bbbc778 100644 --- a/tasmota/language/pt_PT.h +++ b/tasmota/language/pt_PT.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/ro_RO.h b/tasmota/language/ro_RO.h index c4cba2ee3..00ab45bcf 100644 --- a/tasmota/language/ro_RO.h +++ b/tasmota/language/ro_RO.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/ru_RU.h b/tasmota/language/ru_RU.h index f9e576f70..13068198f 100644 --- a/tasmota/language/ru_RU.h +++ b/tasmota/language/ru_RU.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/sk_SK.h b/tasmota/language/sk_SK.h index 072caa53d..911581ee7 100644 --- a/tasmota/language/sk_SK.h +++ b/tasmota/language/sk_SK.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/sv_SE.h b/tasmota/language/sv_SE.h index 1ba0b0b7e..73ecd4bab 100644 --- a/tasmota/language/sv_SE.h +++ b/tasmota/language/sv_SE.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/tr_TR.h b/tasmota/language/tr_TR.h index 928ad30b6..e2b496d1e 100644 --- a/tasmota/language/tr_TR.h +++ b/tasmota/language/tr_TR.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/uk_UA.h b/tasmota/language/uk_UA.h index 8ce7ee877..388aecc9a 100644 --- a/tasmota/language/uk_UA.h +++ b/tasmota/language/uk_UA.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/zh_CN.h b/tasmota/language/zh_CN.h index b1b58b4c4..220f866e9 100644 --- a/tasmota/language/zh_CN.h +++ b/tasmota/language/zh_CN.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "noise detected" #define D_AS3935_DISTDET "disturber detected" #define D_AS3935_INTNOEV "Interrupt with no Event!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "listening..." #define D_AS3935_ON "On" #define D_AS3935_OFF "Off" diff --git a/tasmota/language/zh_TW.h b/tasmota/language/zh_TW.h index fc9606a90..aca69ad6b 100644 --- a/tasmota/language/zh_TW.h +++ b/tasmota/language/zh_TW.h @@ -823,6 +823,8 @@ #define D_AS3935_NOISE "偵測到雜訊" #define D_AS3935_DISTDET "偵測到干擾物" #define D_AS3935_INTNOEV "沒有任何事件觸發中斷!" +#define D_AS3935_FLICKER "IRQ flicker!" +#define D_AS3935_POWEROFF "Power Off" #define D_AS3935_NOMESS "聽取中..." #define D_AS3935_ON "開啟" #define D_AS3935_OFF "關閉" diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 5f94c5fc0..067490f14 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -552,7 +552,7 @@ // #define WEMOS_MOTOR_V1_FREQ 1000 // Default frequency // #define USE_HDC1080 // [I2cDriver45] Enable HDC1080 temperature/humidity sensor (I2C address 0x40) (+1k5 code) // #define USE_IAQ // [I2cDriver46] Enable iAQ-core air quality sensor (I2C address 0x5a) (+0k6 code) -// #define USE_AS3935 // [I2cDriver48] Enable AS3935 Franklin Lightning Sensor (I2C address 0x03) (+5k4 code) + #define USE_AS3935 // [I2cDriver48] Enable AS3935 Franklin Lightning Sensor (I2C address 0x03) (+5k4 code) // #define USE_VEML6075 // [I2cDriver49] Enable VEML6075 UVA/UVB/UVINDEX Sensor (I2C address 0x10) (+2k1 code) // #define USE_VEML7700 // [I2cDriver50] Enable VEML7700 Ambient Light sensor (I2C addresses 0x10) (+4k5 code) // #define USE_MCP9808 // [I2cDriver51] Enable MCP9808 temperature sensor (I2C addresses 0x18 - 0x1F) (+0k9 code) diff --git a/tasmota/settings.h b/tasmota/settings.h index d7a526d41..60c837364 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -282,9 +282,9 @@ typedef union { struct { uint8_t nf_autotune : 1; // Autotune the NF Noise Level uint8_t dist_autotune : 1; // Autotune Disturber on/off - uint8_t nf_autotune_both : 1; // Autotune over both Areas: INDOORS/OUDOORS + uint8_t nf_autotune_both : 1; // Autotune over both Areas: INDOORS/OUDOORS uint8_t mqtt_only_Light_Event : 1; // mqtt only if lightning Irq - uint8_t spare4 : 1; + uint8_t suppress_irq_no_Event : 1; // suppress mqtt "IRQ with no Event". (Chip Bug) uint8_t spare5 : 1; uint8_t spare6 : 1; uint8_t spare7 : 1; diff --git a/tasmota/tasmota_template.h b/tasmota/tasmota_template.h index f6d28e63d..32a914f8f 100644 --- a/tasmota/tasmota_template.h +++ b/tasmota/tasmota_template.h @@ -228,7 +228,7 @@ enum UserSelectablePins { GPIO_CC1101_GDO2, // CC1101 pin for RX GPIO_HRXL_RX, // Data from MaxBotix HRXL sonar range sensor GPIO_ELECTRIQ_MOODL_TX, // ElectriQ iQ-wifiMOODL Serial TX - GPIO_AS3935, + GPIO_AS3935, // Franklin Lightning Sensor GPIO_PMS5003_TX, // Plantower PMS5003 Serial interface GPIO_BOILER_OT_RX, // OpenTherm Boiler RX pin GPIO_BOILER_OT_TX, // OpenTherm Boiler TX pin @@ -710,7 +710,7 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_DYP_RX, #endif #ifdef USE_AS3935 - GPIO_AS3935, + GPIO_AS3935, // AS3935 IRQ Pin #endif #ifdef USE_TELEINFO GPIO_TELEINFO_RX, diff --git a/tasmota/tasmota_template_ESP32.h b/tasmota/tasmota_template_ESP32.h index 75e8e5a70..f0f59b488 100644 --- a/tasmota/tasmota_template_ESP32.h +++ b/tasmota/tasmota_template_ESP32.h @@ -111,7 +111,7 @@ enum UserSelectablePins { GPIO_CC1101_GDO0, GPIO_CC1101_GDO2, // CC1101 Serial interface GPIO_HRXL_RX, // Data from MaxBotix HRXL sonar range sensor GPIO_ELECTRIQ_MOODL_TX, // ElectriQ iQ-wifiMOODL Serial TX - GPIO_AS3935, + GPIO_AS3935, // Franklin Lightning Sensor GPIO_ADC_INPUT, // Analog input GPIO_ADC_TEMP, // Analog Thermistor GPIO_ADC_LIGHT, // Analog Light sensor @@ -557,7 +557,7 @@ const uint16_t kGpioNiceList[] PROGMEM = { AGPIO(GPIO_DYP_RX), #endif #ifdef USE_AS3935 - AGPIO(GPIO_AS3935), + AGPIO(GPIO_AS3935), // AS3935 IRQ Pin #endif #ifdef USE_TELEINFO AGPIO(GPIO_TELEINFO_RX), diff --git a/tasmota/xsns_67_as3935.ino b/tasmota/xsns_67_as3935.ino index 3723079b4..540b59ce0 100644 --- a/tasmota/xsns_67_as3935.ino +++ b/tasmota/xsns_67_as3935.ino @@ -31,15 +31,8 @@ #define D_NAME_AS3935 "AS3935" #define AS3935_ADDR 0x03 -// Reg mask shift -#define IRQ_TBL 0x03, 0x0F, 0 -#define ENERGY_RAW_1 0x04, 0xFF, 0 -#define ENERGY_RAW_2 0x05, 0xFF, 0 -#define ENERGY_RAW_3 0x06, 0x1F, 0 -#define LGHT_DIST 0x07, 0x3F, 0 -#define DISP_TRCO 0x08, 0x20, 5 -#define DISP_LCO 0x08, 0x80, 7 -#define TUNE_CAPS 0x08, 0x0F, 0 +// I2C Registers Reg mask shift +#define PWR_REG 0x00, 0x01, 0 #define AFE_GB 0x00, 0x3E, 0 #define WDTH 0x01, 0x0F, 0 #define NF_LEVEL 0x01, 0x70, 4 @@ -47,12 +40,32 @@ #define MIN_NUM_LIGH 0x02, 0x30, 4 #define DISTURBER 0x03, 0x20, 5 #define LCO_FDIV 0x03, 0xC0, 6 +#define IRQ_TBL 0x03, 0x0F, 0 +#define ENERGY_RAW_1 0x04, 0xFF, 0 +#define ENERGY_RAW_2 0x05, 0xFF, 0 +#define ENERGY_RAW_3 0x06, 0x1F, 0 +#define LGHT_DIST 0x07, 0x3F, 0 +#define DISP_TRCO 0x08, 0x20, 5 // should 31.250 kHz with devide 16 +#define DISP_SRCO 0x08, 0x40, 6 // 1,1 MHz +#define DISP_LCO 0x08, 0x80, 7 // 32.768 kHz +#define TUNE_CAPS 0x08, 0x0F, 0 +#define CAL_TRCO_NOK 0x3A, 0x40, 6 // 1 = NOK +#define CAL_TRCO_DONE 0x3A, 0x80, 7 // 0 = OK +#define CAL_SRCO_NOK 0x3B, 0x40, 6 +#define CAL_SRCO_DONE 0x3B, 0x80, 7 +// I2C Commands +#define RESET_DEFAULT 0x3C, 0x96 +#define CALIBATE_RCO 0x3D, 0x96 + +// NF-Level #define INDOORS 0x24 #define OUTDOORS 0x1C -// Global -const char HTTP_SNS_UNIT_KILOMETER[] PROGMEM = D_UNIT_KILOMETER; +// Load Settings Mask +#define SETREG00MASK 0x3E // For Power On +#define SETREG03MASK 0xF0 // For LCO and Disturber + // Http const char HTTP_SNS_AS3935_ENERGY[] PROGMEM = "{s}" D_NAME_AS3935 " " D_AS3935_ENERGY " {m}%d{e}"; const char HTTP_SNS_AS3935_DISTANZ[] PROGMEM = "{s}" D_NAME_AS3935 " " D_AS3935_DISTANCE " {m}%u " D_UNIT_KILOMETER "{e}"; @@ -66,15 +79,18 @@ const char HTTP_SNS_AS3935_DIST_ON[] PROGMEM = "{s}%s " D_AS3935_DISTURBER " {m} const char HTTP_SNS_AS3935_DIST_OFF[] PROGMEM = "{s}%s " D_AS3935_DISTURBER " {m}" D_AS3935_OFF " {e}"; const char* const HTTP_SNS_AS3935_DISTURBER[] PROGMEM = {HTTP_SNS_AS3935_DIST_OFF, HTTP_SNS_AS3935_DIST_ON}; // http Messages -const char HTTP_SNS_AS3935_EMPTY[] PROGMEM = "{s}%s: " D_AS3935_NOMESS "{e}"; -const char HTTP_SNS_AS3935_OUT[] PROGMEM = "{s}%s: " D_AS3935_OUT "{e}"; -const char HTTP_SNS_AS3935_NOT[] PROGMEM = "{s}%s: " D_AS3935_NOT "{e}"; -const char HTTP_SNS_AS3935_ABOVE[] PROGMEM = "{s}%s: " D_AS3935_ABOVE "{e}"; -const char HTTP_SNS_AS3935_NOISE[] PROGMEM = "{s}%s: " D_AS3935_NOISE "{e}"; -const char HTTP_SNS_AS3935_DISTURB[] PROGMEM = "{s}%s: " D_AS3935_DISTDET "{e}"; -const char HTTP_SNS_AS3935_INTNOEV[] PROGMEM = "{s}%s: " D_AS3935_INTNOEV "{e}"; -const char HTTP_SNS_AS3935_MSG[] PROGMEM = "{s}%s: " D_AS3935_LIGHT " " D_AS3935_APRX " %d " D_UNIT_KILOMETER " " D_AS3935_AWAY "{e}"; -const char* const HTTP_SNS_AS3935_TABLE_1[] PROGMEM = { HTTP_SNS_AS3935_EMPTY, HTTP_SNS_AS3935_MSG, HTTP_SNS_AS3935_OUT, HTTP_SNS_AS3935_NOT, HTTP_SNS_AS3935_ABOVE, HTTP_SNS_AS3935_NOISE, HTTP_SNS_AS3935_DISTURB, HTTP_SNS_AS3935_INTNOEV }; +const char HTTP_SNS_AS3935_EMPTY[] PROGMEM = "{s}%s " D_AS3935_NOMESS "{e}"; +const char HTTP_SNS_AS3935_OUT[] PROGMEM = "{s}%s " D_AS3935_OUT "{e}"; +const char HTTP_SNS_AS3935_NOT[] PROGMEM = "{s}%s " D_AS3935_NOT "{e}"; +const char HTTP_SNS_AS3935_ABOVE[] PROGMEM = "{s}%s " D_AS3935_ABOVE "{e}"; +const char HTTP_SNS_AS3935_NOISE[] PROGMEM = "{s}%s " D_AS3935_NOISE "{e}"; +const char HTTP_SNS_AS3935_DISTURB[] PROGMEM = "{s}%s " D_AS3935_DISTDET "{e}"; +const char HTTP_SNS_AS3935_INTNOEV[] PROGMEM = "{s}%s " D_AS3935_INTNOEV "{e}"; +const char HTTP_SNS_AS3935_FLICKER[] PROGMEM = "{s}%s " D_AS3935_FLICKER "{e}"; +const char HTTP_SNS_AS3935_POWEROFF[] PROGMEM = "{s}%s " D_AS3935_POWEROFF "{e}"; + +const char HTTP_SNS_AS3935_MSG[] PROGMEM = "{s}%s " D_AS3935_LIGHT " " D_AS3935_APRX " %d " D_UNIT_KILOMETER " " D_AS3935_AWAY "{e}"; +const char* const HTTP_SNS_AS3935_TABLE_1[] PROGMEM = { HTTP_SNS_AS3935_EMPTY, HTTP_SNS_AS3935_MSG, HTTP_SNS_AS3935_OUT, HTTP_SNS_AS3935_NOT, HTTP_SNS_AS3935_ABOVE, HTTP_SNS_AS3935_NOISE, HTTP_SNS_AS3935_DISTURB, HTTP_SNS_AS3935_INTNOEV, HTTP_SNS_AS3935_FLICKER, HTTP_SNS_AS3935_POWEROFF }; // Json const char JSON_SNS_AS3935_EVENTS[] PROGMEM = ",\"%s\":{\"" D_JSON_EVENT "\":%d,\"" D_JSON_DISTANCE "\":%d,\"" D_JSON_ENERGY "\":%u,\"" D_JSON_STAGE "\":%d}"; // Json Command @@ -84,13 +100,17 @@ const char* const S_JSON_AS3935_COMMAND_CAL[] PROGMEM = {"" D_AS3935_CAL_FAIL "" const char S_JSON_AS3935_COMMAND_STRING[] PROGMEM = "{\"" D_NAME_AS3935 "\":{\"%s\":%s}}"; const char S_JSON_AS3935_COMMAND_NVALUE[] PROGMEM = "{\"" D_NAME_AS3935 "\":{\"%s\":%d}}"; -const char S_JSON_AS3935_COMMAND_SETTINGS[] PROGMEM = "{\"" D_NAME_AS3935 "\":{\"Gain\":%s,\"NFfloor\":%d,\"uVrms\":%d,\"Tunecaps\":%d,\"MinNumLight\":%d,\"Rejektion\":%d,\"Wdthreshold\":%d,\"MinNFstage\":%d,\"NFAutoTime\":%d,\"DisturberAutoTime\":%d,\"Disturber\":%s,\"NFauto\":%s,\"Disturberauto\":%s,\"NFautomax\":%s,\"Mqttlightevent\":%s}}"; +const char S_JSON_AS3935_COMMAND_SETTINGS[] PROGMEM = "{\"AS3935_Settings\":{\"Gain\":%s,\"NFfloor\":%d,\"uVrms\":%d,\"Tunecaps\":%d,\"MinNumLight\":%d,\"Rejektion\":%d,\"Wdthreshold\":%d,\"MinNFstage\":%d,\"NFAutoTime\":%d,\"DisturberAutoTime\":%d,\"Disturber\":%s,\"NFauto\":%s,\"Disturberauto\":%s,\"NFautomax\":%s,\"Mqttlightevent\":%s,\"Mqttnoirqevent\":%s}}"; -const char kAS3935_Commands[] PROGMEM = "setnf|setminstage|setml|default|setgain|settunecaps|setrej|setwdth|disttime|nftime|disturber|autonf|autodisturber|autonfmax|mqttevent|settings|calibrate"; +const char kAS3935_Commands[] PROGMEM = "power|setnf|setminstage|setml|default|setgain|settunecaps|setrej|setwdth|disttime|nftime|disturber|autonf|autodisturber|autonfmax|lightevent|noirqevent|settings|calibrate"; + +const uint8_t AS3935_VrmsIndoor[] PROGMEM = { 28, 45, 62, 78, 95, 112, 130, 146 }; +const uint16_t AS3935_VrmsOutdoor[] PROGMEM = { 390, 630, 860, 1100, 1140, 1570, 1800, 2000 }; enum AS3935_Commands { // commands for Console + CMND_AS3935_POWER, // Power on/off the device (1 Bit) CMND_AS3935_SET_NF, // Noise Floor Level, value from 0-7 (3 Bit) - CMND_AS3935_SET_MINNF, // Set Min Noise Floor Level when Autotune is active Value von 0-15 + CMND_AS3935_SET_MINNF, // Set Min Noise Floor Level when Autotune is active Value from 0-15 CMND_AS3935_SET_MINLIGHT, // Minimum number of lightning 0=1/1=5/2=9/3=16 Lightnings CMND_AS3935_SET_DEF, // set default for Sensor and Settings CMND_AS3935_SET_GAIN, // Set Inddoor/Outdoor @@ -104,24 +124,25 @@ enum AS3935_Commands { // commands for Console CMND_AS3935_DIST_AUTOTUNE, // Autotune Disturber on/off CMND_AS3935_NF_ATUNE_BOTH, // Autotune over both Areas: INDOORS/OUDOORS CMND_AS3935_MQTT_LIGHT_EVT, // mqtt only if lightning Irq + CMND_AS3935_MQTT_NO_IRQ_EVT, // suppress mqtt "IRQ with no Event" CMND_AS3935_SETTINGS, // Json output of all settings CMND_AS3935_CALIBRATE // caps autocalibrate - }; +}; -struct AS3935STRUCT -{ - bool autodist_activ = false; +struct { + bool active = false; + bool http_count_start = false; + bool poweroff = false; volatile bool detected = false; - volatile bool dispLCO = 0; - uint8_t icount = 0; + volatile bool dispLCO = false; + volatile uint8_t icount = 0; uint8_t irq = 0; - uint8_t mqtt_irq = 0; - uint8_t http_irq = 0; - uint8_t http_count_start = 0; + uint8_t mqtt_event = 0; + uint8_t http_event = 0; + uint8_t http_time = 0; + uint8_t http_count = 0; int16_t http_distance = 0; int16_t distance = 0; - uint16_t http_timer = 0; - uint16_t http_count = 0; uint16_t nftimer = 0; uint16_t disttimer = 0; uint32_t intensity = 0; @@ -129,16 +150,16 @@ struct AS3935STRUCT volatile uint32_t pulse = 0; } as3935_sensor; -uint8_t as3935_active = 0; - -void ICACHE_RAM_ATTR AS3935Isr() { +void ICACHE_RAM_ATTR AS3935Isr(void) { as3935_sensor.detected = true; + as3935_sensor.icount++; } +// we have to store 5 Bytes in the eeprom. Register 8 is mapped to Byte 4 uint8_t AS3935ReadRegister(uint8_t reg, uint8_t mask, uint8_t shift) { uint8_t data = I2cRead8(AS3935_ADDR, reg); if (reg == 0x08) Settings.as3935_sensor_cfg[4] = data; - if (reg < 0x04) Settings.as3935_sensor_cfg[reg] = data; + if (reg <= 0x03) Settings.as3935_sensor_cfg[reg] = data; return ((data & mask) >> shift); } @@ -150,12 +171,12 @@ void AS3935WriteRegister(uint8_t reg, uint8_t mask, uint8_t shift, uint8_t data) data |= currentReg; I2cWrite8(AS3935_ADDR, reg, data); if (reg == 0x08) Settings.as3935_sensor_cfg[4] = I2cRead8(AS3935_ADDR, reg); - if (reg < 0x04) Settings.as3935_sensor_cfg[reg] = I2cRead8(AS3935_ADDR, reg); + if (reg <= 0x03) Settings.as3935_sensor_cfg[reg] = I2cRead8(AS3935_ADDR, reg); } /********************************************************************************************/ // Autotune Caps -void ICACHE_RAM_ATTR AS3935CountFreq() { +void ICACHE_RAM_ATTR AS3935CountFreq(void) { if (as3935_sensor.dispLCO) as3935_sensor.pulse++; } @@ -163,52 +184,161 @@ void ICACHE_RAM_ATTR AS3935CountFreq() { bool AS3935AutoTuneCaps(uint8_t irqpin) { int32_t maxtune = 17500; // there max 3.5 % tol uint8_t besttune; + uint8_t oldvalue = AS3935GetTuneCaps(); AS3935WriteRegister(LCO_FDIV, 0); // Fdiv 16 delay(2); for (uint8_t tune = 0; tune < 16; tune++) { AS3935WriteRegister(TUNE_CAPS, tune); delay(2); - AS3935WriteRegister(DISP_LCO,1); + AS3935WriteRegister(DISP_LCO, 1); delay(1); as3935_sensor.dispLCO = true; as3935_sensor.pulse = 0; attachInterrupt(digitalPinToInterrupt(irqpin), AS3935CountFreq, RISING); - delay(200); // 100ms callback not work accurat for fequ. measure + delay(50); as3935_sensor.dispLCO = false; detachInterrupt(irqpin); - AS3935WriteRegister(DISP_LCO,0); - int32_t currentfreq = 500000 - ((as3935_sensor.pulse * 5) * 16); + AS3935WriteRegister(DISP_LCO, 0); + int32_t currentfreq = 500000 - ((as3935_sensor.pulse * 20) * 16); if(currentfreq < 0) currentfreq = -currentfreq; if(maxtune > currentfreq) { maxtune = currentfreq; besttune = tune; } } - if (maxtune >= 17500) // max. 3.5% + if (maxtune >= 17500) { // max. 3.5% + AS3935SetTuneCaps(oldvalue); return false; + } AS3935SetTuneCaps(besttune); return true; } /********************************************************************************************/ // functions -void AS3935CalibrateRCO() { - I2cWrite8(AS3935_ADDR, 0x3D, 0x96); - AS3935WriteRegister(DISP_TRCO, 1); +bool AS3935CalRCOResult(void) { + if(AS3935ReadRegister(CAL_SRCO_NOK) || AS3935ReadRegister(CAL_TRCO_NOK)) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("I2C: AS3935 Fatal Failure of TRCO or SRCO calibration")); + return false; + } + return true; +} + +bool AS3935CalibrateRCO(void) { + detachInterrupt(Pin(GPIO_AS3935)); // Prevent AS3935Isr from RCO Calibration + I2cWrite8(AS3935_ADDR, CALIBATE_RCO); // Cal TRCO & SRCO + AS3935WriteRegister(DISP_TRCO, 1); // need for Power up delay(2); AS3935WriteRegister(DISP_TRCO, 0); + if(!AS3935CalRCOResult()) + return false; + attachInterrupt(digitalPinToInterrupt(Pin(GPIO_AS3935)), AS3935Isr, RISING); + return true; +} + +void AS3935Reset(void) { + I2cWrite8(AS3935_ADDR, RESET_DEFAULT); + delay(2); +} + +void AS3935PwrDown(void) { + AS3935WriteRegister(PWR_REG ,1); + detachInterrupt(Pin(GPIO_AS3935)); + as3935_sensor.poweroff = true; + as3935_sensor.mqtt_event = 9; + as3935_sensor.http_event = 9; + as3935_sensor.intensity = 0; + as3935_sensor.distance = 0; +} + +void AS3935PwrUp(void) { + AS3935WriteRegister(PWR_REG ,0); + AS3935CalibrateRCO(); + as3935_sensor.poweroff = false; + as3935_sensor.mqtt_event = 0; + as3935_sensor.http_event = 0; +} + +uint8_t AS3935GetPwrStat(void) { + if (AS3935ReadRegister(PWR_REG)) + return 0; + return 1; +} + +uint8_t AS3935GetIRQ(void) { + delay(2); + return AS3935ReadRegister(IRQ_TBL); +} + +uint8_t AS3935TranslIrq(uint8_t irq, uint8_t distance) { + switch(irq) { + case 0: return 7; // Interrupt with no IRQ + case 1: return 5; // Noise level too high + case 4: return 6; // Disturber detected + case 8: + if (distance == -1) return 2; // Lightning out of Distance + else if (distance == 0) return 3; // Distance cannot be determined + else if (distance == 1) return 4; // Storm is Overhead + else return 1; // Lightning with Distance detected + } + return 0; // Fix GCC 10.1 warning +} + +uint8_t AS3935GetDistance(void) { + return AS3935ReadRegister(LGHT_DIST); +} + +int16_t AS3935CalcDistance(void) { + uint8_t dist = AS3935GetDistance(); + switch (dist) { + case 0x3F: return -1; // Out of Range + case 0x00: return 0; // Distance cannot be determined + case 0x01: return 1; // Storm is Overhead + default: + if (40 < dist) return 40; // limited because higher is not accurate + return dist; + } +} + +uint32_t AS3935GetIntensity(void) { + uint32_t energy_raw = (AS3935ReadRegister(ENERGY_RAW_3) << 8); + energy_raw |= AS3935ReadRegister(ENERGY_RAW_2); + energy_raw <<= 8; + energy_raw |= AS3935ReadRegister(ENERGY_RAW_1); + return energy_raw; +} + +uint8_t AS3935GetTuneCaps(void) { + return AS3935ReadRegister(TUNE_CAPS); +} + +void AS3935SetTuneCaps(uint8_t tune) { + AS3935WriteRegister(TUNE_CAPS, tune); + delay(2); + AS3935CalibrateRCO(); +} + +uint8_t AS3935GetDisturber(void) { + return AS3935ReadRegister(DISTURBER); +} + +void AS3935SetDisturber(uint8_t stat) { + AS3935WriteRegister(DISTURBER, stat); +} + +uint8_t AS3935GetMinLights(void) { + return AS3935ReadRegister(MIN_NUM_LIGH); +} + +void AS3935SetMinLights(uint8_t stat) { + AS3935WriteRegister(MIN_NUM_LIGH, stat); } uint8_t AS3935TransMinLights(uint8_t min_lights) { - if (5 > min_lights) { - return 0; - } else if (9 > min_lights) { - return 1; - } else if (16 > min_lights) { - return 2; - } else { - return 3; - } + if (5 > min_lights) return 0; + else if (9 > min_lights) return 1; + else if (16 > min_lights) return 2; + else return 3; } uint8_t AS3935TranslMinLightsInt(uint8_t min_lights) { @@ -221,146 +351,7 @@ uint8_t AS3935TranslMinLightsInt(uint8_t min_lights) { return 0; // Fix GCC 10.1 warning } -uint8_t AS3935TranslIrq(uint8_t irq, uint8_t distance) { - switch(irq) { - case 0: return 7; // Interrupt with no IRQ - case 1: return 5; // Noise level too high - case 4: return 6; // Disturber detected - case 8: - if (distance == -1) return 2; // Lightning out of Distance - else if (distance == 0) return 3; // Distance cannot be determined - else if (distance == 1) return 4; // Storm is Overhead - else return 1; // Lightning with Distance detected - } - return 0; // Fix GCC 10.1 warning -} - -void AS3935CalcVrmsLevel(uint16_t &vrms, uint8_t &stage) -{ - uint8_t room = AS3935GetGain(); - uint8_t nflev = AS3935GetNoiseFloor(); - if (room == 0x24) - { - switch (nflev){ - case 0x00: - vrms = 28; - break; - case 0x01: - vrms = 45; - break; - case 0x02: - vrms = 62; - break; - case 0x03: - vrms = 78; - break; - case 0x04: - vrms = 95; - break; - case 0x05: - vrms = 112; - break; - case 0x06: - vrms = 130; - break; - case 0x07: - vrms = 146; - break; - } - stage = nflev; - } - else - { - switch (nflev) - { - case 0x00: - vrms = 390; - break; - case 0x01: - vrms = 630; - break; - case 0x02: - vrms = 860; - break; - case 0x03: - vrms = 1100; - break; - case 0x04: - vrms = 1140; - break; - case 0x05: - vrms = 1570; - break; - case 0x06: - vrms = 1800; - break; - case 0x07: - vrms = 2000; - break; - } - stage = nflev + 8; - } -} - -/********************************************************************************************/ -uint8_t AS3935GetIRQ() { - delay(2); - return AS3935ReadRegister(IRQ_TBL); -} - -uint8_t AS3935GetDistance() { - return AS3935ReadRegister(LGHT_DIST); -} - -int16_t AS3935CalcDistance() { - uint8_t dist = AS3935GetDistance(); - switch (dist) { - case 0x3F: return -1; // Out of Range - case 0x01: return 1; // Storm is Overhead - case 0x00: return 0; // Distance cannot be determined - default: - if (40 < dist){ - return 40;// limited because higher is not accurate - } - return dist; - } -} - -uint32_t AS3935GetIntensity() { - uint32_t nrgy_raw = (AS3935ReadRegister(ENERGY_RAW_3) << 8); - nrgy_raw |= AS3935ReadRegister(ENERGY_RAW_2); - nrgy_raw <<= 8; - nrgy_raw |= AS3935ReadRegister(ENERGY_RAW_1); - return nrgy_raw; -} - -uint8_t AS3935GetTuneCaps() { - return AS3935ReadRegister(TUNE_CAPS); -} - -void AS3935SetTuneCaps(uint8_t tune) { - AS3935WriteRegister(TUNE_CAPS, tune); - delay(2); - AS3935CalibrateRCO(); -} - -uint8_t AS3935GetDisturber() { - return AS3935ReadRegister(DISTURBER); -} - -void AS3935SetDisturber(uint8_t stat) { - AS3935WriteRegister(DISTURBER, stat); -} - -uint8_t AS3935GetMinLights() { - return AS3935ReadRegister(MIN_NUM_LIGH); -} - -void AS3935SetMinLights(uint8_t stat) { - AS3935WriteRegister(MIN_NUM_LIGH, stat); -} - -uint8_t AS3935GetNoiseFloor() { +uint8_t AS3935GetNoiseFloor(void) { return AS3935ReadRegister(NF_LEVEL); } @@ -368,7 +359,7 @@ void AS3935SetNoiseFloor(uint8_t noise) { AS3935WriteRegister(NF_LEVEL , noise); } -uint8_t AS3935GetGain() { +uint8_t AS3935GetGain(void) { if (AS3935ReadRegister(AFE_GB) == OUTDOORS) return OUTDOORS; return INDOORS; @@ -378,13 +369,25 @@ void AS3935SetGain(uint8_t room) { AS3935WriteRegister(AFE_GB, room); } -uint8_t AS3935GetGainInt() { +uint8_t AS3935GetGainInt(void) { if (AS3935ReadRegister(AFE_GB) == OUTDOORS) - return 1; -return 0; + return 1; + return 0; } -uint8_t AS3935GetSpikeRejection() { +void AS3935CalcVrmsLevel(uint16_t &vrms, uint8_t &stage) { + uint8_t room = AS3935GetGain(); + uint8_t nflev = AS3935GetNoiseFloor(); + if (room == INDOORS) { + vrms = pgm_read_byte(AS3935_VrmsIndoor + nflev); + stage = nflev; + } else { + vrms = pgm_read_word(AS3935_VrmsOutdoor + nflev); + stage = nflev + 8; + } +} + +uint8_t AS3935GetSpikeRejection(void) { return AS3935ReadRegister(SPIKE_REJECT); } @@ -392,7 +395,7 @@ void AS3935SetSpikeRejection(uint8_t rej) { AS3935WriteRegister(SPIKE_REJECT, rej); } -uint8_t AS3935GetWdth() { +uint8_t AS3935GetWdth(void) { return AS3935ReadRegister(WDTH); } @@ -400,17 +403,15 @@ void AS3935SetWdth(uint8_t wdth) { AS3935WriteRegister(WDTH, wdth); } -bool AS3935AutoTune(){ +bool AS3935AutoTune(void) { detachInterrupt(Pin(GPIO_AS3935)); bool result = AS3935AutoTuneCaps(Pin(GPIO_AS3935)); - attachInterrupt(digitalPinToInterrupt(Pin(GPIO_AS3935)), AS3935Isr, RISING); return result; } /********************************************************************************************/ // Noise Floor autofunctions -bool AS3935LowerNoiseFloor() { - uint8_t noise = AS3935GetNoiseFloor(); +bool AS3935LowerNoiseFloor(void) { uint16_t vrms; uint8_t stage; AS3935CalcVrmsLevel(vrms, stage); @@ -421,6 +422,7 @@ bool AS3935LowerNoiseFloor() { return true; } } + uint8_t noise = AS3935GetNoiseFloor(); if (0 < noise && stage > Settings.as3935_parameter.nf_autotune_min) { noise--; AS3935SetNoiseFloor(noise); @@ -429,7 +431,7 @@ bool AS3935LowerNoiseFloor() { return false; } -bool AS3935RaiseNoiseFloor() { +bool AS3935RaiseNoiseFloor(void) { uint8_t noise = AS3935GetNoiseFloor(); uint8_t room = AS3935GetGain(); if (Settings.as3935_functions.nf_autotune_both) { @@ -449,22 +451,25 @@ bool AS3935RaiseNoiseFloor() { /********************************************************************************************/ // init functions -bool AS3935SetDefault() { - I2cWrite8(AS3935_ADDR, 0x3C, 0x96); // Set default - delay(2); +bool AS3935SetDefault(void) { + AS3935Reset(); + AS3935SetDisturber(1); // Disturber on by default + AS3935SetNoiseFloor(7); // NF High on by default Settings.as3935_sensor_cfg[0] = I2cRead8(AS3935_ADDR, 0x00); Settings.as3935_sensor_cfg[1] = I2cRead8(AS3935_ADDR, 0x01); Settings.as3935_sensor_cfg[2] = I2cRead8(AS3935_ADDR, 0x02); Settings.as3935_sensor_cfg[3] = I2cRead8(AS3935_ADDR, 0x03); Settings.as3935_sensor_cfg[4] = I2cRead8(AS3935_ADDR, 0x08); + // set all eeprom functions and values to default + Settings.as3935_functions.data = 0x00; Settings.as3935_parameter.nf_autotune_min = 0x00; Settings.as3935_parameter.nf_autotune_time = 4; Settings.as3935_parameter.dist_autotune_time = 1; return true; } -void AS3935InitSettings() { - if(Settings.as3935_functions.nf_autotune){ +void AS3935InitSettings(void) { + if(Settings.as3935_functions.nf_autotune) { if(Settings.as3935_parameter.nf_autotune_min) { if (Settings.as3935_parameter.nf_autotune_min > 7) { AS3935SetGain(OUTDOORS); @@ -475,114 +480,137 @@ void AS3935InitSettings() { } } } - I2cWrite8(AS3935_ADDR, 0x00, Settings.as3935_sensor_cfg[0]); + I2cWrite8(AS3935_ADDR, 0x00, Settings.as3935_sensor_cfg[0] & SETREG00MASK); I2cWrite8(AS3935_ADDR, 0x01, Settings.as3935_sensor_cfg[1]); I2cWrite8(AS3935_ADDR, 0x02, Settings.as3935_sensor_cfg[2]); - I2cWrite8(AS3935_ADDR, 0x03, Settings.as3935_sensor_cfg[3]); + I2cWrite8(AS3935_ADDR, 0x03, Settings.as3935_sensor_cfg[3] & SETREG03MASK); I2cWrite8(AS3935_ADDR, 0x08, Settings.as3935_sensor_cfg[4]); delay(2); } -void AS3935Setup(void) { +bool AS3935Setup(void) { if (Settings.as3935_sensor_cfg[0] == 0x00) { AS3935SetDefault(); } else { AS3935InitSettings(); } - AS3935CalibrateRCO(); + return AS3935CalibrateRCO(); } -bool AS3935init() { - uint8_t ret = I2cRead8(AS3935_ADDR, 0x00); - if(INDOORS == ret || OUTDOORS == ret) // 0x24 +bool AS3935init(void) { + AS3935Reset(); + uint8_t afe_gb = I2cRead8(AS3935_ADDR, 0x00) & SETREG00MASK; + if(INDOORS == afe_gb) return true; return false; } void AS3935Detect(void) { - if (I2cActive(AS3935_ADDR)) return; - if (AS3935init()) - { + if (!I2cSetDevice(AS3935_ADDR)) return; + if (AS3935init()) { I2cSetActiveFound(AS3935_ADDR, D_NAME_AS3935); - pinMode(Pin(GPIO_AS3935), INPUT); - attachInterrupt(digitalPinToInterrupt(Pin(GPIO_AS3935)), AS3935Isr, RISING); - AS3935Setup(); - as3935_active = 1; + if (PinUsed(GPIO_AS3935)) { + pinMode(Pin(GPIO_AS3935), INPUT); + if (!AS3935Setup()) return; + as3935_sensor.active = true; + } else { + AddLog_P2(LOG_LEVEL_INFO, PSTR("I2C: AS3935 GPIO Pin not defined!")); + } } } -void AS3935EverySecond() { - if (as3935_sensor.detected) { - as3935_sensor.irq = AS3935GetIRQ(); // 1 =Noise, 4 = Disturber, 8 = storm - switch (as3935_sensor.irq) { - case 1: - if (Settings.as3935_functions.nf_autotune) { - if (AS3935RaiseNoiseFloor()) as3935_sensor.nftimer = 0; +void AS3935EverySecond(void) { + if (!as3935_sensor.poweroff) { // Power Off + if (as3935_sensor.detected) { + as3935_sensor.detected = false; + as3935_sensor.irq = AS3935GetIRQ(); // 1 = Noise, 4 = Disturber, 8 = storm + + if (10 > as3935_sensor.icount) { + switch (as3935_sensor.irq) { + case 1: + if (Settings.as3935_functions.nf_autotune) { + if (AS3935RaiseNoiseFloor()) + as3935_sensor.nftimer = 0; + } + break; + case 4: + if (Settings.as3935_functions.dist_autotune) { + AS3935SetDisturber(1); + } + break; + case 8: + as3935_sensor.intensity = AS3935GetIntensity(); + as3935_sensor.distance = AS3935CalcDistance(); + as3935_sensor.http_intensity = as3935_sensor.intensity; + as3935_sensor.http_distance = as3935_sensor.distance; + break; } - break; - case 4: - if (Settings.as3935_functions.dist_autotune) { - AS3935SetDisturber(1); - as3935_sensor.autodist_activ = true; - } - break; - case 8: - as3935_sensor.intensity = AS3935GetIntensity(); - as3935_sensor.distance = AS3935CalcDistance(); - as3935_sensor.http_intensity = as3935_sensor.intensity; - as3935_sensor.http_distance = as3935_sensor.distance; - break; - } - // http show - as3935_sensor.http_irq = AS3935TranslIrq(as3935_sensor.irq, as3935_sensor.distance); - // mqtt publish - as3935_sensor.mqtt_irq = as3935_sensor.http_irq; - switch (as3935_sensor.mqtt_irq) { + // http show + as3935_sensor.http_event = AS3935TranslIrq(as3935_sensor.irq, as3935_sensor.distance); + } else { + as3935_sensor.http_event = 8; // flicker detected + } + + // mqtt publish + as3935_sensor.mqtt_event = as3935_sensor.http_event; + + switch (as3935_sensor.mqtt_event) { case 5: case 6: - if (!Settings.as3935_functions.mqtt_only_Light_Event) { + if (!Settings.as3935_functions.mqtt_only_Light_Event) { MqttPublishSensor(); - as3935_sensor.http_timer = 10; - } - break; - default: - as3935_sensor.http_timer = 60; + as3935_sensor.http_time = 10; + } + break; + case 7: + if (!Settings.as3935_functions.suppress_irq_no_Event) { + MqttPublishSensor(); + as3935_sensor.http_time = 10; + } + break; + default: + as3935_sensor.http_time = 30; MqttPublishSensor(); - } - // clear mqtt events for Teleperiod - as3935_sensor.intensity = 0; - as3935_sensor.distance = 0; - as3935_sensor.mqtt_irq = 0; - // start http times - as3935_sensor.http_count_start = 1; - as3935_sensor.http_count = 0; - as3935_sensor.icount++; // Int counter - as3935_sensor.detected = false; - } + } - if (as3935_sensor.http_count_start) as3935_sensor.http_count++; - // clear Http - if (as3935_sensor.http_count > as3935_sensor.http_timer) { - as3935_sensor.http_count_start = 0; - as3935_sensor.http_intensity = 0; - as3935_sensor.http_distance = 0; - as3935_sensor.http_irq = 0; - } - // Noise Floor Autotune function - if (Settings.as3935_functions.nf_autotune) { - as3935_sensor.nftimer++; - if (as3935_sensor.nftimer > Settings.as3935_parameter.nf_autotune_time * 60) { - AS3935LowerNoiseFloor(); - as3935_sensor.nftimer = 0; + as3935_sensor.irq = 0; + // clear mqtt events for Teleperiod + as3935_sensor.intensity = 0; + as3935_sensor.distance = 0; + as3935_sensor.mqtt_event = 0; + // start http times + as3935_sensor.http_count_start = true; + as3935_sensor.http_count = 0; } - } - // Disturber auto function - if (Settings.as3935_functions.dist_autotune) { - if (as3935_sensor.autodist_activ) as3935_sensor.disttimer++; - if (as3935_sensor.disttimer >= Settings.as3935_parameter.dist_autotune_time * 60) { - AS3935SetDisturber(0); - as3935_sensor.disttimer = 0; - as3935_sensor.autodist_activ = false; + as3935_sensor.icount = 0; + + // count http times + if (as3935_sensor.http_count_start) + as3935_sensor.http_count++; + // clear Http Event + if (as3935_sensor.http_count > as3935_sensor.http_time) { + as3935_sensor.http_count_start = false; + as3935_sensor.http_intensity = 0; + as3935_sensor.http_distance = 0; + as3935_sensor.http_event = 0; + } + // Noise Floor Autotune function + if (Settings.as3935_functions.nf_autotune) { + as3935_sensor.nftimer++; + if (as3935_sensor.nftimer > Settings.as3935_parameter.nf_autotune_time * 60) { + AS3935LowerNoiseFloor(); + as3935_sensor.nftimer = 0; + } + } + // Disturber auto function + if (Settings.as3935_functions.dist_autotune) { + if (AS3935GetDisturber()) { + as3935_sensor.disttimer++; + } + if (as3935_sensor.disttimer >= Settings.as3935_parameter.dist_autotune_time * 60) { + AS3935SetDisturber(0); + as3935_sensor.disttimer = 0; + } } } } @@ -593,6 +621,16 @@ bool AS3935Cmd(void) { if (!strncasecmp_P(XdrvMailbox.topic, PSTR(D_NAME_AS3935), name_len)) { uint32_t command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic + name_len, kAS3935_Commands); switch (command_code) { + case CMND_AS3935_POWER: + if (XdrvMailbox.data_len) { + if (!XdrvMailbox.payload) { + AS3935PwrDown(); + } else { + AS3935PwrUp(); + } + } + Response_P(S_JSON_AS3935_COMMAND_STRING, command, S_JSON_AS3935_COMMAND_ONOFF[AS3935GetPwrStat()]); + break; case CMND_AS3935_SET_NF: if (XdrvMailbox.data_len) { if (15 >= XdrvMailbox.payload) { @@ -712,6 +750,14 @@ bool AS3935Cmd(void) { } Response_P(S_JSON_AS3935_COMMAND_STRING, command, S_JSON_AS3935_COMMAND_ONOFF[Settings.as3935_functions.mqtt_only_Light_Event]); break; + case CMND_AS3935_MQTT_NO_IRQ_EVT: + if (XdrvMailbox.data_len) { + if (2 > XdrvMailbox.payload) { + Settings.as3935_functions.suppress_irq_no_Event = XdrvMailbox.payload; + } + } + Response_P(S_JSON_AS3935_COMMAND_STRING, command, S_JSON_AS3935_COMMAND_ONOFF[Settings.as3935_functions.suppress_irq_no_Event]); + break; case CMND_AS3935_SETTINGS: { if (!XdrvMailbox.data_len) { uint8_t gain = AS3935GetGainInt(); @@ -719,30 +765,31 @@ bool AS3935Cmd(void) { uint8_t stage; AS3935CalcVrmsLevel(vrms, stage); uint8_t nf_floor = AS3935GetNoiseFloor(); - uint8_t min_nf = Settings.as3935_parameter.nf_autotune_min; uint8_t tunecaps = AS3935GetTuneCaps(); uint8_t minnumlight = AS3935TranslMinLightsInt(AS3935GetMinLights()); uint8_t disturber = AS3935GetDisturber(); uint8_t reinj = AS3935GetSpikeRejection(); uint8_t wdth = AS3935GetWdth(); + uint8_t min_nf = Settings.as3935_parameter.nf_autotune_min; + uint8_t nf_time = Settings.as3935_parameter.nf_autotune_time; uint8_t nfauto = Settings.as3935_functions.nf_autotune; - uint8_t distauto = Settings.as3935_functions.dist_autotune; uint8_t nfautomax = Settings.as3935_functions.nf_autotune_both; + uint8_t distauto = Settings.as3935_functions.dist_autotune; uint8_t jsonlight = Settings.as3935_functions.mqtt_only_Light_Event; - uint8_t nf_time = Settings.as3935_parameter.nf_autotune_time; - uint8_t dist_time =Settings.as3935_parameter.dist_autotune_time; - Response_P(S_JSON_AS3935_COMMAND_SETTINGS, S_JSON_AS3935_COMMAND_GAIN[gain], nf_floor, vrms, tunecaps, minnumlight, reinj, wdth, min_nf, nf_time, dist_time, S_JSON_AS3935_COMMAND_ONOFF[disturber], S_JSON_AS3935_COMMAND_ONOFF[nfauto], S_JSON_AS3935_COMMAND_ONOFF[distauto], S_JSON_AS3935_COMMAND_ONOFF[nfautomax], S_JSON_AS3935_COMMAND_ONOFF[jsonlight]); + uint8_t jsonirq = Settings.as3935_functions.suppress_irq_no_Event; + uint8_t dist_time = Settings.as3935_parameter.dist_autotune_time; + Response_P(S_JSON_AS3935_COMMAND_SETTINGS, S_JSON_AS3935_COMMAND_GAIN[gain], nf_floor, vrms, tunecaps, minnumlight, reinj, wdth, min_nf, nf_time, dist_time, S_JSON_AS3935_COMMAND_ONOFF[disturber], S_JSON_AS3935_COMMAND_ONOFF[nfauto], S_JSON_AS3935_COMMAND_ONOFF[distauto], S_JSON_AS3935_COMMAND_ONOFF[nfautomax], S_JSON_AS3935_COMMAND_ONOFF[jsonlight], S_JSON_AS3935_COMMAND_ONOFF[jsonirq]); } } - break; - case CMND_AS3935_CALIBRATE: { - bool calreslt; - if (!XdrvMailbox.data_len) calreslt = AS3935AutoTune(); - Response_P(S_JSON_AS3935_COMMAND_NVALUE, S_JSON_AS3935_COMMAND_CAL[calreslt], AS3935GetTuneCaps()); - } - break; - default: - return false; + break; + case CMND_AS3935_CALIBRATE: { + bool calreslt; + if (!XdrvMailbox.data_len) calreslt = AS3935AutoTune(); + Response_P(S_JSON_AS3935_COMMAND_NVALUE, S_JSON_AS3935_COMMAND_CAL[calreslt], AS3935GetTuneCaps()); + } + break; + default: + return false; } return true; } else { @@ -750,13 +797,12 @@ bool AS3935Cmd(void) { } } -void AH3935Show(bool json) -{ +void AH3935Show(bool json) { if (json) { uint16_t vrms; uint8_t stage; AS3935CalcVrmsLevel(vrms, stage); - ResponseAppend_P(JSON_SNS_AS3935_EVENTS, D_SENSOR_AS3935, as3935_sensor.mqtt_irq, as3935_sensor.distance, as3935_sensor.intensity, stage); + ResponseAppend_P(JSON_SNS_AS3935_EVENTS, D_SENSOR_AS3935, as3935_sensor.mqtt_event, as3935_sensor.distance, as3935_sensor.intensity, stage); #ifdef USE_WEBSERVER } else { uint8_t gain = AS3935GetGainInt(); @@ -765,12 +811,13 @@ void AH3935Show(bool json) uint8_t stage; AS3935CalcVrmsLevel(vrms, stage); - WSContentSend_PD(HTTP_SNS_AS3935_TABLE_1[as3935_sensor.http_irq], D_NAME_AS3935, as3935_sensor.http_distance); + WSContentSend_PD(HTTP_SNS_AS3935_TABLE_1[as3935_sensor.http_event], D_NAME_AS3935, as3935_sensor.http_distance); WSContentSend_PD(HTTP_SNS_AS3935_DISTANZ, as3935_sensor.http_distance); WSContentSend_PD(HTTP_SNS_AS3935_ENERGY, as3935_sensor.http_intensity); WSContentSend_PD(HTTP_SNS_AS3935_GAIN[gain], D_NAME_AS3935); WSContentSend_PD(HTTP_SNS_AS3935_DISTURBER[disturber], D_NAME_AS3935); WSContentSend_PD(HTTP_SNS_AS3935_VRMS, vrms, stage); + #endif // USE_WEBSERVER } } @@ -779,16 +826,13 @@ void AH3935Show(bool json) * Interface \*********************************************************************************************/ -bool Xsns67(uint8_t function) -{ +bool Xsns67(uint8_t function) { if (!I2cEnabled(XI2C_48)) { return false; } - bool result = false; - if (FUNC_INIT == function) { AS3935Detect(); } - else if (as3935_active) { + else if (as3935_sensor.active) { switch (function) { case FUNC_EVERY_SECOND: AS3935EverySecond(); From 2e067977b957296a19ef4623be6e244b0b45d766 Mon Sep 17 00:00:00 2001 From: device111 <48546979+device111@users.noreply.github.com> Date: Thu, 3 Sep 2020 09:39:48 +0200 Subject: [PATCH 2/2] Update de_DE.h --- tasmota/language/de_DE.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/language/de_DE.h b/tasmota/language/de_DE.h index abef862c9..cf7271539 100644 --- a/tasmota/language/de_DE.h +++ b/tasmota/language/de_DE.h @@ -812,7 +812,7 @@ #define D_AS3935_GAIN "Rauschpegel:" #define D_AS3935_ENERGY "Energie:" #define D_AS3935_DISTANCE "Entfernung:" -#define D_AS3935_DISTURBER "Enstörer:" +#define D_AS3935_DISTURBER "Entstörer:" #define D_AS3935_VRMS "µVrms:" #define D_AS3935_APRX "ca.:" #define D_AS3935_AWAY "entfernt"