sonoff.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. sonoff.h - Master header file for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef _SONOFF_H_
  16. #define _SONOFF_H_
  17. /*********************************************************************************************\
  18. * Performance ROM (PROGMEM) vs RAM (RODATA)
  19. \*********************************************************************************************/
  20. //#define XFUNC_PTR_IN_ROM // Enable for keeping tables in ROM (PROGMEM) which seem to have access issues on some flash types
  21. /*********************************************************************************************\
  22. * Default sensor states
  23. \*********************************************************************************************/
  24. #define CODE_IMAGE 0
  25. #define USE_DHT // Default DHT11 sensor needs no external library
  26. #define USE_ENERGY_SENSOR // Use energy sensors (+14k code)
  27. #define USE_HLW8012 // Use energy sensor for Sonoff Pow and WolfBlitz
  28. #define USE_CSE7766 // Use energy sensor for Sonoff S31 and Pow R2
  29. /*********************************************************************************************\
  30. * Power Type
  31. \*********************************************************************************************/
  32. typedef unsigned long power_t; // Power (Relay) type
  33. #define POWER_MASK 0xffffffffUL // Power (Relay) full mask
  34. /*********************************************************************************************\
  35. * Defines
  36. \*********************************************************************************************/
  37. // Changes to the following MAX_ defines will impact settings layout
  38. #define MAX_SWITCHES 8 // Max number of switches
  39. #define MAX_RELAYS 8 // Max number of relays
  40. #define MAX_LEDS 4 // Max number of leds
  41. #define MAX_KEYS 4 // Max number of keys or buttons
  42. #define MAX_PWMS 5 // Max number of PWM channels
  43. #define MAX_COUNTERS 4 // Max number of counter sensors
  44. #define MAX_TIMERS 16 // Max number of Timers
  45. #define MAX_PULSETIMERS 8 // Max number of supported pulse timers
  46. #define MAX_FRIENDLYNAMES 4 // Max number of Friendly names
  47. #define MAX_DOMOTICZ_IDX 4 // Max number of Domoticz device, key and switch indices
  48. #define MAX_DOMOTICZ_SNS_IDX 12 // Max number of Domoticz sensors indices
  49. #define MAX_KNX_GA 10 // Max number of KNX Group Addresses to read that can be set
  50. #define MAX_KNX_CB 10 // Max number of KNX Group Addresses to write that can be set
  51. #define MAX_XNRG_DRIVERS 32 // Max number of allowed energy drivers
  52. #define MAX_XDSP_DRIVERS 32 // Max number of allowed display drivers
  53. #define MAX_XDRV_DRIVERS 96 // Max number of allowed driver drivers
  54. #define MAX_XSNS_DRIVERS 96 // Max number of allowed sensor drivers
  55. #define MAX_RULE_MEMS 5 // Max number of saved vars
  56. #define MAX_RULE_SETS 3 // Max number of rule sets of size 512 characters
  57. #define MAX_RULE_SIZE 512 // Max number of characters in rules
  58. // Changes to the following defines have no impact on settings layout
  59. #define MAX_RULE_TIMERS 8 // Max number of rule timers (4 bytes / timer)
  60. #define MAX_RULE_VARS 5 // Max number of rule variables (10 bytes / variable)
  61. #define MAX_FAN_SPEED 4 // Max number of iFan02 fan speeds (0 .. 3)
  62. #define MQTT_TOKEN_PREFIX "%prefix%" // To be substituted by mqtt_prefix[x]
  63. #define MQTT_TOKEN_TOPIC "%topic%" // To be substituted by mqtt_topic, mqtt_grptopic, mqtt_buttontopic, mqtt_switchtopic
  64. #define MQTT_TOKEN_HOSTNAME "%hostname%" // To be substituted by mqtt_topic, mqtt_grptopic, mqtt_buttontopic, mqtt_switchtopic
  65. #define MQTT_TOKEN_ID "%id%" // To be substituted by mqtt_topic, mqtt_grptopic, mqtt_buttontopic, mqtt_switchtopic
  66. #define WIFI_HOSTNAME "%s-%04d" // Expands to <MQTT_TOPIC>-<last 4 decimal chars of MAC address>
  67. #define CONFIG_FILE_SIGN 0xA5 // Configuration file signature
  68. #define CONFIG_FILE_XOR 0x5A // Configuration file xor (0 = No Xor)
  69. #define HLW_PREF_PULSE 12530 // was 4975us = 201Hz = 1000W
  70. #define HLW_UREF_PULSE 1950 // was 1666us = 600Hz = 220V
  71. #define HLW_IREF_PULSE 3500 // was 1666us = 600Hz = 4.545A
  72. #define MQTT_RETRY_SECS 10 // Minimum seconds to retry MQTT connection
  73. #define GLOBAL_VALUES_VALID 300 // Max number of seconds to keep last received values
  74. #define APP_POWER 0 // Default saved power state Off
  75. #define WS2812_MAX_LEDS 512 // Max number of LEDs
  76. #define PWM_RANGE 1023 // 255..1023 needs to be devisible by 256
  77. //#define PWM_FREQ 1000 // 100..1000 Hz led refresh
  78. //#define PWM_FREQ 910 // 100..1000 Hz led refresh (iTead value)
  79. #define PWM_FREQ 880 // 100..1000 Hz led refresh (BN-SZ01 value)
  80. #define PWM_MAX 4000 // [PWM_MAX] Maximum frequency - Default: 4000
  81. #define PWM_MIN 100 // [PWM_MIN] Minimum frequency - Default: 100
  82. // For Dimmers use double of your mains AC frequecy (100 for 50Hz and 120 for 60Hz)
  83. // For Controlling Servos use 50 and also set PWM_FREQ as 50 (DO NOT USE THESE VALUES FOR DIMMERS)
  84. #define DEFAULT_POWER_DELTA 80 // Power change percentage
  85. #define MAX_POWER_HOLD 10 // Time in SECONDS to allow max agreed power
  86. #define MAX_POWER_WINDOW 30 // Time in SECONDS to disable allow max agreed power
  87. #define SAFE_POWER_HOLD 10 // Time in SECONDS to allow max unit safe power
  88. #define SAFE_POWER_WINDOW 30 // Time in MINUTES to disable allow max unit safe power
  89. #define MAX_POWER_RETRY 5 // Retry count allowing agreed power limit overflow
  90. #define STATES 20 // Number of states per second using 50 mSec interval
  91. #define IMMINENT_RESET_FACTOR 10 // Factor to extent button hold time for imminent Reset to default 40 seconds using KEY_HOLD_TIME of 40
  92. #define BOOT_LOOP_TIME 10 // Number of seconds to stop detecting boot loops
  93. #define SYSLOG_TIMER 600 // Seconds to restore syslog_level
  94. #define SERIALLOG_TIMER 600 // Seconds to disable SerialLog
  95. #define OTA_ATTEMPTS 5 // Number of times to try fetching the new firmware
  96. #define INPUT_BUFFER_SIZE 520 // Max number of characters in (serial and http) command buffer
  97. #define CMDSZ 24 // Max number of characters in command
  98. #define TOPSZ 100 // Max number of characters in topic string
  99. #define LOGSZ 520 // Max number of characters in log
  100. #define MIN_MESSZ 893 // Min number of characters in MQTT message
  101. #define SENSOR_MAX_MISS 5 // Max number of missed sensor reads before deciding it's offline
  102. #ifdef USE_MQTT_TLS
  103. #define WEB_LOG_SIZE 2000 // Max number of characters in weblog
  104. #else
  105. #define WEB_LOG_SIZE 4000 // Max number of characters in weblog
  106. #endif
  107. #define MAX_BACKLOG 30 // Max number of commands in backlog
  108. #define MIN_BACKLOG_DELAY 2 // Minimal backlog delay in 0.1 seconds
  109. #define SOFT_BAUDRATE 9600 // Default software serial baudrate
  110. #define APP_BAUDRATE 115200 // Default serial baudrate
  111. #define SERIAL_POLLING 100 // Serial receive polling in ms
  112. #define MAX_STATUS 11 // Max number of status lines
  113. #define DRIVER_BOOT_DELAY 1 // Number of milliseconds to retard driver cycles during boot-up time to reduce overall CPU load whilst Wifi is connecting
  114. #define LOOP_SLEEP_DELAY 50 // Lowest number of milliseconds to go through the main loop using delay when needed
  115. #define NO_EXTRA_4K_HEAP // Allocate 4k heap for WPS in ESP8166/Arduino core v2.4.2 (was always allocated in previous versions)
  116. /*
  117. // Removed from esp8266 core since 20171105
  118. #define min(a,b) ((a)<(b)?(a):(b))
  119. #define max(a,b) ((a)>(b)?(a):(b))
  120. */
  121. #define tmin(a,b) ((a)<(b)?(a):(b))
  122. #define tmax(a,b) ((a)>(b)?(a):(b))
  123. #define STR_HELPER(x) #x
  124. #ifndef STR
  125. #define STR(x) STR_HELPER(x)
  126. #endif
  127. //enum ws2812NeopixelbusFeature { NEO_RGB, NEO_GRB, NEO_BRG, NEO_RBG, NEO_3LED, NEO_RGBW, NEO_GRBW }; // Doesn't work
  128. #define NEO_RGB 0 // Neopixel RGB leds
  129. #define NEO_GRB 1 // Neopixel GRB leds
  130. #define NEO_BRG 2 // Neopixel BRG leds
  131. #define NEO_RBG 3 // Neopixel RBG leds
  132. #define NEO_3LED 4 // Placeholder to test for 4 led types
  133. #define NEO_RGBW 5 // Neopixel RGBW leds
  134. #define NEO_GRBW 6 // Neopixel GRBW leds
  135. #define MQTT_PUBSUBCLIENT 1 // Mqtt PubSubClient library
  136. #define MQTT_TASMOTAMQTT 2 // Mqtt TasmotaMqtt library based on esp-mqtt-arduino - soon obsolete
  137. #define MQTT_ESPMQTTARDUINO 3 // Mqtt esp-mqtt-arduino library by Ingo Randolf - obsolete but define is present for debugging purposes
  138. #define MQTT_ARDUINOMQTT 4 // Mqtt arduino-mqtt library by Joel Gaehwiler (https://github.com/256dpi/arduino-mqtt)
  139. // Sunrise and Sunset DawnType
  140. #define DAWN_NORMAL -0.8333
  141. #define DAWN_CIVIL -6.0
  142. #define DAWN_NAUTIC -12.0
  143. #define DAWN_ASTRONOMIC -18.0
  144. // Sensor and Commands definition for KNX Driver
  145. #define KNX_TEMPERATURE 17
  146. #define KNX_HUMIDITY 18
  147. #define KNX_ENERGY_VOLTAGE 19
  148. #define KNX_ENERGY_CURRENT 20
  149. #define KNX_ENERGY_POWER 21
  150. #define KNX_ENERGY_POWERFACTOR 22
  151. #define KNX_ENERGY_DAILY 23
  152. #define KNX_ENERGY_START 24
  153. #define KNX_ENERGY_TOTAL 25
  154. #define KNX_SLOT1 26
  155. #define KNX_SLOT2 27
  156. #define KNX_SLOT3 28
  157. #define KNX_SLOT4 29
  158. #define KNX_SLOT5 30
  159. #define KNX_MAX_device_param 30
  160. #define MAX_KNXTX_CMNDS 5
  161. /*********************************************************************************************\
  162. * Enumeration
  163. \*********************************************************************************************/
  164. enum WeekInMonthOptions {Last, First, Second, Third, Fourth};
  165. enum DayOfTheWeekOptions {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};
  166. enum MonthNamesOptions {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
  167. enum HemisphereOptions {North, South};
  168. enum GetDateAndTimeOptions { DT_LOCAL, DT_UTC, DT_RESTART, DT_ENERGY };
  169. enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL};
  170. enum WifiConfigOptions {WIFI_RESTART, WIFI_SMARTCONFIG, WIFI_MANAGER, WIFI_WPSCONFIG, WIFI_RETRY, WIFI_WAIT, WIFI_SERIAL, MAX_WIFI_OPTION};
  171. enum SwitchModeOptions {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE, MAX_SWITCH_OPTION};
  172. enum LedStateOptions {LED_OFF, LED_POWER, LED_MQTTSUB, LED_POWER_MQTTSUB, LED_MQTTPUB, LED_POWER_MQTTPUB, LED_MQTT, LED_POWER_MQTT, MAX_LED_OPTION};
  173. enum EmulationOptions {EMUL_NONE, EMUL_WEMO, EMUL_HUE, EMUL_MAX};
  174. enum TopicOptions { CMND, STAT, TELE, nu1, RESULT_OR_CMND, RESULT_OR_STAT, RESULT_OR_TELE };
  175. enum ExecuteCommandPowerOptions { POWER_OFF, POWER_ON, POWER_TOGGLE, POWER_BLINK, POWER_BLINK_STOP, power_nu1, POWER_OFF_NO_STATE, POWER_ON_NO_STATE, power_nu2, POWER_SHOW_STATE };
  176. enum PowerOnStateOptions { POWER_ALL_OFF, POWER_ALL_ON, POWER_ALL_SAVED_TOGGLE, POWER_ALL_SAVED, POWER_ALL_ALWAYS_ON, POWER_ALL_OFF_PULSETIME_ON };
  177. enum ButtonStates { PRESSED, NOT_PRESSED };
  178. enum Shortcuts { SC_CLEAR, SC_DEFAULT, SC_USER };
  179. enum SettingsParmaIndex {P_HOLD_TIME, P_MAX_POWER_RETRY, P_TUYA_DIMMER_ID, P_MDNS_DELAYED_START, P_MAX_PARAM8}; // Max is PARAM8_SIZE (18) - SetOption32 until SetOption49
  180. enum DomoticzSensors {DZ_TEMP, DZ_TEMP_HUM, DZ_TEMP_HUM_BARO, DZ_POWER_ENERGY, DZ_ILLUMINANCE, DZ_COUNT, DZ_VOLTAGE, DZ_CURRENT, DZ_AIRQUALITY, DZ_MAX_SENSORS};
  181. enum Ws2812ClockIndex { WS_SECOND, WS_MINUTE, WS_HOUR, WS_MARKER };
  182. enum Ws2812Color { WS_RED, WS_GREEN, WS_BLUE };
  183. enum LightSubtypes { LST_NONE, LST_SINGLE, LST_COLDWARM, LST_RGB, LST_RGBW, LST_RGBWC }; // Do not insert new fields
  184. enum LightTypes { LT_BASIC, LT_PWM1, LT_PWM2, LT_PWM3, LT_PWM4, LT_PWM5, LT_PWM6, LT_PWM7,
  185. LT_NU8, LT_SERIAL1, LT_SERIAL2, LT_WS2812, LT_RGBW, LT_RGBWC, LT_NU14, LT_NU15 }; // Do not insert new fields
  186. enum LightSchemes {LS_POWER, LS_WAKEUP, LS_CYCLEUP, LS_CYCLEDN, LS_RANDOM, LS_MAX};
  187. enum XsnsFunctions {FUNC_SETTINGS_OVERRIDE, FUNC_MODULE_INIT, FUNC_PRE_INIT, FUNC_INIT,
  188. FUNC_LOOP, FUNC_EVERY_50_MSECOND, FUNC_EVERY_100_MSECOND, FUNC_EVERY_200_MSECOND, FUNC_EVERY_250_MSECOND, FUNC_EVERY_SECOND,
  189. FUNC_PREP_BEFORE_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_APPEND, FUNC_SAVE_BEFORE_RESTART, FUNC_COMMAND,
  190. FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA,
  191. FUNC_SET_POWER, FUNC_SET_DEVICE_POWER, FUNC_SHOW_SENSOR,
  192. FUNC_RULES_PROCESS, FUNC_SERIAL, FUNC_FREE_MEM, FUNC_BUTTON_PRESSED,
  193. FUNC_WEB_ADD_BUTTON, FUNC_WEB_ADD_MAIN_BUTTON, FUNC_WEB_ADD_HANDLER, FUNC_SET_CHANNELS};
  194. enum CommandSource { SRC_IGNORE, SRC_MQTT, SRC_RESTART, SRC_BUTTON, SRC_SWITCH, SRC_BACKLOG, SRC_SERIAL, SRC_WEBGUI, SRC_WEBCOMMAND, SRC_WEBCONSOLE, SRC_PULSETIMER,
  195. SRC_TIMER, SRC_RULE, SRC_MAXPOWER, SRC_MAXENERGY, SRC_LIGHT, SRC_KNX, SRC_DISPLAY, SRC_WEMO, SRC_HUE, SRC_RETRY, SRC_MAX };
  196. const char kCommandSource[] PROGMEM = "I|MQTT|Restart|Button|Switch|Backlog|Serial|WebGui|WebCommand|WebConsole|PulseTimer|Timer|Rule|MaxPower|MaxEnergy|Light|Knx|Display|Wemo|Hue|Retry";
  197. const uint8_t kDefaultRfCode[9] PROGMEM = { 0x21, 0x16, 0x01, 0x0E, 0x03, 0x48, 0x2E, 0x1A, 0x00 };
  198. const uint8_t kIFan02Speed[MAX_FAN_SPEED][3] = {{6,6,6}, {7,6,6}, {7,7,6}, {7,6,7}}; // Do not use PROGMEM as it fails
  199. /*********************************************************************************************\
  200. * Extern global variables
  201. \*********************************************************************************************/
  202. extern uint8_t light_device; // Light device number
  203. #endif // _SONOFF_H_