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.

587 lines
20 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. typedef enum magnitude_t {
  33. MAGNITUDE_NONE = 0,
  34. MAGNITUDE_TEMPERATURE,
  35. MAGNITUDE_HUMIDITY,
  36. MAGNITUDE_PRESSURE,
  37. MAGNITUDE_CURRENT,
  38. MAGNITUDE_VOLTAGE,
  39. MAGNITUDE_POWER_ACTIVE,
  40. MAGNITUDE_POWER_APPARENT,
  41. MAGNITUDE_POWER_REACTIVE,
  42. MAGNITUDE_ENERGY,
  43. MAGNITUDE_ENERGY_DELTA,
  44. MAGNITUDE_POWER_FACTOR,
  45. MAGNITUDE_ANALOG,
  46. MAGNITUDE_DIGITAL,
  47. MAGNITUDE_EVENTS,
  48. MAGNITUDE_PM1dot0,
  49. MAGNITUDE_PM2dot5,
  50. MAGNITUDE_PM10,
  51. MAGNITUDE_CO2,
  52. MAGNITUDE_MAX,
  53. } magnitude_t;
  54. PROGMEM const unsigned char magnitude_decimals[] = {
  55. 0,
  56. 1, 0, 2,
  57. 3, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0,
  59. 0, 0, 0,
  60. 0
  61. };
  62. PROGMEM const char magnitude_unknown_topic[] = "unknown";
  63. PROGMEM const char magnitude_temperature_topic[] = "temperature";
  64. PROGMEM const char magnitude_humidity_topic[] = "humidity";
  65. PROGMEM const char magnitude_pressure_topic[] = "pressure";
  66. PROGMEM const char magnitude_current_topic[] = "current";
  67. PROGMEM const char magnitude_voltage_topic[] = "voltage";
  68. PROGMEM const char magnitude_active_power_topic[] = "power";
  69. PROGMEM const char magnitude_apparent_power_topic[] = "apparent";
  70. PROGMEM const char magnitude_reactive_power_topic[] = "reactive";
  71. PROGMEM const char magnitude_power_factor_topic[] = "factor";
  72. PROGMEM const char magnitude_energy_topic[] = "energy";
  73. PROGMEM const char magnitude_energy_delta_topic[] = "energy_delta";
  74. PROGMEM const char magnitude_pm1dot0_topic[] = "pm1dot0";
  75. PROGMEM const char magnitude_pm2dot5_topic[] = "pm2dot5";
  76. PROGMEM const char magnitude_pm10_topic[] = "pm10";
  77. PROGMEM const char magnitude_analog_topic[] = "analog";
  78. PROGMEM const char magnitude_digital_topic[] = "digital";
  79. PROGMEM const char magnitude_events_topic[] = "events";
  80. PROGMEM const char magnitude_co2_topic[] = "co2";
  81. PROGMEM const char* const magnitude_topics[] = {
  82. magnitude_unknown_topic, magnitude_temperature_topic, magnitude_humidity_topic,
  83. magnitude_pressure_topic, magnitude_current_topic, magnitude_voltage_topic,
  84. magnitude_active_power_topic, magnitude_apparent_power_topic, magnitude_reactive_power_topic,
  85. magnitude_power_factor_topic, magnitude_energy_topic, magnitude_energy_delta_topic,
  86. magnitude_pm1dot0_topic, magnitude_pm2dot5_topic, magnitude_pm10_topic,
  87. magnitude_analog_topic, magnitude_digital_topic, magnitude_events_topic,
  88. magnitude_co2_topic
  89. };
  90. PROGMEM const char magnitude_empty[] = "";
  91. PROGMEM const char magnitude_celsius[] = "C";
  92. PROGMEM const char magnitude_fahrenheit[] = "F";
  93. PROGMEM const char magnitude_percentage[] = "%";
  94. PROGMEM const char magnitude_hectopascals[] = "hPa";
  95. PROGMEM const char magnitude_amperes[] = "A";
  96. PROGMEM const char magnitude_volts[] = "V";
  97. PROGMEM const char magnitude_watts[] = "W";
  98. PROGMEM const char magnitude_joules[] = "J";
  99. PROGMEM const char magnitude_ugm3[] = "µg/m3";
  100. PROGMEM const char magnitude_ppm[] = "ppm";
  101. PROGMEM const char* const magnitude_units[] = {
  102. magnitude_empty, magnitude_celsius, magnitude_percentage,
  103. magnitude_hectopascals, magnitude_amperes, magnitude_volts,
  104. magnitude_watts, magnitude_watts, magnitude_watts,
  105. magnitude_percentage, magnitude_joules, magnitude_joules,
  106. magnitude_ugm3, magnitude_ugm3, magnitude_ugm3,
  107. magnitude_empty, magnitude_empty, magnitude_empty,
  108. magnitude_ppm
  109. };
  110. //--------------------------------------------------------------------------------
  111. // Sensor ID
  112. // These should remain over time, do not modify them, only add new ones at the end
  113. //--------------------------------------------------------------------------------
  114. #define SENSOR_DHTXX_ID 0x01
  115. #define SENSOR_DALLAS_ID 0x02
  116. #define SENSOR_EMON_ANALOG_ID 0x03
  117. #define SENSOR_EMON_ADC121_ID 0x04
  118. #define SENSOR_EMON_ADS1X15_ID 0x05
  119. #define SENSOR_HLW8012_ID 0x06
  120. #define SENSOR_V9261F_ID 0x07
  121. #define SENSOR_ECH1560_ID 0x08
  122. #define SENSOR_ANALOG_ID 0x09
  123. #define SENSOR_DIGITAL_ID 0x10
  124. #define SENSOR_EVENTS_ID 0x11
  125. #define SENSOR_PMSX003_ID 0x12
  126. #define SENSOR_BMX280_ID 0x13
  127. #define SENSOR_MHZ19_ID 0x14
  128. #define SENSOR_SI7021_ID 0x15
  129. #define SENSOR_SHT3X_I2C_ID 0x16
  130. // =============================================================================
  131. // Specific data for each sensor
  132. // =============================================================================
  133. //------------------------------------------------------------------------------
  134. // Analog sensor
  135. // Enable support by passing ANALOG_SUPPORT=1 build flag
  136. //--------------------------------------------------------------------------------
  137. #ifndef ANALOG_SUPPORT
  138. #define ANALOG_SUPPORT 0
  139. #endif
  140. #if ANALOG_SUPPORT
  141. #undef ADC_VCC_ENABLED
  142. #define ADC_VCC_ENABLED 0
  143. #endif
  144. //------------------------------------------------------------------------------
  145. // BME280/BMP280
  146. // Enable support by passing BMX280_SUPPORT=1 build flag
  147. //------------------------------------------------------------------------------
  148. #ifndef BMX280_SUPPORT
  149. #define BMX280_SUPPORT 1
  150. #endif
  151. #ifndef BMX280_ADDRESS
  152. #define BMX280_ADDRESS 0x00 // 0x00 means auto
  153. #endif
  154. #define BMX280_MODE 1 // 1 for forced mode, 3 for normal mode
  155. #define BMX280_TEMPERATURE 1 // Oversampling for temperature (set to 0 to disable magnitude)
  156. #define BMX280_HUMIDITY 1 // Oversampling for humidity (set to 0 to disable magnitude, only for BME280)
  157. #define BMX280_PRESSURE 1 // Oversampling for pressure (set to 0 to disable magnitude)
  158. #if BMX280_SUPPORT
  159. #undef I2C_SUPPORT
  160. #define I2C_SUPPORT 1
  161. #endif
  162. //------------------------------------------------------------------------------
  163. // Dallas OneWire temperature sensors
  164. // Enable support by passing DALLAS_SUPPORT=1 build flag
  165. //------------------------------------------------------------------------------
  166. #ifndef DALLAS_SUPPORT
  167. #define DALLAS_SUPPORT 0
  168. #endif
  169. #ifndef DALLAS_PIN
  170. #define DALLAS_PIN 13
  171. #endif
  172. #define DALLAS_RESOLUTION 9 // Not used atm
  173. #define DALLAS_READ_INTERVAL 2000 // Force sensor read & cache every 2 seconds
  174. //------------------------------------------------------------------------------
  175. // DHTXX temperature/humidity sensor
  176. // Enable support by passing DHT_SUPPORT=1 build flag
  177. //------------------------------------------------------------------------------
  178. #ifndef DHT_SUPPORT
  179. #define DHT_SUPPORT 0
  180. #endif
  181. #ifndef DHT_PIN
  182. #define DHT_PIN 13
  183. #endif
  184. #ifndef DHT_TYPE
  185. #define DHT_TYPE DHT_CHIP_DHT22
  186. #endif
  187. //------------------------------------------------------------------------------
  188. // Digital sensor
  189. // Enable support by passing DIGITAL_SUPPORT=1 build flag
  190. //------------------------------------------------------------------------------
  191. #ifndef DIGITAL_SUPPORT
  192. #define DIGITAL_SUPPORT 0
  193. #endif
  194. #ifndef DIGITAL_PIN
  195. #define DIGITAL_PIN 2
  196. #endif
  197. #ifndef DIGITAL_PIN_MODE
  198. #define DIGITAL_PIN_MODE INPUT_PULLUP
  199. #endif
  200. #ifndef DIGITAL_DEFAULT_STATE
  201. #define DIGITAL_DEFAULT_STATE 1
  202. #endif
  203. //------------------------------------------------------------------------------
  204. // ECH1560 based power sensor
  205. // Enable support by passing ECH1560_SUPPORT=1 build flag
  206. //------------------------------------------------------------------------------
  207. #ifndef ECH1560_SUPPORT
  208. #define ECH1560_SUPPORT 0
  209. #endif
  210. #ifndef ECH1560_CLK_PIN
  211. #define ECH1560_CLK_PIN 4 // CLK pin for the ECH1560
  212. #endif
  213. #ifndef ECH1560_MISO_PIN
  214. #define ECH1560_MISO_PIN 5 // MISO pin for the ECH1560
  215. #endif
  216. #ifndef ECH1560_INVERTED
  217. #define ECH1560_INVERTED 0 // Signal is inverted
  218. #endif
  219. //------------------------------------------------------------------------------
  220. // Energy Monitor general settings
  221. //------------------------------------------------------------------------------
  222. #define EMON_MAX_SAMPLES 1000 // Max number of samples to get
  223. #define EMON_MAX_TIME 250 // Max time in ms to sample
  224. #define EMON_FILTER_SPEED 512 // Mobile average filter speed
  225. #define EMON_MAINS_VOLTAGE 230 // Mains voltage
  226. #define EMON_REFERENCE_VOLTAGE 3.3 // Reference voltage of the ADC
  227. #define EMON_CURRENT_RATIO 30 // Current ratio in the clamp (30V/1A)
  228. #define EMON_REPORT_CURRENT 0 // Report current
  229. #define EMON_REPORT_POWER 1 // Report power
  230. #define EMON_REPORT_ENERGY 1 // Report energy
  231. //------------------------------------------------------------------------------
  232. // Energy Monitor based on ADC121
  233. // Enable support by passing EMON_ADC121_SUPPORT=1 build flag
  234. //------------------------------------------------------------------------------
  235. #ifndef EMON_ADC121_SUPPORT
  236. #define EMON_ADC121_SUPPORT 0 // Do not build support by default
  237. #endif
  238. #define EMON_ADC121_I2C_ADDRESS 0x00 // 0x00 means auto
  239. #if EMON_ADC121_SUPPORT
  240. #undef I2C_SUPPORT
  241. #define I2C_SUPPORT 1
  242. #endif
  243. //------------------------------------------------------------------------------
  244. // Energy Monitor based on ADS1X15
  245. // Enable support by passing EMON_ADS1X15_SUPPORT=1 build flag
  246. //------------------------------------------------------------------------------
  247. #ifndef EMON_ADS1X15_SUPPORT
  248. #define EMON_ADS1X15_SUPPORT 0 // Do not build support by default
  249. #endif
  250. #define EMON_ADS1X15_I2C_ADDRESS 0x00 // 0x00 means auto
  251. #define EMON_ADS1X15_TYPE ADS1X15_CHIP_ADS1115
  252. #define EMON_ADS1X15_GAIN ADS1X15_REG_CONFIG_PGA_4_096V
  253. #define EMON_ADS1X15_MASK 0x0F // A0=1 A1=2 A2=4 A4=8
  254. #if EMON_ADS1X15_SUPPORT
  255. #undef I2C_SUPPORT
  256. #define I2C_SUPPORT 1
  257. #endif
  258. //------------------------------------------------------------------------------
  259. // Energy Monitor based on interval analog GPIO
  260. // Enable support by passing EMON_ANALOG_SUPPORT=1 build flag
  261. //------------------------------------------------------------------------------
  262. #ifndef EMON_ANALOG_SUPPORT
  263. #define EMON_ANALOG_SUPPORT 0 // Do not build support by default
  264. #endif
  265. #if EMON_ANALOG_SUPPORT
  266. #undef ADC_VCC_ENABLED
  267. #define ADC_VCC_ENABLED 0
  268. #endif
  269. //------------------------------------------------------------------------------
  270. // Counter sensor
  271. // Enable support by passing EVENTS_SUPPORT=1 build flag
  272. //------------------------------------------------------------------------------
  273. #ifndef EVENTS_SUPPORT
  274. #define EVENTS_SUPPORT 0 // Do not build with counter support by default
  275. #endif
  276. #ifndef EVENTS_PIN
  277. #define EVENTS_PIN 2 // GPIO to monitor
  278. #endif
  279. #ifndef EVENTS_PIN_MODE
  280. #define EVENTS_PIN_MODE INPUT // INPUT, INPUT_PULLUP
  281. #endif
  282. #ifndef EVENTS_INTERRUPT_MODE
  283. #define EVENTS_INTERRUPT_MODE RISING // RISING, FALLING, BOTH
  284. #endif
  285. #define EVENTS_DEBOUNCE 50 // Do not register events within less than 10 millis
  286. //------------------------------------------------------------------------------
  287. // HLW8012 Energy monitor IC
  288. // Enable support by passing HLW8012_SUPPORT=1 build flag
  289. //------------------------------------------------------------------------------
  290. #ifndef HLW8012_SUPPORT
  291. #define HLW8012_SUPPORT 0
  292. #endif
  293. #ifndef HLW8012_SEL_PIN
  294. #define HLW8012_SEL_PIN 5
  295. #endif
  296. #ifndef HLW8012_CF1_PIN
  297. #define HLW8012_CF1_PIN 13
  298. #endif
  299. #ifndef HLW8012_CF_PIN
  300. #define HLW8012_CF_PIN 14
  301. #endif
  302. #define HLW8012_USE_INTERRUPTS 1 // Use interrupts to trap HLW8012 signals
  303. #define HLW8012_SEL_CURRENT HIGH // SEL pin to HIGH to measure current
  304. #define HLW8012_CURRENT_R 0.001 // Current resistor
  305. #define HLW8012_VOLTAGE_R_UP ( 5 * 470000 ) // Upstream voltage resistor
  306. #define HLW8012_VOLTAGE_R_DOWN ( 1000 ) // Downstream voltage resistor
  307. //------------------------------------------------------------------------------
  308. // MHZ19 CO2 sensor
  309. // Enable support by passing MHZ19_SUPPORT=1 build flag
  310. //------------------------------------------------------------------------------
  311. #ifndef MHZ19_SUPPORT
  312. #define MHZ19_SUPPORT 0
  313. #endif
  314. #define MHZ19_RX_PIN 13
  315. #define MHZ19_TX_PIN 15
  316. //------------------------------------------------------------------------------
  317. // Particle Monitor based on Plantower PMSX003
  318. // Enable support by passing PMSX003_SUPPORT=1 build flag
  319. //------------------------------------------------------------------------------
  320. #ifndef PMSX003_SUPPORT
  321. #define PMSX003_SUPPORT 0
  322. #endif
  323. #define PMS_RX_PIN 13
  324. #define PMS_TX_PIN 15
  325. //------------------------------------------------------------------------------
  326. // SHT3X I2C (Wemos) temperature & humidity sensor
  327. // Enable support by passing SHT3X_SUPPORT=1 build flag
  328. //------------------------------------------------------------------------------
  329. #ifndef SHT3X_I2C_SUPPORT
  330. #define SHT3X_I2C_SUPPORT 0
  331. #endif
  332. #ifndef SHT3X_I2C_ADDRESS
  333. #define SHT3X_I2C_ADDRESS 0x00 // 0x00 means auto
  334. #endif
  335. #if SHT3X_I2C_SUPPORT
  336. #undef I2C_SUPPORT
  337. #define I2C_SUPPORT 1
  338. #endif
  339. //------------------------------------------------------------------------------
  340. // SI7021 temperature & humidity sensor
  341. // Enable support by passing SI7021_SUPPORT=1 build flag
  342. //------------------------------------------------------------------------------
  343. #ifndef SI7021_SUPPORT
  344. #define SI7021_SUPPORT 0
  345. #endif
  346. #ifndef SI7021_ADDRESS
  347. #define SI7021_ADDRESS 0x00 // 0x00 means auto
  348. #endif
  349. #if SI7021_SUPPORT
  350. #undef I2C_SUPPORT
  351. #define I2C_SUPPORT 1
  352. #endif
  353. //------------------------------------------------------------------------------
  354. // V9261F based power sensor
  355. // Enable support by passing SI7021_SUPPORT=1 build flag
  356. //------------------------------------------------------------------------------
  357. #ifndef V9261F_SUPPORT
  358. #define V9261F_SUPPORT 0
  359. #endif
  360. #ifndef V9261F_PIN
  361. #define V9261F_PIN 2 // TX pin from the V9261F
  362. #endif
  363. #ifndef V9261F_PIN_INVERSE
  364. #define V9261F_PIN_INVERSE 1 // Signal is inverted
  365. #endif
  366. #define V9261F_SYNC_INTERVAL 600 // Sync signal length (ms)
  367. #define V9261F_BAUDRATE 4800 // UART baudrate
  368. // Default ratios
  369. #define V9261F_CURRENT_FACTOR 79371434.0
  370. #define V9261F_VOLTAGE_FACTOR 4160651.0
  371. #define V9261F_POWER_FACTOR 153699.0
  372. #define V9261F_RPOWER_FACTOR V9261F_CURRENT_FACTOR
  373. // =============================================================================
  374. // Sensor helpers configuration
  375. // =============================================================================
  376. // -----------------------------------------------------------------------------
  377. // I2C
  378. // -----------------------------------------------------------------------------
  379. #ifndef I2C_SUPPORT
  380. #define I2C_SUPPORT 0 // I2C enabled (1.98Kb)
  381. #endif
  382. #define I2C_USE_BRZO 0 // Use brzo_i2c library or standard Wire
  383. #ifndef I2C_SDA_PIN
  384. #define I2C_SDA_PIN SDA // SDA GPIO (Sonoff => 4)
  385. #endif
  386. #ifndef I2C_SCL_PIN
  387. #define I2C_SCL_PIN SCL // SCL GPIO (Sonoff => 14)
  388. #endif
  389. #define I2C_CLOCK_STRETCH_TIME 200 // BRZO clock stretch time
  390. #define I2C_SCL_FREQUENCY 1000 // BRZO SCL frequency
  391. #define I2C_CLEAR_BUS 0 // Clear I2C bus at boot
  392. //--------------------------------------------------------------------------------
  393. // Internal power monitor
  394. // Enable support by passing ADC_VCC_ENABLED=1 build flag
  395. // Do not enable this if using the analog GPIO for any other thing
  396. //--------------------------------------------------------------------------------
  397. #ifndef ADC_VCC_ENABLED
  398. #define ADC_VCC_ENABLED 1
  399. #endif
  400. #if ADC_VCC_ENABLED
  401. ADC_MODE(ADC_VCC);
  402. #endif
  403. //--------------------------------------------------------------------------------
  404. // Class loading
  405. //--------------------------------------------------------------------------------
  406. #include "../sensors/BaseSensor.h"
  407. #if ANALOG_SUPPORT
  408. #include "../sensors/AnalogSensor.h"
  409. #endif
  410. #if BMX280_SUPPORT
  411. #include <SparkFunBME280.h>
  412. #include "../sensors/BMX280Sensor.h"
  413. #endif
  414. #if DALLAS_SUPPORT
  415. #include <OneWire.h>
  416. #include "../sensors/DallasSensor.h"
  417. #endif
  418. #if DHT_SUPPORT
  419. #include "../sensors/DHTSensor.h"
  420. #endif
  421. #if DIGITAL_SUPPORT
  422. #include "../sensors/DigitalSensor.h"
  423. #endif
  424. #if ECH1560_SUPPORT
  425. #include "../sensors/ECH1560Sensor.h"
  426. #endif
  427. #if EMON_ADC121_SUPPORT
  428. #include "../sensors/EmonADC121Sensor.h"
  429. #endif
  430. #if EMON_ADS1X15_SUPPORT
  431. #include "../sensors/EmonADS1X15Sensor.h"
  432. #endif
  433. #if EMON_ANALOG_SUPPORT
  434. #include "../sensors/EmonAnalogSensor.h"
  435. #endif
  436. #if EVENTS_SUPPORT
  437. #include "../sensors/EventSensor.h"
  438. #endif
  439. #if HLW8012_SUPPORT
  440. #include <HLW8012.h>
  441. #include "../sensors/HLW8012Sensor.h"
  442. #endif
  443. #if MHZ19_SUPPORT
  444. #include <SoftwareSerial.h>
  445. #include "../sensors/MHZ19Sensor.h"
  446. #endif
  447. #if PMSX003_SUPPORT
  448. #include <SoftwareSerial.h>
  449. #include <PMS.h>
  450. #include "../sensors/PMSX003Sensor.h"
  451. #endif
  452. #if SI7021_SUPPORT
  453. #include "../sensors/SI7021Sensor.h"
  454. #endif
  455. #if SHT3X_I2C_SUPPORT
  456. #include "../sensors/SHT3XI2CSensor.h"
  457. #endif
  458. #if V9261F_SUPPORT
  459. #include <SoftwareSerial.h>
  460. #include "../sensors/V9261FSensor.h"
  461. #endif