xsns_26_lm75ad.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. xsns_26_lm75ad.ino - Support for I2C LM75AD Temperature Sensor
  3. Copyright (C) 2018 Andre Thomas and 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_LM75AD
  17. /*********************************************************************************************\
  18. * LM75AD - Temperature
  19. *
  20. * Docs at https://www.nxp.com/docs/en/data-sheet/LM75A.pdf
  21. *
  22. * I2C Address: 0x48 - 0x4F
  23. \*********************************************************************************************/
  24. #define XSNS_26 26
  25. #define LM75AD_ADDRESS1 0x48
  26. #define LM75AD_ADDRESS2 0x49
  27. #define LM75AD_ADDRESS3 0x4A
  28. #define LM75AD_ADDRESS4 0x4B
  29. #define LM75AD_ADDRESS5 0x4C
  30. #define LM75AD_ADDRESS6 0x4D
  31. #define LM75AD_ADDRESS7 0x4E
  32. #define LM75AD_ADDRESS8 0x4F
  33. #define LM75_TEMP_REGISTER 0x00
  34. #define LM75_CONF_REGISTER 0x01
  35. #define LM75_THYST_REGISTER 0x02
  36. #define LM75_TOS_REGISTER 0x03
  37. uint8_t lm75ad_type = 0;
  38. uint8_t lm75ad_address;
  39. uint8_t lm75ad_addresses[] = { LM75AD_ADDRESS1, LM75AD_ADDRESS2, LM75AD_ADDRESS3, LM75AD_ADDRESS4, LM75AD_ADDRESS5, LM75AD_ADDRESS6, LM75AD_ADDRESS7, LM75AD_ADDRESS8 };
  40. void LM75ADDetect(void)
  41. {
  42. if (lm75ad_type) { return; }
  43. uint16_t buffer;
  44. for (byte i = 0; i < sizeof(lm75ad_addresses); i++) {
  45. lm75ad_address = lm75ad_addresses[i];
  46. if (I2cValidRead16(&buffer, lm75ad_address, LM75_THYST_REGISTER)) {
  47. if (buffer == 0x4B00) {
  48. lm75ad_type = 1;
  49. snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "LM75AD", lm75ad_address);
  50. AddLog(LOG_LEVEL_DEBUG);
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. float LM75ADGetTemp(void) {
  57. int16_t sign = 1;
  58. uint16_t t = I2cRead16(lm75ad_address, LM75_TEMP_REGISTER);
  59. if (t & 0x8000) { // we are getting a negative temperature value
  60. t = (~t) +0x20;
  61. sign = -1;
  62. }
  63. t = t >> 5; // shift value into place (5 LSB not used)
  64. return ConvertTemp(sign * t * 0.125);
  65. }
  66. void LM75ADShow(boolean json)
  67. {
  68. if (lm75ad_type) {
  69. float t = LM75ADGetTemp();
  70. char temperature[33];
  71. dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
  72. if (json) {
  73. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"LM75AD\":{\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, temperature);
  74. #ifdef USE_DOMOTICZ
  75. if (0 == tele_period) DomoticzSensor(DZ_TEMP, temperature);
  76. #endif // USE_DOMOTICZ
  77. #ifdef USE_WEBSERVER
  78. } else {
  79. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, "LM75AD", temperature, TempUnit());
  80. #endif // USE_WEBSERVER
  81. }
  82. }
  83. }
  84. /*********************************************************************************************\
  85. * Interface
  86. \*********************************************************************************************/
  87. boolean Xsns26(byte function)
  88. {
  89. boolean result = false;
  90. if (i2c_flg) {
  91. switch (function) {
  92. case FUNC_EVERY_SECOND:
  93. LM75ADDetect();
  94. break;
  95. case FUNC_JSON_APPEND:
  96. LM75ADShow(1);
  97. break;
  98. #ifdef USE_WEBSERVER
  99. case FUNC_WEB_APPEND:
  100. LM75ADShow(0);
  101. break;
  102. #endif // USE_WEBSERVER
  103. }
  104. }
  105. return result;
  106. }
  107. #endif // USE_LM75AD
  108. #endif // USE_I2C