Skip to main content

How to: Soil Sensor

Soil Moisture sensor measures moisture and temperature.

References​

info

This is the simplest example with a single connected sensor to the Sensor Module.

Writing a single sensor values to the console Code Example

#include <application.h>

// Soil sensor instance
twr_soil_sensor_t soil_sensor;

void soil_sensor_event_handler(twr_soil_sensor_t *self, uint64_t device_address, twr_soil_sensor_event_t event, void *event_param)
{
if (event == TWR_SOIL_SENSOR_EVENT_UPDATE)
{
uint16_t moisture;
float temperature;

twr_soil_sensor_get_cap_raw(self, device_address, &moisture);
twr_soil_sensor_get_temperature_celsius(self, device_address, &temperature);
twr_log_debug("Moisture: %d\tTemperature %.2f", moisture, temperature);
}
}

void application_init(void)
{
twr_log_init(TWR_LOG_LEVEL_DUMP, TWR_LOG_TIMESTAMP_ABS);

// Initialize soil sensor
twr_soil_sensor_init(&soil_sensor);
twr_soil_sensor_set_event_handler(&soil_sensor, soil_sensor_event_handler, NULL);
twr_soil_sensor_set_update_interval(&soil_sensor, 1000);
}
info

When you connect multiple sensors, you need to initialize them with twr_soil_sensor_init_multiple. In the event handler, you then get the device_address in the callback parameter, or you can get the sensor index by calling twr_soil_sensor_get_index_by_device_address().

The example below shows how to work with multiple sensors.

Multiple Connected Sensors Code Example

#include <application.h>

#define MAX_SOIL_SENSORS 5
#define SENSOR_UPDATE_SERVICE_INTERVAL (15 * 60 * 1000)

// Soil sensor instance
twr_soil_sensor_t soil_sensor;

// Sensors array
twr_soil_sensor_sensor_t sensors[MAX_SOIL_SENSORS];

void soil_sensor_event_handler(twr_soil_sensor_t *self, uint64_t device_address, twr_soil_sensor_event_t event, void *event_param)
{
if (event == TWR_SOIL_SENSOR_EVENT_UPDATE)
{
int index = twr_soil_sensor_get_index_by_device_address(self, device_address);
uint16_t moisture;

if (twr_soil_sensor_get_cap_raw(self, device_address, &moisture))
{
twr_log_debug("Sensor %d: moisture %d", index, moisture);
}
}
}

void application_init(void)
{
twr_log_init(TWR_LOG_LEVEL_DUMP, TWR_LOG_TIMESTAMP_ABS);

// Initialize soil sensors
twr_soil_sensor_init_multiple(&soil_sensor, sensors, MAX_SOIL_SENSORS);
twr_soil_sensor_set_event_handler(&soil_sensor, soil_sensor_event_handler, NULL);
twr_soil_sensor_set_update_interval(&soil_sensor, SENSOR_UPDATE_SERVICE_INTERVAL);

}

Moisture in percent​

twr_soil_sensor_get_moisture() converts the raw value to percent with 11 calibration points (0, 10, …, 100 %) stored in the EEPROM of the sensor, linearly between them. In production, each sensor is calibrated at two points, dry and in water; the 11 points are there for a finer calibration. Field tests showed that 0 to 100 % is often not practical, so for most applications read the raw value with twr_soil_sensor_get_cap_raw() and convert it in your application.

To calibrate a sensor, note its raw value dry (in the air) and wet (in water up to the start of the heat-shrink tube), spread the 11 points between them and save them to the sensor:

void soil_sensor_calibrate(uint64_t device_address, uint16_t dry, uint16_t wet)
{
for (int point = 0; point <= 100; point += 10)
{
uint16_t value = dry + (uint32_t) (wet - dry) * point / 100;
twr_soil_sensor_calibration_set_point(&soil_sensor, device_address, point, value);
}

twr_soil_sensor_eeprom_save(&soil_sensor, device_address);
}

Sensor address​

Each sensor has a 64-bit address: the event handler gets it as device_address. Outside the handler, initialize the driver with twr_soil_sensor_init_multiple() (an array of one sensor is enough) and call twr_soil_sensor_get_device_address_by_index(). The SDK searches the bus only after the application has started, so the address is there from the first measurement on. Print it with the PRIx64 format.