https://github.com/endail/si7021
Raspberry Pi Si7021 C++ API
https://github.com/endail/si7021
cpp humidity lgpio raspberry-pi si7021 temperature
Last synced: 8 months ago
JSON representation
Raspberry Pi Si7021 C++ API
- Host: GitHub
- URL: https://github.com/endail/si7021
- Owner: endail
- License: mit
- Created: 2021-09-12T16:01:48.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-11-01T07:22:15.000Z (almost 4 years ago)
- Last Synced: 2024-12-28T01:40:10.940Z (10 months ago)
- Topics: cpp, humidity, lgpio, raspberry-pi, si7021, temperature
- Language: C++
- Homepage:
- Size: 125 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# SI7021
[](https://github.com/endail/SI7021/actions/workflows/buildcheck.yml) [](https://github.com/endail/SI7021/actions/workflows/cppcheck.yml)
- Use with Raspberry Pi
- Requires [lgpio](http://abyz.me.uk/lg/index.html)
- Code tested inside [virtual Raspberry Pi Zero/3/4 environments](.github/workflows/buildcheck.yml) on GitHub## Example
```cpp
#include "SI7021.h"
#includeint main() {
SI7021::SI7021 sensor;
//connect to and initialise the sensor
sensor.connect();//obtain new temperature and humidity readings
sensor.refresh();std::cout
<< "Temperature: "
<< sensor.getTemperature() << " C, "
<< "Humidity: "
<< sensor.getHumidity() << " %RH"
<< std::endl;//prints eg.
//Temperature: 16.0205 C, Humidity: 62.8667 %RHreturn 0;
}
```## Documentation
- `SI7021( int dev, int addr )`. Constructor taking `dev` identifying which I2C device to use (default is device 1), and `addr` identifying which `I2C` address to use (default is 0x40).
- `void connect( )`. Setup and connect to the device.
- `void disconnect( )`. Disconnect from the device.
- `void refresh( )`. Get a humidity and temperature sample from the sensor.
- `double getTemperature( )`. Returns the last read temperature in degrees celsius.
- `double getHumidity( )`. Returns the last read relative humidity as a percentage.
- `void reset( )`. Reset the device.
- `void resetSettings( )`. Reset UserRegister1 register (ie. measurement resolution and heater status) and HeaterControlRegister register (ie. heater power).
- `std::uint8_t getMeasurementResolution( )`. Returns the measurement resolution from the device, between 0 and 3.
- `void setMeasurementResolution( std::uint8_t res )`. Sets the measurement resolution, between 0 and 3.
- `VddStatus getVddStatus( )`. Returns `VddStatus::OK` if voltage level is sufficient, otherwise `VddStatus::LOW`.
- `HeaterStatus getHeaterStatus( )`. Returns `HeaterStatus::ENABLED` if the heater is enabled, or `HeaterStatus::DISABLED` if disabled.
- `void setHeaterStatus( HeaterStatus status )`. Enable or disable the heater.
- `std::uint8_t getHeaterPower( )`. Returns the heater power, between 0 and 15.
- `void setHeaterPower( std::uint8_t power )`. Set the heater power, between 0 and 15.
- `SerialNumber getSerialNumber( )`. Gets the serial number of the device. `SerialNumber` is a typedef for a `std::uint64_t`.
- `DeviceId getDeviceId( )`. Gets the device id.
| identifier | description | devIdToString |
| ------------------------ | ------------------ | ------------------ |
| `DeviceId::ENG_SAMPLE_1` | engineering sample | engineering sample |
| `DeviceId::ENG_SAMPLE_2` | engineering sample | engineering sample |
| `DeviceId::SI7013` | SI7013 | Si7013 |
| `DeviceId::SI7020` | SI7020 | Si7020 |
| `DeviceId::SI7021` | SI7021 | Si7021 |
| `DeviceId::UNKNOWN` | unknown device | unknown |- `FirmwareRevision getFirmwareRevision( )`. Gets the device firmware revision.
| identifier | description | fwRevToString |
| --------------------------- | ---------------- | ------------- |
| `FirmwareRevision::REV_1_0` | revision 1.0 | 1.0 |
| `FirmwareRevision::REV_2_0` | revision 2.0 | 2.0 |
| `FirmwareRevision::UNKNOWN` | unknown firmware | unknown |- `const char* const devIdToString( DeviceId id )`. Returns a C string representing the device id.
- `const char* const fwRevToString( FirmwareRevision rev )`. Returns a C string representing the firmware revision.