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.

636 lines
21 KiB

7 years ago
  1. /*
  2. SENSOR MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <vector>
  6. #include "filters/MaxFilter.h"
  7. #include "filters/MedianFilter.h"
  8. #include "filters/MovingAverageFilter.h"
  9. #include "sensors/BaseSensor.h"
  10. typedef struct {
  11. BaseSensor * sensor;
  12. unsigned char local; // Local index in its provider
  13. magnitude_t type; // Type of measurement
  14. unsigned char global; // Global index in its type
  15. double current; // Current (last) value, unfiltered
  16. double filtered; // Filtered (averaged) value
  17. double reported; // Last reported value
  18. double min_change; // Minimum value change to report
  19. BaseFilter * filter; // Filter object
  20. } sensor_magnitude_t;
  21. std::vector<BaseSensor *> _sensors;
  22. std::vector<sensor_magnitude_t> _magnitudes;
  23. unsigned char _counts[MAGNITUDE_MAX];
  24. bool _sensor_realtime = API_REAL_TIME_VALUES;
  25. unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS;
  26. double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION;
  27. // -----------------------------------------------------------------------------
  28. // Private
  29. // -----------------------------------------------------------------------------
  30. String _sensorTopic(magnitude_t type) {
  31. if (type == MAGNITUDE_TEMPERATURE) return String(MAGNITUDE_TEMPERATURE_TOPIC);
  32. if (type == MAGNITUDE_HUMIDITY) return String(MAGNITUDE_HUMIDITY_TOPIC);
  33. if (type == MAGNITUDE_PRESSURE) return String(MAGNITUDE_PRESSURE_TOPIC);
  34. if (type == MAGNITUDE_CURRENT) return String(MAGNITUDE_CURRENT_TOPIC);
  35. if (type == MAGNITUDE_VOLTAGE) return String(MAGNITUDE_VOLTAGE_TOPIC);
  36. if (type == MAGNITUDE_POWER_ACTIVE) return String(MAGNITUDE_ACTIVE_POWER_TOPIC);
  37. if (type == MAGNITUDE_POWER_APPARENT) return String(MAGNITUDE_APPARENT_POWER_TOPIC);
  38. if (type == MAGNITUDE_POWER_REACTIVE) return String(MAGNITUDE_REACTIVE_POWER_TOPIC);
  39. if (type == MAGNITUDE_POWER_FACTOR) return String(MAGNITUDE_POWER_FACTOR_TOPIC);
  40. if (type == MAGNITUDE_ENERGY) return String(MAGNITUDE_ENERGY_TOPIC);
  41. if (type == MAGNITUDE_ENERGY_DELTA) return String(MAGNITUDE_ENERGY_DELTA_TOPIC);
  42. if (type == MAGNITUDE_ANALOG) return String(MAGNITUDE_ANALOG_TOPIC);
  43. if (type == MAGNITUDE_DIGITAL) return String(MAGNITUDE_DIGITAL_TOPIC);
  44. if (type == MAGNITUDE_EVENTS) return String(MAGNITUDE_EVENTS_TOPIC);
  45. if (type == MAGNITUDE_PM1dot0) return String(MAGNITUDE_PM1dot0_TOPIC);
  46. if (type == MAGNITUDE_PM2dot5) return String(MAGNITUDE_PM2dot5_TOPIC);
  47. if (type == MAGNITUDE_PM10) return String(MAGNITUDE_PM10_TOPIC);
  48. if (type == MAGNITUDE_CO2) return String(MAGNITUDE_CO2_TOPIC);
  49. return String(MAGNITUDE_UNKNOWN_TOPIC);
  50. }
  51. unsigned char _sensorDecimals(magnitude_t type) {
  52. if (type == MAGNITUDE_TEMPERATURE) return MAGNITUDE_TEMPERATURE_DECIMALS;
  53. if (type == MAGNITUDE_HUMIDITY) return MAGNITUDE_HUMIDITY_DECIMALS;
  54. if (type == MAGNITUDE_PRESSURE) return MAGNITUDE_PRESSURE_DECIMALS;
  55. if (type == MAGNITUDE_CURRENT) return MAGNITUDE_CURRENT_DECIMALS;
  56. if (type == MAGNITUDE_VOLTAGE) return MAGNITUDE_VOLTAGE_DECIMALS;
  57. if (type == MAGNITUDE_POWER_ACTIVE) return MAGNITUDE_POWER_DECIMALS;
  58. if (type == MAGNITUDE_POWER_APPARENT) return MAGNITUDE_POWER_DECIMALS;
  59. if (type == MAGNITUDE_POWER_REACTIVE) return MAGNITUDE_POWER_DECIMALS;
  60. if (type == MAGNITUDE_POWER_FACTOR) return MAGNITUDE_POWER_FACTOR_DECIMALS;
  61. if (type == MAGNITUDE_ENERGY) return MAGNITUDE_ENERGY_DECIMALS;
  62. if (type == MAGNITUDE_ENERGY_DELTA) return MAGNITUDE_ENERGY_DECIMALS;
  63. if (type == MAGNITUDE_ANALOG) return MAGNITUDE_ANALOG_DECIMALS;
  64. if (type == MAGNITUDE_EVENTS) return MAGNITUDE_EVENTS_DECIMALS;
  65. if (type == MAGNITUDE_PM1dot0) return MAGNITUDE_PM1dot0_DECIMALS;
  66. if (type == MAGNITUDE_PM2dot5) return MAGNITUDE_PM2dot5_DECIMALS;
  67. if (type == MAGNITUDE_PM10) return MAGNITUDE_PM10_DECIMALS;
  68. if (type == MAGNITUDE_CO2) return MAGNITUDE_CO2_DECIMALS;
  69. return 0;
  70. }
  71. String _sensorUnits(magnitude_t type) {
  72. if (type == MAGNITUDE_TEMPERATURE) return (_sensor_temperature_units == TMP_CELSIUS) ? String("C") : String("F");
  73. if (type == MAGNITUDE_HUMIDITY) return String("%");
  74. if (type == MAGNITUDE_PRESSURE) return String("hPa");
  75. if (type == MAGNITUDE_CURRENT) return String("A");
  76. if (type == MAGNITUDE_VOLTAGE) return String("V");
  77. if (type == MAGNITUDE_POWER_ACTIVE) return String("W");
  78. if (type == MAGNITUDE_POWER_APPARENT) return String("W");
  79. if (type == MAGNITUDE_POWER_REACTIVE) return String("W");
  80. if (type == MAGNITUDE_POWER_FACTOR) return String("%");
  81. if (type == MAGNITUDE_ENERGY) return String("J");
  82. if (type == MAGNITUDE_ENERGY_DELTA) return String("J");
  83. if (type == MAGNITUDE_EVENTS) return String("/min");
  84. if (type == MAGNITUDE_PM1dot0) return String("µg/m3");
  85. if (type == MAGNITUDE_PM2dot5) return String("µg/m3");
  86. if (type == MAGNITUDE_PM10) return String("µg/m3");
  87. if (type == MAGNITUDE_CO2) return String("ppm");
  88. return String();
  89. }
  90. double _sensorProcess(magnitude_t type, double value) {
  91. if (type == MAGNITUDE_TEMPERATURE) {
  92. if (_sensor_temperature_units == TMP_FAHRENHEIT) value = value * 1.8 + 32;
  93. value = value + _sensor_temperature_correction;
  94. }
  95. return roundTo(value, _sensorDecimals(type));
  96. }
  97. #if WEB_SUPPORT
  98. void _sensorWebSocketSendData(JsonObject& root) {
  99. char buffer[10];
  100. bool hasTemperature = false;
  101. JsonArray& list = root.createNestedArray("magnitudes");
  102. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  103. sensor_magnitude_t magnitude = _magnitudes[i];
  104. unsigned char decimals = _sensorDecimals(magnitude.type);
  105. dtostrf(magnitude.current, 1-sizeof(buffer), decimals, buffer);
  106. JsonObject& element = list.createNestedObject();
  107. element["type"] = int(magnitude.type);
  108. element["value"] = String(buffer);
  109. element["units"] = _sensorUnits(magnitude.type);
  110. element["description"] = magnitude.sensor->slot(magnitude.local);
  111. element["error"] = magnitude.sensor->error();
  112. if (magnitude.type == MAGNITUDE_TEMPERATURE) hasTemperature = true;
  113. }
  114. //root["apiRealTime"] = _sensor_realtime;
  115. root["tmpUnits"] = _sensor_temperature_units;
  116. root["tmpCorrection"] = _sensor_temperature_correction;
  117. if (hasTemperature) root["temperatureVisible"] = 1;
  118. }
  119. void _sensorWebSocketStart(JsonObject& root) {
  120. bool hasSensors = false;
  121. for (unsigned char i=0; i<_sensors.size(); i++) {
  122. BaseSensor * sensor = _sensors[i];
  123. #if EMON_ANALOG_SUPPORT
  124. if (sensor->getID() == SENSOR_EMON_ANALOG_ID) {
  125. root["emonVisible"] = 1;
  126. root["pwrRatioC"] = ((EmonAnalogSensor *) sensor)->getCurrentRatio(0);
  127. root["pwrVoltage"] = ((EmonAnalogSensor *) sensor)->getVoltage();
  128. hasSensors = true;
  129. }
  130. #endif
  131. }
  132. if (hasSensors) root["sensorsVisible"] = 1;
  133. /*
  134. // Sensors manifest
  135. JsonArray& manifest = root.createNestedArray("manifest");
  136. #if BMX280_SUPPORT
  137. BMX280Sensor::manifest(manifest);
  138. #endif
  139. // Sensors configuration
  140. JsonArray& sensors = root.createNestedArray("sensors");
  141. for (unsigned char i; i<_sensors.size(); i++) {
  142. JsonObject& sensor = sensors.createNestedObject();
  143. sensor["index"] = i;
  144. sensor["id"] = _sensors[i]->getID();
  145. _sensors[i]->getConfig(sensor);
  146. }
  147. */
  148. }
  149. void _sensorAPISetup() {
  150. for (unsigned char magnitude_id=0; magnitude_id<_magnitudes.size(); magnitude_id++) {
  151. sensor_magnitude_t magnitude = _magnitudes[magnitude_id];
  152. String topic = _sensorTopic(magnitude.type);
  153. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) topic = topic + "/" + String(magnitude.global);
  154. apiRegister(topic.c_str(), topic.c_str(), [magnitude_id](char * buffer, size_t len) {
  155. sensor_magnitude_t magnitude = _magnitudes[magnitude_id];
  156. unsigned char decimals = _sensorDecimals(magnitude.type);
  157. double value = _sensor_realtime ? magnitude.current : magnitude.filtered;
  158. dtostrf(value, 1-len, decimals, buffer);
  159. });
  160. }
  161. }
  162. #endif
  163. void _sensorTick() {
  164. for (unsigned char i=0; i<_sensors.size(); i++) {
  165. _sensors[i]->tick();
  166. }
  167. }
  168. void _sensorPre() {
  169. for (unsigned char i=0; i<_sensors.size(); i++) {
  170. _sensors[i]->pre();
  171. if (!_sensors[i]->status()) {
  172. DEBUG_MSG("[SENSOR] Error reading data from %s (error: %d)\n",
  173. _sensors[i]->description().c_str(),
  174. _sensors[i]->error()
  175. );
  176. }
  177. }
  178. }
  179. void _sensorPost() {
  180. for (unsigned char i=0; i<_sensors.size(); i++) {
  181. _sensors[i]->post();
  182. }
  183. }
  184. // -----------------------------------------------------------------------------
  185. // Sensor initialization
  186. // -----------------------------------------------------------------------------
  187. void _sensorInit() {
  188. #if ANALOG_SUPPORT
  189. {
  190. AnalogSensor * sensor = new AnalogSensor();
  191. _sensors.push_back(sensor);
  192. }
  193. #endif
  194. #if BMX280_SUPPORT
  195. {
  196. BMX280Sensor * sensor = new BMX280Sensor();
  197. sensor->setAddress(BMX280_ADDRESS);
  198. _sensors.push_back(sensor);
  199. }
  200. #endif
  201. #if DALLAS_SUPPORT
  202. {
  203. DallasSensor * sensor = new DallasSensor();
  204. sensor->setGPIO(DALLAS_PIN);
  205. _sensors.push_back(sensor);
  206. }
  207. #endif
  208. #if DHT_SUPPORT
  209. {
  210. DHTSensor * sensor = new DHTSensor();
  211. sensor->setGPIO(DHT_PIN);
  212. sensor->setType(DHT_TYPE);
  213. _sensors.push_back(sensor);
  214. }
  215. #endif
  216. #if DIGITAL_SUPPORT
  217. {
  218. DigitalSensor * sensor = new DigitalSensor();
  219. sensor->setGPIO(DIGITAL_PIN);
  220. sensor->setMode(DIGITAL_PIN_MODE);
  221. sensor->setDefault(DIGITAL_DEFAULT_STATE);
  222. _sensors.push_back(sensor);
  223. }
  224. #endif
  225. #if ECH1560_SUPPORT
  226. {
  227. ECH1560Sensor * sensor = new ECH1560Sensor();
  228. sensor->setCLK(ECH1560_CLK_PIN);
  229. sensor->setMISO(ECH1560_MISO_PIN);
  230. sensor->setInverted(ECH1560_INVERTED);
  231. _sensors.push_back(sensor);
  232. }
  233. #endif
  234. #if EMON_ADC121_SUPPORT
  235. {
  236. EmonADC121Sensor * sensor = new EmonADC121Sensor();
  237. sensor->setAddress(EMON_ADC121_I2C_ADDRESS);
  238. sensor->setVoltage(EMON_MAINS_VOLTAGE);
  239. sensor->setReference(EMON_REFERENCE_VOLTAGE);
  240. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  241. _sensors.push_back(sensor);
  242. }
  243. #endif
  244. #if EMON_ADS1X15_SUPPORT
  245. {
  246. EmonADS1X15Sensor * sensor = new EmonADS1X15Sensor();
  247. sensor->setAddress(EMON_ADS1X15_I2C_ADDRESS);
  248. sensor->setType(EMON_ADS1X15_TYPE);
  249. sensor->setMask(EMON_ADS1X15_MASK);
  250. sensor->setGain(EMON_ADS1X15_GAIN);
  251. sensor->setVoltage(EMON_MAINS_VOLTAGE);
  252. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  253. sensor->setCurrentRatio(1, EMON_CURRENT_RATIO);
  254. sensor->setCurrentRatio(2, EMON_CURRENT_RATIO);
  255. sensor->setCurrentRatio(3, EMON_CURRENT_RATIO);
  256. _sensors.push_back(sensor);
  257. }
  258. #endif
  259. #if EMON_ANALOG_SUPPORT
  260. {
  261. EmonAnalogSensor * sensor = new EmonAnalogSensor();
  262. sensor->setVoltage(EMON_MAINS_VOLTAGE);
  263. sensor->setReference(EMON_REFERENCE_VOLTAGE);
  264. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  265. _sensors.push_back(sensor);
  266. }
  267. #endif
  268. #if EVENTS_SUPPORT
  269. {
  270. EventSensor * sensor = new EventSensor();
  271. sensor->setGPIO(EVENTS_PIN);
  272. sensor->setMode(EVENTS_PIN_MODE);
  273. sensor->setDebounceTime(EVENTS_DEBOUNCE);
  274. sensor->setInterruptMode(EVENTS_INTERRUPT_MODE);
  275. _sensors.push_back(sensor);
  276. }
  277. #endif
  278. #if MHZ19_SUPPORT
  279. {
  280. MHZ19Sensor * sensor = new MHZ19Sensor();
  281. sensor->setRX(MHZ19_RX_PIN);
  282. sensor->setTX(MHZ19_TX_PIN);
  283. _sensors.push_back(sensor);
  284. }
  285. #endif
  286. #if PMSX003_SUPPORT
  287. {
  288. PMSX003Sensor * sensor = new PMSX003Sensor();
  289. sensor->setRX(PMS_RX_PIN);
  290. sensor->setTX(PMS_TX_PIN);
  291. _sensors.push_back(sensor);
  292. }
  293. #endif
  294. #if SHT3X_I2C_SUPPORT
  295. {
  296. SHT3XI2CSensor * sensor = new SHT3XI2CSensor();
  297. sensor->setAddress(SHT3X_I2C_ADDRESS);
  298. _sensors.push_back(sensor);
  299. }
  300. #endif
  301. #if SI7021_SUPPORT
  302. {
  303. SI7021Sensor * sensor = new SI7021Sensor();
  304. sensor->setAddress(SI7021_ADDRESS);
  305. _sensors.push_back(sensor);
  306. }
  307. #endif
  308. #if V9261F_SUPPORT
  309. {
  310. V9261FSensor * sensor = new V9261FSensor();
  311. sensor->setRX(V9261F_PIN);
  312. sensor->setInverted(V9261F_PIN_INVERSE);
  313. _sensors.push_back(sensor);
  314. }
  315. #endif
  316. }
  317. void _sensorConfigure() {
  318. for (unsigned char i=0; i<_sensors.size(); i++) {
  319. BaseSensor * sensor = _sensors[i];
  320. #if EMON_ANALOG_SUPPORT
  321. if (sensor->getID() == SENSOR_EMON_ANALOG_ID) {
  322. unsigned int expected = getSetting("pwrExpectedP", 0).toInt();
  323. if (expected > 0) {
  324. ((EmonAnalogSensor *) sensor)->expectedPower(0, expected);
  325. setSetting("pwrRatioC", ((EmonAnalogSensor *) sensor)->getCurrentRatio(0));
  326. }
  327. if (getSetting("pwrResetCalibration", 0).toInt() == 1) {
  328. ((EmonAnalogSensor *) sensor)->setCurrentRatio(0, EMON_CURRENT_RATIO);
  329. delSetting("pwrRatioC");
  330. }
  331. ((EmonAnalogSensor *) sensor)->setCurrentRatio(0, getSetting("pwrRatioC", EMON_CURRENT_RATIO).toFloat());
  332. ((EmonAnalogSensor *) sensor)->setVoltage(getSetting("pwrVoltage", EMON_MAINS_VOLTAGE).toInt());
  333. delSetting("pwrExpectedP");
  334. delSetting("pwrResetCalibration");
  335. }
  336. #endif
  337. // Force sensor to reload config
  338. sensor->begin();
  339. }
  340. // General sensor settings
  341. _sensor_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
  342. _sensor_temperature_units = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS).toInt();
  343. _sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat();
  344. // Save settings
  345. saveSettings();
  346. }
  347. void _magnitudesInit() {
  348. for (unsigned char i=0; i<_sensors.size(); i++) {
  349. BaseSensor * sensor = _sensors[i];
  350. DEBUG_MSG("[SENSOR] %s\n", sensor->description().c_str());
  351. if (sensor->error() != 0) DEBUG_MSG("[SENSOR] -> ERROR %d\n", sensor->error());
  352. for (unsigned char k=0; k<sensor->count(); k++) {
  353. magnitude_t type = sensor->type(k);
  354. sensor_magnitude_t new_magnitude;
  355. new_magnitude.sensor = sensor;
  356. new_magnitude.local = k;
  357. new_magnitude.type = type;
  358. new_magnitude.global = _counts[type];
  359. new_magnitude.current = 0;
  360. new_magnitude.filtered = 0;
  361. new_magnitude.reported = 0;
  362. new_magnitude.min_change = 0;
  363. if (type == MAGNITUDE_DIGITAL) {
  364. new_magnitude.filter = new MaxFilter();
  365. } else if (type == MAGNITUDE_EVENTS) {
  366. new_magnitude.filter = new MovingAverageFilter();
  367. } else {
  368. new_magnitude.filter = new MedianFilter();
  369. }
  370. _magnitudes.push_back(new_magnitude);
  371. DEBUG_MSG("[SENSOR] -> %s:%d\n", _sensorTopic(type).c_str(), _counts[type]);
  372. _counts[type] = _counts[type] + 1;
  373. }
  374. }
  375. }
  376. // -----------------------------------------------------------------------------
  377. // Public
  378. // -----------------------------------------------------------------------------
  379. unsigned char sensorCount() {
  380. return _sensors.size();
  381. }
  382. unsigned char magnitudeCount() {
  383. return _magnitudes.size();
  384. }
  385. String magnitudeName(unsigned char index) {
  386. if (index < _magnitudes.size()) {
  387. sensor_magnitude_t magnitude = _magnitudes[index];
  388. return magnitude.sensor->slot(magnitude.local);
  389. }
  390. return String();
  391. }
  392. unsigned char magnitudeType(unsigned char index) {
  393. if (index < _magnitudes.size()) {
  394. return int(_magnitudes[index].type);
  395. }
  396. return MAGNITUDE_NONE;
  397. }
  398. // -----------------------------------------------------------------------------
  399. void sensorSetup() {
  400. // Load sensors
  401. _sensorInit();
  402. // Configure stored values
  403. _sensorConfigure();
  404. // Load magnitudes
  405. _magnitudesInit();
  406. #if WEB_SUPPORT
  407. // Websockets
  408. wsOnSendRegister(_sensorWebSocketStart);
  409. wsOnSendRegister(_sensorWebSocketSendData);
  410. wsOnAfterParseRegister(_sensorConfigure);
  411. // API
  412. _sensorAPISetup();
  413. #endif
  414. }
  415. void sensorLoop() {
  416. static unsigned long last_update = 0;
  417. static unsigned long report_count = 0;
  418. // Tick hook
  419. _sensorTick();
  420. // Check if we should read new data
  421. if (millis() - last_update > SENSOR_READ_INTERVAL) {
  422. last_update = millis();
  423. report_count = (report_count + 1) % SENSOR_REPORT_EVERY;
  424. double current;
  425. double filtered;
  426. char buffer[64];
  427. // Pre-read hook
  428. _sensorPre();
  429. // Get readings
  430. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  431. sensor_magnitude_t magnitude = _magnitudes[i];
  432. if (magnitude.sensor->status()) {
  433. unsigned char decimals = _sensorDecimals(magnitude.type);
  434. current = magnitude.sensor->value(magnitude.local);
  435. magnitude.filter->add(current);
  436. // Special case
  437. if (magnitude.type == MAGNITUDE_EVENTS) current = magnitude.filter->result();
  438. current = _sensorProcess(magnitude.type, current);
  439. _magnitudes[i].current = current;
  440. // Debug
  441. #if SENSOR_DEBUG
  442. {
  443. dtostrf(current, 1-sizeof(buffer), decimals, buffer);
  444. DEBUG_MSG("[SENSOR] %s - %s: %s%s\n",
  445. magnitude.sensor->slot(magnitude.local).c_str(),
  446. _sensorTopic(magnitude.type).c_str(),
  447. buffer,
  448. _sensorUnits(magnitude.type).c_str()
  449. );
  450. }
  451. #endif
  452. // Time to report (we do it every SENSOR_REPORT_EVERY readings)
  453. if (report_count == 0) {
  454. filtered = magnitude.filter->result();
  455. magnitude.filter->reset();
  456. filtered = _sensorProcess(magnitude.type, filtered);
  457. _magnitudes[i].filtered = filtered;
  458. // Check if there is a minimum change threshold to report
  459. if (fabs(filtered - magnitude.reported) >= magnitude.min_change) {
  460. _magnitudes[i].reported = filtered;
  461. dtostrf(filtered, 1-sizeof(buffer), decimals, buffer);
  462. #if MQTT_SUPPORT
  463. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
  464. mqttSend(_sensorTopic(magnitude.type).c_str(), magnitude.global, buffer);
  465. } else {
  466. mqttSend(_sensorTopic(magnitude.type).c_str(), buffer);
  467. }
  468. #endif
  469. #if INFLUXDB_SUPPORT
  470. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
  471. idbSend(_sensorTopic(magnitude.type).c_str(), magnitude.global, buffer);
  472. } else {
  473. idbSend(_sensorTopic(magnitude.type).c_str(), buffer);
  474. }
  475. #endif
  476. #if DOMOTICZ_SUPPORT
  477. {
  478. char key[15];
  479. snprintf_P(key, sizeof(key), PSTR("dczSensor%d"), i);
  480. if (magnitude.type == MAGNITUDE_HUMIDITY) {
  481. int status;
  482. if (filtered > 70) {
  483. status = HUMIDITY_WET;
  484. } else if (filtered > 45) {
  485. status = HUMIDITY_COMFORTABLE;
  486. } else if (filtered > 30) {
  487. status = HUMIDITY_NORMAL;
  488. } else {
  489. status = HUMIDITY_DRY;
  490. }
  491. char status_buf[5];
  492. itoa(status, status_buf, 10);
  493. domoticzSend(key, buffer, status_buf);
  494. } else {
  495. domoticzSend(key, 0, buffer);
  496. }
  497. }
  498. #endif
  499. } // if (fabs(filtered - magnitude.reported) >= magnitude.min_change)
  500. } // if (report_count == 0)
  501. } // if (magnitude.sensor->status())
  502. } // for (unsigned char i=0; i<_magnitudes.size(); i++)
  503. // Post-read hook
  504. _sensorPost();
  505. #if WEB_SUPPORT
  506. wsSend(_sensorWebSocketSendData);
  507. #endif
  508. }
  509. }