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