1 /*
2  * Copyright (c) 2021 Leonard Pollak
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/sgp40.h>
13 #include <zephyr/drivers/sensor/sht4x.h>
14 
15 #if !DT_HAS_COMPAT_STATUS_OKAY(sensirion_sgp40)
16 #error "No sensirion,sgp40 compatible node found in the device tree"
17 #endif
18 
19 #if !DT_HAS_COMPAT_STATUS_OKAY(sensirion_sht4x)
20 #error "No sensirion,sht4x compatible node found in the device tree"
21 #endif
22 
main(void)23 int main(void)
24 {
25 
26 #if CONFIG_APP_USE_COMPENSATION
27 	struct sensor_value comp_t;
28 	struct sensor_value comp_rh;
29 #endif
30 	const struct device *const sht = DEVICE_DT_GET_ANY(sensirion_sht4x);
31 	const struct device *const sgp = DEVICE_DT_GET_ANY(sensirion_sgp40);
32 	struct sensor_value temp, hum, gas;
33 
34 	if (!device_is_ready(sht)) {
35 		printf("Device %s is not ready.\n", sht->name);
36 		return 0;
37 	}
38 
39 	if (!device_is_ready(sgp)) {
40 		printf("Device %s is not ready.\n", sgp->name);
41 		return 0;
42 	}
43 
44 #if CONFIG_APP_USE_HEATER
45 	struct sensor_value heater_p;
46 	struct sensor_value heater_d;
47 
48 	heater_p.val1 = CONFIG_APP_HEATER_PULSE_POWER;
49 	heater_d.val1 = CONFIG_APP_HEATER_PULSE_DURATION;
50 	sensor_attr_set(sht, SENSOR_CHAN_ALL,
51 			SENSOR_ATTR_SHT4X_HEATER_POWER, &heater_p);
52 	sensor_attr_set(sht, SENSOR_CHAN_ALL,
53 			SENSOR_ATTR_SHT4X_HEATER_DURATION, &heater_d);
54 #endif
55 
56 	while (true) {
57 
58 		if (sensor_sample_fetch(sht)) {
59 			printf("Failed to fetch sample from SHT4X device\n");
60 			return 0;
61 		}
62 
63 		sensor_channel_get(sht, SENSOR_CHAN_AMBIENT_TEMP, &temp);
64 		sensor_channel_get(sht, SENSOR_CHAN_HUMIDITY, &hum);
65 #if CONFIG_APP_USE_HEATER
66 		/*
67 		 * Conditions in which it makes sense to activate the heater
68 		 * are application/environment specific.
69 		 *
70 		 * The heater should not be used above SHT4X_HEATER_MAX_TEMP (65 °C)
71 		 * as stated in the datasheet.
72 		 *
73 		 * The temperature data will not be updated here for obvious reasons.
74 		 **/
75 		if (hum.val1 > CONFIG_APP_HEATER_HUMIDITY_THRESH &&
76 				temp.val1 < SHT4X_HEATER_MAX_TEMP) {
77 			printf("Activating heater.\n");
78 
79 			if (sht4x_fetch_with_heater(sht)) {
80 				printf("Failed to fetch sample from SHT4X device\n");
81 				return 0;
82 			}
83 
84 			sensor_channel_get(sht, SENSOR_CHAN_HUMIDITY, &hum);
85 		}
86 #endif
87 
88 #if CONFIG_APP_USE_COMPENSATION
89 		comp_t.val1 = temp.val1; /* Temp [°C] */
90 		comp_rh.val1 = hum.val1; /* RH [%] */
91 		sensor_attr_set(sgp,
92 				SENSOR_CHAN_GAS_RES,
93 				SENSOR_ATTR_SGP40_TEMPERATURE,
94 				&comp_t);
95 		sensor_attr_set(sgp,
96 				SENSOR_CHAN_GAS_RES,
97 				SENSOR_ATTR_SGP40_HUMIDITY,
98 				&comp_rh);
99 #endif
100 		if (sensor_sample_fetch(sgp)) {
101 			printf("Failed to fetch sample from SGP40 device.\n");
102 			return 0;
103 		}
104 
105 		sensor_channel_get(sgp, SENSOR_CHAN_GAS_RES, &gas);
106 
107 		printf("SHT4X: %.2f Temp. [C] ; %0.2f RH [%%] -- SGP40: %d Gas [a.u.]\n",
108 		       sensor_value_to_double(&temp),
109 		       sensor_value_to_double(&hum),
110 		       gas.val1);
111 
112 		/* Maximum duty cycle for using the heater is 5% */
113 #if CONFIG_APP_USE_HEATER && CONFIG_APP_HEATER_PULSE_DURATION == 0
114 		k_sleep(K_MSEC(20000));
115 #else
116 		k_sleep(K_MSEC(2000));
117 #endif
118 	}
119 }
120