main/task_ds18b20.c

Sat, 12 Oct 2019 21:05:09 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 12 Oct 2019 21:05:09 +0200
changeset 10
d08c7466bb40
parent 5
4b1c65e4d863
child 12
7dc9003f86a8
permissions
-rw-r--r--

One-wire bus can now handle multiple sensors.

0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file task_ds18b20.c
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 * @brief The FreeRTOS task to query the DS18B20 sensors on one or two
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * one-wire busses. Each bus must have only one sensor. That way
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 * we don't need to care about the DS18B20 internal ROM address.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * The task will update the DS18B20_State structure.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 #include "owb.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 #include "owb_rmt.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 #include "ds18b20.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 #include "config.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 #define GPIO_DS18B20_BUS (CONFIG_ONE_WIRE_BUS)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 #define DS18B20_RESOLUTION (DS18B20_RESOLUTION_12_BIT)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 static const char *TAG = "task_ds18b20";
3
cd760fd45271 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 2
diff changeset
21 static const char *dsErrors[] = { "Ok", "CRC error", "Read error" };
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 SemaphoreHandle_t xSemaphoreDS18B20 = NULL; ///< Semaphire DS18B20 task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 EventGroupHandle_t xEventGroupDS18B20; ///< Events DS18B20 task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 DS18B20_State *ds18b20_state; ///< Public state for other tasks
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 const int TASK_DS18B20_REQUEST_TEMPS = BIT0; ///< Request temperature measurements
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 const int TASK_DS18B20_REQUEST_DONE = BIT1; ///< Request is completed
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 void request_ds18b20(void)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 bool ready_ds18b20(void)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 if (xEventGroupGetBits(xEventGroupDS18B20) & TASK_DS18B20_REQUEST_DONE)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 return true;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 return false;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 * Task to read temperature sensors on request.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 void task_ds18b20(void *pvParameter)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 {
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
55 int i, num_devices = 0;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 bool found = false;
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
57 EventBits_t uxBits;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 ESP_LOGI(TAG, "Starting DS18B20 sensors");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 ds18b20_state = malloc(sizeof(DS18B20_State));
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
61 ds18b20_state->valid = false;
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
62 ds18b20_state->num_sensors = 0;
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
63
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
64 for (i = 0; i < DS18B20_MAX; i++) {
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
65 ds18b20_state->sensor[i].temperature = 0.0;
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
66 ds18b20_state->sensor[i].rom_code[0] = '\0';
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
67 ds18b20_state->sensor[i].error = DS18B20_ERR_READ;
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
68 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
70 /*
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
71 * event handler and event group for the one-wire bus
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
72 */
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 xEventGroupDS18B20 = xEventGroupCreate();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 /*
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
76 * Initialize the one-wire bus.
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 */
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
78 owb_rmt_driver_info rmt_driver_info_bottle;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
79 OneWireBus *owb = owb_rmt_initialize(&rmt_driver_info_bottle, GPIO_DS18B20_BUS, RMT_CHANNEL_1, RMT_CHANNEL_0);
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
80 owb_use_crc(owb, true);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 * Task loop forever.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 uxBits = xEventGroupWaitBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS, pdFALSE, pdFALSE, portMAX_DELAY );
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
88 if (uxBits & TASK_DS18B20_REQUEST_TEMPS) {
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
90 ESP_LOGI(TAG, "Requested DS18B20 readings");
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
91 OneWireBus_ROMCode device_rom_codes[DS18B20_MAX] = {0};
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 num_devices = 0;
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
93 OneWireBus_SearchState search_state = {0};
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 found = false;
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
95 owb_search_first(owb, &search_state, &found);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 while (found) {
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
97 char rom_code_s[17];
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
98 owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s));
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
99 rom_code_s[16] = '\0';
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
100 #if 0
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
101 printf(" %d : %s %d\n", num_devices + 1, rom_code_s, strlen(rom_code_s));
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
102 #endif
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
103 device_rom_codes[num_devices] = search_state.rom_code;
5
4b1c65e4d863 Version 0.1.0.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
104 strncpy(ds18b20_state->sensor[num_devices].rom_code, rom_code_s, 17);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 ++num_devices;
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
106 owb_search_next(owb, &search_state, &found);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
109 if (num_devices) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
110 /*
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
111 * Create DS18B20 devices on the bus
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
112 */
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
113 DS18B20_Info * devices[DS18B20_MAX] = {0};
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
114 for (i = 0; i < num_devices; ++i) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
115 DS18B20_Info * ds18b20_info = ds18b20_malloc(); // heap allocation
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
116 devices[i] = ds18b20_info;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
117
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
118 if (num_devices == 1) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
119 ds18b20_init_solo(ds18b20_info, owb); // only one device on bus
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
120 } else {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
121 ds18b20_init(ds18b20_info, owb, device_rom_codes[i]); // associate with bus and device
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
122 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
123 ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
124 ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION);
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
125 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 // Read temperatures more efficiently by starting conversions on all devices at the same time
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
128 ds18b20_convert_all(owb);
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
129 ds18b20_wait_for_conversion(devices[0]);
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
130
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
131 // Read the results immediatly after conversion.
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
132 float readings[DS18B20_MAX] = { 0 };
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
133 DS18B20_ERROR errors[DS18B20_MAX] = { 0 };
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
135 for (i = 0; i < num_devices; ++i) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
136 errors[i] = ds18b20_read_temp(devices[i], &readings[i]);
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
137 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
139 // Process the results with a locked semaphore
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) {
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
141 ds18b20_state->valid = true;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
142 for (i = 0; i < num_devices; ++i) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
143 if (errors[i] != DS18B20_OK) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
144 if (errors[i] == DS18B20_ERROR_CRC)
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
145 ds18b20_state->sensor[i].error = DS18B20_ERR_CRC;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
146 else
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
147 ds18b20_state->sensor[i].error = DS18B20_ERR_READ; // All other errors
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
148 ds18b20_state->valid = false;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
149 ds18b20_state->sensor[i].temperature = 0.0;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
150 } else {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
151 ds18b20_state->sensor[i].error = DS18B20_ERR_NONE;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
152 ds18b20_state->sensor[i].temperature = readings[i];
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
153 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
154 #if 1
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
155 ESP_LOGI(TAG, "Temperature %d %s %.4f %s", i, ds18b20_state->sensor[i].rom_code,
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
156 ds18b20_state->sensor[i].temperature, dsErrors[ds18b20_state->sensor[i].error]);
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
157 #endif
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
158 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
159 ds18b20_state->num_sensors = num_devices;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 xSemaphoreGive(xSemaphoreDS18B20);
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
161 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 } else {
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
163
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
164 ESP_LOGW(TAG, "No temperature sensors found.");
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
165 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
166
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
167 /*
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
168 * Set the remainder of the ds18b20 entries
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
169 */
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
170 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
171 ds18b20_state->num_sensors = num_devices;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
172 for (i = num_devices; i < DS18B20_MAX; i++) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
173 ds18b20_state->sensor[i].temperature = 0.0;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
174 ds18b20_state->sensor[i].rom_code[0] = '\0';
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
175 ds18b20_state->sensor[i].error = DS18B20_ERR_READ;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
176 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
177 xSemaphoreGive(xSemaphoreDS18B20);
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
178 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181 xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182 }
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
183 vTaskDelay(10 / portTICK_PERIOD_MS);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
185 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
186

mercurial