2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2017-05-13 12:02:10 +01:00
|
|
|
sonoff.ino - Sonoff-Tasmota firmware for iTead Sonoff, Wemos and NodeMCU hardware
|
|
|
|
|
|
|
|
Copyright (C) 2017 Theo Arends
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-05-17 21:49:22 +01:00
|
|
|
/*====================================================
|
2017-05-13 12:02:10 +01:00
|
|
|
Prerequisites:
|
|
|
|
- Change libraries/PubSubClient/src/PubSubClient.h
|
|
|
|
#define MQTT_MAX_PACKET_SIZE 512
|
2017-07-15 14:07:30 +01:00
|
|
|
|
|
|
|
- Select IDE Tools - Flash Mode: "DOUT"
|
|
|
|
- Select IDE Tools - Flash Size: "1M (no SPIFFS)"
|
2017-05-17 21:49:22 +01:00
|
|
|
====================================================*/
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-06 16:28:00 +01:00
|
|
|
#define VERSION 0x05080008 // 5.8.0h
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
enum week_t {Last, First, Second, Third, Fourth};
|
|
|
|
enum dow_t {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};
|
|
|
|
enum month_t {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
|
2017-09-26 14:10:58 +01:00
|
|
|
enum log_t {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL}; // SerialLog, Syslog, Weblog
|
|
|
|
enum wifi_t {WIFI_RESTART, WIFI_SMARTCONFIG, WIFI_MANAGER, WIFI_WPSCONFIG, WIFI_RETRY, WIFI_WAIT, MAX_WIFI_OPTION}; // WifiConfig
|
|
|
|
enum swtch_t {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, MAX_SWITCH_OPTION}; // SwitchMode
|
|
|
|
enum led_t {LED_OFF, LED_POWER, LED_MQTTSUB, LED_POWER_MQTTSUB, LED_MQTTPUB, LED_POWER_MQTTPUB, LED_MQTT, LED_POWER_MQTT, MAX_LED_OPTION}; // LedState
|
|
|
|
enum emul_t {EMUL_NONE, EMUL_WEMO, EMUL_HUE, EMUL_MAX}; // Emulation
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
#include "user_config.h"
|
|
|
|
#include "user_config_override.h"
|
2017-09-02 13:37:02 +01:00
|
|
|
#include "i18n.h"
|
|
|
|
#include "sonoff_template.h"
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* No user configurable items below
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define MODULE SONOFF_BASIC // [Module] Select default model
|
|
|
|
|
2017-02-13 16:25:46 +00:00
|
|
|
#define USE_DHT // Default DHT11 sensor needs no external library
|
|
|
|
#ifndef USE_DS18x20
|
|
|
|
#define USE_DS18B20 // Default DS18B20 sensor needs no external library
|
|
|
|
#endif
|
2017-02-28 15:01:48 +00:00
|
|
|
//#define DEBUG_THEO // Add debug code
|
2017-02-13 16:25:46 +00:00
|
|
|
|
|
|
|
#ifdef BE_MINIMAL
|
2017-03-05 14:07:30 +00:00
|
|
|
#ifdef USE_MQTT_TLS
|
|
|
|
#undef USE_MQTT_TLS // Disable TLS support won't work as the MQTTHost is not set
|
|
|
|
#endif
|
2017-02-13 16:25:46 +00:00
|
|
|
#ifdef USE_DISCOVERY
|
|
|
|
#undef USE_DISCOVERY // Disable Discovery services for both MQTT and web server
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
#undef USE_DOMOTICZ // Disable Domoticz
|
|
|
|
#endif
|
2017-02-14 13:27:08 +00:00
|
|
|
//#ifdef USE_WEBSERVER
|
|
|
|
//#undef USE_WEBSERVER // Disable Webserver
|
|
|
|
//#endif
|
2017-02-13 16:25:46 +00:00
|
|
|
#ifdef USE_EMULATION
|
|
|
|
#undef USE_EMULATION // Disable Wemo or Hue emulation
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DS18x20
|
|
|
|
#undef USE_DS18x20 // Disable DS18x20 sensor
|
|
|
|
#endif
|
|
|
|
#ifdef USE_I2C
|
2017-09-26 14:10:58 +01:00
|
|
|
#undef USE_I2C // Disable all I2C sensors and devices
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SPI
|
|
|
|
#undef USE_SPI // Disable all SPI devices
|
2017-02-13 16:25:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_WS2812
|
|
|
|
#undef USE_WS2812 // Disable WS2812 Led string
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DS18B20
|
|
|
|
#undef USE_DS18B20 // Disable internal DS18B20 sensor
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DHT
|
|
|
|
#undef USE_DHT // Disable internal DHT sensor
|
|
|
|
#endif
|
2017-02-14 13:27:08 +00:00
|
|
|
#ifdef USE_IR_REMOTE
|
|
|
|
#undef USE_IR_REMOTE // Disable IR driver
|
|
|
|
#endif
|
2017-02-28 15:01:48 +00:00
|
|
|
#ifdef DEBUG_THEO
|
|
|
|
#undef DEBUG_THEO // Disable debug code
|
|
|
|
#endif
|
2017-02-13 16:25:46 +00:00
|
|
|
#endif // BE_MINIMAL
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifndef SWITCH_MODE
|
|
|
|
#define SWITCH_MODE TOGGLE // TOGGLE, FOLLOW or FOLLOW_INV (the wall switch state)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MQTT_FINGERPRINT
|
|
|
|
#define MQTT_FINGERPRINT "A5 02 FF 13 99 9F 8B 39 8E F1 83 4F 11 23 65 0B 32 36 FC 07"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WS2812_LEDS
|
|
|
|
#define WS2812_LEDS 30 // [Pixels] Number of LEDs
|
|
|
|
#endif
|
|
|
|
|
2017-05-10 13:19:36 +01:00
|
|
|
#define MQTT_TOKEN_PREFIX "%prefix%" // To be substituted by mqtt_prefix[x]
|
|
|
|
#define MQTT_TOKEN_TOPIC "%topic%" // To be substituted by mqtt_topic, mqtt_grptopic, mqtt_buttontopic, mqtt_switchtopic
|
|
|
|
|
2017-02-08 11:56:51 +00:00
|
|
|
#define WIFI_HOSTNAME "%s-%04d" // Expands to <MQTT_TOPIC>-<last 4 decimal chars of MAC address>
|
2017-02-10 16:03:34 +00:00
|
|
|
#define CONFIG_FILE_SIGN 0xA5 // Configuration file signature
|
|
|
|
#define CONFIG_FILE_XOR 0x5A // Configuration file xor (0 = No Xor)
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
#define HLW_PREF_PULSE 12530 // was 4975us = 201Hz = 1000W
|
|
|
|
#define HLW_UREF_PULSE 1950 // was 1666us = 600Hz = 220V
|
|
|
|
#define HLW_IREF_PULSE 3500 // was 1666us = 600Hz = 4.545A
|
|
|
|
|
2017-05-10 13:19:36 +01:00
|
|
|
#define MQTT_RETRY_SECS 10 // Minimum seconds to retry MQTT connection
|
2017-01-28 13:41:01 +00:00
|
|
|
#define APP_POWER 0 // Default saved power state Off
|
2017-05-20 13:03:34 +01:00
|
|
|
#define MAX_COUNTERS 4 // Max number of counter sensors
|
2017-03-12 17:36:33 +00:00
|
|
|
#define MAX_PULSETIMERS 4 // Max number of supported pulse timers
|
2017-08-08 15:31:43 +01:00
|
|
|
#define WS2812_MAX_LEDS 512 // Max number of LEDs
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-03-21 15:17:28 +00:00
|
|
|
#define PWM_RANGE 1023 // 255..1023 needs to be devisible by 256
|
2017-04-10 16:25:31 +01:00
|
|
|
//#define PWM_FREQ 1000 // 100..1000 Hz led refresh
|
2017-07-03 10:45:15 +01:00
|
|
|
//#define PWM_FREQ 910 // 100..1000 Hz led refresh (iTead value)
|
|
|
|
#define PWM_FREQ 880 // 100..1000 Hz led refresh (BN-SZ01 value)
|
2017-03-19 17:19:08 +00:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#define MAX_POWER_HOLD 10 // Time in SECONDS to allow max agreed power (Pow)
|
|
|
|
#define MAX_POWER_WINDOW 30 // Time in SECONDS to disable allow max agreed power (Pow)
|
|
|
|
#define SAFE_POWER_HOLD 10 // Time in SECONDS to allow max unit safe power (Pow)
|
|
|
|
#define SAFE_POWER_WINDOW 30 // Time in MINUTES to disable allow max unit safe power (Pow)
|
|
|
|
#define MAX_POWER_RETRY 5 // Retry count allowing agreed power limit overflow (Pow)
|
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
#define STATES 20 // State loops per second
|
2017-01-28 13:41:01 +00:00
|
|
|
#define SYSLOG_TIMER 600 // Seconds to restore syslog_level
|
|
|
|
#define SERIALLOG_TIMER 600 // Seconds to disable SerialLog
|
2017-02-24 17:17:48 +00:00
|
|
|
#define OTA_ATTEMPTS 10 // Number of times to try fetching the new firmware
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-07-15 14:07:30 +01:00
|
|
|
#define INPUT_BUFFER_SIZE 250 // Max number of characters in (serial) command buffer
|
2017-05-08 12:21:45 +01:00
|
|
|
#define CMDSZ 20 // Max number of characters in command
|
|
|
|
#define TOPSZ 100 // Max number of characters in topic string
|
2017-03-25 16:24:11 +00:00
|
|
|
#ifdef USE_MQTT_TLS
|
|
|
|
#define MAX_LOG_LINES 10 // Max number of lines in weblog
|
|
|
|
#else
|
|
|
|
#define MAX_LOG_LINES 20 // Max number of lines in weblog
|
|
|
|
#endif
|
2017-07-15 14:07:30 +01:00
|
|
|
#define MAX_BACKLOG 16 // Max number of commands in backlog (chk blogidx and blogptr code)
|
|
|
|
#define MIN_BACKLOG_DELAY 2 // Minimal backlog delay in 0.1 seconds
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
#define APP_BAUDRATE 115200 // Default serial baudrate
|
2017-03-14 17:03:25 +00:00
|
|
|
#define MAX_STATUS 11 // Max number of status lines
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-06-16 13:33:49 +01:00
|
|
|
enum butt_t {PRESSED, NOT_PRESSED};
|
2017-06-19 21:54:49 +01:00
|
|
|
enum opt_t {P_HOLD_TIME, P_MAX_POWER_RETRY, P_MAX_PARAM8}; // Index in sysCfg.param
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
#include "support.h" // Global support
|
2017-03-03 11:35:23 +00:00
|
|
|
|
2017-03-21 15:17:28 +00:00
|
|
|
#include <PubSubClient.h> // MQTT
|
2017-06-01 16:37:30 +01:00
|
|
|
#ifndef MESSZ
|
2017-09-13 13:19:34 +01:00
|
|
|
#define MESSZ 405 // Max number of characters in JSON message string (4 x DS18x20 sensors)
|
2017-06-01 16:37:30 +01:00
|
|
|
#endif
|
2017-09-13 13:19:34 +01:00
|
|
|
|
|
|
|
// Max message size calculated by PubSubClient is (MQTT_MAX_PACKET_SIZE < 5 + 2 + strlen(topic) + plength)
|
2017-03-05 14:07:30 +00:00
|
|
|
#if (MQTT_MAX_PACKET_SIZE -TOPSZ -7) < MESSZ // If the max message size is too small, throw an error at compile time
|
|
|
|
// See pubsubclient.c line 359
|
2017-09-13 13:19:34 +01:00
|
|
|
#error "MQTT_MAX_PACKET_SIZE is too small in libraries/PubSubClient/src/PubSubClient.h, increase it to at least 512"
|
2017-03-03 11:35:23 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-16 15:23:11 +00:00
|
|
|
#include <Ticker.h> // RTC, HLW8012, OSWatch
|
2017-01-28 13:41:01 +00:00
|
|
|
#include <ESP8266WiFi.h> // MQTT, Ota, WifiManager
|
|
|
|
#include <ESP8266HTTPClient.h> // MQTT, Ota
|
|
|
|
#include <ESP8266httpUpdate.h> // Ota
|
2017-06-04 16:40:27 +01:00
|
|
|
#include <StreamString.h> // Webserver, Updater
|
2017-02-17 16:18:41 +00:00
|
|
|
#include <ArduinoJson.h> // WemoHue, IRremote, Domoticz
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
#include <ESP8266WebServer.h> // WifiManager, Webserver
|
|
|
|
#include <DNSServer.h> // WifiManager
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
#ifdef USE_DISCOVERY
|
|
|
|
#include <ESP8266mDNS.h> // MQTT, Webserver
|
|
|
|
#endif // USE_DISCOVERY
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#include <Wire.h> // I2C support library
|
|
|
|
#endif // USE_I2C
|
2017-07-15 14:07:30 +01:00
|
|
|
#ifdef USE_SPI
|
2017-05-10 13:19:36 +01:00
|
|
|
#include <SPI.h> // SPI support, TFT
|
2017-04-29 13:40:53 +01:00
|
|
|
#endif // USE_SPI
|
2017-02-19 16:49:17 +00:00
|
|
|
#include "settings.h"
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
struct TIME_T {
|
|
|
|
uint8_t Second;
|
|
|
|
uint8_t Minute;
|
|
|
|
uint8_t Hour;
|
|
|
|
uint8_t Wday; // day of week, sunday is day 1
|
|
|
|
uint8_t Day;
|
|
|
|
uint8_t Month;
|
|
|
|
char MonthName[4];
|
|
|
|
uint16_t DayOfYear;
|
|
|
|
uint16_t Year;
|
|
|
|
unsigned long Valid;
|
|
|
|
} rtcTime;
|
|
|
|
|
|
|
|
struct TimeChangeRule
|
|
|
|
{
|
|
|
|
uint8_t week; // 1=First, 2=Second, 3=Third, 4=Fourth, or 0=Last week of the month
|
|
|
|
uint8_t dow; // day of week, 1=Sun, 2=Mon, ... 7=Sat
|
|
|
|
uint8_t month; // 1=Jan, 2=Feb, ... 12=Dec
|
|
|
|
uint8_t hour; // 0-23
|
|
|
|
int offset; // offset from UTC in minutes
|
|
|
|
};
|
|
|
|
|
|
|
|
TimeChangeRule myDST = { TIME_DST }; // Daylight Saving Time
|
|
|
|
TimeChangeRule mySTD = { TIME_STD }; // Standard Time
|
|
|
|
|
|
|
|
int Baudrate = APP_BAUDRATE; // Serial interface baud rate
|
|
|
|
byte SerialInByte; // Received byte
|
|
|
|
int SerialInByteCounter = 0; // Index in receive buffer
|
|
|
|
byte Hexcode = 0; // Sonoff dual input flag
|
|
|
|
uint16_t ButtonCode = 0; // Sonoff dual received code
|
2017-04-24 17:25:53 +01:00
|
|
|
int16_t savedatacounter; // Counter and flag for config save to Flash
|
2017-01-28 13:41:01 +00:00
|
|
|
uint8_t mqttcounter = 0; // MQTT connection retry counter
|
2017-06-30 16:54:19 +01:00
|
|
|
uint8_t fallbacktopic = 0; // Use Topic or FallbackTopic
|
2017-01-28 13:41:01 +00:00
|
|
|
unsigned long timerxs = 0; // State loop timer
|
|
|
|
int state = 0; // State per second flag
|
|
|
|
int mqttflag = 2; // MQTT connection messages flag
|
|
|
|
int otaflag = 0; // OTA state flag
|
|
|
|
int otaok = 0; // OTA result
|
2017-02-24 17:17:48 +00:00
|
|
|
byte otaretry = OTA_ATTEMPTS; // OTA retry counter
|
2017-01-28 13:41:01 +00:00
|
|
|
int restartflag = 0; // Sonoff restart flag
|
|
|
|
int wificheckflag = WIFI_RESTART; // Wifi state flag
|
|
|
|
int uptime = 0; // Current uptime in hours
|
2017-03-29 17:42:05 +01:00
|
|
|
boolean uptime_flg = true; // Signal latest uptime
|
2017-01-28 13:41:01 +00:00
|
|
|
int tele_period = 0; // Tele period timer
|
|
|
|
byte logidx = 0; // Index in Web log buffer
|
2017-02-24 17:17:48 +00:00
|
|
|
byte logajaxflg = 0; // Reset web console log
|
2017-05-20 13:03:34 +01:00
|
|
|
byte Maxdevice = 0; // Max number of devices supported
|
2017-01-28 13:41:01 +00:00
|
|
|
int status_update_timer = 0; // Refresh initial status
|
2017-03-12 17:36:33 +00:00
|
|
|
uint16_t pulse_timer[MAX_PULSETIMERS] = { 0 }; // Power off timer
|
2017-01-28 13:41:01 +00:00
|
|
|
uint16_t blink_timer = 0; // Power cycle timer
|
|
|
|
uint16_t blink_counter = 0; // Number of blink cycles
|
|
|
|
uint8_t blink_power; // Blink power state
|
|
|
|
uint8_t blink_mask = 0; // Blink relay active mask
|
|
|
|
uint8_t blink_powersave; // Blink start power save state
|
|
|
|
uint16_t mqtt_cmnd_publish = 0; // ignore flag for publish command
|
2017-02-13 16:25:46 +00:00
|
|
|
uint8_t latching_power = 0; // Power state at latching start
|
|
|
|
uint8_t latching_relay_pulse = 0; // Latching relay pulse timer
|
2017-07-15 14:07:30 +01:00
|
|
|
uint8_t blogidx = 0; // Command backlog index
|
|
|
|
uint8_t blogptr = 0; // Command backlog pointer
|
|
|
|
uint8_t blogmutex = 0; // Command backlog pending
|
|
|
|
uint16_t blogdelay = 0; // Command backlog delay
|
2017-08-18 11:55:08 +01:00
|
|
|
uint8_t interlockmutex = 0; // Interlock power command pending
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
#ifdef USE_MQTT_TLS
|
|
|
|
WiFiClientSecure espClient; // Wifi Secure Client
|
|
|
|
#else
|
|
|
|
WiFiClient espClient; // Wifi Client
|
|
|
|
#endif
|
|
|
|
PubSubClient mqttClient(espClient); // MQTT Client
|
|
|
|
WiFiUDP portUDP; // UDP Syslog and Alexa
|
|
|
|
|
|
|
|
uint8_t power; // Current copy of sysCfg.power
|
|
|
|
byte syslog_level; // Current copy of sysCfg.syslog_level
|
|
|
|
uint16_t syslog_timer = 0; // Timer to re-enable syslog_level
|
|
|
|
byte seriallog_level; // Current copy of sysCfg.seriallog_level
|
|
|
|
uint16_t seriallog_timer = 0; // Timer to disable Seriallog
|
2017-01-29 13:52:31 +00:00
|
|
|
uint8_t sleep; // Current copy of sysCfg.sleep
|
2017-06-25 22:01:41 +01:00
|
|
|
uint8_t stop_flash_rotate = 0; // Allow flash configuration rotation
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
int blinks = 201; // Number of LED blinks
|
|
|
|
uint8_t blinkstate = 0; // LED state
|
|
|
|
|
|
|
|
uint8_t lastbutton[4] = { NOT_PRESSED, NOT_PRESSED, NOT_PRESSED, NOT_PRESSED }; // Last button states
|
2017-06-16 13:33:49 +01:00
|
|
|
uint8_t holdbutton[4] = { 0 }; // Timer for button hold
|
2017-07-25 17:05:47 +01:00
|
|
|
uint8_t multiwindow[4] = { 0 }; // Max time between button presses to record press count
|
|
|
|
uint8_t multipress[4] = { 0 }; // Number of button presses within multiwindow
|
2017-01-28 13:41:01 +00:00
|
|
|
uint8_t lastwallswitch[4]; // Last wall switch states
|
2017-06-16 13:33:49 +01:00
|
|
|
uint8_t holdwallswitch[4] = { 0 }; // Timer for wallswitch push button hold
|
2017-05-04 12:58:26 +01:00
|
|
|
uint8_t blockgpio0 = 4; // Block GPIO0 for 4 seconds after poweron to workaround Wemos D1 RTS circuit
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
mytmplt my_module; // Active copy of GPIOs
|
|
|
|
uint8_t pin[GPIO_MAX]; // Possible pin configurations
|
|
|
|
uint8_t rel_inverted[4] = { 0 }; // Relay inverted flag (1 = (0 = On, 1 = Off))
|
|
|
|
uint8_t led_inverted[4] = { 0 }; // LED inverted flag (1 = (0 = On, 1 = Off))
|
2017-10-04 16:29:49 +01:00
|
|
|
uint8_t pwm_inverted[5] = { 0 }; // PWM inverted flag (1 = inverted)
|
2017-05-03 17:19:13 +01:00
|
|
|
uint8_t dht_flg = 0; // DHT configured
|
2017-01-28 13:41:01 +00:00
|
|
|
uint8_t hlw_flg = 0; // Power monitor configured
|
|
|
|
uint8_t i2c_flg = 0; // I2C configured
|
2017-04-29 13:40:53 +01:00
|
|
|
uint8_t spi_flg = 0; // SPI configured
|
2017-10-05 12:28:31 +01:00
|
|
|
uint8_t sfl_flg = 0; // Led flag (0 = No led, 1 = BN-SZ01, 2 = Sonoff Led, 3 = H801/MagicHome, 4 = H801/MagicHome, 5 = H801, 11 = WS2812, 12 = AiLight, 13 = Sonoff B1)
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
boolean mDNSbegun = false;
|
|
|
|
|
2017-09-14 13:20:27 +01:00
|
|
|
char Version[16]; // Version string from VERSION define
|
|
|
|
char Hostname[33]; // Composed Wifi hostname
|
|
|
|
char MQTTClient[33]; // Composed MQTT Clientname
|
|
|
|
char serialInBuf[INPUT_BUFFER_SIZE + 2]; // Receive buffer
|
2017-09-13 13:19:34 +01:00
|
|
|
char mqtt_data[MESSZ]; // MQTT publish buffer
|
|
|
|
char log_data[TOPSZ + MESSZ]; // Logging
|
|
|
|
String Log[MAX_LOG_LINES]; // Web log buffer
|
|
|
|
String Backlog[MAX_BACKLOG]; // Command backlog
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void getClient(char* output, const char* input, byte size)
|
|
|
|
{
|
|
|
|
char *token;
|
|
|
|
uint8_t digits = 0;
|
|
|
|
|
|
|
|
if (strstr(input, "%")) {
|
|
|
|
strlcpy(output, input, size);
|
|
|
|
token = strtok(output, "%");
|
|
|
|
if (strstr(input, "%") == input) {
|
|
|
|
output[0] = '\0';
|
|
|
|
} else {
|
|
|
|
token = strtok(NULL, "");
|
|
|
|
}
|
|
|
|
if (token != NULL) {
|
|
|
|
digits = atoi(token);
|
|
|
|
if (digits) {
|
|
|
|
snprintf_P(output, size, PSTR("%s%c0%dX"), output, '%', digits);
|
|
|
|
snprintf_P(output, size, output, ESP.getChipId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!digits) {
|
|
|
|
strlcpy(output, input, size);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 13:19:36 +01:00
|
|
|
void getTopic_P(char *stopic, byte prefix, char *topic, const char* subtopic)
|
|
|
|
{
|
|
|
|
char romram[CMDSZ];
|
2017-06-30 16:54:19 +01:00
|
|
|
String fulltopic;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-05-10 13:19:36 +01:00
|
|
|
snprintf_P(romram, sizeof(romram), subtopic);
|
2017-06-30 16:54:19 +01:00
|
|
|
if (fallbacktopic) {
|
|
|
|
fulltopic = FPSTR(PREFIXES[prefix]);
|
|
|
|
fulltopic += F("/");
|
|
|
|
fulltopic += MQTTClient;
|
|
|
|
} else {
|
|
|
|
fulltopic = sysCfg.mqtt_fulltopic;
|
|
|
|
if ((0 == prefix) && (-1 == fulltopic.indexOf(F(MQTT_TOKEN_PREFIX)))) {
|
|
|
|
fulltopic += F("/" MQTT_TOKEN_PREFIX); // Need prefix for commands to handle mqtt topic loops
|
|
|
|
}
|
|
|
|
for (byte i = 0; i < 3; i++) {
|
|
|
|
if ('\0' == sysCfg.mqtt_prefix[i][0]) {
|
|
|
|
snprintf_P(sysCfg.mqtt_prefix[i], sizeof(sysCfg.mqtt_prefix[i]), PREFIXES[i]);
|
|
|
|
}
|
2017-05-10 13:19:36 +01:00
|
|
|
}
|
2017-06-30 16:54:19 +01:00
|
|
|
fulltopic.replace(F(MQTT_TOKEN_PREFIX), sysCfg.mqtt_prefix[prefix]);
|
|
|
|
fulltopic.replace(F(MQTT_TOKEN_TOPIC), topic);
|
2017-05-10 13:19:36 +01:00
|
|
|
}
|
|
|
|
fulltopic.replace(F("#"), "");
|
|
|
|
fulltopic.replace(F("//"), "/");
|
|
|
|
if (!fulltopic.endsWith("/")) {
|
|
|
|
fulltopic += "/";
|
|
|
|
}
|
|
|
|
snprintf_P(stopic, TOPSZ, PSTR("%s%s"), fulltopic.c_str(), romram);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getStateText(byte state)
|
|
|
|
{
|
2017-06-06 22:23:23 +01:00
|
|
|
if (state > 3) {
|
2017-05-10 13:19:36 +01:00
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
return sysCfg.state_text[state];
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-02-13 16:25:46 +00:00
|
|
|
void setLatchingRelay(uint8_t power, uint8_t state)
|
|
|
|
{
|
|
|
|
power &= 1;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (2 == state) { // Reset relay
|
2017-02-13 16:25:46 +00:00
|
|
|
state = 0;
|
2017-02-14 13:27:08 +00:00
|
|
|
latching_power = power;
|
|
|
|
latching_relay_pulse = 0;
|
2017-02-13 16:25:46 +00:00
|
|
|
}
|
2017-02-14 13:27:08 +00:00
|
|
|
else if (state && !latching_relay_pulse) { // Set port power to On
|
2017-02-13 16:25:46 +00:00
|
|
|
latching_power = power;
|
|
|
|
latching_relay_pulse = 2; // max 200mS (initiated by stateloop())
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if (pin[GPIO_REL1 +latching_power] < 99) {
|
|
|
|
digitalWrite(pin[GPIO_REL1 +latching_power], rel_inverted[latching_power] ? !state : state);
|
|
|
|
}
|
2017-02-13 16:25:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 16:57:05 +01:00
|
|
|
void setRelay(uint8_t rpower)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-02-13 16:25:46 +00:00
|
|
|
uint8_t state;
|
2017-05-05 16:57:05 +01:00
|
|
|
|
|
|
|
if (4 == sysCfg.poweronstate) { // All on and stay on
|
|
|
|
power = (1 << Maxdevice) -1;
|
|
|
|
rpower = power;
|
|
|
|
}
|
2017-08-18 11:55:08 +01:00
|
|
|
if (sysCfg.flag.interlock) { // Allow only one or no relay set
|
|
|
|
uint8_t mask = 0x01;
|
|
|
|
uint8_t count = 0;
|
|
|
|
for (byte i = 0; i < Maxdevice; i++) {
|
|
|
|
if (rpower & mask) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
mask <<= 1;
|
|
|
|
}
|
|
|
|
if (count > 1) {
|
|
|
|
power = 0;
|
|
|
|
rpower = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((SONOFF_DUAL == sysCfg.module) || (CH4 == sysCfg.module)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
Serial.write(0xA0);
|
|
|
|
Serial.write(0x04);
|
2017-05-05 16:57:05 +01:00
|
|
|
Serial.write(rpower);
|
2017-01-28 13:41:01 +00:00
|
|
|
Serial.write(0xA1);
|
|
|
|
Serial.write('\n');
|
|
|
|
Serial.flush();
|
2017-02-13 16:25:46 +00:00
|
|
|
}
|
2017-07-03 10:45:15 +01:00
|
|
|
else if (sfl_flg) {
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_setPower(rpower);
|
2017-02-13 16:25:46 +00:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
else if (EXS_RELAY == sysCfg.module) {
|
2017-05-05 16:57:05 +01:00
|
|
|
setLatchingRelay(rpower, 1);
|
2017-02-13 16:25:46 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (byte i = 0; i < Maxdevice; i++) {
|
2017-05-05 16:57:05 +01:00
|
|
|
state = rpower &1;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (pin[GPIO_REL1 +i] < 99) {
|
|
|
|
digitalWrite(pin[GPIO_REL1 +i], rel_inverted[i] ? !state : state);
|
|
|
|
}
|
2017-05-05 16:57:05 +01:00
|
|
|
rpower >>= 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-28 15:31:11 +00:00
|
|
|
hlw_setPowerSteadyCounter(2);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setLed(uint8_t state)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
if (state) {
|
|
|
|
state = 1;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
digitalWrite(pin[GPIO_LED1], (led_inverted[0]) ? !state : state);
|
|
|
|
}
|
|
|
|
|
2017-02-18 17:08:55 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-09-26 14:10:58 +01:00
|
|
|
void mqtt_subscribe(char *topic)
|
|
|
|
{
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT D_SUBSCRIBE_TO " %s"), topic);
|
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
|
|
|
mqttClient.subscribe(topic);
|
|
|
|
mqttClient.loop(); // Solve LmacRxBlk:1 messages
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void mqtt_publish_sec(const char* topic, boolean retained)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.mqtt_enabled) {
|
2017-09-13 13:19:34 +01:00
|
|
|
if (mqttClient.publish(topic, mqtt_data, retained)) {
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT "%s = %s%s"), topic, mqtt_data, (retained) ? " (" D_RETAINED ")" : "");
|
2017-01-28 13:41:01 +00:00
|
|
|
// mqttClient.loop(); // Do not use here! Will block previous publishes
|
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_RESULT "%s = %s"), topic, mqtt_data);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_RESULT "%s = %s"), strrchr(topic,'/')+1, mqtt_data);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
addLog(LOG_LEVEL_INFO);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (sysCfg.ledstate &0x04) {
|
|
|
|
blinks++;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void mqtt_publish(const char* topic, boolean retained)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
char *me;
|
|
|
|
|
2017-03-25 16:24:11 +00:00
|
|
|
if (!strcmp(sysCfg.mqtt_prefix[0],sysCfg.mqtt_prefix[1])) {
|
|
|
|
me = strstr(topic,sysCfg.mqtt_prefix[0]);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (me == topic) {
|
|
|
|
mqtt_cmnd_publish += 8;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_sec(topic, retained);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void mqtt_publish(const char* topic)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish(topic, false);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void mqtt_publish_topic_P(uint8_t prefix, const char* subtopic, boolean retained)
|
2017-02-04 16:09:54 +00:00
|
|
|
{
|
2017-05-10 13:19:36 +01:00
|
|
|
/* prefix 0 = cmnd using subtopic
|
|
|
|
* prefix 1 = stat using subtopic
|
|
|
|
* prefix 2 = tele using subtopic
|
|
|
|
* prefix 4 = cmnd using subtopic or RESULT
|
|
|
|
* prefix 5 = stat using subtopic or RESULT
|
|
|
|
* prefix 6 = tele using subtopic or RESULT
|
|
|
|
*/
|
2017-04-25 17:24:42 +01:00
|
|
|
char romram[16];
|
2017-09-02 13:37:02 +01:00
|
|
|
char stopic[TOPSZ];
|
2017-02-04 16:09:54 +00:00
|
|
|
|
2017-09-02 13:37:02 +01:00
|
|
|
snprintf_P(romram, sizeof(romram), ((prefix > 3) && !sysCfg.flag.mqtt_response) ? S_RSLT_RESULT : subtopic);
|
|
|
|
for (byte i = 0; i < strlen(romram); i++) {
|
|
|
|
romram[i] = toupper(romram[i]);
|
|
|
|
}
|
2017-05-10 13:19:36 +01:00
|
|
|
prefix &= 3;
|
|
|
|
getTopic_P(stopic, prefix, sysCfg.mqtt_topic, romram);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish(stopic, retained);
|
2017-05-04 12:58:26 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void mqtt_publish_topic_P(uint8_t prefix, const char* subtopic)
|
2017-05-04 12:58:26 +01:00
|
|
|
{
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(prefix, subtopic, false);
|
2017-02-04 16:09:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
void mqtt_publishPowerState(byte device)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
char stopic[TOPSZ];
|
2017-09-02 13:37:02 +01:00
|
|
|
char scommand[16];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((device < 1) || (device > Maxdevice)) {
|
|
|
|
device = 1;
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
getPowerDevice(scommand, device, sizeof(scommand));
|
|
|
|
getTopic_P(stopic, 1, sysCfg.mqtt_topic, (sysCfg.flag.mqtt_response) ? scommand : S_RSLT_RESULT);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"), scommand, getStateText(bitRead(power, device -1)));
|
|
|
|
mqtt_publish(stopic);
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-05-08 12:21:45 +01:00
|
|
|
getTopic_P(stopic, 1, sysCfg.mqtt_topic, scommand);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), getStateText(bitRead(power, device -1)));
|
|
|
|
mqtt_publish(stopic, sysCfg.flag.mqtt_power_retain);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_publishPowerBlinkState(byte device)
|
|
|
|
{
|
2017-09-02 13:37:02 +01:00
|
|
|
char scommand[16];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((device < 1) || (device > Maxdevice)) {
|
|
|
|
device = 1;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"" D_BLINK " %s\"}"),
|
2017-09-02 13:37:02 +01:00
|
|
|
getPowerDevice(scommand, device, sizeof(scommand)), getStateText(bitRead(blink_mask, device -1)));
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(5, S_RSLT_POWER);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_connected()
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
char stopic[TOPSZ];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.mqtt_enabled) {
|
2017-02-04 16:09:54 +00:00
|
|
|
|
|
|
|
// Satisfy iobroker (#299)
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
mqtt_publish_topic_P(0, S_RSLT_POWER);
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-05-08 12:21:45 +01:00
|
|
|
getTopic_P(stopic, 0, sysCfg.mqtt_topic, PSTR("#"));
|
2017-09-26 14:10:58 +01:00
|
|
|
mqtt_subscribe(stopic);
|
2017-05-10 13:19:36 +01:00
|
|
|
if (strstr(sysCfg.mqtt_fulltopic, MQTT_TOKEN_TOPIC) != NULL) {
|
|
|
|
getTopic_P(stopic, 0, sysCfg.mqtt_grptopic, PSTR("#"));
|
2017-09-26 14:10:58 +01:00
|
|
|
mqtt_subscribe(stopic);
|
2017-06-30 16:54:19 +01:00
|
|
|
fallbacktopic = 1;
|
2017-05-10 13:19:36 +01:00
|
|
|
getTopic_P(stopic, 0, MQTTClient, PSTR("#"));
|
2017-06-30 16:54:19 +01:00
|
|
|
fallbacktopic = 0;
|
2017-09-26 14:10:58 +01:00
|
|
|
mqtt_subscribe(stopic);
|
2017-05-10 13:19:36 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
domoticz_mqttSubscribe();
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mqttflag) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MODULE "\":\"%s\", \"" D_VERSION "\":\"%s\", \"" D_FALLBACKTOPIC "\":\"%s\", \"" D_CMND_GROUPTOPIC "\":\"%s\"}"),
|
2017-01-28 13:41:01 +00:00
|
|
|
my_module.name, Version, MQTTClient, sysCfg.mqtt_grptopic);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(2, PSTR(D_RSLT_INFO "1"));
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
if (sysCfg.webserver) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_WEBSERVER_MODE "\":\"%s\", \"" D_CMND_HOSTNAME "\":\"%s\", \"" D_CMND_IPADDRESS "\":\"%s\"}"),
|
2017-09-02 13:37:02 +01:00
|
|
|
(2 == sysCfg.webserver) ? D_ADMIN : D_USER, Hostname, WiFi.localIP().toString().c_str());
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(2, PSTR(D_RSLT_INFO "2"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_RESTARTREASON "\":\"%s\"}"),
|
2017-02-28 15:01:48 +00:00
|
|
|
(getResetReason() == "Exception") ? ESP.getResetInfo().c_str() : getResetReason().c_str());
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(2, PSTR(D_RSLT_INFO "3"));
|
2017-04-25 17:24:42 +01:00
|
|
|
if (sysCfg.tele_period) {
|
|
|
|
tele_period = sysCfg.tele_period -9;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
status_update_timer = 2;
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
domoticz_setUpdateTimer(2);
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
}
|
|
|
|
mqttflag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_reconnect()
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
char stopic[TOPSZ];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-10 13:19:36 +01:00
|
|
|
mqttcounter = sysCfg.mqtt_retry;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if (!sysCfg.flag.mqtt_enabled) {
|
2017-01-28 13:41:01 +00:00
|
|
|
mqtt_connected();
|
|
|
|
return;
|
|
|
|
}
|
2017-01-29 20:36:12 +00:00
|
|
|
#ifdef USE_EMULATION
|
2017-01-28 13:41:01 +00:00
|
|
|
UDP_Disconnect();
|
2017-01-29 20:36:12 +00:00
|
|
|
#endif // USE_EMULATION
|
2017-01-28 13:41:01 +00:00
|
|
|
if (mqttflag > 1) {
|
|
|
|
#ifdef USE_MQTT_TLS
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_FINGERPRINT));
|
2017-01-28 13:41:01 +00:00
|
|
|
if (!espClient.connect(sysCfg.mqtt_host, sysCfg.mqtt_port)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT D_TLS_CONNECT_FAILED_TO " %s:%d. " D_RETRY_IN " %d " D_UNIT_SECOND),
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.mqtt_host, sysCfg.mqtt_port, mqttcounter);
|
2017-09-13 13:19:34 +01:00
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-01-28 13:41:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (espClient.verify(sysCfg.mqtt_fingerprint, sysCfg.mqtt_host)) {
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_VERIFIED));
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_DEBUG, S_LOG_MQTT, PSTR(D_INSECURE));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#endif // USE_MQTT_TLS
|
|
|
|
mqttClient.setCallback(mqttDataCb);
|
|
|
|
mqttflag = 1;
|
|
|
|
mqttcounter = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_ATTEMPTING_CONNECTION));
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifndef USE_MQTT_TLS
|
|
|
|
#ifdef USE_DISCOVERY
|
|
|
|
#ifdef MQTT_HOST_DISCOVERY
|
|
|
|
mdns_discoverMQTTServer();
|
|
|
|
#endif // MQTT_HOST_DISCOVERY
|
|
|
|
#endif // USE_DISCOVERY
|
|
|
|
#endif // USE_MQTT_TLS
|
|
|
|
mqttClient.setServer(sysCfg.mqtt_host, sysCfg.mqtt_port);
|
2017-05-08 12:21:45 +01:00
|
|
|
|
2017-09-02 13:37:02 +01:00
|
|
|
getTopic_P(stopic, 2, sysCfg.mqtt_topic, S_LWT);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_OFFLINE);
|
|
|
|
if (mqttClient.connect(MQTTClient, sysCfg.mqtt_user, sysCfg.mqtt_pwd, stopic, 1, true, mqtt_data)) {
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_CONNECTED));
|
2017-01-28 13:41:01 +00:00
|
|
|
mqttcounter = 0;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR(D_ONLINE));
|
|
|
|
mqtt_publish(stopic, true);
|
2017-01-28 13:41:01 +00:00
|
|
|
mqtt_connected();
|
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT D_CONNECT_FAILED_TO " %s:%d, rc %d. " D_RETRY_IN " %d " D_UNIT_SECOND),
|
2017-03-14 17:03:25 +00:00
|
|
|
sysCfg.mqtt_host, sysCfg.mqtt_port, mqttClient.state(), mqttcounter); //status codes are documented here http://pubsubclient.knolleary.net/api.html#state
|
2017-09-13 13:19:34 +01:00
|
|
|
addLog(LOG_LEVEL_INFO);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:49:17 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
boolean mqtt_command(boolean grpflg, char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload, uint16_t payload16)
|
2017-03-08 15:20:45 +00:00
|
|
|
{
|
|
|
|
boolean serviced = true;
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp1[TOPSZ];
|
|
|
|
char stemp2[10];
|
2017-05-08 12:21:45 +01:00
|
|
|
char scommand[CMDSZ];
|
2017-03-08 15:20:45 +00:00
|
|
|
uint16_t i;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
|
|
|
if (!strcasecmp_P(type, PSTR(D_CMND_MQTTHOST))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_host))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.mqtt_host, (1 == payload) ? MQTT_HOST : dataBuf, sizeof(sysCfg.mqtt_host));
|
2017-03-08 15:20:45 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTHOST "\":\"%s\"}"), sysCfg.mqtt_host);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTPORT))) {
|
2017-09-12 09:19:16 +01:00
|
|
|
if (payload16 > 0) {
|
|
|
|
sysCfg.mqtt_port = (1 == payload16) ? MQTT_PORT : payload16;
|
2017-03-08 15:20:45 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTPORT "\":%d}"), sysCfg.mqtt_port);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTRETRY))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= MQTT_RETRY_SECS) && (payload < 32001)) {
|
2017-05-10 13:19:36 +01:00
|
|
|
sysCfg.mqtt_retry = payload;
|
|
|
|
mqttcounter = sysCfg.mqtt_retry;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTRETRY "\":%d}"), sysCfg.mqtt_retry);
|
2017-05-10 13:19:36 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_STATETEXT)) && (index > 0) && (index <= 4)) {
|
2017-03-29 17:42:05 +01:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.state_text[0]))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
for(i = 0; i <= data_len; i++) {
|
|
|
|
if (dataBuf[i] == ' ') {
|
|
|
|
dataBuf[i] = '_';
|
|
|
|
}
|
|
|
|
}
|
2017-04-03 15:38:15 +01:00
|
|
|
strlcpy(sysCfg.state_text[index -1], dataBuf, sizeof(sysCfg.state_text[0]));
|
2017-03-29 17:42:05 +01:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATETEXT "%d\":\"%s\"}"), index, getStateText(index -1));
|
2017-03-29 17:42:05 +01:00
|
|
|
}
|
2017-03-08 15:20:45 +00:00
|
|
|
#ifdef USE_MQTT_TLS
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTFINGERPRINT))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_fingerprint))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.mqtt_fingerprint, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? MQTT_FINGERPRINT : dataBuf, sizeof(sysCfg.mqtt_fingerprint));
|
2017-03-08 15:20:45 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTFINGERPRINT "\":\"%s\"}"), sysCfg.mqtt_fingerprint);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_MQTTCLIENT))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_client))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.mqtt_client, (1 == payload) ? MQTT_CLIENT_ID : dataBuf, sizeof(sysCfg.mqtt_client));
|
2017-03-08 15:20:45 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTCLIENT "\":\"%s\"}"), sysCfg.mqtt_client);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTUSER))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_user))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.mqtt_user, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? MQTT_USER : dataBuf, sizeof(sysCfg.mqtt_user));
|
2017-03-08 15:20:45 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTUSER "\":\"%s\"}"), sysCfg.mqtt_user);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTPASSWORD))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_pwd))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.mqtt_pwd, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? MQTT_PASS : dataBuf, sizeof(sysCfg.mqtt_pwd));
|
2017-03-08 15:20:45 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTPASSWORD "\":\"%s\"}"), sysCfg.mqtt_pwd);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_FULLTOPIC))) {
|
2017-05-08 12:21:45 +01:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_fulltopic))) {
|
2017-06-16 13:33:49 +01:00
|
|
|
mqttfy(1, dataBuf);
|
2017-05-08 12:21:45 +01:00
|
|
|
if (!strcmp(dataBuf, MQTTClient)) {
|
|
|
|
payload = 1;
|
|
|
|
}
|
2017-05-13 12:02:10 +01:00
|
|
|
strlcpy(stemp1, (1 == payload) ? MQTT_FULLTOPIC : dataBuf, sizeof(stemp1));
|
|
|
|
if (strcmp(stemp1, sysCfg.mqtt_fulltopic)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), (sysCfg.flag.mqtt_offline) ? S_OFFLINE : "");
|
|
|
|
mqtt_publish_topic_P(2, PSTR(D_LWT), true); // Offline or remove previous retained topic
|
2017-05-13 12:02:10 +01:00
|
|
|
strlcpy(sysCfg.mqtt_fulltopic, stemp1, sizeof(sysCfg.mqtt_fulltopic));
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-05-08 12:21:45 +01:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_FULLTOPIC "\":\"%s\"}"), sysCfg.mqtt_fulltopic);
|
2017-05-08 12:21:45 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_PREFIX)) && (index > 0) && (index <= 3)) {
|
2017-03-25 16:24:11 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_prefix[0]))) {
|
2017-06-16 13:33:49 +01:00
|
|
|
mqttfy(0, dataBuf);
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.mqtt_prefix[index -1], (1 == payload) ? (1==index)?SUB_PREFIX:(2==index)?PUB_PREFIX:PUB_PREFIX2 : dataBuf, sizeof(sysCfg.mqtt_prefix[0]));
|
2017-03-25 16:24:11 +00:00
|
|
|
// if (sysCfg.mqtt_prefix[index -1][strlen(sysCfg.mqtt_prefix[index -1])] == '/') sysCfg.mqtt_prefix[index -1][strlen(sysCfg.mqtt_prefix[index -1])] = 0;
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PREFIX "%d\":\"%s\"}"), index, sysCfg.mqtt_prefix[index -1]);
|
2017-03-25 16:24:11 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_GROUPTOPIC))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_grptopic))) {
|
2017-06-16 13:33:49 +01:00
|
|
|
mqttfy(0, dataBuf);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!strcmp(dataBuf, MQTTClient)) {
|
|
|
|
payload = 1;
|
|
|
|
}
|
|
|
|
strlcpy(sysCfg.mqtt_grptopic, (1 == payload) ? MQTT_GRPTOPIC : dataBuf, sizeof(sysCfg.mqtt_grptopic));
|
2017-03-08 15:20:45 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_GROUPTOPIC "\":\"%s\"}"), sysCfg.mqtt_grptopic);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_TOPIC))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_topic))) {
|
2017-06-16 13:33:49 +01:00
|
|
|
mqttfy(0, dataBuf);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!strcmp(dataBuf, MQTTClient)) {
|
|
|
|
payload = 1;
|
|
|
|
}
|
2017-05-11 16:47:34 +01:00
|
|
|
strlcpy(stemp1, (1 == payload) ? MQTT_TOPIC : dataBuf, sizeof(stemp1));
|
|
|
|
if (strcmp(stemp1, sysCfg.mqtt_topic)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), (sysCfg.flag.mqtt_offline) ? S_OFFLINE : "");
|
|
|
|
mqtt_publish_topic_P(2, PSTR(D_LWT), true); // Offline or remove previous retained topic
|
2017-05-11 16:47:34 +01:00
|
|
|
strlcpy(sysCfg.mqtt_topic, stemp1, sizeof(sysCfg.mqtt_topic));
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TOPIC "\":\"%s\"}"), sysCfg.mqtt_topic);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_BUTTONTOPIC))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.button_topic))) {
|
2017-06-16 13:33:49 +01:00
|
|
|
mqttfy(0, dataBuf);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!strcmp(dataBuf, MQTTClient)) {
|
|
|
|
payload = 1;
|
|
|
|
}
|
|
|
|
strlcpy(sysCfg.button_topic, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? sysCfg.mqtt_topic : dataBuf, sizeof(sysCfg.button_topic));
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BUTTONTOPIC "\":\"%s\"}"), sysCfg.button_topic);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SWITCHTOPIC))) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.switch_topic))) {
|
2017-06-16 13:33:49 +01:00
|
|
|
mqttfy(0, dataBuf);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!strcmp(dataBuf, MQTTClient)) {
|
|
|
|
payload = 1;
|
|
|
|
}
|
|
|
|
strlcpy(sysCfg.switch_topic, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? sysCfg.mqtt_topic : dataBuf, sizeof(sysCfg.switch_topic));
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SWITCHTOPIC "\":\"%s\"}"), sysCfg.switch_topic);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_BUTTONRETAIN))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 1)) {
|
2017-03-08 15:20:45 +00:00
|
|
|
strlcpy(sysCfg.button_topic, sysCfg.mqtt_topic, sizeof(sysCfg.button_topic));
|
|
|
|
if (!payload) {
|
|
|
|
for(i = 1; i <= Maxdevice; i++) {
|
2017-06-06 22:23:23 +01:00
|
|
|
send_button_power(0, i, 9); // Clear MQTT retain in broker
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.mqtt_button_retain = payload;
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BUTTONRETAIN "\":\"%s\"}"), getStateText(sysCfg.flag.mqtt_button_retain));
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SWITCHRETAIN))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 1)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.button_topic, sysCfg.mqtt_topic, sizeof(sysCfg.button_topic));
|
2017-03-08 15:20:45 +00:00
|
|
|
if (!payload) {
|
|
|
|
for(i = 1; i <= 4; i++) {
|
2017-06-06 22:23:23 +01:00
|
|
|
send_button_power(1, i, 9); // Clear MQTT retain in broker
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.mqtt_switch_retain = payload;
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SWITCHRETAIN "\":\"%s\"}"), getStateText(sysCfg.flag.mqtt_switch_retain));
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_POWERRETAIN))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 1)) {
|
2017-03-08 15:20:45 +00:00
|
|
|
if (!payload) {
|
|
|
|
for(i = 1; i <= Maxdevice; i++) { // Clear MQTT retain in broker
|
2017-09-02 13:37:02 +01:00
|
|
|
getTopic_P(stemp1, 1, sysCfg.mqtt_topic, getPowerDevice(scommand, i, sizeof(scommand)));
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
mqtt_publish(stemp1, sysCfg.flag.mqtt_power_retain);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.mqtt_power_retain = payload;
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWERRETAIN "\":\"%s\"}"), getStateText(sysCfg.flag.mqtt_power_retain));
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SENSORRETAIN))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 1)) {
|
2017-05-04 12:58:26 +01:00
|
|
|
if (!payload) {
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
mqtt_publish_topic_P(2, PSTR(D_RSLT_SENSOR), sysCfg.flag.mqtt_sensor_retain);
|
2017-05-04 12:58:26 +01:00
|
|
|
}
|
|
|
|
sysCfg.flag.mqtt_sensor_retain = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SENSORRETAIN "\":\"%s\"}"), getStateText(sysCfg.flag.mqtt_sensor_retain));
|
2017-05-04 12:58:26 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:20:45 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2017-09-13 13:19:34 +01:00
|
|
|
else if (domoticz_command(type, index, dataBuf, data_len, payload)) {
|
2017-03-08 15:20:45 +00:00
|
|
|
// Serviced
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
else {
|
|
|
|
serviced = false;
|
|
|
|
}
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
void mqttDataCb(char* topic, byte* data, unsigned int data_len)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
2017-03-25 16:24:11 +00:00
|
|
|
if (!strcmp(sysCfg.mqtt_prefix[0],sysCfg.mqtt_prefix[1])) {
|
|
|
|
str = strstr(topic,sysCfg.mqtt_prefix[0]);
|
2017-01-28 13:41:01 +00:00
|
|
|
if ((str == topic) && mqtt_cmnd_publish) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (mqtt_cmnd_publish > 8) {
|
2017-09-02 13:37:02 +01:00
|
|
|
mqtt_cmnd_publish -= 8;
|
2017-04-25 17:24:42 +01:00
|
|
|
} else {
|
|
|
|
mqtt_cmnd_publish = 0;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
char topicBuf[TOPSZ];
|
|
|
|
char dataBuf[data_len+1];
|
|
|
|
char stemp1[TOPSZ];
|
|
|
|
char *p;
|
|
|
|
char *mtopic = NULL;
|
|
|
|
char *type = NULL;
|
2017-06-16 13:33:49 +01:00
|
|
|
byte otype = 0;
|
|
|
|
byte ptype = 0;
|
2017-09-14 14:35:38 +01:00
|
|
|
byte jsflg = 0;
|
|
|
|
byte lines = 1;
|
2017-04-25 17:24:42 +01:00
|
|
|
uint16_t i = 0;
|
|
|
|
uint16_t grpflg = 0;
|
|
|
|
uint16_t index;
|
2017-03-25 16:24:11 +00:00
|
|
|
uint32_t address;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
strncpy(topicBuf, topic, sizeof(topicBuf));
|
2017-09-08 11:57:08 +01:00
|
|
|
for (i = 0; i < data_len; i++) {
|
|
|
|
if (!isspace(data[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data_len -= i;
|
|
|
|
memcpy(dataBuf, data +i, sizeof(dataBuf));
|
2017-01-28 13:41:01 +00:00
|
|
|
dataBuf[sizeof(dataBuf)-1] = 0;
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_RESULT D_RECEIVED_TOPIC " %s, " D_DATA_SIZE " %d, " D_DATA " %s"),
|
2017-09-02 13:37:02 +01:00
|
|
|
topicBuf, data_len, dataBuf);
|
2017-09-13 13:19:34 +01:00
|
|
|
addLog(LOG_LEVEL_DEBUG_MORE);
|
2017-01-28 13:41:01 +00:00
|
|
|
// if (LOG_LEVEL_DEBUG_MORE <= seriallog_level) Serial.println(dataBuf);
|
|
|
|
|
|
|
|
#ifdef USE_DOMOTICZ
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.mqtt_enabled) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (domoticz_mqttData(topicBuf, sizeof(topicBuf), dataBuf, sizeof(dataBuf))) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
|
2017-05-08 12:21:45 +01:00
|
|
|
grpflg = (strstr(topicBuf, sysCfg.mqtt_grptopic) != NULL);
|
2017-06-30 16:54:19 +01:00
|
|
|
fallbacktopic = (strstr(topicBuf, MQTTClient) != NULL);
|
2017-06-16 13:33:49 +01:00
|
|
|
type = strrchr(topicBuf, '/') +1; // Last part of received topic is always the command (type)
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
index = 1;
|
|
|
|
if (type != NULL) {
|
2017-04-25 17:24:42 +01:00
|
|
|
for (i = 0; i < strlen(type); i++) {
|
|
|
|
type[i] = toupper(type[i]);
|
|
|
|
}
|
|
|
|
while (isdigit(type[i-1])) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (i < strlen(type)) {
|
|
|
|
index = atoi(type +i);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
type[i] = '\0';
|
|
|
|
}
|
|
|
|
|
2017-09-16 16:34:03 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_RESULT D_GROUP " %d, " D_INDEX " %d, " D_COMMAND " %s, " D_DATA " %s"),
|
|
|
|
grpflg, index, type, dataBuf);
|
2017-09-13 13:19:34 +01:00
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
if (type != NULL) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_COMMAND "\":\"" D_ERROR "\"}"));
|
2017-04-25 17:24:42 +01:00
|
|
|
if (sysCfg.ledstate &0x02) {
|
|
|
|
blinks++;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-09-16 16:34:03 +01:00
|
|
|
if (!strcmp(dataBuf,"?")) {
|
2017-04-25 17:24:42 +01:00
|
|
|
data_len = 0;
|
|
|
|
}
|
2017-06-25 22:01:41 +01:00
|
|
|
int16_t payload = -99; // No payload
|
|
|
|
uint16_t payload16 = 0;
|
|
|
|
long lnum = strtol(dataBuf, &p, 10);
|
|
|
|
if (p != dataBuf) {
|
|
|
|
payload = (int16_t) lnum; // -32766 - 32767
|
|
|
|
payload16 = (uint16_t) lnum; // 0 - 65535
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
blogdelay = MIN_BACKLOG_DELAY; // Reset backlog delay
|
2017-06-06 22:23:23 +01:00
|
|
|
|
2017-09-18 17:06:46 +01:00
|
|
|
if (!strcasecmp_P(dataBuf, PSTR("OFF")) || !strcasecmp_P(dataBuf, PSTR(D_OFF)) || !strcasecmp_P(dataBuf, sysCfg.state_text[0]) || !strcasecmp_P(dataBuf, PSTR(D_FALSE)) || !strcasecmp_P(dataBuf, PSTR(D_STOP)) || !strcasecmp_P(dataBuf, PSTR(D_CELSIUS))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 0;
|
|
|
|
}
|
2017-09-18 17:06:46 +01:00
|
|
|
if (!strcasecmp_P(dataBuf, PSTR("ON")) || !strcasecmp_P(dataBuf, PSTR(D_ON)) || !strcasecmp_P(dataBuf, sysCfg.state_text[1]) || !strcasecmp_P(dataBuf, PSTR(D_TRUE)) || !strcasecmp_P(dataBuf, PSTR(D_START)) || !strcasecmp_P(dataBuf, PSTR(D_FAHRENHEIT)) || !strcasecmp_P(dataBuf, PSTR(D_USER))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 1;
|
|
|
|
}
|
2017-09-18 17:06:46 +01:00
|
|
|
if (!strcasecmp_P(dataBuf, PSTR("TOGGLE")) || !strcasecmp_P(dataBuf, PSTR(D_TOGGLE)) || !strcasecmp_P(dataBuf, sysCfg.state_text[2]) || !strcasecmp_P(dataBuf, PSTR(D_ADMIN))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 2;
|
|
|
|
}
|
2017-09-18 17:06:46 +01:00
|
|
|
if (!strcasecmp_P(dataBuf, PSTR("BLINK")) || !strcasecmp_P(dataBuf, PSTR(D_BLINK))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 3;
|
|
|
|
}
|
2017-09-18 17:06:46 +01:00
|
|
|
if (!strcasecmp_P(dataBuf, PSTR("BLINKOFF")) || !strcasecmp_P(dataBuf, PSTR(D_BLINKOFF))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 4;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
// snprintf_P(log_data, sizeof(log_data), PSTR("RSLT: Payload %d, Payload16 %d"), payload, payload16);
|
|
|
|
// addLog(LOG_LEVEL_DEBUG);
|
2017-06-06 22:23:23 +01:00
|
|
|
|
2017-09-02 13:37:02 +01:00
|
|
|
if (!strcasecmp_P(type, PSTR(D_CMND_BACKLOG))) {
|
2017-07-15 14:07:30 +01:00
|
|
|
if (data_len) {
|
|
|
|
char *blcommand = strtok(dataBuf, ";");
|
|
|
|
while (blcommand != NULL) {
|
|
|
|
Backlog[blogidx] = String(blcommand);
|
|
|
|
blogidx++;
|
|
|
|
/*
|
|
|
|
if (blogidx >= MAX_BACKLOG) {
|
|
|
|
blogidx = 0;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
blogidx &= 0xF;
|
|
|
|
blcommand = strtok(NULL, ";");
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BACKLOG "\":\"" D_APPENDED "\"}"));
|
2017-07-15 14:07:30 +01:00
|
|
|
} else {
|
|
|
|
uint8_t blflag = (blogptr == blogidx);
|
|
|
|
blogptr = blogidx;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BACKLOG "\":\"%s\"}"), blflag ? D_EMPTY : D_ABORTED);
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_DELAY))) {
|
2017-07-15 14:07:30 +01:00
|
|
|
if ((payload >= MIN_BACKLOG_DELAY) && (payload <= 3600)) {
|
|
|
|
blogdelay = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DELAY "\":%d}"), blogdelay);
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_POWER)) && (index > 0) && (index <= Maxdevice)) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload < 0) || (payload > 4)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 9;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
do_cmnd_power(index, payload);
|
2017-06-30 16:54:19 +01:00
|
|
|
fallbacktopic = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_STATUS))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload < 0) || (payload > MAX_STATUS)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 99;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
publish_status(payload);
|
2017-06-30 16:54:19 +01:00
|
|
|
fallbacktopic = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if ((sysCfg.module != MOTOR) && !strcasecmp_P(type, PSTR(D_CMND_POWERONSTATE))) {
|
2017-08-18 11:55:08 +01:00
|
|
|
/* 0 = Keep relays off after power on
|
|
|
|
* 1 = Turn relays on after power on
|
|
|
|
* 2 = Toggle relays after power on
|
|
|
|
* 3 = Set relays to last saved state after power on
|
|
|
|
* 4 = Turn relays on and disable any relay control (used for Sonoff Pow to always measure power)
|
|
|
|
*/
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 4)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.poweronstate = payload;
|
2017-05-05 16:57:05 +01:00
|
|
|
if (4 == sysCfg.poweronstate) {
|
2017-06-06 22:23:23 +01:00
|
|
|
for (byte i = 1; i <= Maxdevice; i++) {
|
2017-05-05 16:57:05 +01:00
|
|
|
do_cmnd_power(i, 1);
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWERONSTATE "\":%d}"), sysCfg.poweronstate);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_PULSETIME)) && (index > 0) && (index <= MAX_PULSETIMERS)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if (data_len > 0) {
|
2017-03-12 17:36:33 +00:00
|
|
|
sysCfg.pulsetime[index -1] = payload16; // 0 - 65535
|
|
|
|
pulse_timer[index -1] = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PULSETIME "%d\":%d}"), index, sysCfg.pulsetime[index -1]);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_BLINKTIME))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload > 2) && (payload <= 3600)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.blinktime = payload;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (blink_timer) {
|
|
|
|
blink_timer = sysCfg.blinktime;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BLINKTIME "\":%d}"), sysCfg.blinktime);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_BLINKCOUNT))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if (data_len > 0) {
|
|
|
|
sysCfg.blinkcount = payload16; // 0 - 65535
|
2017-04-25 17:24:42 +01:00
|
|
|
if (blink_counter) {
|
|
|
|
blink_counter = sysCfg.blinkcount *2;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BLINKCOUNT "\":%d}"), sysCfg.blinkcount);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
else if (sfl_flg && sl_command(type, index, dataBuf, data_len, payload)) {
|
2017-02-19 16:49:17 +00:00
|
|
|
// Serviced
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SAVEDATA))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 3600)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.savedata = payload;
|
|
|
|
savedatacounter = sysCfg.savedata;
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.savestate) {
|
2017-04-25 17:24:42 +01:00
|
|
|
sysCfg.power = power;
|
|
|
|
}
|
2017-06-19 21:54:49 +01:00
|
|
|
CFG_Save(0);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (sysCfg.savedata > 1) {
|
2017-09-02 13:37:02 +01:00
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR(D_EVERY " %d " D_UNIT_SECOND), sysCfg.savedata);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SAVEDATA "\":\"%s\"}"), (sysCfg.savedata > 1) ? stemp1 : getStateText(sysCfg.savedata));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-10-01 13:32:36 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SETOPTION)) && ((index >= 0) && (index <= 15)) || ((index > 31) && (index <= P_MAX_PARAM8 +31))) {
|
2017-06-16 13:33:49 +01:00
|
|
|
if (index <= 31) {
|
|
|
|
ptype = 0; // SetOption0 .. 31
|
|
|
|
} else {
|
|
|
|
ptype = 1; // SetOption32 ..
|
|
|
|
index = index -32;
|
|
|
|
}
|
|
|
|
if (payload >= 0) {
|
|
|
|
if (0 == ptype) { // SetOption0 .. 31
|
|
|
|
if (payload <= 1) {
|
|
|
|
switch (index) {
|
|
|
|
case 3: // mqtt
|
2017-10-01 13:32:36 +01:00
|
|
|
case 15: // pwm_control
|
2017-06-16 13:33:49 +01:00
|
|
|
restartflag = 2;
|
|
|
|
case 0: // savestate
|
|
|
|
case 1: // button_restrict
|
|
|
|
case 2: // value_units
|
|
|
|
case 4: // mqtt_response
|
|
|
|
case 8: // temperature_conversion
|
|
|
|
case 10: // mqtt_offline
|
|
|
|
case 11: // button_swap
|
2017-06-19 21:54:49 +01:00
|
|
|
case 12: // stop_flash_rotate
|
2017-07-25 17:05:47 +01:00
|
|
|
case 13: // button_single
|
2017-08-18 11:55:08 +01:00
|
|
|
case 14: // interlock
|
2017-06-16 13:33:49 +01:00
|
|
|
bitWrite(sysCfg.flag.data, index, payload);
|
|
|
|
}
|
2017-06-25 22:01:41 +01:00
|
|
|
if (12 == index) { // stop_flash_rotate
|
|
|
|
stop_flash_rotate = payload;
|
2017-06-30 16:54:19 +01:00
|
|
|
CFG_Save(2);
|
2017-06-19 21:54:49 +01:00
|
|
|
}
|
2017-06-16 13:33:49 +01:00
|
|
|
}
|
2017-05-11 16:47:34 +01:00
|
|
|
}
|
2017-06-16 13:33:49 +01:00
|
|
|
else { // SetOption32 ..
|
|
|
|
switch (index) {
|
|
|
|
case P_HOLD_TIME:
|
|
|
|
if ((payload >= 1) && (payload <= 100)) {
|
|
|
|
sysCfg.param[P_HOLD_TIME] = payload;
|
|
|
|
}
|
|
|
|
break;
|
2017-06-19 21:54:49 +01:00
|
|
|
case P_MAX_POWER_RETRY:
|
|
|
|
if ((payload >= 1) && (payload <= 250)) {
|
|
|
|
sysCfg.param[P_MAX_POWER_RETRY] = payload;
|
|
|
|
}
|
|
|
|
break;
|
2017-06-16 13:33:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ptype) {
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR("%d"), sysCfg.param[index]);
|
2017-05-11 16:47:34 +01:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SETOPTION "%d\":\"%s\"}"), (ptype) ? index +32 : index, (ptype) ? stemp1 : getStateText(bitRead(sysCfg.flag.data, index)));
|
2017-05-11 16:47:34 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_TEMPERATURE_RESOLUTION))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 3)) {
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.temperature_resolution = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TEMPERATURE_RESOLUTION "\":%d}"), sysCfg.flag.temperature_resolution);
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_HUMIDITY_RESOLUTION))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 3)) {
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.humidity_resolution = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_HUMIDITY_RESOLUTION "\":%d}"), sysCfg.flag.humidity_resolution);
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_PRESSURE_RESOLUTION))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 3)) {
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.pressure_resolution = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PRESSURE_RESOLUTION "\":%d}"), sysCfg.flag.pressure_resolution);
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_POWER_RESOLUTION))) {
|
2017-08-20 10:40:50 +01:00
|
|
|
if ((payload >= 0) && (payload <= 1)) {
|
|
|
|
sysCfg.flag.wattage_resolution = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWER_RESOLUTION "\":%d}"), sysCfg.flag.wattage_resolution);
|
2017-08-20 10:40:50 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_VOLTAGE_RESOLUTION))) {
|
2017-08-05 14:11:50 +01:00
|
|
|
if ((payload >= 0) && (payload <= 1)) {
|
|
|
|
sysCfg.flag.voltage_resolution = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_VOLTAGE_RESOLUTION "\":%d}"), sysCfg.flag.voltage_resolution);
|
2017-08-05 14:11:50 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_ENERGY_RESOLUTION))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 5)) {
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.energy_resolution = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_ENERGY_RESOLUTION "\":%d}"), sysCfg.flag.energy_resolution);
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_MODULE))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload > 0) && (payload <= MAXMODULE)) {
|
2017-03-19 17:19:08 +00:00
|
|
|
payload--;
|
|
|
|
byte new_modflg = (sysCfg.module != payload);
|
|
|
|
sysCfg.module = payload;
|
2017-04-03 15:38:15 +01:00
|
|
|
if (new_modflg) {
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
|
|
|
sysCfg.my_module.gp.io[i] = 0;
|
|
|
|
}
|
2017-04-03 15:38:15 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), modules[sysCfg.module].name);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MODULE "\":\"%d (%s)\"}"), sysCfg.module +1, stemp1);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_MODULES))) {
|
2017-09-14 14:35:38 +01:00
|
|
|
for (byte i = 0; i < MAXMODULE; i++) {
|
|
|
|
if (!jsflg) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MODULES "%d\":\""), lines);
|
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, "), mqtt_data);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
jsflg = 1;
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), modules[i].name);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%d (%s)"), mqtt_data, i +1, stemp1);
|
2017-09-14 14:35:38 +01:00
|
|
|
if ((strlen(mqtt_data) > 200) || (i == MAXMODULE -1)) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"}"), mqtt_data);
|
|
|
|
mqtt_publish_topic_P(5, type);
|
|
|
|
jsflg = 0;
|
|
|
|
lines++;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-14 14:35:38 +01:00
|
|
|
mqtt_data[0] = '\0';
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_GPIO)) && (index < MAX_GPIO_PIN)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
mytmplt cmodule;
|
|
|
|
memcpy_P(&cmodule, &modules[sysCfg.module], sizeof(cmodule));
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((GPIO_USER == cmodule.gp.io[index]) && (payload >= 0) && (payload < GPIO_SENSOR_END)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((GPIO_USER == cmodule.gp.io[i]) && (sysCfg.my_module.gp.io[i] == payload)) {
|
|
|
|
sysCfg.my_module.gp.io[i] = 0;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
sysCfg.my_module.gp.io[index] = payload;
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{"));
|
2017-01-28 13:41:01 +00:00
|
|
|
byte jsflg = 0;
|
|
|
|
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (GPIO_USER == cmodule.gp.io[i]) {
|
|
|
|
if (jsflg) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, "), mqtt_data);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
jsflg = 1;
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), sensors[sysCfg.my_module.gp.io[i]]);
|
2017-09-23 10:12:16 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"" D_CMND_GPIO "%d\":\"%d (%s)\""), mqtt_data, i, sysCfg.my_module.gp.io[i], stemp1);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (jsflg) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_GPIO "\":\"" D_NOT_SUPPORTED "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_GPIOS))) {
|
2017-09-14 14:35:38 +01:00
|
|
|
for (byte i = 0; i < GPIO_SENSOR_END; i++) {
|
|
|
|
if (!jsflg) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_GPIOS "%d\":\""), lines);
|
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, "), mqtt_data);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-03-09 16:48:04 +00:00
|
|
|
jsflg = 1;
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), sensors[i]);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%d (%s)"), mqtt_data, i, stemp1);
|
2017-09-14 14:35:38 +01:00
|
|
|
if ((strlen(mqtt_data) > 200) || (i == GPIO_SENSOR_END -1)) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"}"), mqtt_data);
|
|
|
|
mqtt_publish_topic_P(5, type);
|
|
|
|
jsflg = 0;
|
|
|
|
lines++;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-14 14:35:38 +01:00
|
|
|
mqtt_data[0] = '\0';
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-10-05 12:28:31 +01:00
|
|
|
else if (!sfl_flg && !strcasecmp_P(type, PSTR(D_CMND_PWM)) && (index > 0) && (index <= 5)) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= PWM_RANGE) && (pin[GPIO_PWM1 + index -1] < 99)) {
|
2017-03-19 17:19:08 +00:00
|
|
|
sysCfg.pwmvalue[index -1] = payload;
|
2017-10-04 16:29:49 +01:00
|
|
|
analogWrite(pin[GPIO_PWM1 + index -1], pwm_inverted[index -1] ? PWM_RANGE - payload : payload);
|
2017-03-19 17:19:08 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PWM "\":{"));
|
2017-03-19 17:19:08 +00:00
|
|
|
bool first = true;
|
|
|
|
for (byte i = 0; i < 5; i++) {
|
|
|
|
if(pin[GPIO_PWM1 + i] < 99) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"" D_CMND_PWM "%d\":%d"), mqtt_data, first ? "" : ", ", i+1, sysCfg.pwmvalue[i]);
|
2017-03-19 17:19:08 +00:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}}"),mqtt_data);
|
2017-03-19 17:19:08 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_COUNTER)) && (index > 0) && (index <= MAX_COUNTERS)) {
|
2017-05-17 21:49:22 +01:00
|
|
|
if ((data_len > 0) && (pin[GPIO_CNTR1 + index -1] < 99)) {
|
|
|
|
rtcMem.pCounter[index -1] = payload16;
|
|
|
|
sysCfg.pCounter[index -1] = payload16;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COUNTER "%d\":%d}"), index, rtcMem.pCounter[index -1]);
|
2017-05-17 21:49:22 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_COUNTERTYPE)) && (index > 0) && (index <= MAX_COUNTERS)) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 1) && (pin[GPIO_CNTR1 + index -1] < 99)) {
|
2017-05-17 21:49:22 +01:00
|
|
|
bitWrite(sysCfg.pCounterType, index -1, payload &1);
|
|
|
|
rtcMem.pCounter[index -1] = 0;
|
|
|
|
sysCfg.pCounter[index -1] = 0;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COUNTERTYPE "%d\":%d}"), index, bitRead(sysCfg.pCounterType, index -1));
|
2017-05-17 21:49:22 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_COUNTERDEBOUNCE))) {
|
2017-06-19 21:54:49 +01:00
|
|
|
if ((data_len > 0) && (payload16 < 32001)) {
|
2017-05-17 21:49:22 +01:00
|
|
|
sysCfg.pCounterDebounce = payload16;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COUNTERDEBOUNCE "\":%d}"), sysCfg.pCounterDebounce);
|
2017-05-17 21:49:22 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SLEEP))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload < 251)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((!sysCfg.sleep && payload) || (sysCfg.sleep && !payload)) {
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.sleep = payload;
|
2017-01-29 13:52:31 +00:00
|
|
|
sleep = payload;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SLEEP "\":\"%d%s (%d%s)\"}"), sleep, (sysCfg.flag.value_units) ? " " D_UNIT_MILLISECOND : "", sysCfg.sleep, (sysCfg.flag.value_units) ? " " D_UNIT_MILLISECOND : "");
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_UPGRADE)) || !strcasecmp_P(type, PSTR(D_CMND_UPLOAD))) {
|
2017-06-30 16:54:19 +01:00
|
|
|
// Check if the payload is numerically 1, and had no trailing chars.
|
|
|
|
// e.g. "1foo" or "1.2.3" could fool us.
|
|
|
|
// Check if the version we have been asked to upgrade to is higher than our current version.
|
|
|
|
// We also need at least 3 chars to make a valid version number string.
|
|
|
|
if (((1 == data_len) && (1 == payload)) || ((data_len >= 3) && newerVersion(dataBuf))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
otaflag = 3;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), "{\"" D_CMND_UPGRADE "\":\"" D_VERSION " %s " D_FROM " %s\"}", Version, sysCfg.otaUrl);
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), "{\"" D_CMND_UPGRADE "\":\"" D_ONE_OR_GT "\"}", Version);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_OTAURL))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.otaUrl)))
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.otaUrl, (1 == payload) ? OTA_URL : dataBuf, sizeof(sysCfg.otaUrl));
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_OTAURL "\":\"%s\"}"), sysCfg.otaUrl);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SERIALLOG))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= LOG_LEVEL_NONE) && (payload <= LOG_LEVEL_ALL)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.seriallog_level = payload;
|
|
|
|
seriallog_level = payload;
|
|
|
|
seriallog_timer = 0;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SERIALLOG "\":\"%d (" D_ACTIVE " %d)\"}"), sysCfg.seriallog_level, seriallog_level);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SYSLOG))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= LOG_LEVEL_NONE) && (payload <= LOG_LEVEL_ALL)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.syslog_level = payload;
|
2017-05-03 17:19:13 +01:00
|
|
|
syslog_level = (sysCfg.flag.emulation) ? 0 : payload;
|
2017-01-28 13:41:01 +00:00
|
|
|
syslog_timer = 0;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SYSLOG "\":\"%d (" D_ACTIVE " %d)\"}"), sysCfg.syslog_level, syslog_level);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_LOGHOST))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.syslog_host))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.syslog_host, (1 == payload) ? SYS_LOG_HOST : dataBuf, sizeof(sysCfg.syslog_host));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LOGHOST "\":\"%s\"}"), sysCfg.syslog_host);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_LOGPORT))) {
|
2017-09-12 09:19:16 +01:00
|
|
|
if (payload16 > 0) {
|
|
|
|
sysCfg.syslog_port = (1 == payload16) ? SYS_LOG_PORT : payload16;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LOGPORT "\":%d}"), sysCfg.syslog_port);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_IPADDRESS)) && (index > 0) && (index <= 4)) {
|
2017-03-25 16:24:11 +00:00
|
|
|
if (parseIP(&address, dataBuf)) {
|
2017-03-29 17:42:05 +01:00
|
|
|
sysCfg.ip_address[index -1] = address;
|
|
|
|
// restartflag = 2;
|
2017-03-25 16:24:11 +00:00
|
|
|
}
|
2017-03-29 17:42:05 +01:00
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR(" (%s)"), WiFi.localIP().toString().c_str());
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_IPADDRESS "%d\":\"%s%s\"}"), index, IPAddress(sysCfg.ip_address[index -1]).toString().c_str(), (1 == index) ? stemp1:"");
|
2017-03-25 16:24:11 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_NTPSERVER)) && (index > 0) && (index <= 3)) {
|
2017-03-12 17:36:33 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.ntp_server[0]))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.ntp_server[index -1], (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? (1==index)?NTP_SERVER1:(2==index)?NTP_SERVER2:NTP_SERVER3 : dataBuf, sizeof(sysCfg.ntp_server[0]));
|
|
|
|
for (i = 0; i < strlen(sysCfg.ntp_server[index -1]); i++) {
|
|
|
|
if (sysCfg.ntp_server[index -1][i] == ',') {
|
|
|
|
sysCfg.ntp_server[index -1][i] = '.';
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 17:36:33 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_NTPSERVER "%d\":\"%s\"}"), index, sysCfg.ntp_server[index -1]);
|
2017-03-12 17:36:33 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_AP))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 2)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
switch (payload) {
|
|
|
|
case 0: // Toggle
|
|
|
|
sysCfg.sta_active ^= 1;
|
|
|
|
break;
|
|
|
|
case 1: // AP1
|
|
|
|
case 2: // AP2
|
|
|
|
sysCfg.sta_active = payload -1;
|
|
|
|
}
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_AP "\":\"%d (%s)\"}"), sysCfg.sta_active +1, sysCfg.sta_ssid[sysCfg.sta_active]);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SSID)) && (index > 0) && (index <= 2)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.sta_ssid[0]))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.sta_ssid[index -1], (1 == payload) ? (1 == index) ? STA_SSID1 : STA_SSID2 : dataBuf, sizeof(sysCfg.sta_ssid[0]));
|
2017-09-16 13:19:47 +01:00
|
|
|
sysCfg.sta_active = index -1;
|
2017-01-28 13:41:01 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SSID "%d\":\"%s\"}"), index, sysCfg.sta_ssid[index -1]);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_PASSWORD)) && (index > 0) && (index <= 2)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.sta_pwd[0]))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.sta_pwd[index -1], (1 == payload) ? (1 == index) ? STA_PASS1 : STA_PASS2 : dataBuf, sizeof(sysCfg.sta_pwd[0]));
|
2017-09-16 13:19:47 +01:00
|
|
|
sysCfg.sta_active = index -1;
|
2017-01-28 13:41:01 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PASSWORD "%d\":\"%s\"}"), index, sysCfg.sta_pwd[index -1]);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_HOSTNAME))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.hostname))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.hostname, (1 == payload) ? WIFI_HOSTNAME : dataBuf, sizeof(sysCfg.hostname));
|
|
|
|
if (strstr(sysCfg.hostname,"%")) {
|
|
|
|
strlcpy(sysCfg.hostname, WIFI_HOSTNAME, sizeof(sysCfg.hostname));
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_HOSTNAME "\":\"%s\"}"), sysCfg.hostname);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_WIFICONFIG))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= WIFI_RESTART) && (payload < MAX_WIFI_OPTION)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.sta_config = payload;
|
|
|
|
wificheckflag = sysCfg.sta_config;
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), wificfg[sysCfg.sta_config]);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WIFICONFIG "\":\"%s " D_SELECTED "\"}"), stemp1);
|
2017-01-28 13:41:01 +00:00
|
|
|
if (WIFI_State() != WIFI_RESTART) {
|
2017-09-13 13:19:34 +01:00
|
|
|
// snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s after restart"), mqtt_data);
|
2017-01-28 13:41:01 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), wificfg[sysCfg.sta_config]);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WIFICONFIG "\":\"%d (%s)\"}"), sysCfg.sta_config, stemp1);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_FRIENDLYNAME)) && (index > 0) && (index <= 4)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.friendlyname[0]))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (1 == index) {
|
2017-01-29 20:36:12 +00:00
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR(FRIENDLY_NAME));
|
|
|
|
} else {
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR(FRIENDLY_NAME "%d"), index);
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.friendlyname[index -1], (1 == payload) ? stemp1 : dataBuf, sizeof(sysCfg.friendlyname[index -1]));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_FRIENDLYNAME "%d\":\"%s\"}"), index, sysCfg.friendlyname[index -1]);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SWITCHMODE)) && (index > 0) && (index <= 4)) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload < MAX_SWITCH_OPTION)) {
|
2017-02-24 17:17:48 +00:00
|
|
|
sysCfg.switchmode[index -1] = payload;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SWITCHMODE "%d\":%d}"), index, sysCfg.switchmode[index-1]);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#ifdef USE_WEBSERVER
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_WEBSERVER))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 2)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.webserver = payload;
|
|
|
|
}
|
|
|
|
if (sysCfg.webserver) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBSERVER "\":\"" D_ACTIVE_FOR " %s " D_ON_DEVICE " %s " D_WITH_IP_ADDRESS " %s\"}"),
|
2017-09-02 13:37:02 +01:00
|
|
|
(2 == sysCfg.webserver) ? D_ADMIN : D_USER, Hostname, WiFi.localIP().toString().c_str());
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBSERVER "\":\"%s\"}"), getStateText(0));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_WEBPASSWORD))) {
|
2017-02-21 17:14:33 +00:00
|
|
|
if ((data_len > 0) && (data_len < sizeof(sysCfg.web_password))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
strlcpy(sysCfg.web_password, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? WEB_PASSWORD : dataBuf, sizeof(sysCfg.web_password));
|
2017-02-21 17:14:33 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBPASSWORD "\":\"%s\"}"), sysCfg.web_password);
|
2017-02-21 17:14:33 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_WEBLOG))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= LOG_LEVEL_NONE) && (payload <= LOG_LEVEL_ALL)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.weblog_level = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBLOG "\":%d}"), sysCfg.weblog_level);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-01-29 20:36:12 +00:00
|
|
|
#ifdef USE_EMULATION
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_EMULATION))) {
|
2017-09-26 14:10:58 +01:00
|
|
|
if ((payload >= EMUL_NONE) && (payload < EMUL_MAX)) {
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.emulation = payload;
|
2017-01-29 20:36:12 +00:00
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_EMULATION "\":%d}"), sysCfg.flag.emulation);
|
2017-01-29 20:36:12 +00:00
|
|
|
}
|
|
|
|
#endif // USE_EMULATION
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_TELEPERIOD))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload < 3601)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
sysCfg.tele_period = (1 == payload) ? TELE_PERIOD : payload;
|
|
|
|
if ((sysCfg.tele_period > 0) && (sysCfg.tele_period < 10)) {
|
|
|
|
sysCfg.tele_period = 10; // Do not allow periods < 10 seconds
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
tele_period = sysCfg.tele_period;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TELEPERIOD "\":\"%d%s\"}"), sysCfg.tele_period, (sysCfg.flag.value_units) ? " " D_UNIT_SECOND : "");
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_RESTART))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
switch (payload) {
|
|
|
|
case 1:
|
|
|
|
restartflag = 2;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESTART "\":\"" D_RESTARTING "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
case 99:
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_RESTARTING));
|
2017-01-28 13:41:01 +00:00
|
|
|
ESP.restart();
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESTART "\":\"" D_ONE_TO_RESTART "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_RESET))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
switch (payload) {
|
|
|
|
case 1:
|
|
|
|
restartflag = 211;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESET "\":\"" D_RESET_AND_RESTARTING "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
restartflag = 212;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESET "\":\"" D_ERASE ", " D_RESET_AND_RESTARTING "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESET "\":\"" D_ONE_TO_RESET "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_TIMEZONE))) {
|
2017-10-05 13:10:42 +01:00
|
|
|
if ((data_len > 0) && (((payload >= -13) && (payload <= 13)) || (99 == payload))) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.timezone = payload;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TIMEZONE "\":%d}"), sysCfg.timezone);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_LEDPOWER))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 2)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.ledstate &= 8;
|
|
|
|
switch (payload) {
|
|
|
|
case 0: // Off
|
|
|
|
case 1: // On
|
|
|
|
sysCfg.ledstate = payload << 3;
|
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
|
|
|
sysCfg.ledstate ^= 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
blinks = 0;
|
|
|
|
setLed(sysCfg.ledstate &8);
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LEDPOWER "\":\"%s\"}"), getStateText(bitRead(sysCfg.ledstate, 3)));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_LEDSTATE))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload < MAX_LED_OPTION)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.ledstate = payload;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!sysCfg.ledstate) {
|
|
|
|
setLed(0);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LEDSTATE "\":%d}"), sysCfg.ledstate);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_CFGDUMP))) {
|
2017-07-30 16:55:37 +01:00
|
|
|
CFG_Dump(dataBuf);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_CFGDUMP "\":\"" D_DONE "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
else if (sysCfg.flag.mqtt_enabled && mqtt_command(grpflg, type, index, dataBuf, data_len, payload, payload16)) {
|
2017-01-28 15:31:11 +00:00
|
|
|
// Serviced
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
else if (hlw_flg && hlw_command(type, index, dataBuf, data_len, payload)) {
|
2017-01-28 15:31:11 +00:00
|
|
|
// Serviced
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
else if ((SONOFF_BRIDGE == sysCfg.module) && sb_command(type, index, dataBuf, data_len, payload)) {
|
2017-07-30 16:55:37 +01:00
|
|
|
// Serviced
|
|
|
|
}
|
2017-03-08 15:20:45 +00:00
|
|
|
#ifdef USE_I2C
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (i2c_flg && !strcasecmp_P(type, PSTR(D_CMND_I2CSCAN))) {
|
2017-09-13 13:19:34 +01:00
|
|
|
i2c_scan(mqtt_data, sizeof(mqtt_data));
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
|
|
|
#endif // USE_I2C
|
2017-02-14 13:27:08 +00:00
|
|
|
#ifdef USE_IR_REMOTE
|
2017-09-16 16:34:03 +01:00
|
|
|
else if ((pin[GPIO_IRSEND] < 99) && ir_send_command(type, index, dataBuf, data_len, payload)) {
|
2017-02-14 13:27:08 +00:00
|
|
|
// Serviced
|
|
|
|
}
|
2017-02-28 15:01:48 +00:00
|
|
|
#endif // USE_IR_REMOTE
|
|
|
|
#ifdef DEBUG_THEO
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_EXCEPTION))) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (data_len > 0) {
|
|
|
|
exception_tst(payload);
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_EXCEPTION "\":\"" D_DONE "\"}"));
|
2017-02-28 15:01:48 +00:00
|
|
|
}
|
|
|
|
#endif // DEBUG_THEO
|
2017-01-28 13:41:01 +00:00
|
|
|
else {
|
|
|
|
type = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type == NULL) {
|
|
|
|
blinks = 201;
|
2017-09-02 13:37:02 +01:00
|
|
|
snprintf_P(topicBuf, sizeof(topicBuf), PSTR(D_COMMAND));
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_COMMAND "\":\"" D_UNKNOWN "\"}"));
|
2017-04-03 15:38:15 +01:00
|
|
|
type = (char*)topicBuf;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
if (mqtt_data[0] != '\0') {
|
|
|
|
mqtt_publish_topic_P(5, type);
|
2017-05-17 21:49:22 +01:00
|
|
|
}
|
2017-06-30 16:54:19 +01:00
|
|
|
fallbacktopic = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-06-06 22:23:23 +01:00
|
|
|
boolean send_button_power(byte key, byte device, byte state)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
// key 0 = button_topic
|
|
|
|
// key 1 = switch_topic
|
2017-06-06 22:23:23 +01:00
|
|
|
// state 0 = off
|
|
|
|
// state 1 = on
|
|
|
|
// state 2 = toggle
|
|
|
|
// state 3 = hold
|
|
|
|
// state 9 = clear retain flag
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
char stopic[TOPSZ];
|
2017-05-08 12:21:45 +01:00
|
|
|
char scommand[CMDSZ];
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp1[10];
|
2017-06-06 22:23:23 +01:00
|
|
|
boolean result = false;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-06-06 22:23:23 +01:00
|
|
|
char *key_topic = (key) ? sysCfg.switch_topic : sysCfg.button_topic;
|
|
|
|
if (sysCfg.flag.mqtt_enabled && mqttClient.connected() && (strlen(key_topic) != 0) && strcmp(key_topic, "0")) {
|
|
|
|
if (!key && (device > Maxdevice)) {
|
|
|
|
device = 1;
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
|
|
|
// snprintf_P(stemp1, sizeof(stemp1), PSTR("%d"), device);
|
|
|
|
// snprintf_P(scommand, sizeof(scommand), PSTR("POWER%s"), (key || (Maxdevice > 1)) ? stemp1 : "");
|
|
|
|
// getTopic_P(stopic, 0, key_topic, scommand);
|
|
|
|
|
|
|
|
getTopic_P(stopic, 0, key_topic, getPowerDevice(scommand, device, sizeof(scommand), key));
|
|
|
|
|
2017-06-06 22:23:23 +01:00
|
|
|
if (9 == state) {
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
2017-06-06 22:23:23 +01:00
|
|
|
} else {
|
2017-07-30 16:55:37 +01:00
|
|
|
if ((!strcmp(sysCfg.mqtt_topic, key_topic) || !strcmp(sysCfg.mqtt_grptopic, key_topic)) && (2 == state)) {
|
2017-06-06 22:23:23 +01:00
|
|
|
state = ~(power >> (device -1)) & 0x01;
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), getStateText(state));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#ifdef USE_DOMOTICZ
|
2017-09-13 13:19:34 +01:00
|
|
|
if (!(domoticz_button(key, device, state, strlen(mqtt_data)))) {
|
|
|
|
mqtt_publish_sec(stopic, (key) ? sysCfg.flag.mqtt_switch_retain : sysCfg.flag.mqtt_button_retain);
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#else
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_sec(stopic, (key) ? sysCfg.flag.mqtt_switch_retain : sysCfg.flag.mqtt_button_retain);
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
2017-06-06 22:23:23 +01:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return result;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_cmnd_power(byte device, byte state)
|
|
|
|
{
|
|
|
|
// device = Relay number 1 and up
|
|
|
|
// state 0 = Relay Off
|
2017-07-15 14:07:30 +01:00
|
|
|
// state 1 = Relay On (turn off after sysCfg.pulsetime * 100 mSec if enabled)
|
2017-01-28 13:41:01 +00:00
|
|
|
// state 2 = Toggle relay
|
|
|
|
// state 3 = Blink relay
|
|
|
|
// state 4 = Stop blinking relay
|
2017-07-15 14:07:30 +01:00
|
|
|
// state 6 = Relay Off and no publishPowerState
|
|
|
|
// state 7 = Relay On and no publishPowerState
|
2017-01-28 13:41:01 +00:00
|
|
|
// state 9 = Show power state
|
|
|
|
|
2017-07-15 14:07:30 +01:00
|
|
|
uint8_t publishPower = 1;
|
|
|
|
if ((6 == state) || (7 == state)) {
|
|
|
|
state &= 1;
|
|
|
|
publishPower = 0;
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((device < 1) || (device > Maxdevice)) {
|
|
|
|
device = 1;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
byte mask = 0x01 << (device -1);
|
2017-03-12 17:36:33 +00:00
|
|
|
pulse_timer[(device -1)&3] = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
if (state <= 2) {
|
|
|
|
if ((blink_mask & mask)) {
|
|
|
|
blink_mask &= (0xFF ^ mask); // Clear device mask
|
|
|
|
mqtt_publishPowerBlinkState(device);
|
|
|
|
}
|
2017-08-18 11:55:08 +01:00
|
|
|
if (sysCfg.flag.interlock && !interlockmutex) { // Clear all but masked relay
|
|
|
|
interlockmutex = 1;
|
|
|
|
for (byte i = 0; i < Maxdevice; i++) {
|
|
|
|
byte imask = 0x01 << i;
|
|
|
|
if ((power & imask) && (mask != imask)) {
|
|
|
|
do_cmnd_power(i +1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
interlockmutex = 0;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
switch (state) {
|
|
|
|
case 0: { // Off
|
|
|
|
power &= (0xFF ^ mask);
|
|
|
|
break; }
|
|
|
|
case 1: // On
|
|
|
|
power |= mask;
|
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
|
|
|
power ^= mask;
|
|
|
|
}
|
|
|
|
setRelay(power);
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
domoticz_updatePowerState(device);
|
|
|
|
#endif // USE_DOMOTICZ
|
2017-03-12 17:36:33 +00:00
|
|
|
pulse_timer[(device -1)&3] = (power & mask) ? sysCfg.pulsetime[(device -1)&3] : 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
else if (3 == state) { // Blink
|
2017-01-28 13:41:01 +00:00
|
|
|
if (!(blink_mask & mask)) {
|
|
|
|
blink_powersave = (blink_powersave & (0xFF ^ mask)) | (power & mask); // Save state
|
|
|
|
blink_power = (power >> (device -1))&1; // Prep to Toggle
|
|
|
|
}
|
|
|
|
blink_timer = 1;
|
|
|
|
blink_counter = ((!sysCfg.blinkcount) ? 64000 : (sysCfg.blinkcount *2)) +1;
|
|
|
|
blink_mask |= mask; // Set device mask
|
|
|
|
mqtt_publishPowerBlinkState(device);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
else if (4 == state) { // No Blink
|
2017-01-28 13:41:01 +00:00
|
|
|
byte flag = (blink_mask & mask);
|
|
|
|
blink_mask &= (0xFF ^ mask); // Clear device mask
|
|
|
|
mqtt_publishPowerBlinkState(device);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (flag) {
|
|
|
|
do_cmnd_power(device, (blink_powersave >> (device -1))&1); // Restore state
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
if (publishPower) {
|
|
|
|
mqtt_publishPowerState(device);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void stop_all_power_blink()
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
byte mask;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 1; i <= Maxdevice; i++) {
|
2017-01-28 13:41:01 +00:00
|
|
|
mask = 0x01 << (i -1);
|
|
|
|
if (blink_mask & mask) {
|
|
|
|
blink_mask &= (0xFF ^ mask); // Clear device mask
|
|
|
|
mqtt_publishPowerBlinkState(i);
|
|
|
|
do_cmnd_power(i, (blink_powersave >> (i -1))&1); // Restore state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_cmnd(char *cmnd)
|
|
|
|
{
|
2017-05-08 12:21:45 +01:00
|
|
|
char stopic[CMDSZ];
|
2017-07-15 14:07:30 +01:00
|
|
|
char svalue[INPUT_BUFFER_SIZE];
|
2017-01-28 13:41:01 +00:00
|
|
|
char *start;
|
|
|
|
char *token;
|
|
|
|
|
|
|
|
token = strtok(cmnd, " ");
|
|
|
|
if (token != NULL) {
|
|
|
|
start = strrchr(token, '/'); // Skip possible cmnd/sonoff/ preamble
|
2017-04-25 17:24:42 +01:00
|
|
|
if (start) {
|
2017-05-08 12:21:45 +01:00
|
|
|
token = start +1;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-05-11 16:47:34 +01:00
|
|
|
snprintf_P(stopic, sizeof(stopic), PSTR("/%s"), (token == NULL) ? "" : token);
|
2017-01-28 13:41:01 +00:00
|
|
|
token = strtok(NULL, "");
|
2017-09-26 14:10:58 +01:00
|
|
|
// snprintf_P(svalue, sizeof(svalue), (token == NULL) ? "" : token); // Fails with command FullTopic home/%prefix%/%topic% as it processes %p of %prefix%
|
|
|
|
strlcpy(svalue, (token == NULL) ? "" : token, sizeof(svalue)); // Fixed 5.8.0b
|
2017-01-28 13:41:01 +00:00
|
|
|
mqttDataCb(stopic, (byte*)svalue, strlen(svalue));
|
|
|
|
}
|
|
|
|
|
|
|
|
void publish_status(uint8_t payload)
|
|
|
|
{
|
2017-05-10 13:19:36 +01:00
|
|
|
uint8_t option = 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
// Workaround MQTT - TCP/IP stack queueing when SUB_PREFIX = PUB_PREFIX
|
2017-05-10 13:19:36 +01:00
|
|
|
if (!strcmp(sysCfg.mqtt_prefix[0],sysCfg.mqtt_prefix[1]) && (!payload)) {
|
|
|
|
option++;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if ((!sysCfg.flag.mqtt_enabled) && (6 == payload)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
payload = 99;
|
|
|
|
}
|
|
|
|
if ((!hlw_flg) && ((8 == payload) || (9 == payload))) {
|
|
|
|
payload = 99;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (99 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS "\":{\"" D_CMND_MODULE "\":%d, \"" D_CMND_FRIENDLYNAME "\":\"%s\", \"" D_CMND_TOPIC "\":\"%s\", \"" D_CMND_BUTTONTOPIC "\":\"%s\", \"" D_CMND_POWER "\":%d, \"" D_CMND_POWERONSTATE "\":%d, \"" D_CMND_LEDSTATE "\":%d, \"" D_CMND_SAVEDATA "\":%d, \"" D_SAVESTATE "\":%d, \"" D_CMND_BUTTONRETAIN "\":%d, \"" D_CMND_POWERRETAIN "\":%d}}"),
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.module +1, sysCfg.friendlyname[0], sysCfg.mqtt_topic, sysCfg.button_topic, power, sysCfg.poweronstate, sysCfg.ledstate, sysCfg.savedata, sysCfg.flag.savestate, sysCfg.flag.mqtt_button_retain, sysCfg.flag.mqtt_power_retain);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (1 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS1_PARAMETER "\":{\"" D_BAUDRATE "\":%d, \"" D_CMND_GROUPTOPIC "\":\"%s\", \"" D_CMND_OTAURL "\":\"%s\", \"" D_UPTIME "\":%d, \"" D_CMND_SLEEP "\":%d, \"" D_BOOTCOUNT "\":%d, \"" D_SAVECOUNT "\":%d, \"" D_SAVEADDRESS "\":\"%X\"}}"),
|
2017-06-25 22:01:41 +01:00
|
|
|
Baudrate, sysCfg.mqtt_grptopic, sysCfg.otaUrl, uptime, sysCfg.sleep, sysCfg.bootcount, sysCfg.saveFlag, CFG_Address());
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "1"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (2 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS2_FIRMWARE "\":{\"" D_VERSION "\":\"%s\", \"" D_BUILDDATETIME "\":\"%s\", \"" D_BOOTVERSION "\":%d, \"" D_COREVERSION "\":\"%s\", \"" D_SDKVERSION "\":\"%s\"}}"),
|
2017-03-08 15:20:45 +00:00
|
|
|
Version, getBuildDateTime().c_str(), ESP.getBootVersion(), ESP.getCoreVersion().c_str(), ESP.getSdkVersion());
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "2"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (3 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS3_LOGGING "\":{\"" D_CMND_SERIALLOG "\":%d, \"" D_CMND_WEBLOG "\":%d, \"" D_CMND_SYSLOG "\":%d, \"" D_CMND_LOGHOST "\":\"%s\", \"" D_CMND_LOGPORT "\":%d, \"" D_CMND_SSID "1\":\"%s\", \"" D_CMND_SSID "2\":\"%s\", \"" D_CMND_TELEPERIOD "\":%d, \"" D_CMND_SETOPTION "\":\"%08X\"}}"),
|
2017-09-12 09:19:16 +01:00
|
|
|
sysCfg.seriallog_level, sysCfg.weblog_level, sysCfg.syslog_level, sysCfg.syslog_host, sysCfg.syslog_port, sysCfg.sta_ssid[0], sysCfg.sta_ssid[1], sysCfg.tele_period, sysCfg.flag.data);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "3"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (4 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS4_MEMORY "\":{\"" D_PROGRAMSIZE "\":%d, \"" D_FREEMEMORY "\":%d, \"" D_HEAPSIZE "\":%d, \"" D_PROGRAMFLASHSIZE "\":%d, \"" D_FLASHSIZE "\":%d, \"" D_FLASHMODE "\":%d}}"),
|
2017-04-25 17:24:42 +01:00
|
|
|
ESP.getSketchSize()/1024, ESP.getFreeSketchSpace()/1024, ESP.getFreeHeap()/1024, ESP.getFlashChipSize()/1024, ESP.getFlashChipRealSize()/1024, ESP.getFlashChipMode());
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "4"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (5 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS5_NETWORK "\":{\"" D_CMND_HOSTNAME "\":\"%s\", \"" D_CMND_IPADDRESS "\":\"%s\", \"" D_GATEWAY "\":\"%s\", \"" D_SUBNETMASK "\":\"%s\", \"" D_DNSSERVER "\":\"%s\", \"" D_MAC "\":\"%s\", \"" D_CMND_WEBSERVER "\":%d, \"" D_CMND_WIFICONFIG "\":%d}}"),
|
2017-03-25 16:24:11 +00:00
|
|
|
Hostname, WiFi.localIP().toString().c_str(), IPAddress(sysCfg.ip_address[1]).toString().c_str(), IPAddress(sysCfg.ip_address[2]).toString().c_str(), IPAddress(sysCfg.ip_address[3]).toString().c_str(),
|
2017-01-28 13:41:01 +00:00
|
|
|
WiFi.macAddress().c_str(), sysCfg.webserver, sysCfg.sta_config);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "5"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if (((0 == payload) || (6 == payload)) && sysCfg.flag.mqtt_enabled) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS6_MQTT "\":{\"" D_CMND_MQTTHOST "\":\"%s\", \"" D_CMND_MQTTPORT "\":%d, \"" D_CMND_MQTTCLIENT D_MASK "\":\"%s\", \"" D_CMND_MQTTCLIENT "\":\"%s\", \"" D_CMND_MQTTUSER "\":\"%s\", \"MAX_PACKET_SIZE\":%d, \"KEEPALIVE\":%d}}"),
|
2017-01-28 13:41:01 +00:00
|
|
|
sysCfg.mqtt_host, sysCfg.mqtt_port, sysCfg.mqtt_client, MQTTClient, sysCfg.mqtt_user, MQTT_MAX_PACKET_SIZE, MQTT_KEEPALIVE);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "6"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (7 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS7_TIME "\":{\"" D_UTC_TIME "\":\"%s\", \"" D_LOCAL_TIME "\":\"%s\", \"" D_STARTDST "\":\"%s\", \"" D_ENDDST "\":\"%s\", \"" D_CMND_TIMEZONE "\":%d}}"),
|
2017-01-28 13:41:01 +00:00
|
|
|
rtc_time(0).c_str(), rtc_time(1).c_str(), rtc_time(2).c_str(), rtc_time(3).c_str(), sysCfg.timezone);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "7"));
|
2017-02-04 16:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hlw_flg) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (8 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
hlw_mqttStatus();
|
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "8"));
|
2017-02-04 16:09:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (9 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS9_MARGIN "\":{\"" D_CMND_POWERLOW "\":%d, \"" D_CMND_POWERHIGH "\":%d, \"" D_CMND_VOLTAGELOW "\":%d, \"" D_CMND_VOLTAGEHIGH "\":%d, \"" D_CMND_CURRENTLOW "\":%d, \"" D_CMND_CURRENTHIGH "\":%d}}"),
|
2017-02-04 16:09:54 +00:00
|
|
|
sysCfg.hlw_pmin, sysCfg.hlw_pmax, sysCfg.hlw_umin, sysCfg.hlw_umax, sysCfg.hlw_imin, sysCfg.hlw_imax);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "9"));
|
2017-02-04 16:09:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (10 == payload)) {
|
2017-02-04 16:09:54 +00:00
|
|
|
uint8_t djson = 0;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS10_SENSOR "\":"));
|
|
|
|
sensors_mqttPresent(&djson);
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
|
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "10"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-03-14 17:03:25 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == payload) || (11 == payload)) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS11_STATUS "\":"));
|
|
|
|
state_mqttPresent();
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
|
|
|
|
mqtt_publish_topic_P(option, PSTR(D_CMND_STATUS "11"));
|
2017-03-14 17:03:25 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-03-14 17:03:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void state_mqttPresent()
|
2017-03-14 17:03:25 +00:00
|
|
|
{
|
2017-09-02 13:37:02 +01:00
|
|
|
char stemp1[16];
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_TIME "\":\"%s\", \"" D_UPTIME "\":%d"), mqtt_data, getDateTime().c_str(), uptime);
|
2017-03-14 17:03:25 +00:00
|
|
|
#ifdef USE_ADC_VCC
|
2017-09-02 13:37:02 +01:00
|
|
|
dtostrfd((double)ESP.getVcc()/1000, 3, stemp1);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_VCC "\":%s"), mqtt_data, stemp1);
|
2017-09-02 13:37:02 +01:00
|
|
|
#endif
|
2017-03-14 17:03:25 +00:00
|
|
|
for (byte i = 0; i < Maxdevice; i++) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"%s\":\"%s\""), mqtt_data, getPowerDevice(stemp1, i +1, sizeof(stemp1)), getStateText(bitRead(power, i)));
|
2017-03-14 17:03:25 +00:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_WIFI "\":{\"" D_AP "\":%d, \"" D_SSID "\":\"%s\", \"" D_RSSI "\":%d, \"" D_APMAC_ADDRESS "\":\"%s\"}}"),
|
|
|
|
mqtt_data, sysCfg.sta_active +1, sysCfg.sta_ssid[sysCfg.sta_active], WIFI_getRSSIasQuality(WiFi.RSSI()), WiFi.BSSIDstr().c_str());
|
2017-02-04 16:09:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
void sensors_mqttPresent(uint8_t* djson)
|
2017-02-04 16:09:54 +00:00
|
|
|
{
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_TIME "\":\"%s\""), mqtt_data, getDateTime().c_str());
|
2017-03-19 17:19:08 +00:00
|
|
|
for (byte i = 0; i < 4; i++) {
|
|
|
|
if (pin[GPIO_SWT1 +i] < 99) {
|
2017-06-06 22:23:23 +01:00
|
|
|
boolean swm = ((FOLLOW_INV == sysCfg.switchmode[i]) || (PUSHBUTTON_INV == sysCfg.switchmode[i]) || (PUSHBUTTONHOLD_INV == sysCfg.switchmode[i]));
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_SWITCH "%d\":\"%s\""), mqtt_data, i +1, getStateText(swm ^ lastwallswitch[i]));
|
2017-03-29 17:42:05 +01:00
|
|
|
*djson = 1;
|
2017-03-19 17:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
counter_mqttPresent(djson);
|
2017-03-14 17:03:25 +00:00
|
|
|
#ifndef USE_ADC_VCC
|
|
|
|
if (pin[GPIO_ADC0] < 99) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_ANALOG_INPUT0 "\":%d"), mqtt_data, getAdc0());
|
2017-03-14 17:03:25 +00:00
|
|
|
*djson = 1;
|
|
|
|
}
|
2017-04-10 16:25:31 +01:00
|
|
|
#endif
|
2017-04-25 17:24:42 +01:00
|
|
|
if (SONOFF_SC == sysCfg.module) {
|
2017-09-13 13:19:34 +01:00
|
|
|
sc_mqttPresent(djson);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-02-04 16:09:54 +00:00
|
|
|
if (pin[GPIO_DSB] < 99) {
|
|
|
|
#ifdef USE_DS18B20
|
2017-09-13 13:19:34 +01:00
|
|
|
dsb_mqttPresent(djson);
|
2017-02-04 16:09:54 +00:00
|
|
|
#endif // USE_DS18B20
|
|
|
|
#ifdef USE_DS18x20
|
2017-09-13 13:19:34 +01:00
|
|
|
ds18x20_mqttPresent(djson);
|
2017-02-04 16:09:54 +00:00
|
|
|
#endif // USE_DS18x20
|
|
|
|
}
|
2017-02-11 14:06:23 +00:00
|
|
|
#ifdef USE_DHT
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_flg) {
|
2017-09-13 13:19:34 +01:00
|
|
|
dht_mqttPresent(djson);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-02-11 14:06:23 +00:00
|
|
|
#endif // USE_DHT
|
2017-02-04 16:09:54 +00:00
|
|
|
#ifdef USE_I2C
|
|
|
|
if (i2c_flg) {
|
2017-03-16 15:23:11 +00:00
|
|
|
#ifdef USE_SHT
|
2017-09-13 13:19:34 +01:00
|
|
|
sht_mqttPresent(djson);
|
2017-03-16 15:23:11 +00:00
|
|
|
#endif // USE_SHT
|
2017-02-04 16:09:54 +00:00
|
|
|
#ifdef USE_HTU
|
2017-09-13 13:19:34 +01:00
|
|
|
htu_mqttPresent(djson);
|
2017-02-04 16:09:54 +00:00
|
|
|
#endif // USE_HTU
|
|
|
|
#ifdef USE_BMP
|
2017-09-13 13:19:34 +01:00
|
|
|
bmp_mqttPresent(djson);
|
2017-02-04 16:09:54 +00:00
|
|
|
#endif // USE_BMP
|
|
|
|
#ifdef USE_BH1750
|
2017-09-13 13:19:34 +01:00
|
|
|
bh1750_mqttPresent(djson);
|
2017-02-04 16:09:54 +00:00
|
|
|
#endif // USE_BH1750
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
#endif // USE_I2C
|
2017-09-13 13:19:34 +01:00
|
|
|
if (strstr_P(mqtt_data, PSTR(D_TEMPERATURE))) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"" D_TEMPERATURE_UNIT "\":\"%c\""), mqtt_data, tempUnit());
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void every_second()
|
|
|
|
{
|
2017-05-04 12:58:26 +01:00
|
|
|
if (blockgpio0) {
|
|
|
|
blockgpio0--;
|
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 0; i < MAX_PULSETIMERS; i++) {
|
|
|
|
if (pulse_timer[i] > 111) {
|
|
|
|
pulse_timer[i]--;
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
if (seriallog_timer) {
|
|
|
|
seriallog_timer--;
|
|
|
|
if (!seriallog_timer) {
|
|
|
|
if (seriallog_level) {
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_SERIAL_LOGGING_DISABLED));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
seriallog_level = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syslog_timer) { // Restore syslog level
|
|
|
|
syslog_timer--;
|
|
|
|
if (!syslog_timer) {
|
2017-05-03 17:19:13 +01:00
|
|
|
syslog_level = (sysCfg.flag.emulation) ? 0 : sysCfg.syslog_level;
|
2017-01-28 13:41:01 +00:00
|
|
|
if (sysCfg.syslog_level) {
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_SYSLOG_LOGGING_REENABLED)); // Might trigger disable again (on purpose)
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
domoticz_mqttUpdate();
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
|
|
|
|
if (status_update_timer) {
|
|
|
|
status_update_timer--;
|
|
|
|
if (!status_update_timer) {
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 1; i <= Maxdevice; i++) {
|
|
|
|
mqtt_publishPowerState(i);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sysCfg.tele_period) {
|
|
|
|
tele_period++;
|
|
|
|
if (tele_period == sysCfg.tele_period -1) {
|
2017-02-11 14:06:23 +00:00
|
|
|
if (pin[GPIO_DSB] < 99) {
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DS18B20
|
|
|
|
dsb_readTempPrep();
|
|
|
|
#endif // USE_DS18B20
|
|
|
|
#ifdef USE_DS18x20
|
|
|
|
ds18x20_search(); // Check for changes in sensors number
|
|
|
|
ds18x20_convert(); // Start Conversion, takes up to one second
|
|
|
|
#endif // USE_DS18x20
|
|
|
|
}
|
|
|
|
#ifdef USE_DHT
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_flg) {
|
2017-04-25 17:24:42 +01:00
|
|
|
dht_readPrep();
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DHT
|
|
|
|
#ifdef USE_I2C
|
|
|
|
if (i2c_flg) {
|
2017-03-16 15:23:11 +00:00
|
|
|
#ifdef USE_SHT
|
|
|
|
sht_detect();
|
|
|
|
#endif // USE_SHT
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_HTU
|
|
|
|
htu_detect();
|
|
|
|
#endif // USE_HTU
|
|
|
|
#ifdef USE_BMP
|
|
|
|
bmp_detect();
|
|
|
|
#endif // USE_BMP
|
|
|
|
#ifdef USE_BH1750
|
|
|
|
bh1750_detect();
|
|
|
|
#endif // USE_BH1750
|
|
|
|
}
|
|
|
|
#endif // USE_I2C
|
|
|
|
}
|
|
|
|
if (tele_period >= sysCfg.tele_period) {
|
|
|
|
tele_period = 0;
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
state_mqttPresent();
|
|
|
|
mqtt_publish_topic_P(2, PSTR(D_RSLT_STATE));
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
uint8_t djson = 0;
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
sensors_mqttPresent(&djson);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (djson) {
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(2, PSTR(D_RSLT_SENSOR), sysCfg.flag.mqtt_sensor_retain);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (hlw_flg) {
|
2017-08-10 08:12:18 +01:00
|
|
|
hlw_mqttPresent(1);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (hlw_flg) {
|
|
|
|
hlw_margin_chk();
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((2 == rtcTime.Minute) && uptime_flg) {
|
2017-03-29 17:42:05 +01:00
|
|
|
uptime_flg = false;
|
2017-01-28 13:41:01 +00:00
|
|
|
uptime++;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_TIME "\":\"%s\", \"" D_UPTIME "\":%d}"), getDateTime().c_str(), uptime);
|
|
|
|
mqtt_publish_topic_P(2, PSTR(D_RSLT_UPTIME));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((3 == rtcTime.Minute) && !uptime_flg) {
|
|
|
|
uptime_flg = true;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Button handler with single press only or multi-press and hold on all buttons
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void button_handler()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
uint8_t button = NOT_PRESSED;
|
2017-07-25 17:05:47 +01:00
|
|
|
uint8_t butt_present = 0;
|
|
|
|
uint8_t flag = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
char scmnd[20];
|
2017-02-13 16:25:46 +00:00
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
for (byte i = 0; i < Maxdevice; i++) {
|
2017-07-30 16:55:37 +01:00
|
|
|
button = NOT_PRESSED;
|
2017-07-25 17:05:47 +01:00
|
|
|
butt_present = 0;
|
|
|
|
|
|
|
|
if (!i && ((SONOFF_DUAL == sysCfg.module) || (CH4 == sysCfg.module))) {
|
|
|
|
butt_present = 1;
|
|
|
|
if (ButtonCode) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON " " D_CODE " %04X"), ButtonCode);
|
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-07-25 17:05:47 +01:00
|
|
|
button = PRESSED;
|
|
|
|
if (0xF500 == ButtonCode) { // Button hold
|
|
|
|
holdbutton[i] = (sysCfg.param[P_HOLD_TIME] * (STATES / 10)) -1;
|
|
|
|
}
|
|
|
|
ButtonCode = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-07-25 17:05:47 +01:00
|
|
|
if ((pin[GPIO_KEY1 +i] < 99) && !blockgpio0) {
|
|
|
|
butt_present = 1;
|
|
|
|
button = digitalRead(pin[GPIO_KEY1 +i]);
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-07-25 17:05:47 +01:00
|
|
|
|
|
|
|
if (butt_present) {
|
|
|
|
if (SONOFF_4CHPRO == sysCfg.module) {
|
|
|
|
if (holdbutton[i]) {
|
|
|
|
holdbutton[i]--;
|
|
|
|
}
|
|
|
|
flag = 0;
|
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == lastbutton[i])) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON " %d " D_LEVEL_10), i +1);
|
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-07-25 17:05:47 +01:00
|
|
|
holdbutton[i] = STATES;
|
|
|
|
flag = 1;
|
|
|
|
}
|
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == lastbutton[i])) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON " %d " D_LEVEL_01), i +1);
|
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-07-25 17:05:47 +01:00
|
|
|
if (!holdbutton[i]) { // Do not allow within 1 second
|
|
|
|
flag = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flag) {
|
|
|
|
if (!send_button_power(0, i +1, 2)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
|
|
|
do_cmnd_power(i +1, 2); // Execute Toggle command internally
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 15:20:45 +00:00
|
|
|
} else {
|
2017-07-25 17:05:47 +01:00
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == lastbutton[i])) {
|
|
|
|
if (sysCfg.flag.button_single) { // Allow only single button press for immediate action
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON " %d " D_IMMEDIATE), i +1);
|
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-07-25 17:05:47 +01:00
|
|
|
if (!send_button_power(0, i +1, 2)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
|
|
|
do_cmnd_power(i +1, 2); // Execute Toggle command internally
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-07-25 17:05:47 +01:00
|
|
|
multipress[i] = (multiwindow[i]) ? multipress[i] +1 : 1;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON " %d " D_MULTI_PRESS " %d"), i +1, multipress[i]);
|
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-07-25 17:05:47 +01:00
|
|
|
multiwindow[i] = STATES /2; // 0.5 second multi press window
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-07-25 17:05:47 +01:00
|
|
|
blinks = 201;
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
if (NOT_PRESSED == button) {
|
|
|
|
holdbutton[i] = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-07-25 17:05:47 +01:00
|
|
|
holdbutton[i]++;
|
|
|
|
if (sysCfg.flag.button_single) { // Allow only single button press for immediate action
|
|
|
|
if (holdbutton[i] == sysCfg.param[P_HOLD_TIME] * (STATES / 10) * 4) { // Button hold for four times longer
|
|
|
|
// sysCfg.flag.button_single = 0;
|
2017-09-02 13:37:02 +01:00
|
|
|
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_SETOPTION "13 0")); // Disable single press only
|
2017-07-25 17:05:47 +01:00
|
|
|
do_cmnd(scmnd);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (holdbutton[i] == sysCfg.param[P_HOLD_TIME] * (STATES / 10)) { // Button hold
|
|
|
|
multipress[i] = 0;
|
|
|
|
if (!sysCfg.flag.button_restrict) { // No button restriction
|
2017-09-02 13:37:02 +01:00
|
|
|
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_RESET " 1"));
|
2017-07-25 17:05:47 +01:00
|
|
|
do_cmnd(scmnd);
|
|
|
|
} else {
|
|
|
|
send_button_power(0, i +1, 3); // Execute Hold command via MQTT if ButtonTopic is set
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
if (!sysCfg.flag.button_single) { // Allow multi-press
|
|
|
|
if (multiwindow[i]) {
|
|
|
|
multiwindow[i]--;
|
|
|
|
} else {
|
|
|
|
if (!restartflag && !holdbutton[i] && (multipress[i] > 0) && (multipress[i] < MAX_BUTTON_COMMANDS +3)) {
|
|
|
|
flag = 0;
|
|
|
|
if (multipress[i] < 3) { // Single or Double press
|
|
|
|
if ((SONOFF_DUAL == sysCfg.module) || (CH4 == sysCfg.module)) {
|
|
|
|
flag = 1;
|
|
|
|
} else {
|
|
|
|
flag = (sysCfg.flag.button_swap +1 == multipress[i]);
|
|
|
|
multipress[i] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flag && send_button_power(0, i + multipress[i], 2)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
|
|
|
// Success
|
|
|
|
} else {
|
|
|
|
if (multipress[i] < 3) { // Single or Double press
|
|
|
|
if (WIFI_State()) { // WPSconfig, Smartconfig or Wifimanager active
|
|
|
|
restartflag = 1;
|
|
|
|
} else {
|
|
|
|
do_cmnd_power(i + multipress[i], 2); // Execute Toggle command internally
|
|
|
|
}
|
|
|
|
} else { // 3 - 7 press
|
|
|
|
if (!sysCfg.flag.button_restrict) {
|
|
|
|
snprintf_P(scmnd, sizeof(scmnd), commands[multipress[i] -3]);
|
|
|
|
do_cmnd(scmnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
multipress[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-30 16:55:37 +01:00
|
|
|
lastbutton[i] = button;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-07-25 17:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Switch handler
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void switch_handler()
|
|
|
|
{
|
|
|
|
uint8_t button = NOT_PRESSED;
|
|
|
|
uint8_t switchflag;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 0; i < 4; i++) {
|
|
|
|
if (pin[GPIO_SWT1 +i] < 99) {
|
2017-06-06 22:23:23 +01:00
|
|
|
|
2017-06-16 13:33:49 +01:00
|
|
|
if (holdwallswitch[i]) {
|
|
|
|
holdwallswitch[i]--;
|
|
|
|
if (0 == holdwallswitch[i]) {
|
2017-07-25 17:05:47 +01:00
|
|
|
send_button_power(1, i +1, 3); // Execute command via MQTT
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
button = digitalRead(pin[GPIO_SWT1 +i]);
|
|
|
|
if (button != lastwallswitch[i]) {
|
|
|
|
switchflag = 3;
|
|
|
|
switch (sysCfg.switchmode[i]) {
|
|
|
|
case TOGGLE:
|
2017-07-25 17:05:47 +01:00
|
|
|
switchflag = 2; // Toggle
|
2017-04-25 17:24:42 +01:00
|
|
|
break;
|
|
|
|
case FOLLOW:
|
|
|
|
switchflag = button & 0x01; // Follow wall switch state
|
|
|
|
break;
|
|
|
|
case FOLLOW_INV:
|
|
|
|
switchflag = ~button & 0x01; // Follow inverted wall switch state
|
|
|
|
break;
|
|
|
|
case PUSHBUTTON:
|
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == lastwallswitch[i])) {
|
2017-06-16 13:33:49 +01:00
|
|
|
switchflag = 2; // Toggle with pushbutton to Gnd
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PUSHBUTTON_INV:
|
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == lastwallswitch[i])) {
|
2017-06-16 13:33:49 +01:00
|
|
|
switchflag = 2; // Toggle with releasing pushbutton from Gnd
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-06-06 22:23:23 +01:00
|
|
|
break;
|
|
|
|
case PUSHBUTTONHOLD:
|
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == lastwallswitch[i])) {
|
2017-07-25 17:05:47 +01:00
|
|
|
holdwallswitch[i] = sysCfg.param[P_HOLD_TIME] * (STATES / 10);
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
2017-06-16 13:33:49 +01:00
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == lastwallswitch[i]) && (holdwallswitch[i])) {
|
|
|
|
holdwallswitch[i] = 0;
|
|
|
|
switchflag = 2; // Toggle with pushbutton to Gnd
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PUSHBUTTONHOLD_INV:
|
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == lastwallswitch[i])) {
|
2017-07-25 17:05:47 +01:00
|
|
|
holdwallswitch[i] = sysCfg.param[P_HOLD_TIME] * (STATES / 10);
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
2017-06-16 13:33:49 +01:00
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == lastwallswitch[i]) && (holdwallswitch[i])) {
|
|
|
|
holdwallswitch[i] = 0;
|
|
|
|
switchflag = 2; // Toggle with pushbutton to Gnd
|
2017-06-06 22:23:23 +01:00
|
|
|
}
|
|
|
|
break;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (switchflag < 3) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if (!send_button_power(1, i +1, switchflag)) { // Execute command via MQTT
|
|
|
|
do_cmnd_power(i +1, switchflag); // Execute command internally (if i < Maxdevice)
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
lastwallswitch[i] = button;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 17:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* State loop
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void stateloop()
|
|
|
|
{
|
|
|
|
uint8_t power_now;
|
|
|
|
|
|
|
|
timerxs = millis() + (1000 / STATES);
|
|
|
|
state++;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Every second
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
if (STATES == state) {
|
|
|
|
state = 0;
|
|
|
|
every_second();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Every 0.1 second
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
if (!(state % (STATES/10))) {
|
|
|
|
|
|
|
|
if (mqtt_cmnd_publish) {
|
|
|
|
mqtt_cmnd_publish--; // Clean up
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
if (latching_relay_pulse) {
|
|
|
|
latching_relay_pulse--;
|
|
|
|
if (!latching_relay_pulse) {
|
|
|
|
setLatchingRelay(0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (byte i = 0; i < MAX_PULSETIMERS; i++) {
|
|
|
|
if ((pulse_timer[i] > 0) && (pulse_timer[i] < 112)) {
|
|
|
|
pulse_timer[i]--;
|
|
|
|
if (!pulse_timer[i]) {
|
|
|
|
do_cmnd_power(i +1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blink_mask) {
|
|
|
|
blink_timer--;
|
|
|
|
if (!blink_timer) {
|
|
|
|
blink_timer = sysCfg.blinktime;
|
|
|
|
blink_counter--;
|
|
|
|
if (!blink_counter) {
|
|
|
|
stop_all_power_blink();
|
|
|
|
} else {
|
|
|
|
blink_power ^= 1;
|
|
|
|
power_now = (power & (0xFF ^ blink_mask)) | ((blink_power) ? blink_mask : 0);
|
|
|
|
setRelay(power_now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backlog
|
|
|
|
if (blogdelay) {
|
|
|
|
blogdelay--;
|
|
|
|
}
|
|
|
|
if ((blogptr != blogidx) && !blogdelay && !blogmutex) {
|
|
|
|
blogmutex = 1;
|
|
|
|
do_cmnd((char*)Backlog[blogptr].c_str());
|
|
|
|
blogmutex = 0;
|
|
|
|
blogptr++;
|
|
|
|
/*
|
|
|
|
if (blogptr >= MAX_BACKLOG) {
|
|
|
|
blogptr = 0;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
blogptr &= 0xF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 16:28:00 +01:00
|
|
|
#ifdef USE_IR_REMOTE
|
|
|
|
#ifdef USE_IR_RECEIVE
|
|
|
|
if (pin[GPIO_IRRECV] < 99) {
|
|
|
|
ir_recv_check(); // check if there's anything on IR side
|
|
|
|
}
|
|
|
|
#endif // USE_IR_RECEIVE
|
|
|
|
#endif // USE_IR_REMOTE
|
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Every 0.05 second
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
button_handler();
|
|
|
|
switch_handler();
|
|
|
|
|
2017-10-01 13:32:36 +01:00
|
|
|
if (sfl_flg) {
|
2017-07-30 16:55:37 +01:00
|
|
|
sl_animate();
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Every 0.2 second
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
if (!(state % ((STATES/10)*2))) {
|
|
|
|
if (blinks || restartflag || otaflag) {
|
|
|
|
if (restartflag || otaflag) {
|
|
|
|
blinkstate = 1; // Stay lit
|
|
|
|
} else {
|
|
|
|
blinkstate ^= 1; // Blink
|
|
|
|
}
|
|
|
|
if ((!(sysCfg.ledstate &0x08)) && ((sysCfg.ledstate &0x06) || (blinks > 200) || (blinkstate))) {
|
|
|
|
setLed(blinkstate);
|
|
|
|
}
|
|
|
|
if (!blinkstate) {
|
|
|
|
blinks--;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (200 == blinks) {
|
|
|
|
blinks = 0;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (sysCfg.ledstate &0x01) {
|
2017-08-18 09:40:21 +01:00
|
|
|
boolean tstate = power;
|
|
|
|
if ((SONOFF_TOUCH == sysCfg.module) || (SONOFF_T11 == sysCfg.module) || (SONOFF_T12 == sysCfg.module) || (SONOFF_T13 == sysCfg.module)) {
|
|
|
|
tstate = (!power) ? 1 : 0;
|
|
|
|
}
|
|
|
|
setLed(tstate);
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:05:47 +01:00
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Every second at 0.2 second interval
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
2017-07-15 14:07:30 +01:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
switch (state) {
|
|
|
|
case (STATES/10)*2:
|
2017-07-15 14:07:30 +01:00
|
|
|
if (otaflag && (blogptr == blogidx)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
otaflag--;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (2 == otaflag) {
|
2017-02-24 17:17:48 +00:00
|
|
|
otaretry = OTA_ATTEMPTS;
|
2017-01-28 13:41:01 +00:00
|
|
|
ESPhttpUpdate.rebootOnUpdate(false);
|
2017-06-19 21:54:49 +01:00
|
|
|
CFG_Save(1); // Free flash for OTA update
|
2017-02-24 17:17:48 +00:00
|
|
|
}
|
|
|
|
if (otaflag <= 0) {
|
|
|
|
#ifdef USE_WEBSERVER
|
2017-04-25 17:24:42 +01:00
|
|
|
if (sysCfg.webserver) {
|
|
|
|
stopWebserver();
|
|
|
|
}
|
2017-02-24 17:17:48 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
otaflag = 92;
|
|
|
|
otaok = 0;
|
|
|
|
otaretry--;
|
|
|
|
if (otaretry) {
|
2017-09-13 13:19:34 +01:00
|
|
|
// snprintf_P(log_data, sizeof(log_data), PSTR("OTA: Attempt %d"), OTA_ATTEMPTS - otaretry);
|
|
|
|
// addLog(LOG_LEVEL_INFO);
|
2017-08-08 15:31:43 +01:00
|
|
|
otaok = (HTTP_UPDATE_FAILED != ESPhttpUpdate.update(sysCfg.otaUrl));
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!otaok) {
|
|
|
|
otaflag = 2;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 15:31:43 +01:00
|
|
|
if (90 == otaflag) { // Allow MQTT to reconnect
|
2017-01-28 13:41:01 +00:00
|
|
|
otaflag = 0;
|
|
|
|
if (otaok) {
|
2017-08-08 15:31:43 +01:00
|
|
|
setFlashModeDout(); // Force DOUT for both ESP8266 and ESP8285
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR(D_SUCCESSFUL ". " D_RESTARTING));
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR(D_FAILED " %s"), ESPhttpUpdate.getLastErrorString().c_str());
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-08-08 15:31:43 +01:00
|
|
|
restartflag = 2; // Restart anyway to keep memory clean webserver
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_publish_topic_P(1, PSTR(D_CMND_UPGRADE));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (STATES/10)*4:
|
2017-05-17 21:49:22 +01:00
|
|
|
if (rtc_midnight_now()) {
|
|
|
|
counter_savestate();
|
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
if (savedatacounter && (blogptr == blogidx)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
savedatacounter--;
|
|
|
|
if (savedatacounter <= 0) {
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.savestate) {
|
2017-03-12 17:36:33 +00:00
|
|
|
byte mask = 0xFF;
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 0; i < MAX_PULSETIMERS; i++) {
|
|
|
|
if ((sysCfg.pulsetime[i] > 0) && (sysCfg.pulsetime[i] < 30)) {
|
|
|
|
mask &= ~(1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!((sysCfg.power &mask) == (power &mask))) {
|
|
|
|
sysCfg.power = power;
|
|
|
|
}
|
2017-10-04 16:29:49 +01:00
|
|
|
} else {
|
|
|
|
sysCfg.power = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-06-19 21:54:49 +01:00
|
|
|
CFG_Save(0);
|
2017-01-28 13:41:01 +00:00
|
|
|
savedatacounter = sysCfg.savedata;
|
|
|
|
}
|
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
if (restartflag && (blogptr == blogidx)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (211 == restartflag) {
|
2017-01-28 13:41:01 +00:00
|
|
|
CFG_Default();
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if (212 == restartflag) {
|
2017-01-28 13:41:01 +00:00
|
|
|
CFG_Erase();
|
|
|
|
CFG_Default();
|
|
|
|
restartflag = 2;
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.savestate) {
|
2017-04-25 17:24:42 +01:00
|
|
|
sysCfg.power = power;
|
2017-10-04 16:29:49 +01:00
|
|
|
} else {
|
|
|
|
sysCfg.power = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
|
|
|
if (hlw_flg) {
|
|
|
|
hlw_savestate();
|
|
|
|
}
|
2017-05-17 21:49:22 +01:00
|
|
|
counter_savestate();
|
2017-06-19 21:54:49 +01:00
|
|
|
CFG_Save(0);
|
2017-01-28 13:41:01 +00:00
|
|
|
restartflag--;
|
|
|
|
if (restartflag <= 0) {
|
2017-09-02 13:37:02 +01:00
|
|
|
addLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_RESTARTING));
|
2017-01-28 13:41:01 +00:00
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (STATES/10)*6:
|
|
|
|
WIFI_Check(wificheckflag);
|
|
|
|
wificheckflag = WIFI_RESTART;
|
|
|
|
break;
|
|
|
|
case (STATES/10)*8:
|
2017-04-25 17:24:42 +01:00
|
|
|
if (WL_CONNECTED == WiFi.status()) {
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.mqtt_enabled) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if (!mqttClient.connected()) {
|
|
|
|
if (!mqttcounter) {
|
|
|
|
mqtt_reconnect();
|
|
|
|
} else {
|
|
|
|
mqttcounter--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!mqttcounter) {
|
|
|
|
mqtt_reconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:25:31 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
void serial()
|
|
|
|
{
|
|
|
|
while (Serial.available()) {
|
|
|
|
yield();
|
|
|
|
SerialInByte = Serial.read();
|
|
|
|
|
2017-07-30 16:55:37 +01:00
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Sonoff dual 19200 baud serial interface
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
if (Hexcode) {
|
|
|
|
Hexcode--;
|
|
|
|
if (Hexcode) {
|
|
|
|
ButtonCode = (ButtonCode << 8) | SerialInByte;
|
|
|
|
SerialInByte = 0;
|
|
|
|
} else {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (SerialInByte != 0xA1) {
|
2017-07-30 16:55:37 +01:00
|
|
|
ButtonCode = 0; // 0xA1 - End of Sonoff dual button code
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-30 16:55:37 +01:00
|
|
|
if (0xA0 == SerialInByte) { // 0xA0 - Start of Sonoff dual button code
|
2017-01-28 13:41:01 +00:00
|
|
|
SerialInByte = 0;
|
|
|
|
ButtonCode = 0;
|
|
|
|
Hexcode = 3;
|
|
|
|
}
|
|
|
|
|
2017-07-30 16:55:37 +01:00
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Sonoff bridge 19200 baud serial interface
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
if (sb_serial()) {
|
|
|
|
SerialInByteCounter = 0;
|
|
|
|
Serial.flush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
if (SerialInByte > 127) { // binary data...
|
2017-01-28 13:41:01 +00:00
|
|
|
SerialInByteCounter = 0;
|
|
|
|
Serial.flush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isprint(SerialInByte)) {
|
|
|
|
if (SerialInByteCounter < INPUT_BUFFER_SIZE) { // add char to string if it still fits
|
|
|
|
serialInBuf[SerialInByteCounter++] = SerialInByte;
|
|
|
|
} else {
|
|
|
|
SerialInByteCounter = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-09-16 16:34:03 +01:00
|
|
|
/*-------------------------------------------------------------------------------------------*\
|
|
|
|
* Sonoff SC 19200 baud serial interface
|
|
|
|
\*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
2017-07-30 16:55:37 +01:00
|
|
|
if (SerialInByte == '\x1B') { // Sonoff SC status from ATMEGA328P
|
2017-04-10 16:25:31 +01:00
|
|
|
serialInBuf[SerialInByteCounter] = 0; // serial data completed
|
|
|
|
sc_rcvstat(serialInBuf);
|
|
|
|
SerialInByteCounter = 0;
|
|
|
|
Serial.flush();
|
|
|
|
return;
|
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------------------------*/
|
|
|
|
|
2017-04-10 16:25:31 +01:00
|
|
|
else if (SerialInByte == '\n') {
|
2017-01-28 13:41:01 +00:00
|
|
|
serialInBuf[SerialInByteCounter] = 0; // serial data completed
|
2017-03-08 15:20:45 +00:00
|
|
|
seriallog_level = (sysCfg.seriallog_level < LOG_LEVEL_INFO) ? LOG_LEVEL_INFO : sysCfg.seriallog_level;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_COMMAND "%s"), serialInBuf);
|
|
|
|
addLog(LOG_LEVEL_INFO);
|
2017-01-28 13:41:01 +00:00
|
|
|
do_cmnd(serialInBuf);
|
|
|
|
SerialInByteCounter = 0;
|
|
|
|
Serial.flush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void GPIO_init()
|
|
|
|
{
|
|
|
|
uint8_t mpin;
|
|
|
|
mytmplt def_module;
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!sysCfg.module || (sysCfg.module >= MAXMODULE)) {
|
|
|
|
sysCfg.module = MODULE;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
memcpy_P(&def_module, &modules[sysCfg.module], sizeof(def_module));
|
2017-07-15 14:07:30 +01:00
|
|
|
// sysCfg.my_module.flag = def_module.flag;
|
2017-01-28 13:41:01 +00:00
|
|
|
strlcpy(my_module.name, def_module.name, sizeof(my_module.name));
|
|
|
|
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (sysCfg.my_module.gp.io[i] > GPIO_NONE) {
|
|
|
|
my_module.gp.io[i] = sysCfg.my_module.gp.io[i];
|
|
|
|
}
|
|
|
|
if ((def_module.gp.io[i] > GPIO_NONE) && (def_module.gp.io[i] < GPIO_USER)) {
|
|
|
|
my_module.gp.io[i] = def_module.gp.io[i];
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-02-11 14:06:23 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 0; i < GPIO_MAX; i++) {
|
|
|
|
pin[i] = 99;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
|
|
|
mpin = my_module.gp.io[i];
|
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
// snprintf_P(log_data, sizeof(log_data), PSTR("DBG: gpio pin %d, mpin %d"), i, mpin);
|
|
|
|
// addLog(LOG_LEVEL_DEBUG);
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
if (mpin) {
|
|
|
|
if ((mpin >= GPIO_REL1_INV) && (mpin <= GPIO_REL4_INV)) {
|
|
|
|
rel_inverted[mpin - GPIO_REL1_INV] = 1;
|
2017-10-05 12:28:31 +01:00
|
|
|
mpin -= (GPIO_REL1_INV - GPIO_REL1);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
else if ((mpin >= GPIO_LED1_INV) && (mpin <= GPIO_LED4_INV)) {
|
|
|
|
led_inverted[mpin - GPIO_LED1_INV] = 1;
|
2017-10-05 12:28:31 +01:00
|
|
|
mpin -= (GPIO_LED1_INV - GPIO_LED1);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-10-04 16:29:49 +01:00
|
|
|
else if ((mpin >= GPIO_PWM1_INV) && (mpin <= GPIO_PWM5_INV)) {
|
|
|
|
pwm_inverted[mpin - GPIO_PWM1_INV] = 1;
|
2017-10-05 12:28:31 +01:00
|
|
|
mpin -= (GPIO_PWM1_INV - GPIO_PWM1);
|
2017-10-04 16:29:49 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
#ifdef USE_DHT
|
2017-05-03 17:19:13 +01:00
|
|
|
else if ((mpin >= GPIO_DHT11) && (mpin <= GPIO_DHT22)) {
|
|
|
|
if (dht_setup(i, mpin)) {
|
|
|
|
dht_flg = 1;
|
|
|
|
mpin = GPIO_DHT11;
|
|
|
|
} else {
|
|
|
|
mpin = 0;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
#endif // USE_DHT
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
|
|
|
if (mpin) {
|
2017-01-28 13:41:01 +00:00
|
|
|
pin[mpin] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (2 == pin[GPIO_TXD]) {
|
|
|
|
Serial.set_tx(2);
|
|
|
|
}
|
2017-03-25 16:24:11 +00:00
|
|
|
|
2017-10-05 12:28:31 +01:00
|
|
|
analogWriteRange(PWM_RANGE); // Default is 1023 (Arduino.h)
|
|
|
|
analogWriteFreq(PWM_FREQ); // Default is 1000 (core_esp8266_wiring_pwm.c)
|
2017-03-19 17:19:08 +00:00
|
|
|
|
2017-10-05 12:28:31 +01:00
|
|
|
Maxdevice = 1;
|
2017-10-01 13:32:36 +01:00
|
|
|
if (sysCfg.flag.pwm_control) {
|
|
|
|
sfl_flg = 0;
|
|
|
|
for (byte i = 0; i < 5; i++) {
|
|
|
|
if (pin[GPIO_PWM1 +i] < 99) {
|
2017-10-05 12:28:31 +01:00
|
|
|
sfl_flg++; // Use Dimmer/Color control for all PWM as SetOption15 = 1
|
2017-10-01 13:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-30 16:55:37 +01:00
|
|
|
if (SONOFF_BRIDGE == sysCfg.module) {
|
|
|
|
Baudrate = 19200;
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if (SONOFF_DUAL == sysCfg.module) {
|
2017-02-28 15:01:48 +00:00
|
|
|
Maxdevice = 2;
|
|
|
|
Baudrate = 19200;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
else if (CH4 == sysCfg.module) {
|
2017-02-28 15:01:48 +00:00
|
|
|
Maxdevice = 4;
|
2017-01-28 13:41:01 +00:00
|
|
|
Baudrate = 19200;
|
2017-02-13 16:25:46 +00:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
else if (SONOFF_SC == sysCfg.module) {
|
2017-04-10 16:25:31 +01:00
|
|
|
Maxdevice = 0;
|
|
|
|
Baudrate = 19200;
|
|
|
|
}
|
2017-10-01 13:32:36 +01:00
|
|
|
else if ((H801 == sysCfg.module) || (MAGICHOME == sysCfg.module)) { // PWM RGBCW led
|
|
|
|
if (!sysCfg.flag.pwm_control) {
|
2017-10-05 12:28:31 +01:00
|
|
|
sfl_flg = 0; // Use basic PWM control if SetOption15 = 0
|
2017-10-01 13:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (SONOFF_BN == sysCfg.module) { // PWM Single color led (White)
|
2017-07-03 10:45:15 +01:00
|
|
|
sfl_flg = 1;
|
|
|
|
}
|
2017-10-01 13:32:36 +01:00
|
|
|
else if (SONOFF_LED == sysCfg.module) { // PWM Dual color led (White warm and cold)
|
2017-07-03 10:45:15 +01:00
|
|
|
sfl_flg = 2;
|
2017-02-13 16:25:46 +00:00
|
|
|
}
|
2017-08-13 14:31:49 +01:00
|
|
|
else if (AILIGHT == sysCfg.module) { // RGBW led
|
2017-10-01 13:32:36 +01:00
|
|
|
sfl_flg = 12;
|
2017-08-13 14:31:49 +01:00
|
|
|
}
|
|
|
|
else if (SONOFF_B1 == sysCfg.module) { // RGBWC led
|
2017-10-01 13:32:36 +01:00
|
|
|
sfl_flg = 13;
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-02-13 16:25:46 +00:00
|
|
|
else {
|
2017-10-05 12:28:31 +01:00
|
|
|
if (!sfl_flg) {
|
|
|
|
Maxdevice = 0;
|
|
|
|
}
|
2017-02-13 16:25:46 +00:00
|
|
|
for (byte i = 0; i < 4; i++) {
|
2017-02-28 15:01:48 +00:00
|
|
|
if (pin[GPIO_REL1 +i] < 99) {
|
|
|
|
pinMode(pin[GPIO_REL1 +i], OUTPUT);
|
|
|
|
Maxdevice++;
|
|
|
|
}
|
2017-07-30 16:55:37 +01:00
|
|
|
// if (pin[GPIO_KEY1 +i] < 99) {
|
2017-09-10 14:51:44 +01:00
|
|
|
// pinMode(pin[GPIO_KEY1 +i], (16 == pin[GPIO_KEY1 +i]) ? INPUT_PULLDOWN_16 : INPUT_PULLUP);
|
2017-07-30 16:55:37 +01:00
|
|
|
// }
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (byte i = 0; i < 4; i++) {
|
2017-07-30 16:55:37 +01:00
|
|
|
if (pin[GPIO_KEY1 +i] < 99) {
|
2017-09-10 14:51:44 +01:00
|
|
|
pinMode(pin[GPIO_KEY1 +i], (16 == pin[GPIO_KEY1 +i]) ? INPUT_PULLDOWN_16 : INPUT_PULLUP);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
if (pin[GPIO_LED1 +i] < 99) {
|
|
|
|
pinMode(pin[GPIO_LED1 +i], OUTPUT);
|
|
|
|
digitalWrite(pin[GPIO_LED1 +i], led_inverted[i]);
|
|
|
|
}
|
|
|
|
if (pin[GPIO_SWT1 +i] < 99) {
|
2017-09-10 14:51:44 +01:00
|
|
|
pinMode(pin[GPIO_SWT1 +i], (16 == pin[GPIO_SWT1 +i]) ? INPUT_PULLDOWN_16 :INPUT_PULLUP);
|
2017-01-28 13:41:01 +00:00
|
|
|
lastwallswitch[i] = digitalRead(pin[GPIO_SWT1 +i]); // set global now so doesn't change the saved power state on first switch check
|
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-09-16 16:34:03 +01:00
|
|
|
#ifdef USE_WS2812
|
2017-10-01 13:32:36 +01:00
|
|
|
if (!sfl_flg && (pin[GPIO_WS2812] < 99)) { // RGB led
|
2017-09-16 16:34:03 +01:00
|
|
|
Maxdevice++;
|
2017-10-01 13:32:36 +01:00
|
|
|
sfl_flg = 11;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
#endif // USE_WS2812
|
2017-10-05 12:28:31 +01:00
|
|
|
if (sfl_flg) { // Any Led light under Dimmer/Color control
|
2017-07-03 10:45:15 +01:00
|
|
|
sl_init();
|
2017-10-05 12:28:31 +01:00
|
|
|
} else {
|
|
|
|
for (byte i = 0; i < 5; i++) {
|
|
|
|
if (pin[GPIO_PWM1 +i] < 99) {
|
|
|
|
pinMode(pin[GPIO_PWM1 +i], OUTPUT);
|
|
|
|
analogWrite(pin[GPIO_PWM1 +i], pwm_inverted[i] ? PWM_RANGE - sysCfg.pwmvalue[i] : sysCfg.pwmvalue[i]);
|
|
|
|
}
|
2017-03-19 17:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 21:49:22 +01:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (EXS_RELAY == sysCfg.module) {
|
2017-02-13 16:25:46 +00:00
|
|
|
setLatchingRelay(0,2);
|
|
|
|
setLatchingRelay(1,2);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
setLed(sysCfg.ledstate &8);
|
|
|
|
|
2017-05-19 15:09:25 +01:00
|
|
|
#ifdef USE_IR_REMOTE
|
|
|
|
if (pin[GPIO_IRSEND] < 99) {
|
|
|
|
ir_send_init();
|
|
|
|
}
|
2017-10-06 16:28:00 +01:00
|
|
|
#ifdef USE_IR_RECEIVE
|
|
|
|
if (pin[GPIO_IRRECV] < 99) {
|
|
|
|
ir_recv_init();
|
|
|
|
}
|
|
|
|
#endif // USE_IR_RECEIVE
|
|
|
|
#endif // USE_IR_REMOTE
|
2017-05-19 15:09:25 +01:00
|
|
|
|
|
|
|
counter_init();
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
hlw_flg = ((pin[GPIO_HLW_SEL] < 99) && (pin[GPIO_HLW_CF1] < 99) && (pin[GPIO_HLW_CF] < 99));
|
2017-04-25 17:24:42 +01:00
|
|
|
if (hlw_flg) {
|
|
|
|
hlw_init();
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-02-11 14:06:23 +00:00
|
|
|
#ifdef USE_DHT
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_flg) {
|
2017-04-25 17:24:42 +01:00
|
|
|
dht_init();
|
|
|
|
}
|
2017-02-11 14:06:23 +00:00
|
|
|
#endif // USE_DHT
|
|
|
|
|
|
|
|
#ifdef USE_DS18x20
|
2017-04-25 17:24:42 +01:00
|
|
|
if (pin[GPIO_DSB] < 99) {
|
|
|
|
ds18x20_init();
|
|
|
|
}
|
2017-02-11 14:06:23 +00:00
|
|
|
#endif // USE_DS18x20
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
i2c_flg = ((pin[GPIO_I2C_SCL] < 99) && (pin[GPIO_I2C_SDA] < 99));
|
2017-04-25 17:24:42 +01:00
|
|
|
if (i2c_flg) {
|
|
|
|
Wire.begin(pin[GPIO_I2C_SDA], pin[GPIO_I2C_SCL]);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_I2C
|
|
|
|
}
|
|
|
|
|
2017-04-29 13:40:53 +01:00
|
|
|
extern "C" {
|
|
|
|
extern struct rst_info resetInfo;
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
byte idx;
|
|
|
|
|
|
|
|
Serial.begin(Baudrate);
|
|
|
|
delay(10);
|
|
|
|
Serial.println();
|
|
|
|
seriallog_level = LOG_LEVEL_INFO; // Allow specific serial messages until config loaded
|
|
|
|
|
|
|
|
snprintf_P(Version, sizeof(Version), PSTR("%d.%d.%d"), VERSION >> 24 & 0xff, VERSION >> 16 & 0xff, VERSION >> 8 & 0xff);
|
|
|
|
if (VERSION & 0x1f) {
|
|
|
|
idx = strlen(Version);
|
|
|
|
Version[idx] = 96 + (VERSION & 0x1f);
|
|
|
|
Version[idx +1] = 0;
|
|
|
|
}
|
|
|
|
CFG_Load();
|
|
|
|
CFG_Delta();
|
|
|
|
|
2017-02-28 15:01:48 +00:00
|
|
|
osw_init();
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
seriallog_level = sysCfg.seriallog_level;
|
2017-08-24 10:09:52 +01:00
|
|
|
seriallog_timer = SERIALLOG_TIMER;
|
2017-03-05 14:07:30 +00:00
|
|
|
#ifndef USE_EMULATION
|
2017-05-03 17:19:13 +01:00
|
|
|
sysCfg.flag.emulation = 0;
|
2017-03-05 14:07:30 +00:00
|
|
|
#endif // USE_EMULATION
|
2017-05-03 17:19:13 +01:00
|
|
|
syslog_level = (sysCfg.flag.emulation) ? 0 : sysCfg.syslog_level;
|
2017-08-24 10:09:52 +01:00
|
|
|
stop_flash_rotate = sysCfg.flag.stop_flash_rotate;
|
|
|
|
savedatacounter = sysCfg.savedata;
|
2017-01-29 13:52:31 +00:00
|
|
|
sleep = sysCfg.sleep;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-08-24 10:09:52 +01:00
|
|
|
sysCfg.bootcount++;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BOOT_COUNT " %d"), sysCfg.bootcount);
|
|
|
|
addLog(LOG_LEVEL_DEBUG);
|
2017-08-24 10:09:52 +01:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
GPIO_init();
|
|
|
|
|
|
|
|
if (Serial.baudRate() != Baudrate) {
|
|
|
|
if (seriallog_level) {
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_SET_BAUDRATE_TO " %d"), Baudrate);
|
|
|
|
addLog(LOG_LEVEL_INFO);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
delay(100);
|
|
|
|
Serial.flush();
|
|
|
|
Serial.begin(Baudrate);
|
|
|
|
delay(10);
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
2017-02-08 11:56:51 +00:00
|
|
|
if (strstr(sysCfg.hostname, "%")) {
|
|
|
|
strlcpy(sysCfg.hostname, WIFI_HOSTNAME, sizeof(sysCfg.hostname));
|
2017-01-28 13:41:01 +00:00
|
|
|
snprintf_P(Hostname, sizeof(Hostname)-1, sysCfg.hostname, sysCfg.mqtt_topic, ESP.getChipId() & 0x1FFF);
|
|
|
|
} else {
|
|
|
|
snprintf_P(Hostname, sizeof(Hostname)-1, sysCfg.hostname);
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
WIFI_Connect();
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
getClient(MQTTClient, sysCfg.mqtt_client, sizeof(MQTTClient));
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (MOTOR == sysCfg.module) {
|
|
|
|
sysCfg.poweronstate = 1; // Needs always on else in limbo!
|
|
|
|
}
|
2017-05-05 16:57:05 +01:00
|
|
|
if (4 == sysCfg.poweronstate) { // Allways on
|
|
|
|
setRelay(power);
|
|
|
|
} else {
|
|
|
|
if ((resetInfo.reason == REASON_DEFAULT_RST) || (resetInfo.reason == REASON_EXT_SYS_RST)) {
|
|
|
|
switch (sysCfg.poweronstate) {
|
|
|
|
case 0: // All off
|
|
|
|
power = 0;
|
|
|
|
setRelay(power);
|
|
|
|
break;
|
|
|
|
case 1: // All on
|
|
|
|
power = (1 << Maxdevice) -1;
|
2017-04-25 17:24:42 +01:00
|
|
|
setRelay(power);
|
2017-05-05 16:57:05 +01:00
|
|
|
break;
|
|
|
|
case 2: // All saved state toggle
|
|
|
|
power = sysCfg.power & ((1 << Maxdevice) -1) ^ 0xFF;
|
|
|
|
if (sysCfg.flag.savestate) {
|
|
|
|
setRelay(power);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // All saved state
|
|
|
|
power = sysCfg.power & ((1 << Maxdevice) -1);
|
|
|
|
if (sysCfg.flag.savestate) {
|
|
|
|
setRelay(power);
|
|
|
|
}
|
|
|
|
break;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-05-05 16:57:05 +01:00
|
|
|
} else {
|
2017-04-29 13:40:53 +01:00
|
|
|
power = sysCfg.power & ((1 << Maxdevice) -1);
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.savestate) {
|
2017-04-25 17:24:42 +01:00
|
|
|
setRelay(power);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 15:35:45 +01:00
|
|
|
|
|
|
|
// Issue #526
|
|
|
|
for (byte i = 0; i < Maxdevice; i++) {
|
2017-10-01 13:32:36 +01:00
|
|
|
if ((pin[GPIO_REL1 +i] < 99) && (digitalRead(pin[GPIO_REL1 +i]) ^ rel_inverted[i])) {
|
2017-08-29 15:35:45 +01:00
|
|
|
bitSet(power, i);
|
2017-08-30 10:08:18 +01:00
|
|
|
pulse_timer[i] = sysCfg.pulsetime[i];
|
2017-08-29 15:35:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
blink_powersave = power;
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (SONOFF_SC == sysCfg.module) {
|
|
|
|
sc_init();
|
|
|
|
}
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-03-16 15:23:11 +00:00
|
|
|
rtc_init();
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_PROJECT " %s %s (" D_CMND_TOPIC " %s, " D_FALLBACK " %s, " D_CMND_GROUPTOPIC " %s) " D_VERSION " %s"),
|
2017-01-28 13:41:01 +00:00
|
|
|
PROJECT, sysCfg.friendlyname[0], sysCfg.mqtt_topic, MQTTClient, sysCfg.mqtt_grptopic, Version);
|
2017-09-13 13:19:34 +01:00
|
|
|
addLog(LOG_LEVEL_INFO);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2017-02-28 15:01:48 +00:00
|
|
|
osw_loop();
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
pollDnsWeb();
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2017-01-29 20:36:12 +00:00
|
|
|
#ifdef USE_EMULATION
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.emulation) {
|
2017-04-25 17:24:42 +01:00
|
|
|
pollUDP();
|
|
|
|
}
|
2017-01-29 20:36:12 +00:00
|
|
|
#endif // USE_EMULATION
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (millis() >= timerxs) {
|
|
|
|
stateloop();
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
if (sysCfg.flag.mqtt_enabled) {
|
2017-04-25 17:24:42 +01:00
|
|
|
mqttClient.loop();
|
|
|
|
}
|
|
|
|
if (Serial.available()){
|
|
|
|
serial();
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-01-29 13:52:31 +00:00
|
|
|
// yield(); // yield == delay(0), delay contains yield, auto yield in loop
|
|
|
|
delay(sleep); // https://github.com/esp8266/Arduino/issues/2021
|
2017-08-08 15:08:08 +01:00
|
|
|
}
|