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.

530 lines
18 KiB

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