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.

466 lines
16 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_DHT_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. // Energy Monitor general settings
  155. //------------------------------------------------------------------------------
  156. #define EMON_MAX_SAMPLES 1000 // Max number of samples to get
  157. #define EMON_MAX_TIME 250 // Max time in ms to sample
  158. #define EMON_FILTER_SPEED 512 // Mobile average filter speed
  159. #define EMON_MAINS_VOLTAGE 230 // Mains voltage
  160. #define EMON_REFERENCE_VOLTAGE 3.3 // Reference voltage of the ADC
  161. #define EMON_CURRENT_RATIO 30 // Current ratio in the clamp (30V/1A)
  162. #define EMON_REPORT_CURRENT 0 // Report current
  163. #define EMON_REPORT_POWER 1 // Report power
  164. #define EMON_REPORT_ENERGY 1 // Report energy
  165. //------------------------------------------------------------------------------
  166. // Energy Monitor based on ADC121
  167. // Enable support by passing EMON_ADC121_SUPPORT=1 build flag
  168. //------------------------------------------------------------------------------
  169. #ifndef EMON_ADC121_SUPPORT
  170. #define EMON_ADC121_SUPPORT 0 // Do not build support by default
  171. #endif
  172. #define EMON_ADC121_I2C_ADDRESS 0x00 // 0x00 means auto
  173. #if EMON_ADC121_SUPPORT
  174. #undef I2C_SUPPORT
  175. #define I2C_SUPPORT 1
  176. #endif
  177. //------------------------------------------------------------------------------
  178. // Energy Monitor based on ADS1X15
  179. // Enable support by passing EMON_ADS1X15_SUPPORT=1 build flag
  180. //------------------------------------------------------------------------------
  181. #ifndef EMON_ADS1X15_SUPPORT
  182. #define EMON_ADS1X15_SUPPORT 0 // Do not build support by default
  183. #endif
  184. #define EMON_ADS1X15_I2C_ADDRESS 0x00 // 0x00 means auto
  185. #define EMON_ADS1X15_TYPE ADS1X15_CHIP_ADS1115
  186. #define EMON_ADS1X15_GAIN ADS1X15_REG_CONFIG_PGA_4_096V
  187. #define EMON_ADS1X15_MASK 0x0F // A0=1 A1=2 A2=4 A4=8
  188. #if EMON_ADS1X15_SUPPORT
  189. #undef I2C_SUPPORT
  190. #define I2C_SUPPORT 1
  191. #endif
  192. //------------------------------------------------------------------------------
  193. // Energy Monitor based on interval analog GPIO
  194. // Enable support by passing EMON_ANALOG_SUPPORT=1 build flag
  195. //------------------------------------------------------------------------------
  196. #ifndef EMON_ANALOG_SUPPORT
  197. #define EMON_ANALOG_SUPPORT 0 // Do not build support by default
  198. #endif
  199. #if EMON_ANALOG_SUPPORT
  200. #undef ADC_VCC_ENABLED
  201. #define ADC_VCC_ENABLED 0
  202. #endif
  203. //------------------------------------------------------------------------------
  204. // Counter sensor
  205. // Enable support by passing EVENTS_SUPPORT=1 build flag
  206. //------------------------------------------------------------------------------
  207. #ifndef EVENTS_SUPPORT
  208. #define EVENTS_SUPPORT 0 // Do not build with counter support by default
  209. #endif
  210. #ifndef EVENTS_PIN
  211. #define EVENTS_PIN 2 // GPIO to monitor
  212. #endif
  213. #ifndef EVENTS_PIN_MODE
  214. #define EVENTS_PIN_MODE INPUT // INPUT, INPUT_PULLUP
  215. #endif
  216. #ifndef EVENTS_INTERRUPT_MODE
  217. #define EVENTS_INTERRUPT_MODE RISING // RISING, FALLING, BOTH
  218. #endif
  219. #define EVENTS_DEBOUNCE 50 // Do not register events within less than 10 millis
  220. //------------------------------------------------------------------------------
  221. // MHZ19 CO2 sensor
  222. // Enable support by passing MHZ19_SUPPORT=1 build flag
  223. //------------------------------------------------------------------------------
  224. #ifndef MHZ19_SUPPORT
  225. #define MHZ19_SUPPORT 0
  226. #endif
  227. #define MHZ19_RX_PIN 13
  228. #define MHZ19_TX_PIN 15
  229. //------------------------------------------------------------------------------
  230. // Particle Monitor based on Plantower PMSX003
  231. // Enable support by passing PMSX003_SUPPORT=1 build flag
  232. //------------------------------------------------------------------------------
  233. #ifndef PMSX003_SUPPORT
  234. #define PMSX003_SUPPORT 0
  235. #endif
  236. #define PMS_RX_PIN 13
  237. #define PMS_TX_PIN 15
  238. //------------------------------------------------------------------------------
  239. // SHT3X I2C (Wemos) temperature & humidity sensor
  240. // Enable support by passing SHT3X_SUPPORT=1 build flag
  241. //------------------------------------------------------------------------------
  242. #ifndef SHT3X_I2C_SUPPORT
  243. #define SHT3X_I2C_SUPPORT 0
  244. #endif
  245. #ifndef SHT3X_I2C_ADDRESS
  246. #define SHT3X_I2C_ADDRESS 0x00 // 0x00 means auto
  247. #endif
  248. #if SHT3X_I2C_SUPPORT
  249. #undef I2C_SUPPORT
  250. #define I2C_SUPPORT 1
  251. #endif
  252. //------------------------------------------------------------------------------
  253. // SI7021 temperature & humidity sensor
  254. // Enable support by passing SI7021_SUPPORT=1 build flag
  255. //------------------------------------------------------------------------------
  256. #ifndef SI7021_SUPPORT
  257. #define SI7021_SUPPORT 0
  258. #endif
  259. #ifndef SI7021_ADDRESS
  260. #define SI7021_ADDRESS 0x00 // 0x00 means auto
  261. #endif
  262. #if SI7021_SUPPORT
  263. #undef I2C_SUPPORT
  264. #define I2C_SUPPORT 1
  265. #endif
  266. //------------------------------------------------------------------------------
  267. // V9261F based power sensor
  268. // Enable support by passing SI7021_SUPPORT=1 build flag
  269. //------------------------------------------------------------------------------
  270. #ifndef V9261F_SUPPORT
  271. #define V9261F_SUPPORT 0
  272. #endif
  273. #ifndef V9261F_PIN
  274. #define V9261F_PIN 2 // TX pin from the V9261F
  275. #endif
  276. #ifndef V9261F_PIN_INVERSE
  277. #define V9261F_PIN_INVERSE 1 // Signal is inverted
  278. #endif
  279. #define V9261F_SYNC_INTERVAL 600 // Sync signal length (ms)
  280. #define V9261F_BAUDRATE 4800 // UART baudrate
  281. // Default ratios
  282. #define V9261F_CURRENT_FACTOR 79371434.0
  283. #define V9261F_VOLTAGE_FACTOR 4160651.0
  284. #define V9261F_POWER_FACTOR 153699.0
  285. #define V9261F_RPOWER_FACTOR V9261F_CURRENT_FACTOR
  286. // =============================================================================
  287. // Sensor helpers configuration
  288. // =============================================================================
  289. // -----------------------------------------------------------------------------
  290. // I2C
  291. // -----------------------------------------------------------------------------
  292. #ifndef I2C_SUPPORT
  293. #define I2C_SUPPORT 0 // I2C enabled (1.98Kb)
  294. #endif
  295. #define I2C_USE_BRZO 0 // Use brzo_i2c library or standard Wire
  296. #ifndef I2C_SDA_PIN
  297. #define I2C_SDA_PIN SDA // SDA GPIO (Sonoff => 4)
  298. #endif
  299. #ifndef I2C_SCL_PIN
  300. #define I2C_SCL_PIN SCL // SCL GPIO (Sonoff => 14)
  301. #endif
  302. #define I2C_CLOCK_STRETCH_TIME 200 // BRZO clock stretch time
  303. #define I2C_SCL_FREQUENCY 1000 // BRZO SCL frequency
  304. #define I2C_CLEAR_BUS 0 // Clear I2C bus at boot
  305. //--------------------------------------------------------------------------------
  306. // Internal power monitor
  307. // Enable support by passing ADC_VCC_ENABLED=1 build flag
  308. // Do not enable this if using the analog GPIO for any other thing
  309. //--------------------------------------------------------------------------------
  310. #ifndef ADC_VCC_ENABLED
  311. #define ADC_VCC_ENABLED 1
  312. #endif
  313. #if ADC_VCC_ENABLED
  314. ADC_MODE(ADC_VCC);
  315. #endif
  316. //--------------------------------------------------------------------------------
  317. // Class loading
  318. //--------------------------------------------------------------------------------
  319. #include "sensors/BaseSensor.h"
  320. #if ANALOG_SUPPORT
  321. #include "sensors/AnalogSensor.h"
  322. #endif
  323. #if BMX280_SUPPORT
  324. #include <SparkFunBME280.h>
  325. #include "sensors/BMX280Sensor.h"
  326. #endif
  327. #if DALLAS_SUPPORT
  328. #include <OneWire.h>
  329. #include "sensors/DallasSensor.h"
  330. #endif
  331. #if DHT_SUPPORT
  332. #include "sensors/DHTSensor.h"
  333. #endif
  334. #if DIGITAL_SUPPORT
  335. #include "sensors/DigitalSensor.h"
  336. #endif
  337. #if EMON_ADC121_SUPPORT
  338. #include "sensors/EmonADC121Sensor.h"
  339. #endif
  340. #if EMON_ADS1X15_SUPPORT
  341. #include "sensors/EmonADS1X15Sensor.h"
  342. #endif
  343. #if EMON_ANALOG_SUPPORT
  344. #include "sensors/EmonAnalogSensor.h"
  345. #endif
  346. #if EVENTS_SUPPORT
  347. #include "sensors/EventSensor.h"
  348. #endif
  349. #if MHZ19_SUPPORT
  350. #include <SoftwareSerial.h>
  351. #include "sensors/MHZ19Sensor.h"
  352. #endif
  353. #if PMSX003_SUPPORT
  354. #include <SoftwareSerial.h>
  355. #include <PMS.h>
  356. #include "sensors/PMSX003Sensor.h"
  357. #endif
  358. #if SI7021_SUPPORT
  359. #include "sensors/SI7021Sensor.h"
  360. #endif
  361. #if SHT3X_I2C_SUPPORT
  362. #include "sensors/SHT3XI2CSensor.h"
  363. #endif
  364. #if V9261F_SUPPORT
  365. #include <SoftwareSerial.h>
  366. #include "sensors/V9261FSensor.h"
  367. #endif