Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

523 lines
18 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
  1. // =============================================================================
  2. // SENSORS - General data
  3. // =============================================================================
  4. #define SENSOR_DEBUG 0 // Debug sensors
  5. #define SENSOR_READ_INTERVAL 6000 // Read data from sensors every 6 seconds
  6. #define SENSOR_REPORT_EVERY 10 // Report every this many readings
  7. #define SENSOR_USE_INDEX 0 // Use the index in topic (i.e. temperature/0)
  8. // even if just one sensor (0 for backwards compatibility)
  9. #ifndef SENSOR_TEMPERATURE_UNITS
  10. #define SENSOR_TEMPERATURE_UNITS TMP_CELSIUS // Temperature units (TMP_CELSIUS | TMP_FAHRENHEIT)
  11. #endif
  12. #ifndef SENSOR_TEMPERATURE_CORRECTION
  13. #define SENSOR_TEMPERATURE_CORRECTION 0.0 // Offset correction
  14. #endif
  15. #ifndef TEMPERATURE_MIN_CHANGE
  16. #define TEMPERATURE_MIN_CHANGE 0.0 // Minimum temperature change to report
  17. #endif
  18. #ifndef HUMIDITY_MIN_CHANGE
  19. #define HUMIDITY_MIN_CHANGE 0 // Minimum humidity change to report
  20. #endif
  21. #define HUMIDITY_NORMAL 0
  22. #define HUMIDITY_COMFORTABLE 1
  23. #define HUMIDITY_DRY 2
  24. #define HUMIDITY_WET 3
  25. //--------------------------------------------------------------------------------
  26. // Magnitudes
  27. //--------------------------------------------------------------------------------
  28. #define MAGNITUDE_TEMPERATURE_DECIMALS 1
  29. #define MAGNITUDE_HUMIDITY_DECIMALS 0
  30. #define MAGNITUDE_PRESSURE_DECIMALS 2
  31. #define MAGNITUDE_ANALOG_DECIMALS 0
  32. #define MAGNITUDE_EVENTS_DECIMALS 0
  33. #define MAGNITUDE_CURRENT_DECIMALS 3
  34. #define MAGNITUDE_VOLTAGE_DECIMALS 0
  35. #define MAGNITUDE_POWER_DECIMALS 0
  36. #define MAGNITUDE_POWER_FACTOR_DECIMALS 0
  37. #define MAGNITUDE_ENERGY_DECIMALS 0
  38. #define MAGNITUDE_PM1dot0_DECIMALS 0
  39. #define MAGNITUDE_PM2dot5_DECIMALS 0
  40. #define MAGNITUDE_PM10_DECIMALS 0
  41. #define MAGNITUDE_CO2_DECIMALS 0
  42. #define MAGNITUDE_UNKNOWN_TOPIC "unknown"
  43. #define MAGNITUDE_TEMPERATURE_TOPIC "temperature"
  44. #define MAGNITUDE_HUMIDITY_TOPIC "humidity"
  45. #define MAGNITUDE_PRESSURE_TOPIC "pressure"
  46. #define MAGNITUDE_CURRENT_TOPIC "current"
  47. #define MAGNITUDE_VOLTAGE_TOPIC "voltage"
  48. #define MAGNITUDE_ACTIVE_POWER_TOPIC "power"
  49. #define MAGNITUDE_APPARENT_POWER_TOPIC "apparent"
  50. #define MAGNITUDE_REACTIVE_POWER_TOPIC "reactive"
  51. #define MAGNITUDE_POWER_FACTOR_TOPIC "factor"
  52. #define MAGNITUDE_ENERGY_TOPIC "energy"
  53. #define MAGNITUDE_ENERGY_DELTA_TOPIC "energy_delta"
  54. #define MAGNITUDE_PM1dot0_TOPIC "pm1dot0"
  55. #define MAGNITUDE_PM2dot5_TOPIC "pm2dot5"
  56. #define MAGNITUDE_PM10_TOPIC "pm10"
  57. #define MAGNITUDE_ANALOG_TOPIC "analog"
  58. #define MAGNITUDE_DIGITAL_TOPIC "digital"
  59. #define MAGNITUDE_EVENTS_TOPIC "events"
  60. #define MAGNITUDE_CO2_TOPIC "co2"
  61. //--------------------------------------------------------------------------------
  62. // Sensor ID
  63. // These should remain over time, do not modify them, only add new ones at the end
  64. //--------------------------------------------------------------------------------
  65. #define SENSOR_DHTXX_ID 0x01
  66. #define SENSOR_DALLAS_ID 0x02
  67. #define SENSOR_EMON_ANALOG_ID 0x03
  68. #define SENSOR_EMON_ADC121_ID 0x04
  69. #define SENSOR_EMON_ADS1X15_ID 0x05
  70. #define SENSOR_HLW8012_ID 0x06
  71. #define SENSOR_V9261F_ID 0x07
  72. #define SENSOR_ECH1560_ID 0x08
  73. #define SENSOR_ANALOG_ID 0x09
  74. #define SENSOR_DIGITAL_ID 0x10
  75. #define SENSOR_EVENTS_ID 0x11
  76. #define SENSOR_PMSX003_ID 0x12
  77. #define SENSOR_BMX280_ID 0x13
  78. #define SENSOR_MHZ19_ID 0x14
  79. #define SENSOR_SI7021_ID 0x15
  80. #define SENSOR_SHT3X_I2C_ID 0x16
  81. // =============================================================================
  82. // Specific data for each sensor
  83. // =============================================================================
  84. //------------------------------------------------------------------------------
  85. // Analog sensor
  86. // Enable support by passing ANALOG_SUPPORT=1 build flag
  87. //--------------------------------------------------------------------------------
  88. #ifndef ANALOG_SUPPORT
  89. #define ANALOG_SUPPORT 0
  90. #endif
  91. #if ANALOG_SUPPORT
  92. #undef ADC_VCC_ENABLED
  93. #define ADC_VCC_ENABLED 0
  94. #endif
  95. //------------------------------------------------------------------------------
  96. // BME280/BMP280
  97. // Enable support by passing BMX280_SUPPORT=1 build flag
  98. //------------------------------------------------------------------------------
  99. #ifndef BMX280_SUPPORT
  100. #define BMX280_SUPPORT 0
  101. #endif
  102. #ifndef BMX280_ADDRESS
  103. #define BMX280_ADDRESS 0x00 // 0x00 means auto
  104. #endif
  105. #define BMX280_MODE 1 // 1 for forced mode, 3 for normal mode
  106. #define BMX280_TEMPERATURE 1 // Oversampling for temperature (set to 0 to disable magnitude)
  107. #define BMX280_HUMIDITY 1 // Oversampling for humidity (set to 0 to disable magnitude, only for BME280)
  108. #define BMX280_PRESSURE 1 // Oversampling for pressure (set to 0 to disable magnitude)
  109. #if BMX280_SUPPORT
  110. #undef I2C_SUPPORT
  111. #define I2C_SUPPORT 1
  112. #endif
  113. //------------------------------------------------------------------------------
  114. // Dallas OneWire temperature sensors
  115. // Enable support by passing DALLAS_SUPPORT=1 build flag
  116. //------------------------------------------------------------------------------
  117. #ifndef DALLAS_SUPPORT
  118. #define DALLAS_SUPPORT 0
  119. #endif
  120. #ifndef DALLAS_PIN
  121. #define DALLAS_PIN 13
  122. #endif
  123. #define DALLAS_RESOLUTION 9 // Not used atm
  124. //------------------------------------------------------------------------------
  125. // DHTXX temperature/humidity sensor
  126. // Enable support by passing DHT_SUPPORT=1 build flag
  127. //------------------------------------------------------------------------------
  128. #ifndef DHT_SUPPORT
  129. #define DHT_SUPPORT 0
  130. #endif
  131. #ifndef DHT_PIN
  132. #define DHT_PIN 13
  133. #endif
  134. #ifndef DHT_TYPE
  135. #define DHT_TYPE DHT_CHIP_DHT22
  136. #endif
  137. //------------------------------------------------------------------------------
  138. // Digital sensor
  139. // Enable support by passing DIGITAL_SUPPORT=1 build flag
  140. //------------------------------------------------------------------------------
  141. #ifndef DIGITAL_SUPPORT
  142. #define DIGITAL_SUPPORT 0
  143. #endif
  144. #ifndef DIGITAL_PIN
  145. #define DIGITAL_PIN 2
  146. #endif
  147. #ifndef DIGITAL_PIN_MODE
  148. #define DIGITAL_PIN_MODE INPUT_PULLUP
  149. #endif
  150. #ifndef DIGITAL_DEFAULT_STATE
  151. #define DIGITAL_DEFAULT_STATE 1
  152. #endif
  153. //------------------------------------------------------------------------------
  154. // ECH1560 based power sensor
  155. // Enable support by passing ECH1560_SUPPORT=1 build flag
  156. //------------------------------------------------------------------------------
  157. #ifndef ECH1560_SUPPORT
  158. #define ECH1560_SUPPORT 0
  159. #endif
  160. #ifndef ECH1560_CLK_PIN
  161. #define ECH1560_CLK_PIN 4 // CLK pin for the ECH1560
  162. #endif
  163. #ifndef ECH1560_MISO_PIN
  164. #define ECH1560_MISO_PIN 5 // MISO pin for the ECH1560
  165. #endif
  166. #ifndef ECH1560_INVERTED
  167. #define ECH1560_INVERTED 0 // Signal is inverted
  168. #endif
  169. //------------------------------------------------------------------------------
  170. // Energy Monitor general settings
  171. //------------------------------------------------------------------------------
  172. #define EMON_MAX_SAMPLES 1000 // Max number of samples to get
  173. #define EMON_MAX_TIME 250 // Max time in ms to sample
  174. #define EMON_FILTER_SPEED 512 // Mobile average filter speed
  175. #define EMON_MAINS_VOLTAGE 230 // Mains voltage
  176. #define EMON_REFERENCE_VOLTAGE 3.3 // Reference voltage of the ADC
  177. #define EMON_CURRENT_RATIO 30 // Current ratio in the clamp (30V/1A)
  178. #define EMON_REPORT_CURRENT 0 // Report current
  179. #define EMON_REPORT_POWER 1 // Report power
  180. #define EMON_REPORT_ENERGY 1 // Report energy
  181. //------------------------------------------------------------------------------
  182. // Energy Monitor based on ADC121
  183. // Enable support by passing EMON_ADC121_SUPPORT=1 build flag
  184. //------------------------------------------------------------------------------
  185. #ifndef EMON_ADC121_SUPPORT
  186. #define EMON_ADC121_SUPPORT 0 // Do not build support by default
  187. #endif
  188. #define EMON_ADC121_I2C_ADDRESS 0x00 // 0x00 means auto
  189. #if EMON_ADC121_SUPPORT
  190. #undef I2C_SUPPORT
  191. #define I2C_SUPPORT 1
  192. #endif
  193. //------------------------------------------------------------------------------
  194. // Energy Monitor based on ADS1X15
  195. // Enable support by passing EMON_ADS1X15_SUPPORT=1 build flag
  196. //------------------------------------------------------------------------------
  197. #ifndef EMON_ADS1X15_SUPPORT
  198. #define EMON_ADS1X15_SUPPORT 0 // Do not build support by default
  199. #endif
  200. #define EMON_ADS1X15_I2C_ADDRESS 0x00 // 0x00 means auto
  201. #define EMON_ADS1X15_TYPE ADS1X15_CHIP_ADS1115
  202. #define EMON_ADS1X15_GAIN ADS1X15_REG_CONFIG_PGA_4_096V
  203. #define EMON_ADS1X15_MASK 0x0F // A0=1 A1=2 A2=4 A4=8
  204. #if EMON_ADS1X15_SUPPORT
  205. #undef I2C_SUPPORT
  206. #define I2C_SUPPORT 1
  207. #endif
  208. //------------------------------------------------------------------------------
  209. // Energy Monitor based on interval analog GPIO
  210. // Enable support by passing EMON_ANALOG_SUPPORT=1 build flag
  211. //------------------------------------------------------------------------------
  212. #ifndef EMON_ANALOG_SUPPORT
  213. #define EMON_ANALOG_SUPPORT 0 // Do not build support by default
  214. #endif
  215. #if EMON_ANALOG_SUPPORT
  216. #undef ADC_VCC_ENABLED
  217. #define ADC_VCC_ENABLED 0
  218. #endif
  219. //------------------------------------------------------------------------------
  220. // Counter sensor
  221. // Enable support by passing EVENTS_SUPPORT=1 build flag
  222. //------------------------------------------------------------------------------
  223. #ifndef EVENTS_SUPPORT
  224. #define EVENTS_SUPPORT 0 // Do not build with counter support by default
  225. #endif
  226. #ifndef EVENTS_PIN
  227. #define EVENTS_PIN 2 // GPIO to monitor
  228. #endif
  229. #ifndef EVENTS_PIN_MODE
  230. #define EVENTS_PIN_MODE INPUT // INPUT, INPUT_PULLUP
  231. #endif
  232. #ifndef EVENTS_INTERRUPT_MODE
  233. #define EVENTS_INTERRUPT_MODE RISING // RISING, FALLING, BOTH
  234. #endif
  235. #define EVENTS_DEBOUNCE 50 // Do not register events within less than 10 millis
  236. //------------------------------------------------------------------------------
  237. // HLW8012 Energy monitor IC
  238. // Enable support by passing HLW8012_SUPPORT=1 build flag
  239. //------------------------------------------------------------------------------
  240. #ifndef HLW8012_SUPPORT
  241. #define HLW8012_SUPPORT 0
  242. #endif
  243. #ifndef HLW8012_SEL_PIN
  244. #define HLW8012_SEL_PIN 5
  245. #endif
  246. #ifndef HLW8012_CF1_PIN
  247. #define HLW8012_CF1_PIN 13
  248. #endif
  249. #ifndef HLW8012_CF_PIN
  250. #define HLW8012_CF_PIN 14
  251. #endif
  252. #define HLW8012_USE_INTERRUPTS 1 // Use interrupts to trap HLW8012 signals
  253. #define HLW8012_SEL_CURRENT HIGH // SEL pin to HIGH to measure current
  254. #define HLW8012_CURRENT_R 0.001 // Current resistor
  255. #define HLW8012_VOLTAGE_R_UP ( 5 * 470000 ) // Upstream voltage resistor
  256. #define HLW8012_VOLTAGE_R_DOWN ( 1000 ) // Downstream voltage resistor
  257. //------------------------------------------------------------------------------
  258. // MHZ19 CO2 sensor
  259. // Enable support by passing MHZ19_SUPPORT=1 build flag
  260. //------------------------------------------------------------------------------
  261. #ifndef MHZ19_SUPPORT
  262. #define MHZ19_SUPPORT 0
  263. #endif
  264. #define MHZ19_RX_PIN 13
  265. #define MHZ19_TX_PIN 15
  266. //------------------------------------------------------------------------------
  267. // Particle Monitor based on Plantower PMSX003
  268. // Enable support by passing PMSX003_SUPPORT=1 build flag
  269. //------------------------------------------------------------------------------
  270. #ifndef PMSX003_SUPPORT
  271. #define PMSX003_SUPPORT 0
  272. #endif
  273. #define PMS_RX_PIN 13
  274. #define PMS_TX_PIN 15
  275. //------------------------------------------------------------------------------
  276. // SHT3X I2C (Wemos) temperature & humidity sensor
  277. // Enable support by passing SHT3X_SUPPORT=1 build flag
  278. //------------------------------------------------------------------------------
  279. #ifndef SHT3X_I2C_SUPPORT
  280. #define SHT3X_I2C_SUPPORT 0
  281. #endif
  282. #ifndef SHT3X_I2C_ADDRESS
  283. #define SHT3X_I2C_ADDRESS 0x00 // 0x00 means auto
  284. #endif
  285. #if SHT3X_I2C_SUPPORT
  286. #undef I2C_SUPPORT
  287. #define I2C_SUPPORT 1
  288. #endif
  289. //------------------------------------------------------------------------------
  290. // SI7021 temperature & humidity sensor
  291. // Enable support by passing SI7021_SUPPORT=1 build flag
  292. //------------------------------------------------------------------------------
  293. #ifndef SI7021_SUPPORT
  294. #define SI7021_SUPPORT 0
  295. #endif
  296. #ifndef SI7021_ADDRESS
  297. #define SI7021_ADDRESS 0x00 // 0x00 means auto
  298. #endif
  299. #if SI7021_SUPPORT
  300. #undef I2C_SUPPORT
  301. #define I2C_SUPPORT 1
  302. #endif
  303. //------------------------------------------------------------------------------
  304. // V9261F based power sensor
  305. // Enable support by passing SI7021_SUPPORT=1 build flag
  306. //------------------------------------------------------------------------------
  307. #ifndef V9261F_SUPPORT
  308. #define V9261F_SUPPORT 0
  309. #endif
  310. #ifndef V9261F_PIN
  311. #define V9261F_PIN 2 // TX pin from the V9261F
  312. #endif
  313. #ifndef V9261F_PIN_INVERSE
  314. #define V9261F_PIN_INVERSE 1 // Signal is inverted
  315. #endif
  316. #define V9261F_SYNC_INTERVAL 600 // Sync signal length (ms)
  317. #define V9261F_BAUDRATE 4800 // UART baudrate
  318. // Default ratios
  319. #define V9261F_CURRENT_FACTOR 79371434.0
  320. #define V9261F_VOLTAGE_FACTOR 4160651.0
  321. #define V9261F_POWER_FACTOR 153699.0
  322. #define V9261F_RPOWER_FACTOR V9261F_CURRENT_FACTOR
  323. // =============================================================================
  324. // Sensor helpers configuration
  325. // =============================================================================
  326. // -----------------------------------------------------------------------------
  327. // I2C
  328. // -----------------------------------------------------------------------------
  329. #ifndef I2C_SUPPORT
  330. #define I2C_SUPPORT 0 // I2C enabled (1.98Kb)
  331. #endif
  332. #define I2C_USE_BRZO 0 // Use brzo_i2c library or standard Wire
  333. #ifndef I2C_SDA_PIN
  334. #define I2C_SDA_PIN SDA // SDA GPIO (Sonoff => 4)
  335. #endif
  336. #ifndef I2C_SCL_PIN
  337. #define I2C_SCL_PIN SCL // SCL GPIO (Sonoff => 14)
  338. #endif
  339. #define I2C_CLOCK_STRETCH_TIME 200 // BRZO clock stretch time
  340. #define I2C_SCL_FREQUENCY 1000 // BRZO SCL frequency
  341. #define I2C_CLEAR_BUS 0 // Clear I2C bus at boot
  342. //--------------------------------------------------------------------------------
  343. // Internal power monitor
  344. // Enable support by passing ADC_VCC_ENABLED=1 build flag
  345. // Do not enable this if using the analog GPIO for any other thing
  346. //--------------------------------------------------------------------------------
  347. #ifndef ADC_VCC_ENABLED
  348. #define ADC_VCC_ENABLED 1
  349. #endif
  350. #if ADC_VCC_ENABLED
  351. ADC_MODE(ADC_VCC);
  352. #endif
  353. //--------------------------------------------------------------------------------
  354. // Class loading
  355. //--------------------------------------------------------------------------------
  356. #include "sensors/BaseSensor.h"
  357. #if ANALOG_SUPPORT
  358. #include "sensors/AnalogSensor.h"
  359. #endif
  360. #if BMX280_SUPPORT
  361. #include <SparkFunBME280.h>
  362. #include "sensors/BMX280Sensor.h"
  363. #endif
  364. #if DALLAS_SUPPORT
  365. #include <OneWire.h>
  366. #include "sensors/DallasSensor.h"
  367. #endif
  368. #if DHT_SUPPORT
  369. #include "sensors/DHTSensor.h"
  370. #endif
  371. #if DIGITAL_SUPPORT
  372. #include "sensors/DigitalSensor.h"
  373. #endif
  374. #if ECH1560_SUPPORT
  375. #include "sensors/ECH1560Sensor.h"
  376. #endif
  377. #if EMON_ADC121_SUPPORT
  378. #include "sensors/EmonADC121Sensor.h"
  379. #endif
  380. #if EMON_ADS1X15_SUPPORT
  381. #include "sensors/EmonADS1X15Sensor.h"
  382. #endif
  383. #if EMON_ANALOG_SUPPORT
  384. #include "sensors/EmonAnalogSensor.h"
  385. #endif
  386. #if EVENTS_SUPPORT
  387. #include "sensors/EventSensor.h"
  388. #endif
  389. #if HLW8012_SUPPORT
  390. #include <HLW8012.h>
  391. #include "sensors/HLW8012Sensor.h"
  392. #endif
  393. #if MHZ19_SUPPORT
  394. #include <SoftwareSerial.h>
  395. #include "sensors/MHZ19Sensor.h"
  396. #endif
  397. #if PMSX003_SUPPORT
  398. #include <SoftwareSerial.h>
  399. #include <PMS.h>
  400. #include "sensors/PMSX003Sensor.h"
  401. #endif
  402. #if SI7021_SUPPORT
  403. #include "sensors/SI7021Sensor.h"
  404. #endif
  405. #if SHT3X_I2C_SUPPORT
  406. #include "sensors/SHT3XI2CSensor.h"
  407. #endif
  408. #if V9261F_SUPPORT
  409. #include <SoftwareSerial.h>
  410. #include "sensors/V9261FSensor.h"
  411. #endif