Github Repo | C Header | C source | JS source |
---|---|---|---|
mongoose-os-libs/si7021-i2c | mgos_si7021.h |
A Mongoose library for Silicon Si7021 integrated circuit.
The Si7021 I2C Humidity and Temperature Sensor is a monolithic CMOS IC integrating humidity and temperature sensor elements, an analog-to-digital converter, signal processing, calibration data, and an I2C Interface. The patented use of industry-standard, low-K polymeric dielectrics for sensing humidity enables the construction of low-power, monolithic CMOS Sensor ICs with low drift and hysteresis, and excellent long term stability.
The humidity and temperature sensors are factory-calibrated and the calibration data is stored in the on-chip non-volatile memory. This ensures that the sensors are fully interchangeable, with no recalibration or software changes required.
The Si7021 is available in a 3x3 mm DFN package and is reflow solderable. It can be used as a hardware- and software-compatible drop-in upgrade for existing RH/ temperature sensors in 3x3 mm DFN-6 packages, featuring precision sensing over a wider range and lower power consumption. The optional factory-installed cover offers a low profile, convenient means of protecting the sensor during assembly (e.g., reflow soldering) and throughout the life of the product, excluding liquids (hydrophobic/oleophobic) and particulates.
The Si7021 offers an accurate, low-power, factory-calibrated digital solution ideal for measuring humidity, dew-point, and temperature, in applications ranging from HVAC/R and asset tracking to industrial and consumer platforms.
See datasheet for implementation details.
A great place to buy a ready made and tested unit is at Adafruit.
An example program using a timer to read data from the sensor every 5 seconds:
#include "mgos.h"
#include "mgos_i2c.h"
#include "mgos_si7021.h"
static struct mgos_si7021 *s_si7021;
static void timer_cb(void *user_data) {
float temperature, humidity;
temperature=mgos_si7021_getTemperature(s_si7021);
humidity=mgos_si7021_getHumidity(s_si7021);
LOG(LL_INFO, ("si7021 temperature=%.2f humidity=%.2f", temperature, humidity));
(void) user_data;
}
enum mgos_app_init_result mgos_app_init(void) {
struct mgos_i2c *i2c;
i2c=mgos_i2c_get_global();
if (!i2c) {
LOG(LL_ERROR, ("I2C bus missing, set i2c.enable=true in mos.yml"));
} else {
s_si7021=mgos_si7021_create(i2c, 0x40); // Default I2C address
if (s_si7021) {
mgos_set_timer(5000, true, timer_cb, NULL);
} else {
LOG(LL_ERROR, ("Could not initialize sensor"));
}
}
return MGOS_APP_INIT_SUCCESS;
}
This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.
uint32_t read; // calls to _read()
uint32_t read_success; // successful _read()
uint32_t read_success_cached; // calls to _read() which were cached
// Note: read_errors := read - read_success - read_success_cached
double read_success_usecs; // time spent in successful uncached _read()
};
value of mg_time() upon last call to _read()
struct mgos_si7021 *mgos_si7021_create(struct mgos_i2c *i2c, uint8_t i2caddr);
Initialize a Si7021 on the I2C bus
i2c
at address specified ini2caddr
parameter (default Si7021 is on address 0x40). The sensor will be polled for validity, upon success a newstruct mgos_si7021
is allocated and returned. If the device could not be found, NULL is returned.
void mgos_si7021_destroy(struct mgos_si7021 **sensor);
Destroy the data structure associated with a Si7021 device. The reference to the pointer of the
struct mgos_si7021
has to be provided, and upon successful destruction, its associated memory will be freed and the pointer set to NULL.
bool mgos_si7021_read(struct mgos_si7021 *sensor);
The sensor will be polled for its temperature and humidity data. If the poll has occured in the last
MGOS_SI7021_READ_DELAY
seconds, the cached data is used (so as not to repeatedly poll the bus upon subsequent calls).
float mgos_si7021_getTemperature(struct mgos_si7021 *sensor);
The sensor will be polled for its temperature and humidity data. If the poll has occured in the last
MGOS_SI7021_READ_DELAY
seconds, the cached data is used (so as not to repeatedly poll the bus upon subsequent calls).The return value is the temperature of the sensor in Celsius, or NAN if no data was found.
float mgos_si7021_getHumidity(struct mgos_si7021 *sensor);
The sensor will be polled for its temperature and humidity data. If the poll has occured in the last
MGOS_SI7021_READ_DELAY
seconds, the cached data is used (so as not to repeatedly poll the bus upon subsequent calls).The return value is the humidity of the sensor in percent relative humidity, or NAN if no data was found.
bool mgos_si7021_getStats(struct mgos_si7021 *sensor, struct mgos_si7021_stats *stats);
Returns the running statistics on the sensor interaction, the user provides a pointer to a
struct mgos_si7021_stats
object, which is filled in by this call.Upon success, true is returned. Otherwise, false is returned, in which case the contents of
stats
is undetermined.
bool mgos_si7021_i2c_init(void);
edit this docInitialization function for MGOS -- currently a noop.