xsns_16_tsl2561.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. xsns_16_tsl2561.ino - TSL2561 light sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends and Joachim Banzhaf
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_I2C
  16. #ifdef USE_TSL2561
  17. /*********************************************************************************************\
  18. * TSL2561 - Light Intensity
  19. *
  20. * Using library https://github.com/joba-1/Joba_Tsl2561
  21. *
  22. * I2C Addresses: 0x29 (low), 0x39 (float) or 0x49 (high)
  23. \*********************************************************************************************/
  24. #define XSNS_16 16
  25. #include <Tsl2561Util.h>
  26. Tsl2561 Tsl(Wire);
  27. uint8_t tsl2561_type = 0;
  28. uint8_t tsl2561_valid = 0;
  29. uint32_t tsl2561_milliLux = 0;
  30. char tsl2561_types[] = "TSL2561";
  31. bool Tsl2561Read(void)
  32. {
  33. if (tsl2561_valid) { tsl2561_valid--; }
  34. uint8_t id;
  35. bool gain;
  36. Tsl2561::exposure_t exposure;
  37. uint16_t scaledFull, scaledIr;
  38. uint32_t full, ir;
  39. if (Tsl.available()) {
  40. if (Tsl.on()) {
  41. if (Tsl.id(id)
  42. && Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr)
  43. && Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr)
  44. && Tsl2561Util::milliLux(full, ir, tsl2561_milliLux, Tsl2561::packageCS(id))) {
  45. } else{
  46. tsl2561_milliLux = 0;
  47. }
  48. }
  49. }
  50. tsl2561_valid = SENSOR_MAX_MISS;
  51. return true;
  52. }
  53. void Tsl2561Detect(void)
  54. {
  55. if (tsl2561_type) { return; }
  56. if (!Tsl.available()) {
  57. Tsl.begin();
  58. if (Tsl.available()) {
  59. tsl2561_type = 1;
  60. snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address());
  61. AddLog(LOG_LEVEL_DEBUG);
  62. }
  63. }
  64. }
  65. void Tsl2561EverySecond(void)
  66. {
  67. if (90 == (uptime %100)) {
  68. // 1mS
  69. Tsl2561Detect();
  70. }
  71. else if (!(uptime %2)) { // Update every 2 seconds
  72. // ?mS - 4Sec
  73. if (tsl2561_type) {
  74. if (!Tsl2561Read()) {
  75. AddLogMissed(tsl2561_types, tsl2561_valid);
  76. // if (!tsl2561_valid) { tsl2561_type = 0; }
  77. }
  78. }
  79. }
  80. }
  81. #ifdef USE_WEBSERVER
  82. const char HTTP_SNS_TSL2561[] PROGMEM =
  83. "%s{s}TSL2561 " D_ILLUMINANCE "{m}%u.%03u " D_UNIT_LUX "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
  84. #endif // USE_WEBSERVER
  85. void Tsl2561Show(boolean json)
  86. {
  87. if (tsl2561_valid) {
  88. if (json) {
  89. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u}"),
  90. mqtt_data, tsl2561_milliLux / 1000, tsl2561_milliLux % 1000);
  91. #ifdef USE_DOMOTICZ
  92. if (0 == tele_period) { DomoticzSensor(DZ_ILLUMINANCE, (tsl2561_milliLux + 500) / 1000); }
  93. #endif // USE_DOMOTICZ
  94. #ifdef USE_WEBSERVER
  95. } else {
  96. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TSL2561, mqtt_data, tsl2561_milliLux / 1000, tsl2561_milliLux % 1000);
  97. #endif // USE_WEBSERVER
  98. }
  99. }
  100. }
  101. /*********************************************************************************************\
  102. * Interface
  103. \*********************************************************************************************/
  104. boolean Xsns16(byte function)
  105. {
  106. boolean result = false;
  107. if (i2c_flg) {
  108. switch (function) {
  109. case FUNC_INIT:
  110. Tsl2561Detect();
  111. break;
  112. case FUNC_EVERY_SECOND:
  113. Tsl2561EverySecond();
  114. break;
  115. case FUNC_JSON_APPEND:
  116. Tsl2561Show(1);
  117. break;
  118. #ifdef USE_WEBSERVER
  119. case FUNC_WEB_APPEND:
  120. Tsl2561Show(0);
  121. break;
  122. #endif // USE_WEBSERVER
  123. }
  124. }
  125. return result;
  126. }
  127. #endif // USE_TSL2561
  128. #endif // USE_I2C