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.

1775 lines
58 KiB

7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. SENSOR MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if SENSOR_SUPPORT
  6. #include <vector>
  7. #include "filters/LastFilter.h"
  8. #include "filters/MaxFilter.h"
  9. #include "filters/MedianFilter.h"
  10. #include "filters/MovingAverageFilter.h"
  11. #include "sensors/BaseSensor.h"
  12. #include <float.h>
  13. typedef struct {
  14. BaseSensor * sensor; // Sensor object
  15. BaseFilter * filter; // Filter object
  16. unsigned char local; // Local index in its provider
  17. unsigned char type; // Type of measurement
  18. unsigned char decimals; // Number of decimals in textual representation
  19. unsigned char global; // Global index in its type
  20. double last; // Last raw value from sensor (unfiltered)
  21. double reported; // Last reported value
  22. double min_change; // Minimum value change to report
  23. double max_change; // Maximum value change to report
  24. } sensor_magnitude_t;
  25. std::vector<BaseSensor *> _sensors;
  26. std::vector<sensor_magnitude_t> _magnitudes;
  27. bool _sensors_ready = false;
  28. unsigned char _counts[MAGNITUDE_MAX];
  29. bool _sensor_realtime = API_REAL_TIME_VALUES;
  30. unsigned long _sensor_read_interval = 1000 * SENSOR_READ_INTERVAL;
  31. unsigned char _sensor_report_every = SENSOR_REPORT_EVERY;
  32. unsigned char _sensor_save_every = SENSOR_SAVE_EVERY;
  33. unsigned char _sensor_power_units = SENSOR_POWER_UNITS;
  34. unsigned char _sensor_energy_units = SENSOR_ENERGY_UNITS;
  35. unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS;
  36. double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION;
  37. double _sensor_humidity_correction = SENSOR_HUMIDITY_CORRECTION;
  38. double _sensor_lux_correction = SENSOR_LUX_CORRECTION;
  39. #if PZEM004T_SUPPORT
  40. PZEM004TSensor *pzem004t_sensor;
  41. #endif
  42. String _sensor_energy_reset_ts = String();
  43. // -----------------------------------------------------------------------------
  44. // Private
  45. // -----------------------------------------------------------------------------
  46. unsigned char _magnitudeDecimals(unsigned char type) {
  47. // Hardcoded decimals (these should be linked to the unit, instead of the magnitude)
  48. if (type == MAGNITUDE_ANALOG) return ANALOG_DECIMALS;
  49. if (type == MAGNITUDE_ENERGY ||
  50. type == MAGNITUDE_ENERGY_DELTA) {
  51. _sensor_energy_units = getSetting("eneUnits", SENSOR_ENERGY_UNITS).toInt();
  52. if (_sensor_energy_units == ENERGY_KWH) return 3;
  53. }
  54. if (type == MAGNITUDE_POWER_ACTIVE ||
  55. type == MAGNITUDE_POWER_APPARENT ||
  56. type == MAGNITUDE_POWER_REACTIVE) {
  57. if (_sensor_power_units == POWER_KILOWATTS) return 3;
  58. }
  59. if (type < MAGNITUDE_MAX) return pgm_read_byte(magnitude_decimals + type);
  60. return 0;
  61. }
  62. double _magnitudeProcess(unsigned char type, unsigned char decimals, double value) {
  63. // Hardcoded conversions (these should be linked to the unit, instead of the magnitude)
  64. if (type == MAGNITUDE_TEMPERATURE) {
  65. if (_sensor_temperature_units == TMP_FAHRENHEIT) value = value * 1.8 + 32;
  66. value = value + _sensor_temperature_correction;
  67. }
  68. if (type == MAGNITUDE_HUMIDITY) {
  69. value = constrain(value + _sensor_humidity_correction, 0, 100);
  70. }
  71. if (type == MAGNITUDE_LUX) {
  72. value = value + _sensor_lux_correction;
  73. }
  74. if (type == MAGNITUDE_ENERGY ||
  75. type == MAGNITUDE_ENERGY_DELTA) {
  76. if (_sensor_energy_units == ENERGY_KWH) value = value / 3600000;
  77. }
  78. if (type == MAGNITUDE_POWER_ACTIVE ||
  79. type == MAGNITUDE_POWER_APPARENT ||
  80. type == MAGNITUDE_POWER_REACTIVE) {
  81. if (_sensor_power_units == POWER_KILOWATTS) value = value / 1000;
  82. }
  83. return roundTo(value, decimals);
  84. }
  85. // -----------------------------------------------------------------------------
  86. #if WEB_SUPPORT
  87. template<typename T> void _sensorWebSocketMagnitudes(JsonObject& root, T prefix) {
  88. // ws produces flat list <prefix>Magnitudes
  89. String ws_name = String(prefix);
  90. ws_name.concat("Magnitudes");
  91. // config uses <prefix>Magnitude<index> (cut 's')
  92. String conf_name = ws_name.substring(0, ws_name.length() - 1);
  93. JsonObject& list = root.createNestedObject(ws_name);
  94. list["size"] = magnitudeCount();
  95. JsonArray& name = list.createNestedArray("name");
  96. JsonArray& type = list.createNestedArray("type");
  97. JsonArray& index = list.createNestedArray("index");
  98. JsonArray& idx = list.createNestedArray("idx");
  99. for (unsigned char i=0; i<magnitudeCount(); ++i) {
  100. name.add(magnitudeName(i));
  101. type.add(magnitudeType(i));
  102. index.add(magnitudeIndex(i));
  103. idx.add(getSetting(conf_name, i, 0).toInt());
  104. }
  105. }
  106. bool _sensorWebSocketOnReceive(const char * key, JsonVariant& value) {
  107. if (strncmp(key, "pwr", 3) == 0) return true;
  108. if (strncmp(key, "sns", 3) == 0) return true;
  109. if (strncmp(key, "tmp", 3) == 0) return true;
  110. if (strncmp(key, "hum", 3) == 0) return true;
  111. if (strncmp(key, "ene", 3) == 0) return true;
  112. if (strncmp(key, "lux", 3) == 0) return true;
  113. return false;
  114. }
  115. void _sensorWebSocketSendData(JsonObject& root) {
  116. char buffer[10];
  117. bool hasTemperature = false;
  118. bool hasHumidity = false;
  119. bool hasMICS = false;
  120. JsonObject& magnitudes = root.createNestedObject("magnitudes");
  121. uint8_t size = 0;
  122. JsonArray& index = magnitudes.createNestedArray("index");
  123. JsonArray& type = magnitudes.createNestedArray("type");
  124. JsonArray& value = magnitudes.createNestedArray("value");
  125. JsonArray& units = magnitudes.createNestedArray("units");
  126. JsonArray& error = magnitudes.createNestedArray("error");
  127. JsonArray& description = magnitudes.createNestedArray("description");
  128. for (unsigned char i=0; i<magnitudeCount(); i++) {
  129. sensor_magnitude_t magnitude = _magnitudes[i];
  130. if (magnitude.type == MAGNITUDE_EVENT) continue;
  131. ++size;
  132. double value_show = _magnitudeProcess(magnitude.type, magnitude.decimals, magnitude.last);
  133. dtostrf(value_show, 1-sizeof(buffer), magnitude.decimals, buffer);
  134. index.add<uint8_t>(magnitude.global);
  135. type.add<uint8_t>(magnitude.type);
  136. value.add(buffer);
  137. units.add(magnitudeUnits(magnitude.type));
  138. error.add(magnitude.sensor->error());
  139. if (magnitude.type == MAGNITUDE_ENERGY) {
  140. if (_sensor_energy_reset_ts.length() == 0) _sensorResetTS();
  141. description.add(magnitude.sensor->slot(magnitude.local) + String(" (since ") + _sensor_energy_reset_ts + String(")"));
  142. } else {
  143. description.add(magnitude.sensor->slot(magnitude.local));
  144. }
  145. if (magnitude.type == MAGNITUDE_TEMPERATURE) hasTemperature = true;
  146. if (magnitude.type == MAGNITUDE_HUMIDITY) hasHumidity = true;
  147. #if MICS2710_SUPPORT || MICS5525_SUPPORT
  148. if (magnitude.type == MAGNITUDE_CO || magnitude.type == MAGNITUDE_NO2) hasMICS = true;
  149. #endif
  150. }
  151. magnitudes["size"] = size;
  152. if (hasTemperature) root["temperatureVisible"] = 1;
  153. if (hasHumidity) root["humidityVisible"] = 1;
  154. if (hasMICS) root["micsVisible"] = 1;
  155. }
  156. void _sensorWebSocketStart(JsonObject& root) {
  157. for (unsigned char i=0; i<_sensors.size(); i++) {
  158. BaseSensor * sensor = _sensors[i];
  159. #if EMON_ANALOG_SUPPORT
  160. if (sensor->getID() == SENSOR_EMON_ANALOG_ID) {
  161. root["emonVisible"] = 1;
  162. root["pwrVisible"] = 1;
  163. root["pwrVoltage"] = ((EmonAnalogSensor *) sensor)->getVoltage();
  164. }
  165. #endif
  166. #if HLW8012_SUPPORT
  167. if (sensor->getID() == SENSOR_HLW8012_ID) {
  168. root["hlwVisible"] = 1;
  169. root["pwrVisible"] = 1;
  170. }
  171. #endif
  172. #if CSE7766_SUPPORT
  173. if (sensor->getID() == SENSOR_CSE7766_ID) {
  174. root["cseVisible"] = 1;
  175. root["pwrVisible"] = 1;
  176. }
  177. #endif
  178. #if V9261F_SUPPORT
  179. if (sensor->getID() == SENSOR_V9261F_ID) {
  180. root["pwrVisible"] = 1;
  181. }
  182. #endif
  183. #if ECH1560_SUPPORT
  184. if (sensor->getID() == SENSOR_ECH1560_ID) {
  185. root["pwrVisible"] = 1;
  186. }
  187. #endif
  188. #if PZEM004T_SUPPORT
  189. if (sensor->getID() == SENSOR_PZEM004T_ID) {
  190. root["pzemVisible"] = 1;
  191. root["pwrVisible"] = 1;
  192. }
  193. #endif
  194. #if PULSEMETER_SUPPORT
  195. if (sensor->getID() == SENSOR_PULSEMETER_ID) {
  196. root["pmVisible"] = 1;
  197. root["pwrRatioE"] = ((PulseMeterSensor *) sensor)->getEnergyRatio();
  198. }
  199. #endif
  200. }
  201. if (magnitudeCount()) {
  202. root["snsVisible"] = 1;
  203. //root["apiRealTime"] = _sensor_realtime;
  204. root["pwrUnits"] = _sensor_power_units;
  205. root["eneUnits"] = _sensor_energy_units;
  206. root["tmpUnits"] = _sensor_temperature_units;
  207. root["tmpCorrection"] = _sensor_temperature_correction;
  208. root["humCorrection"] = _sensor_humidity_correction;
  209. root["snsRead"] = _sensor_read_interval / 1000;
  210. root["snsReport"] = _sensor_report_every;
  211. root["snsSave"] = _sensor_save_every;
  212. }
  213. /*
  214. // Sensors manifest
  215. JsonArray& manifest = root.createNestedArray("manifest");
  216. #if BMX280_SUPPORT
  217. BMX280Sensor::manifest(manifest);
  218. #endif
  219. // Sensors configuration
  220. JsonArray& sensors = root.createNestedArray("sensors");
  221. for (unsigned char i; i<_sensors.size(); i++) {
  222. JsonObject& sensor = sensors.createNestedObject();
  223. sensor["index"] = i;
  224. sensor["id"] = _sensors[i]->getID();
  225. _sensors[i]->getConfig(sensor);
  226. }
  227. */
  228. }
  229. #endif // WEB_SUPPORT
  230. #if API_SUPPORT
  231. void _sensorAPISetup() {
  232. for (unsigned char magnitude_id=0; magnitude_id<_magnitudes.size(); magnitude_id++) {
  233. sensor_magnitude_t magnitude = _magnitudes[magnitude_id];
  234. String topic = magnitudeTopic(magnitude.type);
  235. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) topic = topic + "/" + String(magnitude.global);
  236. apiRegister(topic.c_str(), [magnitude_id](char * buffer, size_t len) {
  237. sensor_magnitude_t magnitude = _magnitudes[magnitude_id];
  238. double value = _sensor_realtime ? magnitude.last : magnitude.reported;
  239. dtostrf(value, 1-len, magnitude.decimals, buffer);
  240. });
  241. }
  242. }
  243. #endif // API_SUPPORT
  244. #if TERMINAL_SUPPORT
  245. void _sensorInitCommands() {
  246. terminalRegisterCommand(F("MAGNITUDES"), [](Embedis* e) {
  247. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  248. sensor_magnitude_t magnitude = _magnitudes[i];
  249. DEBUG_MSG_P(PSTR("[SENSOR] * %2d: %s @ %s (%s/%d)\n"),
  250. i,
  251. magnitudeTopic(magnitude.type).c_str(),
  252. magnitude.sensor->slot(magnitude.local).c_str(),
  253. magnitudeTopic(magnitude.type).c_str(),
  254. magnitude.global
  255. );
  256. }
  257. terminalOK();
  258. });
  259. #if PZEM004T_SUPPORT
  260. terminalRegisterCommand(F("PZ.ADDRESS"), [](Embedis* e) {
  261. if (e->argc == 1) {
  262. DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n"));
  263. unsigned char dev_count = pzem004t_sensor->getAddressesCount();
  264. for(unsigned char dev = 0; dev < dev_count; dev++) {
  265. DEBUG_MSG_P(PSTR("Device %d/%s\n"), dev, pzem004t_sensor->getAddress(dev).c_str());
  266. }
  267. terminalOK();
  268. } else if(e->argc == 2) {
  269. IPAddress addr;
  270. if (addr.fromString(String(e->argv[1]))) {
  271. if(pzem004t_sensor->setDeviceAddress(&addr)) {
  272. terminalOK();
  273. }
  274. } else {
  275. terminalError(F("Invalid address argument"));
  276. }
  277. } else {
  278. terminalError(F("Wrong arguments"));
  279. }
  280. });
  281. terminalRegisterCommand(F("PZ.RESET"), [](Embedis* e) {
  282. if(e->argc > 2) {
  283. terminalError(F("Wrong arguments"));
  284. } else {
  285. unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0;
  286. unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount();
  287. DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n"));
  288. for(unsigned char dev = init; dev < limit; dev++) {
  289. float offset = pzem004t_sensor->resetEnergy(dev);
  290. setSetting("pzemEneTotal", dev, offset);
  291. DEBUG_MSG_P(PSTR("Device %d/%s - Offset: %s\n"), dev, pzem004t_sensor->getAddress(dev).c_str(), String(offset).c_str());
  292. }
  293. terminalOK();
  294. }
  295. });
  296. terminalRegisterCommand(F("PZ.VALUE"), [](Embedis* e) {
  297. if(e->argc > 2) {
  298. terminalError(F("Wrong arguments"));
  299. } else {
  300. unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0;
  301. unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount();
  302. DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n"));
  303. for(unsigned char dev = init; dev < limit; dev++) {
  304. DEBUG_MSG_P(PSTR("Device %d/%s - Current: %s Voltage: %s Power: %s Energy: %s\n"), //
  305. dev,
  306. pzem004t_sensor->getAddress(dev).c_str(),
  307. String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_CURRENT_INDEX)).c_str(),
  308. String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_VOLTAGE_INDEX)).c_str(),
  309. String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_POWER_ACTIVE_INDEX)).c_str(),
  310. String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_ENERGY_INDEX)).c_str());
  311. }
  312. terminalOK();
  313. }
  314. });
  315. #endif
  316. }
  317. #endif
  318. void _sensorTick() {
  319. for (unsigned char i=0; i<_sensors.size(); i++) {
  320. _sensors[i]->tick();
  321. }
  322. }
  323. void _sensorPre() {
  324. for (unsigned char i=0; i<_sensors.size(); i++) {
  325. _sensors[i]->pre();
  326. if (!_sensors[i]->status()) {
  327. DEBUG_MSG_P(PSTR("[SENSOR] Error reading data from %s (error: %d)\n"),
  328. _sensors[i]->description().c_str(),
  329. _sensors[i]->error()
  330. );
  331. }
  332. }
  333. }
  334. void _sensorPost() {
  335. for (unsigned char i=0; i<_sensors.size(); i++) {
  336. _sensors[i]->post();
  337. }
  338. }
  339. void _sensorResetTS() {
  340. #if NTP_SUPPORT
  341. if (ntpSynced()) {
  342. if (_sensor_energy_reset_ts.length() == 0) {
  343. _sensor_energy_reset_ts = ntpDateTime(now() - millis() / 1000);
  344. } else {
  345. _sensor_energy_reset_ts = ntpDateTime(now());
  346. }
  347. } else {
  348. _sensor_energy_reset_ts = String();
  349. }
  350. setSetting("snsResetTS", _sensor_energy_reset_ts);
  351. #endif
  352. }
  353. double _sensorEnergyTotal() {
  354. double value = 0;
  355. if (rtcmemStatus()) {
  356. value = Rtcmem->energy;
  357. } else {
  358. value = (_sensor_save_every > 0) ? getSetting("eneTotal", 0).toInt() : 0;
  359. }
  360. return value;
  361. }
  362. void _sensorEnergyTotal(double value) {
  363. static unsigned long save_count = 0;
  364. // Save to EEPROM every '_sensor_save_every' readings
  365. if (_sensor_save_every > 0) {
  366. save_count = (save_count + 1) % _sensor_save_every;
  367. if (0 == save_count) {
  368. setSetting("eneTotal", value);
  369. saveSettings();
  370. }
  371. }
  372. // Always save to RTCMEM
  373. Rtcmem->energy = value;
  374. }
  375. // -----------------------------------------------------------------------------
  376. // Sensor initialization
  377. // -----------------------------------------------------------------------------
  378. void _sensorLoad() {
  379. /*
  380. This is temporal, in the future sensors will be initialized based on
  381. soft configuration (data stored in EEPROM config) so you will be able
  382. to define and configure new sensors on the fly
  383. At the time being, only enabled sensors (those with *_SUPPORT to 1) are being
  384. loaded and initialized here. If you want to add new sensors of the same type
  385. just duplicate the block and change the arguments for the set* methods.
  386. Check the DHT block below for an example
  387. */
  388. #if AM2320_SUPPORT
  389. {
  390. AM2320Sensor * sensor = new AM2320Sensor();
  391. sensor->setAddress(AM2320_ADDRESS);
  392. _sensors.push_back(sensor);
  393. }
  394. #endif
  395. #if ANALOG_SUPPORT
  396. {
  397. AnalogSensor * sensor = new AnalogSensor();
  398. sensor->setSamples(ANALOG_SAMPLES);
  399. sensor->setDelay(ANALOG_DELAY);
  400. //CICM For analog scaling
  401. sensor->setFactor(ANALOG_FACTOR);
  402. sensor->setOffset(ANALOG_OFFSET);
  403. _sensors.push_back(sensor);
  404. }
  405. #endif
  406. #if BH1750_SUPPORT
  407. {
  408. BH1750Sensor * sensor = new BH1750Sensor();
  409. sensor->setAddress(BH1750_ADDRESS);
  410. sensor->setMode(BH1750_MODE);
  411. _sensors.push_back(sensor);
  412. }
  413. #endif
  414. #if BMP180_SUPPORT
  415. {
  416. BMP180Sensor * sensor = new BMP180Sensor();
  417. sensor->setAddress(BMP180_ADDRESS);
  418. _sensors.push_back(sensor);
  419. }
  420. #endif
  421. #if BMX280_SUPPORT
  422. {
  423. // Support up to two sensors with full auto-discovery.
  424. const unsigned char number = constrain(getSetting("bmx280Number", BMX280_NUMBER).toInt(), 1, 2);
  425. // For second sensor, if BMX280_ADDRESS is 0x00 then auto-discover
  426. // otherwise choose the other unnamed sensor address
  427. const unsigned char first = getSetting("bmx280Address", BMX280_ADDRESS).toInt();
  428. const unsigned char second = (first == 0x00) ? 0x00 : (0x76 + 0x77 - first);
  429. const unsigned char address_map[2] = { first, second };
  430. for (unsigned char n=0; n < number; ++n) {
  431. BMX280Sensor * sensor = new BMX280Sensor();
  432. sensor->setAddress(address_map[n]);
  433. _sensors.push_back(sensor);
  434. }
  435. }
  436. #endif
  437. #if CSE7766_SUPPORT
  438. {
  439. CSE7766Sensor * sensor = new CSE7766Sensor();
  440. sensor->setRX(CSE7766_PIN);
  441. _sensors.push_back(sensor);
  442. }
  443. #endif
  444. #if DALLAS_SUPPORT
  445. {
  446. DallasSensor * sensor = new DallasSensor();
  447. sensor->setGPIO(DALLAS_PIN);
  448. _sensors.push_back(sensor);
  449. }
  450. #endif
  451. #if DHT_SUPPORT
  452. {
  453. DHTSensor * sensor = new DHTSensor();
  454. sensor->setGPIO(DHT_PIN);
  455. sensor->setType(DHT_TYPE);
  456. _sensors.push_back(sensor);
  457. }
  458. #endif
  459. /*
  460. // Example on how to add a second DHT sensor
  461. // DHT2_PIN and DHT2_TYPE should be defined in sensors.h file
  462. #if DHT_SUPPORT
  463. {
  464. DHTSensor * sensor = new DHTSensor();
  465. sensor->setGPIO(DHT2_PIN);
  466. sensor->setType(DHT2_TYPE);
  467. _sensors.push_back(sensor);
  468. }
  469. #endif
  470. */
  471. #if DIGITAL_SUPPORT
  472. {
  473. #if (DIGITAL1_PIN != GPIO_NONE)
  474. {
  475. DigitalSensor * sensor = new DigitalSensor();
  476. sensor->setGPIO(DIGITAL1_PIN);
  477. sensor->setMode(DIGITAL1_PIN_MODE);
  478. sensor->setDefault(DIGITAL1_DEFAULT_STATE);
  479. _sensors.push_back(sensor);
  480. }
  481. #endif
  482. #if (DIGITAL2_PIN != GPIO_NONE)
  483. {
  484. DigitalSensor * sensor = new DigitalSensor();
  485. sensor->setGPIO(DIGITAL2_PIN);
  486. sensor->setMode(DIGITAL2_PIN_MODE);
  487. sensor->setDefault(DIGITAL2_DEFAULT_STATE);
  488. _sensors.push_back(sensor);
  489. }
  490. #endif
  491. #if (DIGITAL3_PIN != GPIO_NONE)
  492. {
  493. DigitalSensor * sensor = new DigitalSensor();
  494. sensor->setGPIO(DIGITAL3_PIN);
  495. sensor->setMode(DIGITAL3_PIN_MODE);
  496. sensor->setDefault(DIGITAL3_DEFAULT_STATE);
  497. _sensors.push_back(sensor);
  498. }
  499. #endif
  500. #if (DIGITAL4_PIN != GPIO_NONE)
  501. {
  502. DigitalSensor * sensor = new DigitalSensor();
  503. sensor->setGPIO(DIGITAL4_PIN);
  504. sensor->setMode(DIGITAL4_PIN_MODE);
  505. sensor->setDefault(DIGITAL4_DEFAULT_STATE);
  506. _sensors.push_back(sensor);
  507. }
  508. #endif
  509. #if (DIGITAL5_PIN != GPIO_NONE)
  510. {
  511. DigitalSensor * sensor = new DigitalSensor();
  512. sensor->setGPIO(DIGITAL5_PIN);
  513. sensor->setMode(DIGITAL5_PIN_MODE);
  514. sensor->setDefault(DIGITAL5_DEFAULT_STATE);
  515. _sensors.push_back(sensor);
  516. }
  517. #endif
  518. #if (DIGITAL6_PIN != GPIO_NONE)
  519. {
  520. DigitalSensor * sensor = new DigitalSensor();
  521. sensor->setGPIO(DIGITAL6_PIN);
  522. sensor->setMode(DIGITAL6_PIN_MODE);
  523. sensor->setDefault(DIGITAL6_DEFAULT_STATE);
  524. _sensors.push_back(sensor);
  525. }
  526. #endif
  527. #if (DIGITAL7_PIN != GPIO_NONE)
  528. {
  529. DigitalSensor * sensor = new DigitalSensor();
  530. sensor->setGPIO(DIGITAL7_PIN);
  531. sensor->setMode(DIGITAL7_PIN_MODE);
  532. sensor->setDefault(DIGITAL7_DEFAULT_STATE);
  533. _sensors.push_back(sensor);
  534. }
  535. #endif
  536. #if (DIGITAL8_PIN != GPIO_NONE)
  537. {
  538. DigitalSensor * sensor = new DigitalSensor();
  539. sensor->setGPIO(DIGITAL8_PIN);
  540. sensor->setMode(DIGITAL8_PIN_MODE);
  541. sensor->setDefault(DIGITAL8_DEFAULT_STATE);
  542. _sensors.push_back(sensor);
  543. }
  544. #endif
  545. }
  546. #endif
  547. #if ECH1560_SUPPORT
  548. {
  549. ECH1560Sensor * sensor = new ECH1560Sensor();
  550. sensor->setCLK(ECH1560_CLK_PIN);
  551. sensor->setMISO(ECH1560_MISO_PIN);
  552. sensor->setInverted(ECH1560_INVERTED);
  553. _sensors.push_back(sensor);
  554. }
  555. #endif
  556. #if EMON_ADC121_SUPPORT
  557. {
  558. EmonADC121Sensor * sensor = new EmonADC121Sensor();
  559. sensor->setAddress(EMON_ADC121_I2C_ADDRESS);
  560. sensor->setVoltage(EMON_MAINS_VOLTAGE);
  561. sensor->setReference(EMON_REFERENCE_VOLTAGE);
  562. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  563. _sensors.push_back(sensor);
  564. }
  565. #endif
  566. #if EMON_ADS1X15_SUPPORT
  567. {
  568. EmonADS1X15Sensor * sensor = new EmonADS1X15Sensor();
  569. sensor->setAddress(EMON_ADS1X15_I2C_ADDRESS);
  570. sensor->setType(EMON_ADS1X15_TYPE);
  571. sensor->setMask(EMON_ADS1X15_MASK);
  572. sensor->setGain(EMON_ADS1X15_GAIN);
  573. sensor->setVoltage(EMON_MAINS_VOLTAGE);
  574. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  575. sensor->setCurrentRatio(1, EMON_CURRENT_RATIO);
  576. sensor->setCurrentRatio(2, EMON_CURRENT_RATIO);
  577. sensor->setCurrentRatio(3, EMON_CURRENT_RATIO);
  578. _sensors.push_back(sensor);
  579. }
  580. #endif
  581. #if EMON_ANALOG_SUPPORT
  582. {
  583. EmonAnalogSensor * sensor = new EmonAnalogSensor();
  584. sensor->setVoltage(EMON_MAINS_VOLTAGE);
  585. sensor->setReference(EMON_REFERENCE_VOLTAGE);
  586. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  587. _sensors.push_back(sensor);
  588. }
  589. #endif
  590. #if EVENTS_SUPPORT
  591. {
  592. #if (EVENTS1_PIN != GPIO_NONE)
  593. {
  594. EventSensor * sensor = new EventSensor();
  595. sensor->setGPIO(EVENTS1_PIN);
  596. sensor->setTrigger(EVENTS1_TRIGGER);
  597. sensor->setPinMode(EVENTS1_PIN_MODE);
  598. sensor->setDebounceTime(EVENTS1_DEBOUNCE);
  599. sensor->setInterruptMode(EVENTS1_INTERRUPT_MODE);
  600. _sensors.push_back(sensor);
  601. }
  602. #endif
  603. #if (EVENTS2_PIN != GPIO_NONE)
  604. {
  605. EventSensor * sensor = new EventSensor();
  606. sensor->setGPIO(EVENTS2_PIN);
  607. sensor->setTrigger(EVENTS2_TRIGGER);
  608. sensor->setPinMode(EVENTS2_PIN_MODE);
  609. sensor->setDebounceTime(EVENTS2_DEBOUNCE);
  610. sensor->setInterruptMode(EVENTS2_INTERRUPT_MODE);
  611. _sensors.push_back(sensor);
  612. }
  613. #endif
  614. #if (EVENTS3_PIN != GPIO_NONE)
  615. {
  616. EventSensor * sensor = new EventSensor();
  617. sensor->setGPIO(EVENTS3_PIN);
  618. sensor->setTrigger(EVENTS3_TRIGGER);
  619. sensor->setPinMode(EVENTS3_PIN_MODE);
  620. sensor->setDebounceTime(EVENTS3_DEBOUNCE);
  621. sensor->setInterruptMode(EVENTS3_INTERRUPT_MODE);
  622. _sensors.push_back(sensor);
  623. }
  624. #endif
  625. #if (EVENTS4_PIN != GPIO_NONE)
  626. {
  627. EventSensor * sensor = new EventSensor();
  628. sensor->setGPIO(EVENTS4_PIN);
  629. sensor->setTrigger(EVENTS4_TRIGGER);
  630. sensor->setPinMode(EVENTS4_PIN_MODE);
  631. sensor->setDebounceTime(EVENTS4_DEBOUNCE);
  632. sensor->setInterruptMode(EVENTS4_INTERRUPT_MODE);
  633. _sensors.push_back(sensor);
  634. }
  635. #endif
  636. #if (EVENTS5_PIN != GPIO_NONE)
  637. {
  638. EventSensor * sensor = new EventSensor();
  639. sensor->setGPIO(EVENTS5_PIN);
  640. sensor->setTrigger(EVENTS5_TRIGGER);
  641. sensor->setPinMode(EVENTS5_PIN_MODE);
  642. sensor->setDebounceTime(EVENTS5_DEBOUNCE);
  643. sensor->setInterruptMode(EVENTS5_INTERRUPT_MODE);
  644. _sensors.push_back(sensor);
  645. }
  646. #endif
  647. #if (EVENTS6_PIN != GPIO_NONE)
  648. {
  649. EventSensor * sensor = new EventSensor();
  650. sensor->setGPIO(EVENTS6_PIN);
  651. sensor->setTrigger(EVENTS6_TRIGGER);
  652. sensor->setPinMode(EVENTS6_PIN_MODE);
  653. sensor->setDebounceTime(EVENTS6_DEBOUNCE);
  654. sensor->setInterruptMode(EVENTS6_INTERRUPT_MODE);
  655. _sensors.push_back(sensor);
  656. }
  657. #endif
  658. #if (EVENTS7_PIN != GPIO_NONE)
  659. {
  660. EventSensor * sensor = new EventSensor();
  661. sensor->setGPIO(EVENTS7_PIN);
  662. sensor->setTrigger(EVENTS7_TRIGGER);
  663. sensor->setPinMode(EVENTS7_PIN_MODE);
  664. sensor->setDebounceTime(EVENTS7_DEBOUNCE);
  665. sensor->setInterruptMode(EVENTS7_INTERRUPT_MODE);
  666. _sensors.push_back(sensor);
  667. }
  668. #endif
  669. #if (EVENTS8_PIN != GPIO_NONE)
  670. {
  671. EventSensor * sensor = new EventSensor();
  672. sensor->setGPIO(EVENTS8_PIN);
  673. sensor->setTrigger(EVENTS8_TRIGGER);
  674. sensor->setPinMode(EVENTS8_PIN_MODE);
  675. sensor->setDebounceTime(EVENTS8_DEBOUNCE);
  676. sensor->setInterruptMode(EVENTS8_INTERRUPT_MODE);
  677. _sensors.push_back(sensor);
  678. }
  679. #endif
  680. }
  681. #endif
  682. #if GEIGER_SUPPORT
  683. {
  684. GeigerSensor * sensor = new GeigerSensor(); // Create instance of thr Geiger module.
  685. sensor->setGPIO(GEIGER_PIN); // Interrupt pin of the attached geiger counter board.
  686. sensor->setMode(GEIGER_PIN_MODE); // This pin is an input.
  687. sensor->setDebounceTime(GEIGER_DEBOUNCE); // Debounce time 25ms, because https://github.com/Trickx/espurna/wiki/Geiger-counter
  688. sensor->setInterruptMode(GEIGER_INTERRUPT_MODE); // Interrupt triggering: edge detection rising.
  689. sensor->setCPM2SievertFactor(GEIGER_CPM2SIEVERT); // Conversion factor from counts per minute to µSv/h
  690. _sensors.push_back(sensor);
  691. }
  692. #endif
  693. #if GUVAS12SD_SUPPORT
  694. {
  695. GUVAS12SDSensor * sensor = new GUVAS12SDSensor();
  696. sensor->setGPIO(GUVAS12SD_PIN);
  697. _sensors.push_back(sensor);
  698. }
  699. #endif
  700. #if SONAR_SUPPORT
  701. {
  702. SonarSensor * sensor = new SonarSensor();
  703. sensor->setEcho(SONAR_ECHO);
  704. sensor->setIterations(SONAR_ITERATIONS);
  705. sensor->setMaxDistance(SONAR_MAX_DISTANCE);
  706. sensor->setTrigger(SONAR_TRIGGER);
  707. _sensors.push_back(sensor);
  708. }
  709. #endif
  710. #if HLW8012_SUPPORT
  711. {
  712. HLW8012Sensor * sensor = new HLW8012Sensor();
  713. sensor->setSEL(HLW8012_SEL_PIN);
  714. sensor->setCF(HLW8012_CF_PIN);
  715. sensor->setCF1(HLW8012_CF1_PIN);
  716. sensor->setSELCurrent(HLW8012_SEL_CURRENT);
  717. _sensors.push_back(sensor);
  718. }
  719. #endif
  720. #if LDR_SUPPORT
  721. {
  722. LDRSensor * sensor = new LDRSensor();
  723. sensor->setSamples(LDR_SAMPLES);
  724. sensor->setDelay(LDR_DELAY);
  725. sensor->setType(LDR_TYPE);
  726. sensor->setPhotocellPositionOnGround(LDR_ON_GROUND);
  727. sensor->setResistor(LDR_RESISTOR);
  728. sensor->setPhotocellParameters(LDR_MULTIPLICATION, LDR_POWER);
  729. _sensors.push_back(sensor);
  730. }
  731. #endif
  732. #if MHZ19_SUPPORT
  733. {
  734. MHZ19Sensor * sensor = new MHZ19Sensor();
  735. sensor->setRX(MHZ19_RX_PIN);
  736. sensor->setTX(MHZ19_TX_PIN);
  737. if (getSetting("mhz19CalibrateAuto", 0).toInt() == 1)
  738. sensor->setCalibrateAuto(true);
  739. _sensors.push_back(sensor);
  740. }
  741. #endif
  742. #if MICS2710_SUPPORT
  743. {
  744. MICS2710Sensor * sensor = new MICS2710Sensor();
  745. sensor->setAnalogGPIO(MICS2710_NOX_PIN);
  746. sensor->setPreHeatGPIO(MICS2710_PRE_PIN);
  747. sensor->setRL(MICS2710_RL);
  748. _sensors.push_back(sensor);
  749. }
  750. #endif
  751. #if MICS5525_SUPPORT
  752. {
  753. MICS5525Sensor * sensor = new MICS5525Sensor();
  754. sensor->setAnalogGPIO(MICS5525_RED_PIN);
  755. sensor->setRL(MICS5525_RL);
  756. _sensors.push_back(sensor);
  757. }
  758. #endif
  759. #if NTC_SUPPORT
  760. {
  761. NTCSensor * sensor = new NTCSensor();
  762. sensor->setSamples(NTC_SAMPLES);
  763. sensor->setDelay(NTC_DELAY);
  764. sensor->setUpstreamResistor(NTC_R_UP);
  765. sensor->setDownstreamResistor(NTC_R_DOWN);
  766. sensor->setBeta(NTC_BETA);
  767. sensor->setR0(NTC_R0);
  768. sensor->setT0(NTC_T0);
  769. _sensors.push_back(sensor);
  770. }
  771. #endif
  772. #if PMSX003_SUPPORT
  773. {
  774. PMSX003Sensor * sensor = new PMSX003Sensor();
  775. #if PMS_USE_SOFT
  776. sensor->setRX(PMS_RX_PIN);
  777. sensor->setTX(PMS_TX_PIN);
  778. #else
  779. sensor->setSerial(& PMS_HW_PORT);
  780. #endif
  781. sensor->setType(PMS_TYPE);
  782. _sensors.push_back(sensor);
  783. }
  784. #endif
  785. #if PULSEMETER_SUPPORT
  786. {
  787. PulseMeterSensor * sensor = new PulseMeterSensor();
  788. sensor->setGPIO(PULSEMETER_PIN);
  789. sensor->setEnergyRatio(PULSEMETER_ENERGY_RATIO);
  790. sensor->setInterruptMode(PULSEMETER_INTERRUPT_ON);
  791. sensor->setDebounceTime(PULSEMETER_DEBOUNCE);
  792. _sensors.push_back(sensor);
  793. }
  794. #endif
  795. #if PZEM004T_SUPPORT
  796. {
  797. String addresses = getSetting("pzemAddr", PZEM004T_ADDRESSES);
  798. if (!addresses.length()) {
  799. DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T Error: no addresses are configured\n"));
  800. return;
  801. }
  802. PZEM004TSensor * sensor = pzem004t_sensor = new PZEM004TSensor();
  803. sensor->setAddresses(addresses.c_str());
  804. if (getSetting("pzemSoft", PZEM004T_USE_SOFT).toInt() == 1) {
  805. sensor->setRX(getSetting("pzemRX", PZEM004T_RX_PIN).toInt());
  806. sensor->setTX(getSetting("pzemTX", PZEM004T_TX_PIN).toInt());
  807. } else {
  808. sensor->setSerial(& PZEM004T_HW_PORT);
  809. }
  810. // Read saved energy offset
  811. unsigned char dev_count = sensor->getAddressesCount();
  812. for(unsigned char dev = 0; dev < dev_count; dev++) {
  813. float value = getSetting("pzemEneTotal", dev, 0).toFloat();
  814. if (value > 0) sensor->resetEnergy(dev, value);
  815. }
  816. _sensors.push_back(sensor);
  817. }
  818. #endif
  819. #if SENSEAIR_SUPPORT
  820. {
  821. SenseAirSensor * sensor = new SenseAirSensor();
  822. sensor->setRX(SENSEAIR_RX_PIN);
  823. sensor->setTX(SENSEAIR_TX_PIN);
  824. _sensors.push_back(sensor);
  825. }
  826. #endif
  827. #if SDS011_SUPPORT
  828. {
  829. SDS011Sensor * sensor = new SDS011Sensor();
  830. sensor->setRX(SDS011_RX_PIN);
  831. sensor->setTX(SDS011_TX_PIN);
  832. _sensors.push_back(sensor);
  833. }
  834. #endif
  835. #if SHT3X_I2C_SUPPORT
  836. {
  837. SHT3XI2CSensor * sensor = new SHT3XI2CSensor();
  838. sensor->setAddress(SHT3X_I2C_ADDRESS);
  839. _sensors.push_back(sensor);
  840. }
  841. #endif
  842. #if SI7021_SUPPORT
  843. {
  844. SI7021Sensor * sensor = new SI7021Sensor();
  845. sensor->setAddress(SI7021_ADDRESS);
  846. _sensors.push_back(sensor);
  847. }
  848. #endif
  849. #if TMP3X_SUPPORT
  850. {
  851. TMP3XSensor * sensor = new TMP3XSensor();
  852. sensor->setType(TMP3X_TYPE);
  853. _sensors.push_back(sensor);
  854. }
  855. #endif
  856. #if V9261F_SUPPORT
  857. {
  858. V9261FSensor * sensor = new V9261FSensor();
  859. sensor->setRX(V9261F_PIN);
  860. sensor->setInverted(V9261F_PIN_INVERSE);
  861. _sensors.push_back(sensor);
  862. }
  863. #endif
  864. #if MAX6675_SUPPORT
  865. {
  866. MAX6675Sensor * sensor = new MAX6675Sensor();
  867. sensor->setCS(MAX6675_CS_PIN);
  868. sensor->setSO(MAX6675_SO_PIN);
  869. sensor->setSCK(MAX6675_SCK_PIN);
  870. _sensors.push_back(sensor);
  871. }
  872. #endif
  873. #if VEML6075_SUPPORT
  874. {
  875. VEML6075Sensor * sensor = new VEML6075Sensor();
  876. sensor->setIntegrationTime(VEML6075_INTEGRATION_TIME);
  877. sensor->setDynamicMode(VEML6075_DYNAMIC_MODE);
  878. _sensors.push_back(sensor);
  879. }
  880. #endif
  881. #if VL53L1X_SUPPORT
  882. {
  883. VL53L1XSensor * sensor = new VL53L1XSensor();
  884. sensor->setInterMeasurementPeriod(VL53L1X_INTER_MEASUREMENT_PERIOD);
  885. sensor->setDistanceMode(VL53L1X_DISTANCE_MODE);
  886. sensor->setMeasurementTimingBudget(VL53L1X_MEASUREMENT_TIMING_BUDGET);
  887. _sensors.push_back(sensor);
  888. }
  889. #endif
  890. #if EZOPH_SUPPORT
  891. {
  892. EZOPHSensor * sensor = new EZOPHSensor();
  893. sensor->setRX(EZOPH_RX_PIN);
  894. sensor->setTX(EZOPH_TX_PIN);
  895. _sensors.push_back(sensor);
  896. }
  897. #endif
  898. }
  899. void _sensorCallback(unsigned char i, unsigned char type, double value) {
  900. DEBUG_MSG_P(PSTR("[SENSOR] Sensor #%u callback, type %u, payload: '%s'\n"), i, type, String(value).c_str());
  901. for (unsigned char k=0; k<_magnitudes.size(); k++) {
  902. if ((_sensors[i] == _magnitudes[k].sensor) && (type == _magnitudes[k].type)) {
  903. _sensorReport(k, value);
  904. return;
  905. }
  906. }
  907. }
  908. void _sensorInit() {
  909. _sensors_ready = true;
  910. _sensor_save_every = getSetting("snsSave", 0).toInt();
  911. for (unsigned char i=0; i<_sensors.size(); i++) {
  912. // Do not process an already initialized sensor
  913. if (_sensors[i]->ready()) continue;
  914. DEBUG_MSG_P(PSTR("[SENSOR] Initializing %s\n"), _sensors[i]->description().c_str());
  915. // Force sensor to reload config
  916. _sensors[i]->begin();
  917. if (!_sensors[i]->ready()) {
  918. if (_sensors[i]->error() != 0) DEBUG_MSG_P(PSTR("[SENSOR] -> ERROR %d\n"), _sensors[i]->error());
  919. _sensors_ready = false;
  920. continue;
  921. }
  922. // Initialize magnitudes
  923. for (unsigned char k=0; k<_sensors[i]->count(); k++) {
  924. unsigned char type = _sensors[i]->type(k);
  925. signed char decimals = _sensors[i]->decimals(type);
  926. if (decimals < 0) decimals = _magnitudeDecimals(type);
  927. sensor_magnitude_t new_magnitude;
  928. new_magnitude.sensor = _sensors[i];
  929. new_magnitude.local = k;
  930. new_magnitude.type = type;
  931. new_magnitude.decimals = (unsigned char) decimals;
  932. new_magnitude.global = _counts[type];
  933. new_magnitude.last = 0;
  934. new_magnitude.reported = 0;
  935. new_magnitude.min_change = 0;
  936. new_magnitude.max_change = 0;
  937. // TODO: find a proper way to extend this to min/max of any magnitude
  938. if (MAGNITUDE_ENERGY == type) {
  939. new_magnitude.max_change = getSetting("eneMaxDelta", ENERGY_MAX_CHANGE).toFloat();
  940. } else if (MAGNITUDE_TEMPERATURE == type) {
  941. new_magnitude.min_change = getSetting("tmpMinDelta", TEMPERATURE_MIN_CHANGE).toFloat();
  942. } else if (MAGNITUDE_HUMIDITY == type) {
  943. new_magnitude.min_change = getSetting("humMinDelta", HUMIDITY_MIN_CHANGE).toFloat();
  944. }
  945. if (MAGNITUDE_ENERGY == type) {
  946. new_magnitude.filter = new LastFilter();
  947. } else if (MAGNITUDE_DIGITAL == type) {
  948. new_magnitude.filter = new MaxFilter();
  949. } else if (MAGNITUDE_COUNT == type || MAGNITUDE_GEIGER_CPM == type || MAGNITUDE_GEIGER_SIEVERT == type) { // For geiger counting moving average filter is the most appropriate if needed at all.
  950. new_magnitude.filter = new MovingAverageFilter();
  951. } else {
  952. new_magnitude.filter = new MedianFilter();
  953. }
  954. new_magnitude.filter->resize(_sensor_report_every);
  955. _magnitudes.push_back(new_magnitude);
  956. DEBUG_MSG_P(PSTR("[SENSOR] -> %s:%d\n"), magnitudeTopic(type).c_str(), _counts[type]);
  957. _counts[type] = _counts[type] + 1;
  958. }
  959. // Hook callback
  960. _sensors[i]->onEvent([i](unsigned char type, double value) {
  961. _sensorCallback(i, type, value);
  962. });
  963. // Custom initializations
  964. #if MICS2710_SUPPORT
  965. if (_sensors[i]->getID() == SENSOR_MICS2710_ID) {
  966. MICS2710Sensor * sensor = (MICS2710Sensor *) _sensors[i];
  967. sensor->setR0(getSetting("snsR0", MICS2710_R0).toInt());
  968. }
  969. #endif // MICS2710_SUPPORT
  970. #if MICS5525_SUPPORT
  971. if (_sensors[i]->getID() == SENSOR_MICS5525_ID) {
  972. MICS5525Sensor * sensor = (MICS5525Sensor *) _sensors[i];
  973. sensor->setR0(getSetting("snsR0", MICS5525_R0).toInt());
  974. }
  975. #endif // MICS5525_SUPPORT
  976. #if EMON_ANALOG_SUPPORT
  977. if (_sensors[i]->getID() == SENSOR_EMON_ANALOG_ID) {
  978. EmonAnalogSensor * sensor = (EmonAnalogSensor *) _sensors[i];
  979. sensor->setCurrentRatio(0, getSetting("pwrRatioC", EMON_CURRENT_RATIO).toFloat());
  980. sensor->setVoltage(getSetting("pwrVoltage", EMON_MAINS_VOLTAGE).toInt());
  981. double value = _sensorEnergyTotal();
  982. if (value > 0) sensor->resetEnergy(0, value);
  983. }
  984. #endif // EMON_ANALOG_SUPPORT
  985. #if HLW8012_SUPPORT
  986. if (_sensors[i]->getID() == SENSOR_HLW8012_ID) {
  987. HLW8012Sensor * sensor = (HLW8012Sensor *) _sensors[i];
  988. double value;
  989. value = getSetting("pwrRatioC", HLW8012_CURRENT_RATIO).toFloat();
  990. if (value > 0) sensor->setCurrentRatio(value);
  991. value = getSetting("pwrRatioV", HLW8012_VOLTAGE_RATIO).toFloat();
  992. if (value > 0) sensor->setVoltageRatio(value);
  993. value = getSetting("pwrRatioP", HLW8012_POWER_RATIO).toFloat();
  994. if (value > 0) sensor->setPowerRatio(value);
  995. value = _sensorEnergyTotal();
  996. if (value > 0) sensor->resetEnergy(value);
  997. }
  998. #endif // HLW8012_SUPPORT
  999. #if CSE7766_SUPPORT
  1000. if (_sensors[i]->getID() == SENSOR_CSE7766_ID) {
  1001. CSE7766Sensor * sensor = (CSE7766Sensor *) _sensors[i];
  1002. double value;
  1003. value = getSetting("pwrRatioC", 0).toFloat();
  1004. if (value > 0) sensor->setCurrentRatio(value);
  1005. value = getSetting("pwrRatioV", 0).toFloat();
  1006. if (value > 0) sensor->setVoltageRatio(value);
  1007. value = getSetting("pwrRatioP", 0).toFloat();
  1008. if (value > 0) sensor->setPowerRatio(value);
  1009. value = _sensorEnergyTotal();
  1010. if (value > 0) sensor->resetEnergy(value);
  1011. }
  1012. #endif // CSE7766_SUPPORT
  1013. #if PULSEMETER_SUPPORT
  1014. if (_sensors[i]->getID() == SENSOR_PULSEMETER_ID) {
  1015. PulseMeterSensor * sensor = (PulseMeterSensor *) _sensors[i];
  1016. sensor->setEnergyRatio(getSetting("pwrRatioE", sensor->getEnergyRatio()).toInt());
  1017. }
  1018. #endif // PULSEMETER_SUPPORT
  1019. }
  1020. }
  1021. void _sensorConfigure() {
  1022. // General sensor settings
  1023. _sensor_read_interval = 1000 * constrain(getSetting("snsRead", SENSOR_READ_INTERVAL).toInt(), SENSOR_READ_MIN_INTERVAL, SENSOR_READ_MAX_INTERVAL);
  1024. _sensor_report_every = constrain(getSetting("snsReport", SENSOR_REPORT_EVERY).toInt(), SENSOR_REPORT_MIN_EVERY, SENSOR_REPORT_MAX_EVERY);
  1025. _sensor_save_every = getSetting("snsSave", SENSOR_SAVE_EVERY).toInt();
  1026. _sensor_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
  1027. _sensor_power_units = getSetting("pwrUnits", SENSOR_POWER_UNITS).toInt();
  1028. _sensor_energy_units = getSetting("eneUnits", SENSOR_ENERGY_UNITS).toInt();
  1029. _sensor_temperature_units = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS).toInt();
  1030. _sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat();
  1031. _sensor_humidity_correction = getSetting("humCorrection", SENSOR_HUMIDITY_CORRECTION).toFloat();
  1032. _sensor_energy_reset_ts = getSetting("snsResetTS", "");
  1033. _sensor_lux_correction = getSetting("luxCorrection", SENSOR_LUX_CORRECTION).toFloat();
  1034. // Specific sensor settings
  1035. for (unsigned char i=0; i<_sensors.size(); i++) {
  1036. #if MICS2710_SUPPORT
  1037. if (_sensors[i]->getID() == SENSOR_MICS2710_ID) {
  1038. if (getSetting("snsResetCalibration", 0).toInt() == 1) {
  1039. MICS2710Sensor * sensor = (MICS2710Sensor *) _sensors[i];
  1040. sensor->calibrate();
  1041. setSetting("snsR0", sensor->getR0());
  1042. }
  1043. }
  1044. #endif // MICS2710_SUPPORT
  1045. #if MICS5525_SUPPORT
  1046. if (_sensors[i]->getID() == SENSOR_MICS5525_ID) {
  1047. if (getSetting("snsResetCalibration", 0).toInt() == 1) {
  1048. MICS5525Sensor * sensor = (MICS5525Sensor *) _sensors[i];
  1049. sensor->calibrate();
  1050. setSetting("snsR0", sensor->getR0());
  1051. }
  1052. }
  1053. #endif // MICS5525_SUPPORT
  1054. #if EMON_ANALOG_SUPPORT
  1055. if (_sensors[i]->getID() == SENSOR_EMON_ANALOG_ID) {
  1056. double value;
  1057. EmonAnalogSensor * sensor = (EmonAnalogSensor *) _sensors[i];
  1058. if ((value = getSetting("pwrExpectedP", 0).toInt())) {
  1059. sensor->expectedPower(0, value);
  1060. setSetting("pwrRatioC", sensor->getCurrentRatio(0));
  1061. }
  1062. if (getSetting("pwrResetCalibration", 0).toInt() == 1) {
  1063. sensor->setCurrentRatio(0, EMON_CURRENT_RATIO);
  1064. delSetting("pwrRatioC");
  1065. }
  1066. if (getSetting("pwrResetE", 0).toInt() == 1) {
  1067. sensor->resetEnergy();
  1068. delSetting("eneTotal");
  1069. _sensorResetTS();
  1070. }
  1071. sensor->setVoltage(getSetting("pwrVoltage", EMON_MAINS_VOLTAGE).toInt());
  1072. }
  1073. #endif // EMON_ANALOG_SUPPORT
  1074. #if EMON_ADC121_SUPPORT
  1075. if (_sensors[i]->getID() == SENSOR_EMON_ADC121_ID) {
  1076. EmonADC121Sensor * sensor = (EmonADC121Sensor *) _sensors[i];
  1077. if (getSetting("pwrResetE", 0).toInt() == 1) {
  1078. sensor->resetEnergy();
  1079. delSetting("eneTotal");
  1080. _sensorResetTS();
  1081. }
  1082. }
  1083. #endif
  1084. #if EMON_ADS1X15_SUPPORT
  1085. if (_sensors[i]->getID() == SENSOR_EMON_ADS1X15_ID) {
  1086. EmonADS1X15Sensor * sensor = (EmonADS1X15Sensor *) _sensors[i];
  1087. if (getSetting("pwrResetE", 0).toInt() == 1) {
  1088. sensor->resetEnergy();
  1089. delSetting("eneTotal");
  1090. _sensorResetTS();
  1091. }
  1092. }
  1093. #endif
  1094. #if HLW8012_SUPPORT
  1095. if (_sensors[i]->getID() == SENSOR_HLW8012_ID) {
  1096. double value;
  1097. HLW8012Sensor * sensor = (HLW8012Sensor *) _sensors[i];
  1098. if (value = getSetting("pwrExpectedC", 0).toFloat()) {
  1099. sensor->expectedCurrent(value);
  1100. setSetting("pwrRatioC", sensor->getCurrentRatio());
  1101. }
  1102. if (value = getSetting("pwrExpectedV", 0).toInt()) {
  1103. sensor->expectedVoltage(value);
  1104. setSetting("pwrRatioV", sensor->getVoltageRatio());
  1105. }
  1106. if (value = getSetting("pwrExpectedP", 0).toInt()) {
  1107. sensor->expectedPower(value);
  1108. setSetting("pwrRatioP", sensor->getPowerRatio());
  1109. }
  1110. if (getSetting("pwrResetE", 0).toInt() == 1) {
  1111. sensor->resetEnergy();
  1112. delSetting("eneTotal");
  1113. _sensorResetTS();
  1114. }
  1115. if (getSetting("pwrResetCalibration", 0).toInt() == 1) {
  1116. sensor->resetRatios();
  1117. delSetting("pwrRatioC");
  1118. delSetting("pwrRatioV");
  1119. delSetting("pwrRatioP");
  1120. }
  1121. }
  1122. #endif // HLW8012_SUPPORT
  1123. #if CSE7766_SUPPORT
  1124. if (_sensors[i]->getID() == SENSOR_CSE7766_ID) {
  1125. double value;
  1126. CSE7766Sensor * sensor = (CSE7766Sensor *) _sensors[i];
  1127. if ((value = getSetting("pwrExpectedC", 0).toFloat())) {
  1128. sensor->expectedCurrent(value);
  1129. setSetting("pwrRatioC", sensor->getCurrentRatio());
  1130. }
  1131. if ((value = getSetting("pwrExpectedV", 0).toInt())) {
  1132. sensor->expectedVoltage(value);
  1133. setSetting("pwrRatioV", sensor->getVoltageRatio());
  1134. }
  1135. if ((value = getSetting("pwrExpectedP", 0).toInt())) {
  1136. sensor->expectedPower(value);
  1137. setSetting("pwrRatioP", sensor->getPowerRatio());
  1138. }
  1139. if (getSetting("pwrResetE", 0).toInt() == 1) {
  1140. sensor->resetEnergy();
  1141. delSetting("eneTotal");
  1142. _sensorResetTS();
  1143. }
  1144. if (getSetting("pwrResetCalibration", 0).toInt() == 1) {
  1145. sensor->resetRatios();
  1146. delSetting("pwrRatioC");
  1147. delSetting("pwrRatioV");
  1148. delSetting("pwrRatioP");
  1149. }
  1150. }
  1151. #endif // CSE7766_SUPPORT
  1152. #if PULSEMETER_SUPPORT
  1153. if (_sensors[i]->getID() == SENSOR_PULSEMETER_ID) {
  1154. PulseMeterSensor * sensor = (PulseMeterSensor *) _sensors[i];
  1155. if (getSetting("pwrResetE", 0).toInt() == 1) {
  1156. sensor->resetEnergy();
  1157. delSetting("eneTotal");
  1158. _sensorResetTS();
  1159. }
  1160. sensor->setEnergyRatio(getSetting("pwrRatioE", sensor->getEnergyRatio()).toInt());
  1161. }
  1162. #endif // PULSEMETER_SUPPORT
  1163. #if PZEM004T_SUPPORT
  1164. if (_sensors[i]->getID() == SENSOR_PZEM004T_ID) {
  1165. PZEM004TSensor * sensor = (PZEM004TSensor *) _sensors[i];
  1166. if (getSetting("pwrResetE", 0).toInt() == 1) {
  1167. unsigned char dev_count = sensor->getAddressesCount();
  1168. for(unsigned char dev = 0; dev < dev_count; dev++) {
  1169. sensor->resetEnergy(dev, 0);
  1170. delSetting("pzemEneTotal", dev);
  1171. }
  1172. _sensorResetTS();
  1173. }
  1174. }
  1175. #endif // PZEM004T_SUPPORT
  1176. }
  1177. // Update filter sizes
  1178. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  1179. _magnitudes[i].filter->resize(_sensor_report_every);
  1180. }
  1181. // General processing
  1182. if (0 == _sensor_save_every) {
  1183. delSetting("eneTotal");
  1184. }
  1185. // Save settings
  1186. delSetting("snsResetCalibration");
  1187. delSetting("pwrExpectedP");
  1188. delSetting("pwrExpectedC");
  1189. delSetting("pwrExpectedV");
  1190. delSetting("pwrResetCalibration");
  1191. delSetting("pwrResetE");
  1192. saveSettings();
  1193. }
  1194. void _sensorReport(unsigned char index, double value) {
  1195. sensor_magnitude_t magnitude = _magnitudes[index];
  1196. unsigned char decimals = magnitude.decimals;
  1197. char buffer[10];
  1198. dtostrf(value, 1-sizeof(buffer), decimals, buffer);
  1199. #if BROKER_SUPPORT
  1200. brokerPublish(BROKER_MSG_TYPE_SENSOR ,magnitudeTopic(magnitude.type).c_str(), magnitude.local, buffer);
  1201. #endif
  1202. #if MQTT_SUPPORT
  1203. mqttSend(magnitudeTopicIndex(index).c_str(), buffer);
  1204. #if SENSOR_PUBLISH_ADDRESSES
  1205. char topic[32];
  1206. snprintf(topic, sizeof(topic), "%s/%s", SENSOR_ADDRESS_TOPIC, magnitudeTopic(magnitude.type).c_str());
  1207. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
  1208. mqttSend(topic, magnitude.global, magnitude.sensor->address(magnitude.local).c_str());
  1209. } else {
  1210. mqttSend(topic, magnitude.sensor->address(magnitude.local).c_str());
  1211. }
  1212. #endif // SENSOR_PUBLISH_ADDRESSES
  1213. #endif // MQTT_SUPPORT
  1214. #if THINGSPEAK_SUPPORT
  1215. tspkEnqueueMeasurement(index, buffer);
  1216. #endif
  1217. #if DOMOTICZ_SUPPORT
  1218. {
  1219. char key[15];
  1220. snprintf_P(key, sizeof(key), PSTR("dczMagnitude%d"), index);
  1221. if (magnitude.type == MAGNITUDE_HUMIDITY) {
  1222. int status;
  1223. if (value > 70) {
  1224. status = HUMIDITY_WET;
  1225. } else if (value > 45) {
  1226. status = HUMIDITY_COMFORTABLE;
  1227. } else if (value > 30) {
  1228. status = HUMIDITY_NORMAL;
  1229. } else {
  1230. status = HUMIDITY_DRY;
  1231. }
  1232. char status_buf[5];
  1233. itoa(status, status_buf, 10);
  1234. domoticzSend(key, buffer, status_buf);
  1235. } else {
  1236. domoticzSend(key, 0, buffer);
  1237. }
  1238. }
  1239. #endif // DOMOTICZ_SUPPORT
  1240. }
  1241. // -----------------------------------------------------------------------------
  1242. // Public
  1243. // -----------------------------------------------------------------------------
  1244. unsigned char sensorCount() {
  1245. return _sensors.size();
  1246. }
  1247. unsigned char magnitudeCount() {
  1248. return _magnitudes.size();
  1249. }
  1250. String magnitudeName(unsigned char index) {
  1251. if (index < _magnitudes.size()) {
  1252. sensor_magnitude_t magnitude = _magnitudes[index];
  1253. return magnitude.sensor->slot(magnitude.local);
  1254. }
  1255. return String();
  1256. }
  1257. unsigned char magnitudeType(unsigned char index) {
  1258. if (index < _magnitudes.size()) {
  1259. return int(_magnitudes[index].type);
  1260. }
  1261. return MAGNITUDE_NONE;
  1262. }
  1263. double magnitudeValue(unsigned char index) {
  1264. if (index < _magnitudes.size()) {
  1265. return _sensor_realtime ? _magnitudes[index].last : _magnitudes[index].reported;
  1266. }
  1267. return DBL_MIN;
  1268. }
  1269. unsigned char magnitudeIndex(unsigned char index) {
  1270. if (index < _magnitudes.size()) {
  1271. return int(_magnitudes[index].global);
  1272. }
  1273. return 0;
  1274. }
  1275. String magnitudeTopic(unsigned char type) {
  1276. char buffer[16] = {0};
  1277. if (type < MAGNITUDE_MAX) strncpy_P(buffer, magnitude_topics[type], sizeof(buffer));
  1278. return String(buffer);
  1279. }
  1280. String magnitudeTopicIndex(unsigned char index) {
  1281. char topic[32] = {0};
  1282. if (index < _magnitudes.size()) {
  1283. sensor_magnitude_t magnitude = _magnitudes[index];
  1284. if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
  1285. snprintf(topic, sizeof(topic), "%s/%u", magnitudeTopic(magnitude.type).c_str(), magnitude.global);
  1286. } else {
  1287. snprintf(topic, sizeof(topic), "%s", magnitudeTopic(magnitude.type).c_str());
  1288. }
  1289. }
  1290. return String(topic);
  1291. }
  1292. String magnitudeUnits(unsigned char type) {
  1293. char buffer[8] = {0};
  1294. if (type < MAGNITUDE_MAX) {
  1295. if ((type == MAGNITUDE_TEMPERATURE) && (_sensor_temperature_units == TMP_FAHRENHEIT)) {
  1296. strncpy_P(buffer, magnitude_fahrenheit, sizeof(buffer));
  1297. } else if (
  1298. (type == MAGNITUDE_ENERGY || type == MAGNITUDE_ENERGY_DELTA) &&
  1299. (_sensor_energy_units == ENERGY_KWH)) {
  1300. strncpy_P(buffer, magnitude_kwh, sizeof(buffer));
  1301. } else if (
  1302. (type == MAGNITUDE_POWER_ACTIVE || type == MAGNITUDE_POWER_APPARENT || type == MAGNITUDE_POWER_REACTIVE) &&
  1303. (_sensor_power_units == POWER_KILOWATTS)) {
  1304. strncpy_P(buffer, magnitude_kw, sizeof(buffer));
  1305. } else {
  1306. strncpy_P(buffer, magnitude_units[type], sizeof(buffer));
  1307. }
  1308. }
  1309. return String(buffer);
  1310. }
  1311. // -----------------------------------------------------------------------------
  1312. void sensorSetup() {
  1313. // Backwards compatibility
  1314. moveSetting("powerUnits", "pwrUnits");
  1315. moveSetting("energyUnits", "eneUnits");
  1316. // Update PZEM004T energy total across multiple devices
  1317. moveSettings("pzEneTotal", "pzemEneTotal");
  1318. // Load sensors
  1319. _sensorLoad();
  1320. _sensorInit();
  1321. // Configure stored values
  1322. _sensorConfigure();
  1323. // Websockets
  1324. #if WEB_SUPPORT
  1325. wsOnSendRegister(_sensorWebSocketStart);
  1326. wsOnReceiveRegister(_sensorWebSocketOnReceive);
  1327. wsOnSendRegister(_sensorWebSocketSendData);
  1328. #endif
  1329. // API
  1330. #if API_SUPPORT
  1331. _sensorAPISetup();
  1332. #endif
  1333. // Terminal
  1334. #if TERMINAL_SUPPORT
  1335. _sensorInitCommands();
  1336. #endif
  1337. // Main callbacks
  1338. espurnaRegisterLoop(sensorLoop);
  1339. espurnaRegisterReload(_sensorConfigure);
  1340. }
  1341. void sensorLoop() {
  1342. // Check if we still have uninitialized sensors
  1343. static unsigned long last_init = 0;
  1344. if (!_sensors_ready) {
  1345. if (millis() - last_init > SENSOR_INIT_INTERVAL) {
  1346. last_init = millis();
  1347. _sensorInit();
  1348. }
  1349. }
  1350. if (_magnitudes.size() == 0) return;
  1351. // Tick hook
  1352. _sensorTick();
  1353. // Check if we should read new data
  1354. static unsigned long last_update = 0;
  1355. static unsigned long report_count = 0;
  1356. if (millis() - last_update > _sensor_read_interval) {
  1357. last_update = millis();
  1358. report_count = (report_count + 1) % _sensor_report_every;
  1359. double value_raw; // holds the raw value as the sensor returns it
  1360. double value_show; // holds the processed value applying units and decimals
  1361. double value_filtered; // holds the processed value applying filters, and the units and decimals
  1362. // Pre-read hook
  1363. _sensorPre();
  1364. // Get the first relay state
  1365. #if SENSOR_POWER_CHECK_STATUS
  1366. bool relay_off = (relayCount() == 1) && (relayStatus(0) == 0);
  1367. #endif
  1368. // Get readings
  1369. for (unsigned char i=0; i<_magnitudes.size(); i++) {
  1370. sensor_magnitude_t magnitude = _magnitudes[i];
  1371. if (magnitude.sensor->status()) {
  1372. // -------------------------------------------------------------
  1373. // Instant value
  1374. // -------------------------------------------------------------
  1375. value_raw = magnitude.sensor->value(magnitude.local);
  1376. // Completely remove spurious values if relay is OFF
  1377. #if SENSOR_POWER_CHECK_STATUS
  1378. if (relay_off) {
  1379. if (magnitude.type == MAGNITUDE_POWER_ACTIVE ||
  1380. magnitude.type == MAGNITUDE_POWER_REACTIVE ||
  1381. magnitude.type == MAGNITUDE_POWER_APPARENT ||
  1382. magnitude.type == MAGNITUDE_CURRENT ||
  1383. magnitude.type == MAGNITUDE_ENERGY_DELTA
  1384. ) {
  1385. value_raw = 0;
  1386. }
  1387. }
  1388. #endif
  1389. _magnitudes[i].last = value_raw;
  1390. // -------------------------------------------------------------
  1391. // Processing (filters)
  1392. // -------------------------------------------------------------
  1393. magnitude.filter->add(value_raw);
  1394. // Special case for MovingAverageFilter
  1395. if (MAGNITUDE_COUNT == magnitude.type ||
  1396. MAGNITUDE_GEIGER_CPM ==magnitude. type ||
  1397. MAGNITUDE_GEIGER_SIEVERT == magnitude.type) {
  1398. value_raw = magnitude.filter->result();
  1399. }
  1400. // -------------------------------------------------------------
  1401. // Procesing (units and decimals)
  1402. // -------------------------------------------------------------
  1403. value_show = _magnitudeProcess(magnitude.type, magnitude.decimals, value_raw);
  1404. // -------------------------------------------------------------
  1405. // Debug
  1406. // -------------------------------------------------------------
  1407. #if SENSOR_DEBUG
  1408. {
  1409. char buffer[64];
  1410. dtostrf(value_show, 1-sizeof(buffer), magnitude.decimals, buffer);
  1411. DEBUG_MSG_P(PSTR("[SENSOR] %s - %s: %s%s\n"),
  1412. magnitude.sensor->slot(magnitude.local).c_str(),
  1413. magnitudeTopic(magnitude.type).c_str(),
  1414. buffer,
  1415. magnitudeUnits(magnitude.type).c_str()
  1416. );
  1417. }
  1418. #endif // SENSOR_DEBUG
  1419. // -------------------------------------------------------------
  1420. // Report
  1421. // (we do it every _sensor_report_every readings)
  1422. // -------------------------------------------------------------
  1423. bool report = (0 == report_count);
  1424. if ((MAGNITUDE_ENERGY == magnitude.type) && (magnitude.max_change > 0)) {
  1425. // for MAGNITUDE_ENERGY, filtered value is last value
  1426. report = (fabs(value_show - magnitude.reported) >= magnitude.max_change);
  1427. } // if ((MAGNITUDE_ENERGY == magnitude.type) && (magnitude.max_change > 0))
  1428. if (report) {
  1429. value_filtered = magnitude.filter->result();
  1430. value_filtered = _magnitudeProcess(magnitude.type, magnitude.decimals, value_filtered);
  1431. magnitude.filter->reset();
  1432. // Check if there is a minimum change threshold to report
  1433. if (fabs(value_filtered - magnitude.reported) >= magnitude.min_change) {
  1434. _magnitudes[i].reported = value_filtered;
  1435. _sensorReport(i, value_filtered);
  1436. } // if (fabs(value_filtered - magnitude.reported) >= magnitude.min_change)
  1437. // Persist total energy value
  1438. if (MAGNITUDE_ENERGY == magnitude.type) {
  1439. _sensorEnergyTotal(value_raw);
  1440. }
  1441. } // if (report_count == 0)
  1442. } // if (magnitude.sensor->status())
  1443. } // for (unsigned char i=0; i<_magnitudes.size(); i++)
  1444. // Post-read hook
  1445. _sensorPost();
  1446. #if WEB_SUPPORT
  1447. wsSend(_sensorWebSocketSendData);
  1448. #endif
  1449. #if THINGSPEAK_SUPPORT
  1450. if (report_count == 0) tspkFlush();
  1451. #endif
  1452. }
  1453. }
  1454. #endif // SENSOR_SUPPORT