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.

504 lines
17 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 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 1
  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. //------------------------------------------------------------------------------
  244. // MHZ19 CO2 sensor
  245. // Enable support by passing MHZ19_SUPPORT=1 build flag
  246. //------------------------------------------------------------------------------
  247. #ifndef MHZ19_SUPPORT
  248. #define MHZ19_SUPPORT 0
  249. #endif
  250. #define MHZ19_RX_PIN 13
  251. #define MHZ19_TX_PIN 15
  252. //------------------------------------------------------------------------------
  253. // Particle Monitor based on Plantower PMSX003
  254. // Enable support by passing PMSX003_SUPPORT=1 build flag
  255. //------------------------------------------------------------------------------
  256. #ifndef PMSX003_SUPPORT
  257. #define PMSX003_SUPPORT 0
  258. #endif
  259. #define PMS_RX_PIN 13
  260. #define PMS_TX_PIN 15
  261. //------------------------------------------------------------------------------
  262. // SHT3X I2C (Wemos) temperature & humidity sensor
  263. // Enable support by passing SHT3X_SUPPORT=1 build flag
  264. //------------------------------------------------------------------------------
  265. #ifndef SHT3X_I2C_SUPPORT
  266. #define SHT3X_I2C_SUPPORT 0
  267. #endif
  268. #ifndef SHT3X_I2C_ADDRESS
  269. #define SHT3X_I2C_ADDRESS 0x00 // 0x00 means auto
  270. #endif
  271. #if SHT3X_I2C_SUPPORT
  272. #undef I2C_SUPPORT
  273. #define I2C_SUPPORT 1
  274. #endif
  275. //------------------------------------------------------------------------------
  276. // SI7021 temperature & humidity sensor
  277. // Enable support by passing SI7021_SUPPORT=1 build flag
  278. //------------------------------------------------------------------------------
  279. #ifndef SI7021_SUPPORT
  280. #define SI7021_SUPPORT 0
  281. #endif
  282. #ifndef SI7021_ADDRESS
  283. #define SI7021_ADDRESS 0x00 // 0x00 means auto
  284. #endif
  285. #if SI7021_SUPPORT
  286. #undef I2C_SUPPORT
  287. #define I2C_SUPPORT 1
  288. #endif
  289. //------------------------------------------------------------------------------
  290. // V9261F based power sensor
  291. // Enable support by passing SI7021_SUPPORT=1 build flag
  292. //------------------------------------------------------------------------------
  293. #ifndef V9261F_SUPPORT
  294. #define V9261F_SUPPORT 0
  295. #endif
  296. #ifndef V9261F_PIN
  297. #define V9261F_PIN 2 // TX pin from the V9261F
  298. #endif
  299. #ifndef V9261F_PIN_INVERSE
  300. #define V9261F_PIN_INVERSE 1 // Signal is inverted
  301. #endif
  302. #define V9261F_SYNC_INTERVAL 600 // Sync signal length (ms)
  303. #define V9261F_BAUDRATE 4800 // UART baudrate
  304. // Default ratios
  305. #define V9261F_CURRENT_FACTOR 79371434.0
  306. #define V9261F_VOLTAGE_FACTOR 4160651.0
  307. #define V9261F_POWER_FACTOR 153699.0
  308. #define V9261F_RPOWER_FACTOR V9261F_CURRENT_FACTOR
  309. // =============================================================================
  310. // Sensor helpers configuration
  311. // =============================================================================
  312. // -----------------------------------------------------------------------------
  313. // I2C
  314. // -----------------------------------------------------------------------------
  315. #ifndef I2C_SUPPORT
  316. #define I2C_SUPPORT 0 // I2C enabled (1.98Kb)
  317. #endif
  318. #define I2C_USE_BRZO 0 // Use brzo_i2c library or standard Wire
  319. #ifndef I2C_SDA_PIN
  320. #define I2C_SDA_PIN SDA // SDA GPIO (Sonoff => 4)
  321. #endif
  322. #ifndef I2C_SCL_PIN
  323. #define I2C_SCL_PIN SCL // SCL GPIO (Sonoff => 14)
  324. #endif
  325. #define I2C_CLOCK_STRETCH_TIME 200 // BRZO clock stretch time
  326. #define I2C_SCL_FREQUENCY 1000 // BRZO SCL frequency
  327. #define I2C_CLEAR_BUS 0 // Clear I2C bus at boot
  328. //--------------------------------------------------------------------------------
  329. // Internal power monitor
  330. // Enable support by passing ADC_VCC_ENABLED=1 build flag
  331. // Do not enable this if using the analog GPIO for any other thing
  332. //--------------------------------------------------------------------------------
  333. #ifndef ADC_VCC_ENABLED
  334. #define ADC_VCC_ENABLED 1
  335. #endif
  336. #if ADC_VCC_ENABLED
  337. ADC_MODE(ADC_VCC);
  338. #endif
  339. //--------------------------------------------------------------------------------
  340. // Class loading
  341. //--------------------------------------------------------------------------------
  342. #include "sensors/BaseSensor.h"
  343. #if ANALOG_SUPPORT
  344. #include "sensors/AnalogSensor.h"
  345. #endif
  346. #if BMX280_SUPPORT
  347. #include <SparkFunBME280.h>
  348. #include "sensors/BMX280Sensor.h"
  349. #endif
  350. #if DALLAS_SUPPORT
  351. #include <OneWire.h>
  352. #include "sensors/DallasSensor.h"
  353. #endif
  354. #if DHT_SUPPORT
  355. #include "sensors/DHTSensor.h"
  356. #endif
  357. #if DIGITAL_SUPPORT
  358. #include "sensors/DigitalSensor.h"
  359. #endif
  360. #if ECH1560_SUPPORT
  361. #include "sensors/ECH1560Sensor.h"
  362. #endif
  363. #if EMON_ADC121_SUPPORT
  364. #include "sensors/EmonADC121Sensor.h"
  365. #endif
  366. #if EMON_ADS1X15_SUPPORT
  367. #include "sensors/EmonADS1X15Sensor.h"
  368. #endif
  369. #if EMON_ANALOG_SUPPORT
  370. #include "sensors/EmonAnalogSensor.h"
  371. #endif
  372. #if EVENTS_SUPPORT
  373. #include "sensors/EventSensor.h"
  374. #endif
  375. #if HLW8012_SUPPORT
  376. #include "sensors/HLW8012Sensor.h"
  377. #endif
  378. #if MHZ19_SUPPORT
  379. #include <SoftwareSerial.h>
  380. #include "sensors/MHZ19Sensor.h"
  381. #endif
  382. #if PMSX003_SUPPORT
  383. #include <SoftwareSerial.h>
  384. #include <PMS.h>
  385. #include "sensors/PMSX003Sensor.h"
  386. #endif
  387. #if SI7021_SUPPORT
  388. #include "sensors/SI7021Sensor.h"
  389. #endif
  390. #if SHT3X_I2C_SUPPORT
  391. #include "sensors/SHT3XI2CSensor.h"
  392. #endif
  393. #if V9261F_SUPPORT
  394. #include <SoftwareSerial.h>
  395. #include "sensors/V9261FSensor.h"
  396. #endif