Initial driver for HTU21DF
This commit is contained in:
25
src/main.c
25
src/main.c
@ -2,6 +2,7 @@
|
||||
#include "mgos_i2c.h"
|
||||
#include "mgos_sht31.h"
|
||||
#include "mgos_si7021.h"
|
||||
#include "mgos_htu21df.h"
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
@ -63,10 +64,23 @@ bool do_si7021(struct mgos_si7021 *sensor) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool do_htu21df(struct mgos_htu21df *sensor) {
|
||||
float temp, humid;
|
||||
|
||||
if (!sensor) return false;
|
||||
|
||||
temp=mgos_htu21df_getTemperature(sensor);
|
||||
humid=mgos_htu21df_getHumidity(sensor);
|
||||
LOG(LL_INFO, ("temperature=%.2fC humidity=%.1f%%", temp, humid));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct mgos_i2c *i2c;
|
||||
struct mgos_si7021 *si7021;
|
||||
struct mgos_sht31 *sht31;
|
||||
struct mgos_htu21df *htu21df;
|
||||
|
||||
if (!mgos_i2c_open(I2CBUSNR)) {
|
||||
LOG(LL_ERROR, ("Cannot open I2C bus %u", I2CBUSNR));
|
||||
@ -79,22 +93,25 @@ int main() {
|
||||
|
||||
i2c_scanner(i2c);
|
||||
|
||||
if (!(sht31 = mgos_sht31_create(i2c, 0x44))) {
|
||||
if (!(sht31 = mgos_sht31_create(i2c, 0x44)))
|
||||
LOG(LL_ERROR, ("Cannot create SHT31 device"));
|
||||
}
|
||||
|
||||
if (!(si7021 = mgos_si7021_create(i2c, 0x40))) {
|
||||
if (!(si7021 = mgos_si7021_create(i2c, 0x40)))
|
||||
LOG(LL_ERROR, ("Cannot create SI7021 device"));
|
||||
}
|
||||
|
||||
if (!(htu21df = mgos_htu21df_create(i2c, 0x40)))
|
||||
LOG(LL_ERROR, ("Cannot create HTU21DF device"));
|
||||
|
||||
for (;;) {
|
||||
do_sht31(sht31);
|
||||
do_si7021(si7021);
|
||||
do_htu21df(htu21df);
|
||||
sleep(5);
|
||||
}
|
||||
|
||||
mgos_sht31_destroy(&sht31);
|
||||
mgos_si7021_destroy(&si7021);
|
||||
mgos_htu21df_destroy(&htu21df);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user