xsns_10_bh1750.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. xsns_10_bh1750.ino - BH1750 ambient light sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends
  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_BH1750
  17. /*********************************************************************************************\
  18. * BH1750 - Ambient Light Intensity
  19. *
  20. * I2C Address: 0x23 or 0x5C
  21. \*********************************************************************************************/
  22. #define XSNS_10 10
  23. #define BH1750_ADDR1 0x23
  24. #define BH1750_ADDR2 0x5C
  25. #define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  26. uint8_t bh1750_address;
  27. uint8_t bh1750_addresses[] = { BH1750_ADDR1, BH1750_ADDR2 };
  28. uint8_t bh1750_type = 0;
  29. uint8_t bh1750_valid = 0;
  30. uint16_t bh1750_illuminance = 0;
  31. char bh1750_types[] = "BH1750";
  32. bool Bh1750Read(void)
  33. {
  34. if (bh1750_valid) { bh1750_valid--; }
  35. if (2 != Wire.requestFrom(bh1750_address, (uint8_t)2)) { return false; }
  36. byte msb = Wire.read();
  37. byte lsb = Wire.read();
  38. bh1750_illuminance = ((msb << 8) | lsb) / 1.2;
  39. bh1750_valid = SENSOR_MAX_MISS;
  40. return true;
  41. }
  42. /********************************************************************************************/
  43. void Bh1750Detect(void)
  44. {
  45. if (bh1750_type) {
  46. return;
  47. }
  48. for (byte i = 0; i < sizeof(bh1750_addresses); i++) {
  49. bh1750_address = bh1750_addresses[i];
  50. Wire.beginTransmission(bh1750_address);
  51. Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE);
  52. if (!Wire.endTransmission()) {
  53. bh1750_type = 1;
  54. snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, bh1750_types, bh1750_address);
  55. AddLog(LOG_LEVEL_DEBUG);
  56. break;
  57. }
  58. }
  59. }
  60. void Bh1750EverySecond(void)
  61. {
  62. if (90 == (uptime %100)) {
  63. // 1mS
  64. Bh1750Detect();
  65. }
  66. else {
  67. // 1mS
  68. if (bh1750_type) {
  69. if (!Bh1750Read()) {
  70. AddLogMissed(bh1750_types, bh1750_valid);
  71. // if (!bh1750_valid) { bh1750_type = 0; }
  72. }
  73. }
  74. }
  75. }
  76. void Bh1750Show(boolean json)
  77. {
  78. if (bh1750_valid) {
  79. if (json) {
  80. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_JSON_ILLUMINANCE "\":%d}"), mqtt_data, bh1750_types, bh1750_illuminance);
  81. #ifdef USE_DOMOTICZ
  82. if (0 == tele_period) {
  83. DomoticzSensor(DZ_ILLUMINANCE, bh1750_illuminance);
  84. }
  85. #endif // USE_DOMOTICZ
  86. #ifdef USE_WEBSERVER
  87. } else {
  88. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ILLUMINANCE, mqtt_data, bh1750_types, bh1750_illuminance);
  89. #endif // USE_WEBSERVER
  90. }
  91. }
  92. }
  93. /*********************************************************************************************\
  94. * Interface
  95. \*********************************************************************************************/
  96. boolean Xsns10(byte function)
  97. {
  98. boolean result = false;
  99. if (i2c_flg) {
  100. switch (function) {
  101. case FUNC_INIT:
  102. Bh1750Detect();
  103. break;
  104. case FUNC_EVERY_SECOND:
  105. Bh1750EverySecond();
  106. break;
  107. case FUNC_JSON_APPEND:
  108. Bh1750Show(1);
  109. break;
  110. #ifdef USE_WEBSERVER
  111. case FUNC_WEB_APPEND:
  112. Bh1750Show(0);
  113. break;
  114. #endif // USE_WEBSERVER
  115. }
  116. }
  117. return result;
  118. }
  119. #endif // USE_BH1750
  120. #endif // USE_I2C