1 /*
2 * Copyright (c) 2025 Antmicro <www.antmicro.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <stdio.h>
11
12 #include <zephyr/drivers/sensor/sht4x.h>
13
14 #if !DT_HAS_COMPAT_STATUS_OKAY(sensirion_sht4x)
15 #error "No sensirion,sht4x compatible node found in the device tree"
16 #endif
17
main(void)18 int main(void)
19 {
20 const struct device *const sht = DEVICE_DT_GET_ANY(sensirion_sht4x);
21 struct sensor_value temp, hum;
22
23 if (!device_is_ready(sht)) {
24 printf("Device %s is not ready.\n", sht->name);
25 return 0;
26 }
27
28 #if CONFIG_APP_USE_HEATER
29 struct sensor_value heater_p;
30 struct sensor_value heater_d;
31
32 heater_p.val1 = CONFIG_APP_HEATER_PULSE_POWER;
33 heater_d.val1 = !!CONFIG_APP_HEATER_PULSE_DURATION_LONG;
34 sensor_attr_set(sht, SENSOR_CHAN_ALL, SENSOR_ATTR_SHT4X_HEATER_POWER, &heater_p);
35 sensor_attr_set(sht, SENSOR_CHAN_ALL, SENSOR_ATTR_SHT4X_HEATER_DURATION, &heater_d);
36 #endif
37
38 while (true) {
39 if (sensor_sample_fetch(sht)) {
40 printf("Failed to fetch sample from SHT4X device\n");
41 return 0;
42 }
43
44 sensor_channel_get(sht, SENSOR_CHAN_AMBIENT_TEMP, &temp);
45 sensor_channel_get(sht, SENSOR_CHAN_HUMIDITY, &hum);
46
47 #if CONFIG_APP_USE_HEATER
48 /*
49 * Conditions in which it makes sense to activate the heater
50 * are application/environment specific.
51 *
52 * The heater should not be used above SHT4X_HEATER_MAX_TEMP (65 °C)
53 * as stated in the datasheet.
54 *
55 * The temperature data will not be updated here for obvious reasons.
56 **/
57 if (hum.val1 > CONFIG_APP_HEATER_HUMIDITY_THRESH &&
58 temp.val1 < SHT4X_HEATER_MAX_TEMP) {
59 printf("Activating heater.\n");
60
61 if (sht4x_fetch_with_heater(sht)) {
62 printf("Failed to fetch sample from SHT4X device\n");
63 return 0;
64 }
65
66 sensor_channel_get(sht, SENSOR_CHAN_HUMIDITY, &hum);
67 }
68 #endif
69
70 printf("SHT4X: %.2f Temp. [C] ; %0.2f RH [%%]\n", sensor_value_to_double(&temp),
71 sensor_value_to_double(&hum));
72
73 #if CONFIG_APP_USE_HEATER && !CONFIG_APP_HEATER_PULSE_DURATION
74 k_sleep(K_MSEC(20000));
75 #else
76 k_sleep(K_MSEC(2000));
77 #endif
78 }
79 }
80