From 6c724faaf72448a8e1b3a9a91b5c998725f4ef12 Mon Sep 17 00:00:00 2001 From: David Hunt Date: Mon, 3 Apr 2017 19:42:25 +0100 Subject: [PATCH] Add Analog input plus alarm functionality Can now send an alarm MQTT message on a preset analog level. Useful for Smoke/gas detectors, etc. Signed-off-by: David Hunt --- sonoff/sonoff.ino | 39 +++++++++++++++++++++++++++++++++++---- sonoff/user_config.h | 21 ++++++++++++++------- sonoff/webserver.ino | 12 ++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index c2711ac..255fc1a 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -327,6 +327,7 @@ struct SYSCFG { uint16_t ws_wakeup; char friendlyname[4][33]; + uint16_t analog_trigger; char switch_topic[33]; byte mqtt_switch_retain; uint8_t mqtt_enabled; @@ -399,6 +400,7 @@ uint16_t mqtt_cmnd_publish = 0; // ignore flag for publish command PubSubClient mqttClient(espClient); // MQTT Client #endif // USE_MQTT WiFiUDP portUDP; // UDP Syslog and Alexa +long t1, t2; uint8_t power; // Current copy of sysCfg.power byte syslog_level; // Current copy of sysCfg.syslog_level @@ -461,6 +463,7 @@ void CFG_DefaultSet() sysCfg.timezone = APP_TIMEZONE; strlcpy(sysCfg.otaUrl, OTA_URL, sizeof(sysCfg.otaUrl)); strlcpy(sysCfg.ex_friendlyname, FRIENDLY_NAME1, sizeof(sysCfg.ex_friendlyname)); + sysCfg.analog_trigger = ANALOG_TRIGGER; sysCfg.seriallog_level = SERIAL_LOG_LEVEL; sysCfg.sta_active = 0; @@ -1620,6 +1623,7 @@ void mqttDataCb(char* topic, byte* data, unsigned int data_len) snprintf_P(svalue, sizeof(svalue), PSTR("{\"Restart\":\"1 to restart\"}")); } } + #if 0 else if (!strcmp(type,"RESET")) { switch (payload) { case 1: @@ -1634,6 +1638,7 @@ void mqttDataCb(char* topic, byte* data, unsigned int data_len) snprintf_P(svalue, sizeof(svalue), PSTR("{\"Reset\":\"1 to reset\"}")); } } + #endif else if (!strcmp(type,"TIMEZONE")) { if ((data_len > 0) && (((payload >= -12) && (payload <= 12)) || (payload == 99))) { sysCfg.timezone = payload; @@ -2267,6 +2272,14 @@ void every_second() dht_readPrep(); #endif // SEND_TELEMETRY_DHT +#ifdef SEND_TELEMETRY_ANALOG + int analog; + analog = analogRead(ANALOG_PIN); + snprintf_P(stopic, sizeof(stopic), PSTR("%s/%s/ANALOG"), PUB_PREFIX2, sysCfg.mqtt_topic); + snprintf_P(svalue, sizeof(svalue), PSTR("{ \"Analog\": %d }"), analog); + mqtt_publish(stopic, svalue); +#endif // SEND_TELEMETRY_MQ + #ifdef SEND_TELEMETRY_I2C htu_detect(); bmp_detect(); @@ -2409,6 +2422,22 @@ void stateloop() } } + t1 = millis(); + if (t1 > t2) { +#ifdef SEND_ANALOG_ALARM + int analog; + analog = analogRead(ANALOG_PIN); + if (analog > sysCfg.analog_trigger) { + snprintf_P(stopic, sizeof(stopic), PSTR("%s/%s/ANALOG_ALARM"), PUB_PREFIX2, sysCfg.mqtt_topic); + snprintf_P(svalue, sizeof(svalue), PSTR("{ \"Analog\": %d }"), analog); + mqtt_publish(stopic, svalue); + } +#endif // SEND_ANALOG_ALARM + t2 = millis() + 2000; + } + + + #ifdef USE_WS2812 ws2812_animate(); #endif // USE_WS2812 @@ -2744,10 +2773,12 @@ void setup() if (!sysCfg.model) { sysCfg.model = SONOFF; -#if (MODULE == SONOFF) - pinMode(REL_PIN, INPUT_PULLUP); - if (digitalRead(REL_PIN)) sysCfg.model = SONOFF_DUAL; -#endif + +//#if (MODULE == SONOFF) +// pinMode(REL_PIN, INPUT_PULLUP); +// if (digitalRead(REL_PIN)) sysCfg.model = SONOFF_DUAL; +//#endif + #if (MODULE == SONOFF_2) #ifdef REL3_PIN pinMode(REL3_PIN, INPUT_PULLUP); diff --git a/sonoff/user_config.h b/sonoff/user_config.h index 0b8890d..98178fc 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -174,6 +174,13 @@ // *** Option 2 - Use Adafruit DHT library - Select either Option 1 OR Option 2 // #define SEND_TELEMETRY_DHT2 // Enable sending temperature and humidity telemetry /*-------------------------------------------------------------------------------------------*\ + * ANALOG INPUT DATA +\*-------------------------------------------------------------------------------------------*/ +#define ANALOG_PIN A0 +#define ANALOG_TRIGGER 400 +#define SEND_ANALOG_ALARM +#define SEND_TELEMETRY_ANALOG +/*-------------------------------------------------------------------------------------------*\ * I2C devices BH1750, BMP085, BMP180, BMP280, BME280 and HTU21D \*-------------------------------------------------------------------------------------------*/ #define I2C_SDA_PIN 4 // GPIO 04 = I2C SDA (Sonoff_TH10A(16A)- Needs extra hardware) diff --git a/sonoff/webserver.ino b/sonoff/webserver.ino index 00636c6..ce6c911 100644 --- a/sonoff/webserver.ino +++ b/sonoff/webserver.ino @@ -463,6 +463,18 @@ void handleRoot() #if defined(SEND_TELEMETRY_DHT) || defined(SEND_TELEMETRY_DHT2) tpage += dht_webPresent(); #endif // SEND_TELEMETRY_DHT/2 + +#if defined(SEND_TELEMETRY_ANALOG) + char str[10]; + int analog; + + analog = analogRead(ANALOG_PIN); + page += F(""); + sprintf(str,"%d", analog); + page += F(""); + page += F("
Analog: "); page += str; page += F("

"); +#endif // SEND_TELEMETRY_ANALOG + #if defined(SEND_TELEMETRY_I2C) tpage += htu_webPresent(); tpage += bmp_webPresent(); -- 2.11.0.windows.1