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.

738 lines
23 KiB

7 years ago
7 years ago
7 years ago
  1. /*
  2. SENSOR MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if SENSOR_SUPPORT
  6. #include <vector>
  7. #include "filters/MaxFilter.h"
  8. #include "filters/MedianFilter.h"
  9. #include "filters/MovingAverageFilter.h"
  10. #include "sensors/BaseSensor.h"
  11. typedef struct {
  12. BaseSensor * sensor; // Sensor object
  13. BaseFilter * filter; // Filter object
  14. unsigned char local; // Local index in its provider
  15. unsigned char type; // Type of measurement
  16. unsigned char global; // Global index in its type
  17. double current; // Current (last) value, unfiltered
  18. double filtered; // Filtered (averaged) value
  19. double reported; // Last reported value
  20. double min_change; // Minimum value change to report
  21. } sensor_magnitude_t;
  22. std::vector<BaseSensor *> _sensors;
  23. std::vector<sensor_magnitude_t> _magnitudes;
  24. unsigned char _counts[MAGNITUDE_MAX];
  25. bool _sensor_realtime = API_REAL_TIME_VALUES;
  26. unsigned long _sensor_read_interval = 1000 * SENSOR_READ_INTERVAL;
  27. unsigned char _sensor_report_every = SENSOR_REPORT_EVERY;
  28. unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS;
  29. double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION;
  30. // -----------------------------------------------------------------------------
  31. // Private
  32. // -----------------------------------------------------------------------------
  33. String _magnitudeTopic(unsigned char type) {
  34. char buffer[16] = {0};
  35. if (type < MAGNITUDE_MAX) strncpy_P(buffer, magnitude_topics[type], sizeof(buffer));
  36. return String(buffer);
  37. }
  38. unsigned char _magnitudeDecimals(unsigned char type) {
  39. if (type < MAGNITUDE_MAX) return pgm_read_byte(magnitude_decimals + type);
  40. return 0;
  41. }
  42. String _magnitudeUnits(unsigned char type) {
  43. char buffer[8] = {0};
  44. if (type < MAGNITUDE_MAX) {
  45. if ((type == MAGNITUDE_TEMPERATURE) && (_sensor_temperature_units == TMP_FAHRENHEIT)) {
  46. strncpy_P(buffer, magnitude_fahrenheit, sizeof(buffer));
  47. } else {
  48. strncpy_P(buffer, magnitude_units[type], sizeof(buffer));
  49. }
  50. }
  51. return String(buffer);
  52. }
  53. double _magnitudeProcess(unsigned char type, double value) {
  54. if (type == MAGNITUDE_TEMPERATURE) {
  55. if (_sensor_temperature_units == TMP_FAHRENHEIT) value = value * 1.8 + 32;
  56. value = value + _sensor_temperature_correction;
  57. }
  58. return roundTo(value, _magnitudeDecimals(type));
  59. }
  60. // -----------------------------------------------------------------------------
  61. #if WEB_SUPPORT
  62. void _sensorWebSocketSendData(JsonObject& root) {
  63. char buffer[10];
  64. bool hasTemperature = false;
  65. JsonArray& list = root.createNestedArray("magnitudes");
  66. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  67. sensor_magnitude_t magnitude = _magnitudes[i];
  68. unsigned char decimals = _magnitudeDecimals(magnitude.type);
  69. dtostrf(magnitude.current, 1-sizeof(buffer), decimals, buffer);
  70. JsonObject& element = list.createNestedObject();
  71. element["index"] = int(magnitude.global);
  72. element["type"] = int(magnitude.type);
  73. element["value"] = String(buffer);
  74. element["units"] = _magnitudeUnits(magnitude.type);
  75. element["description"] = magnitude.sensor->slot(magnitude.local);
  76. element["error"] = magnitude.sensor->error();
  77. if (magnitude.type == MAGNITUDE_TEMPERATURE) hasTemperature = true;
  78. }
  79. if (hasTemperature) root["temperatureVisible"] = 1;
  80. }
  81. void _sensorWebSocketStart(JsonObject& root) {
  82. for (unsigned char i=0; i<_sensors.size(); i++) {
  83. BaseSensor * sensor = _sensors[i];
  84. #if EMON_ANALOG_SUPPORT
  85. if (sensor->getID() == SENSOR_EMON_ANALOG_ID) {
  86. root["emonVisible"] = 1;
  87. root["pwrVoltage"] = ((EmonAnalogSensor *) sensor)->getVoltage();
  88. }
  89. #endif
  90. #if HLW8012_SUPPORT
  91. if (sensor->getID() == SENSOR_HLW8012_ID) {
  92. root["hlwVisible"] = 1;
  93. }
  94. #endif
  95. }
  96. if (_magnitudes.size() > 0) {
  97. root["sensorsVisible"] = 1;
  98. //root["apiRealTime"] = _sensor_realtime;
  99. root["tmpUnits"] = _sensor_temperature_units;
  100. root["tmpCorrection"] = _sensor_temperature_correction;
  101. root["snsRead"] = _sensor_read_interval / 1000;
  102. root["snsReport"] = _sensor_report_every;
  103. }
  104. /*
  105. // Sensors manifest
  106. JsonArray& manifest = root.createNestedArray("manifest");
  107. #if BMX280_SUPPORT
  108. BMX280Sensor::manifest(manifest);
  109. #endif
  110. // Sensors configuration
  111. JsonArray& sensors = root.createNestedArray("sensors");
  112. for (unsigned char i; i<_sensors.size(); i++) {
  113. JsonObject& sensor = sensors.createNestedObject();
  114. sensor["index"] = i;
  115. sensor["id"] = _sensors[i]->getID();
  116. _sensors[i]->getConfig(sensor);
  117. }
  118. */
  119. }
  120. void _sensorAPISetup() {
  121. for (unsigned char magnitude_id=0; magnitude_id<_magnitudes.size(); magnitude_id++) {
  122. sensor_magnitude_t magnitude = _magnitudes[magnitude_id];
  123. String topic = _magnitudeTopic(magnitude.type);
  124. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) topic = topic + "/" + String(magnitude.global);
  125. apiRegister(topic.c_str(), [magnitude_id](char * buffer, size_t len) {
  126. sensor_magnitude_t magnitude = _magnitudes[magnitude_id];
  127. unsigned char decimals = _magnitudeDecimals(magnitude.type);
  128. double value = _sensor_realtime ? magnitude.current : magnitude.filtered;
  129. dtostrf(value, 1-len, decimals, buffer);
  130. });
  131. }
  132. }
  133. #endif
  134. void _sensorTick() {
  135. for (unsigned char i=0; i<_sensors.size(); i++) {
  136. _sensors[i]->tick();
  137. }
  138. }
  139. void _sensorPre() {
  140. for (unsigned char i=0; i<_sensors.size(); i++) {
  141. _sensors[i]->pre();
  142. if (!_sensors[i]->status()) {
  143. DEBUG_MSG_P(PSTR("[SENSOR] Error reading data from %s (error: %d)\n"),
  144. _sensors[i]->description().c_str(),
  145. _sensors[i]->error()
  146. );
  147. }
  148. }
  149. }
  150. void _sensorPost() {
  151. for (unsigned char i=0; i<_sensors.size(); i++) {
  152. _sensors[i]->post();
  153. }
  154. }
  155. // -----------------------------------------------------------------------------
  156. // Sensor initialization
  157. // -----------------------------------------------------------------------------
  158. void _sensorInit() {
  159. /*
  160. This is temporal, in the future sensors will be initialized based on
  161. soft configuration (data stored in EEPROM config) so you will be able
  162. to define and configure new sensors on the fly
  163. At the time being, only enabled sensors (those with *_SUPPORT to 1) are being
  164. loaded and initialized here. If you want to add new sensors of the same type
  165. just duplicate the block and change the arguments for the set* methods.
  166. Check the DHT block below for an example
  167. */
  168. #if ANALOG_SUPPORT
  169. {
  170. AnalogSensor * sensor = new AnalogSensor();
  171. _sensors.push_back(sensor);
  172. }
  173. #endif
  174. #if BH1750_SUPPORT
  175. {
  176. BH1750Sensor * sensor = new BH1750Sensor();
  177. sensor->setAddress(BH1750_ADDRESS);
  178. sensor->setMode(BH1750_MODE);
  179. _sensors.push_back(sensor);
  180. }
  181. #endif
  182. #if BMX280_SUPPORT
  183. {
  184. BMX280Sensor * sensor = new BMX280Sensor();
  185. sensor->setAddress(BMX280_ADDRESS);
  186. _sensors.push_back(sensor);
  187. }
  188. #endif
  189. #if DALLAS_SUPPORT
  190. {
  191. DallasSensor * sensor = new DallasSensor();
  192. sensor->setGPIO(DALLAS_PIN);
  193. _sensors.push_back(sensor);
  194. }
  195. #endif
  196. #if DHT_SUPPORT
  197. {
  198. DHTSensor * sensor = new DHTSensor();
  199. sensor->setGPIO(DHT_PIN);
  200. sensor->setType(DHT_TYPE);
  201. _sensors.push_back(sensor);
  202. }
  203. #endif
  204. /*
  205. // Example on how to add a second DHT sensor
  206. // DHT2_PIN and DHT2_TYPE should be defined in sensors.h file
  207. #if DHT_SUPPORT
  208. {
  209. DHTSensor * sensor = new DHTSensor();
  210. sensor->setGPIO(DHT2_PIN);
  211. sensor->setType(DHT2_TYPE);
  212. _sensors.push_back(sensor);
  213. }
  214. #endif
  215. */
  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 HLW8012_SUPPORT
  279. {
  280. HLW8012Sensor * sensor = new HLW8012Sensor();
  281. sensor->setSEL(HLW8012_SEL_PIN);
  282. sensor->setCF(HLW8012_CF_PIN);
  283. sensor->setCF1(HLW8012_CF1_PIN);
  284. sensor->setSELCurrent(HLW8012_SEL_CURRENT);
  285. _sensors.push_back(sensor);
  286. }
  287. #endif
  288. #if MHZ19_SUPPORT
  289. {
  290. MHZ19Sensor * sensor = new MHZ19Sensor();
  291. sensor->setRX(MHZ19_RX_PIN);
  292. sensor->setTX(MHZ19_TX_PIN);
  293. _sensors.push_back(sensor);
  294. }
  295. #endif
  296. #if PMSX003_SUPPORT
  297. {
  298. PMSX003Sensor * sensor = new PMSX003Sensor();
  299. sensor->setRX(PMS_RX_PIN);
  300. sensor->setTX(PMS_TX_PIN);
  301. _sensors.push_back(sensor);
  302. }
  303. #endif
  304. #if SHT3X_I2C_SUPPORT
  305. {
  306. SHT3XI2CSensor * sensor = new SHT3XI2CSensor();
  307. sensor->setAddress(SHT3X_I2C_ADDRESS);
  308. _sensors.push_back(sensor);
  309. }
  310. #endif
  311. #if SI7021_SUPPORT
  312. {
  313. SI7021Sensor * sensor = new SI7021Sensor();
  314. sensor->setAddress(SI7021_ADDRESS);
  315. _sensors.push_back(sensor);
  316. }
  317. #endif
  318. #if V9261F_SUPPORT
  319. {
  320. V9261FSensor * sensor = new V9261FSensor();
  321. sensor->setRX(V9261F_PIN);
  322. sensor->setInverted(V9261F_PIN_INVERSE);
  323. _sensors.push_back(sensor);
  324. }
  325. #endif
  326. }
  327. void _sensorConfigure() {
  328. double value;
  329. for (unsigned char i=0; i<_sensors.size(); i++) {
  330. #if EMON_ANALOG_SUPPORT
  331. if (_sensors[i]->getID() == SENSOR_EMON_ANALOG_ID) {
  332. EmonAnalogSensor * sensor = (EmonAnalogSensor *) _sensors[i];
  333. if (value = getSetting("pwrExpectedP", 0).toInt() == 0) {
  334. value = getSetting("pwrRatioC", EMON_CURRENT_RATIO).toFloat();
  335. if (value > 0) sensor->setCurrentRatio(0, value);
  336. } else {
  337. sensor->expectedPower(0, value);
  338. setSetting("pwrRatioC", sensor->getCurrentRatio(0));
  339. }
  340. if (getSetting("pwrResetCalibration", 0).toInt() == 1) {
  341. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  342. delSetting("pwrRatioC");
  343. }
  344. sensor->setVoltage(getSetting("pwrVoltage", EMON_MAINS_VOLTAGE).toInt());
  345. }
  346. #endif // EMON_ANALOG_SUPPORT
  347. // Force sensor to reload config
  348. _sensors[i]->begin();
  349. #if HLW8012_SUPPORT
  350. if (_sensors[i]->getID() == SENSOR_HLW8012_ID) {
  351. HLW8012Sensor * sensor = (HLW8012Sensor *) _sensors[i];
  352. if (value = getSetting("pwrExpectedC", 0).toFloat()) {
  353. sensor->expectedCurrent(value);
  354. setSetting("pwrRatioC", sensor->getCurrentRatio());
  355. } else {
  356. value = getSetting("pwrRatioC", 0).toFloat();
  357. if (value > 0) sensor->setCurrentRatio(value);
  358. }
  359. if (value = getSetting("pwrExpectedV", 0).toInt()) {
  360. sensor->expectedVoltage(value);
  361. setSetting("pwrRatioV", sensor->getVoltageRatio());
  362. } else {
  363. value = getSetting("pwrRatioV", 0).toFloat();
  364. if (value > 0) sensor->setVoltageRatio(value);
  365. }
  366. if (value = getSetting("pwrExpectedP", 0).toInt()) {
  367. sensor->expectedPower(value);
  368. setSetting("pwrRatioP", sensor->getPowerRatio());
  369. } else {
  370. value = getSetting("pwrRatioP", 0).toFloat();
  371. if (value > 0) sensor->setPowerRatio(value);
  372. }
  373. if (getSetting("pwrResetCalibration", 0).toInt() == 1) {
  374. sensor->resetRatios();
  375. delSetting("pwrRatioC");
  376. delSetting("pwrRatioV");
  377. delSetting("pwrRatioP");
  378. }
  379. }
  380. #endif // HLW8012_SUPPORT
  381. }
  382. // General sensor settings
  383. _sensor_read_interval = 1000 * constrain(getSetting("snsRead", SENSOR_READ_INTERVAL).toInt(), SENSOR_READ_MIN_INTERVAL, SENSOR_READ_MAX_INTERVAL);
  384. _sensor_report_every = constrain(getSetting("snsReport", SENSOR_REPORT_EVERY).toInt(), SENSOR_REPORT_MIN_EVERY, SENSOR_REPORT_MAX_EVERY);
  385. _sensor_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
  386. _sensor_temperature_units = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS).toInt();
  387. _sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat();
  388. // Update filter sizes
  389. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  390. _magnitudes[i].filter->resize(_sensor_report_every);
  391. }
  392. // Save settings
  393. delSetting("pwrExpectedP");
  394. delSetting("pwrExpectedC");
  395. delSetting("pwrExpectedV");
  396. delSetting("pwrResetCalibration");
  397. //saveSettings();
  398. }
  399. void _magnitudesInit() {
  400. for (unsigned char i=0; i<_sensors.size(); i++) {
  401. BaseSensor * sensor = _sensors[i];
  402. DEBUG_MSG_P(PSTR("[SENSOR] %s\n"), sensor->description().c_str());
  403. if (sensor->error() != 0) DEBUG_MSG_P(PSTR("[SENSOR] -> ERROR %d\n"), sensor->error());
  404. for (unsigned char k=0; k<sensor->count(); k++) {
  405. unsigned char type = sensor->type(k);
  406. sensor_magnitude_t new_magnitude;
  407. new_magnitude.sensor = sensor;
  408. new_magnitude.local = k;
  409. new_magnitude.type = type;
  410. new_magnitude.global = _counts[type];
  411. new_magnitude.current = 0;
  412. new_magnitude.filtered = 0;
  413. new_magnitude.reported = 0;
  414. new_magnitude.min_change = 0;
  415. if (type == MAGNITUDE_DIGITAL) {
  416. new_magnitude.filter = new MaxFilter();
  417. } else if (type == MAGNITUDE_EVENTS) {
  418. new_magnitude.filter = new MovingAverageFilter();
  419. } else {
  420. new_magnitude.filter = new MedianFilter();
  421. }
  422. new_magnitude.filter->resize(_sensor_report_every);
  423. _magnitudes.push_back(new_magnitude);
  424. DEBUG_MSG_P(PSTR("[SENSOR] -> %s:%d\n"), _magnitudeTopic(type).c_str(), _counts[type]);
  425. _counts[type] = _counts[type] + 1;
  426. }
  427. }
  428. }
  429. // -----------------------------------------------------------------------------
  430. // Public
  431. // -----------------------------------------------------------------------------
  432. unsigned char sensorCount() {
  433. return _sensors.size();
  434. }
  435. unsigned char magnitudeCount() {
  436. return _magnitudes.size();
  437. }
  438. String magnitudeName(unsigned char index) {
  439. if (index < _magnitudes.size()) {
  440. sensor_magnitude_t magnitude = _magnitudes[index];
  441. return magnitude.sensor->slot(magnitude.local);
  442. }
  443. return String();
  444. }
  445. unsigned char magnitudeType(unsigned char index) {
  446. if (index < _magnitudes.size()) {
  447. return int(_magnitudes[index].type);
  448. }
  449. return MAGNITUDE_NONE;
  450. }
  451. unsigned char magnitudeIndex(unsigned char index) {
  452. if (index < _magnitudes.size()) {
  453. return int(_magnitudes[index].global);
  454. }
  455. return 0;
  456. }
  457. // -----------------------------------------------------------------------------
  458. void sensorSetup() {
  459. // Load sensors
  460. _sensorInit();
  461. // Configure stored values
  462. _sensorConfigure();
  463. // Load magnitudes
  464. _magnitudesInit();
  465. #if WEB_SUPPORT
  466. // Websockets
  467. wsOnSendRegister(_sensorWebSocketStart);
  468. wsOnSendRegister(_sensorWebSocketSendData);
  469. wsOnAfterParseRegister(_sensorConfigure);
  470. // API
  471. _sensorAPISetup();
  472. #endif
  473. }
  474. void sensorLoop() {
  475. static unsigned long last_update = 0;
  476. static unsigned long report_count = 0;
  477. if (_magnitudes.size() == 0) return;
  478. // Tick hook
  479. _sensorTick();
  480. // Check if we should read new data
  481. if (millis() - last_update > _sensor_read_interval) {
  482. last_update = millis();
  483. report_count = (report_count + 1) % _sensor_report_every;
  484. double current;
  485. double filtered;
  486. char buffer[64];
  487. // Pre-read hook
  488. _sensorPre();
  489. // Get readings
  490. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  491. sensor_magnitude_t magnitude = _magnitudes[i];
  492. if (magnitude.sensor->status()) {
  493. unsigned char decimals = _magnitudeDecimals(magnitude.type);
  494. current = magnitude.sensor->value(magnitude.local);
  495. magnitude.filter->add(current);
  496. // Special case
  497. if (magnitude.type == MAGNITUDE_EVENTS) current = magnitude.filter->result();
  498. current = _magnitudeProcess(magnitude.type, current);
  499. _magnitudes[i].current = current;
  500. // Debug
  501. #if SENSOR_DEBUG
  502. {
  503. dtostrf(current, 1-sizeof(buffer), decimals, buffer);
  504. DEBUG_MSG_P(PSTR("[SENSOR] %s - %s: %s%s\n"),
  505. magnitude.sensor->slot(magnitude.local).c_str(),
  506. _magnitudeTopic(magnitude.type).c_str(),
  507. buffer,
  508. _magnitudeUnits(magnitude.type).c_str()
  509. );
  510. }
  511. #endif // SENSOR_DEBUG
  512. // Time to report (we do it every _sensor_report_every readings)
  513. if (report_count == 0) {
  514. filtered = magnitude.filter->result();
  515. magnitude.filter->reset();
  516. filtered = _magnitudeProcess(magnitude.type, filtered);
  517. _magnitudes[i].filtered = filtered;
  518. // Check if there is a minimum change threshold to report
  519. if (fabs(filtered - magnitude.reported) >= magnitude.min_change) {
  520. _magnitudes[i].reported = filtered;
  521. dtostrf(filtered, 1-sizeof(buffer), decimals, buffer);
  522. #if MQTT_SUPPORT
  523. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
  524. mqttSend(_magnitudeTopic(magnitude.type).c_str(), magnitude.global, buffer);
  525. } else {
  526. mqttSend(_magnitudeTopic(magnitude.type).c_str(), buffer);
  527. }
  528. #if SENSOR_PUBLISH_ADDRESSES
  529. char topic[32];
  530. snprintf(topic, sizeof(topic), "%s/%s", SENSOR_ADDRESS_TOPIC, _magnitudeTopic(magnitude.type).c_str());
  531. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
  532. mqttSend(topic, magnitude.global, magnitude.sensor->address(magnitude.local).c_str());
  533. } else {
  534. mqttSend(topic, magnitude.sensor->address(magnitude.local).c_str());
  535. }
  536. #endif // SENSOR_PUBLISH_ADDRESSES
  537. #endif // MQTT_SUPPORT
  538. #if INFLUXDB_SUPPORT
  539. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
  540. idbSend(_magnitudeTopic(magnitude.type).c_str(), magnitude.global, buffer);
  541. } else {
  542. idbSend(_magnitudeTopic(magnitude.type).c_str(), buffer);
  543. }
  544. #endif // INFLUXDB_SUPPORT
  545. #if THINGSPEAK_SUPPORT
  546. tspkEnqueueMeasurement(i, buffer);
  547. #endif
  548. #if DOMOTICZ_SUPPORT
  549. {
  550. char key[15];
  551. snprintf_P(key, sizeof(key), PSTR("dczMagnitude%d"), i);
  552. if (magnitude.type == MAGNITUDE_HUMIDITY) {
  553. int status;
  554. if (filtered > 70) {
  555. status = HUMIDITY_WET;
  556. } else if (filtered > 45) {
  557. status = HUMIDITY_COMFORTABLE;
  558. } else if (filtered > 30) {
  559. status = HUMIDITY_NORMAL;
  560. } else {
  561. status = HUMIDITY_DRY;
  562. }
  563. char status_buf[5];
  564. itoa(status, status_buf, 10);
  565. domoticzSend(key, buffer, status_buf);
  566. } else {
  567. domoticzSend(key, 0, buffer);
  568. }
  569. }
  570. #endif // DOMOTICZ_SUPPORT
  571. } // if (fabs(filtered - magnitude.reported) >= magnitude.min_change)
  572. } // if (report_count == 0)
  573. } // if (magnitude.sensor->status())
  574. } // for (unsigned char i=0; i<_magnitudes.size(); i++)
  575. // Post-read hook
  576. _sensorPost();
  577. #if WEB_SUPPORT
  578. wsSend(_sensorWebSocketSendData);
  579. #endif
  580. #if THINGSPEAK_SUPPORT
  581. if (report_count == 0) tspkFlush();
  582. #endif
  583. }
  584. }
  585. #endif // SENSOR_SUPPORT