Room climate sensor node (temperature, humidity) using ESP32 and SI7021 powered by a 18650 LiIon battery.
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.

si7021.cpp 2.8KB

  1. /*
  2. * si7021 library
  3. * SI7021 Temperature and Humidity Sensor @ ESP32 i2c
  4. *
  5. * copyright: Jannik Beyerstedt | http://jannikbeyerstedt.de | code@jannikbeyerstedt.de
  6. * license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
  7. */
  8. #include <Arduino.h>
  9. #include <Wire.h>
  10. #include "si7021.hpp"
  11. /**
  12. * Constuctor of the SI7021 sensor wrapper
  13. */
  14. Si7021::Si7021(void) {
  15. _i2caddr = SI7021_DEFAULT_ADDRESS;
  16. }
  17. /**
  18. * Start the I2C interface
  19. * Attention: leave at least 25 ms power up time before the first measurement
  20. */
  21. bool Si7021::begin(void) {
  22. Wire.begin();
  23. Wire.setClock(200000);
  24. reset();
  25. if (readRegister8(SI7021_READRHT_REG_CMD) != 0x3A) {
  26. return false;
  27. }
  28. return true;
  29. }
  30. /**
  31. * Send a reset command to the sensor
  32. */
  33. void Si7021::reset(void) {
  34. Wire.beginTransmission(_i2caddr);
  35. Wire.write((uint8_t)SI7021_RESET_CMD);
  36. Wire.endTransmission(true);
  37. delay(10); // SI7021 reset time
  38. }
  39. /**
  40. * Initiate a humidity and temperature measurement
  41. * Get the values from the public variables temperature and humudity
  42. *
  43. * @return true, if successful measurement
  44. */
  45. bool Si7021::measure(void) {
  46. uint8_t chxsum;
  47. uint8_t cnt;
  48. // TRIGGER HUMIDITY MEASUREMENT AND GET VALUE
  49. Wire.beginTransmission(_i2caddr);
  50. Wire.write((uint8_t)SI7021_MEASRH_NOHOLD_CMD);
  51. Wire.endTransmission(true); // true, otherwise the read will not work
  52. delay(23); // wait for the Conversion Time of the Sensor
  53. cnt = Wire.requestFrom(_i2caddr, 3);
  54. if (cnt < 3) {
  55. // Serial.printf("[ERROR] SI7021 humi, only %d bytes received\n", cnt);
  56. return false;
  57. }
  58. uint16_t hum_raw = Wire.read();
  59. hum_raw <<= 8;
  60. hum_raw |= Wire.read();
  61. chxsum = Wire.read();
  62. this->humidity = hum_raw;
  63. this->humidity *= 125;
  64. this->humidity /= 65536;
  65. this->humidity -= 6;
  66. // GET TEMPERATURE VALUE OF PREVIOUS HUMIDITY MEASUREMENT
  67. Wire.beginTransmission(_i2caddr);
  68. Wire.write((uint8_t)SI7021_READPREVTEMP_CMD);
  69. Wire.endTransmission(true); // true, otherwise the read will not work
  70. cnt = Wire.requestFrom(_i2caddr, 3);
  71. if (cnt < 3) {
  72. // Serial.printf("[ERROR] SI7021 temp, only %d bytes received\n", cnt);
  73. return false;
  74. }
  75. uint16_t temp_raw = Wire.read();
  76. temp_raw <<= 8;
  77. temp_raw |= Wire.read();
  78. chxsum = Wire.read();
  79. this->temperature = temp_raw;
  80. this->temperature *= 175.72;
  81. this->temperature /= 65536;
  82. this->temperature -= 46.85;
  83. return true;
  84. }
  85. // void Si7021::writeRegister8(uint8_t reg, uint8_t value) {
  86. // // TODO: Test
  87. // Wire.beginTransmission(_i2caddr);
  88. // Wire.write((uint8_t)reg);
  89. // Wire.write((uint8_t)value);
  90. // Wire.endTransmission();
  91. // }
  92. uint8_t Si7021::readRegister8(uint8_t reg) {
  93. uint8_t value;
  94. Wire.beginTransmission(_i2caddr);
  95. Wire.write((uint8_t)reg);
  96. Wire.endTransmission(true); // true, otherwise the value does not get read
  97. Wire.requestFrom(_i2caddr, 1);
  98. value = Wire.read();
  99. return value;
  100. }