main/task_ds18b20.c

Fri, 18 Sep 2020 12:55:03 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 18 Sep 2020 12:55:03 +0200
changeset 61
a322cfcff6b8
parent 57
232f318a6b51
permissions
-rw-r--r--

Leave old temperature when there is a sensor read error.

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
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
3 * @brief The FreeRTOS task to query the DS18B20 sensors on the
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
4 * one-wire bus.
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 * The task will update the DS18B20_State structure.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 */
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 #include "owb.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 #include "owb_rmt.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 #include "ds18b20.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 #include "config.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
44
e52d11b8f252 Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 28
diff changeset
15 #define GPIO_DS18B20_BUS (CONFIG_ONE_WIRE_BUS) ///< GPIO pin for the one-wire bus.
e52d11b8f252 Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 28
diff changeset
16 #define DS18B20_RESOLUTION (DS18B20_RESOLUTION_12_BIT) ///< Desired DS18B20 resolution.
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17
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 static const char *TAG = "task_ds18b20";
3
cd760fd45271 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 2
diff changeset
20 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
21
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 SemaphoreHandle_t xSemaphoreDS18B20 = NULL; ///< Semaphire DS18B20 task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 EventGroupHandle_t xEventGroupDS18B20; ///< Events DS18B20 task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 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
25
55
43362bb8f3c0 Added DS18B20 error counters. Bigger font for the error counters screen and added the DS18B20 counter. Patched the u8g2_esp32_hal to fix the sudden system reboots.
Michiel Broek <mbroek@mbse.eu>
parents: 44
diff changeset
26 extern uint32_t err_temp;
0
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
57
232f318a6b51 Code cleanup, doxygen comments added.
Michiel Broek <mbroek@mbse.eu>
parents: 55
diff changeset
59 ESP_LOGI(TAG, "Start DS18B20 sensors");
0
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
57
232f318a6b51 Code cleanup, doxygen comments added.
Michiel Broek <mbroek@mbse.eu>
parents: 55
diff changeset
90 ESP_LOGD(TAG, "Requested DS18B20 readings");
10
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
28
6d825e2962e4 Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
101 ESP_LOGI(TAG, " %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;
61
a322cfcff6b8 Leave old temperature when there is a sensor read error.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
149 // ds18b20_state->sensor[i].temperature = 0.0;
55
43362bb8f3c0 Added DS18B20 error counters. Bigger font for the error counters screen and added the DS18B20 counter. Patched the u8g2_esp32_hal to fix the sudden system reboots.
Michiel Broek <mbroek@mbse.eu>
parents: 44
diff changeset
150 err_temp++;
23
58a328e91881 Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
151 } else if (readings[i] == 85.0) { // Error value
58a328e91881 Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
152 ds18b20_state->sensor[i].error = DS18B20_ERR_READ;
58a328e91881 Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
153 ds18b20_state->valid = false;
61
a322cfcff6b8 Leave old temperature when there is a sensor read error.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
154 // ds18b20_state->sensor[i].temperature = 0.0;
55
43362bb8f3c0 Added DS18B20 error counters. Bigger font for the error counters screen and added the DS18B20 counter. Patched the u8g2_esp32_hal to fix the sudden system reboots.
Michiel Broek <mbroek@mbse.eu>
parents: 44
diff changeset
155 err_temp++;
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
156 } else {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
157 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
158 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
159 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
160 #if 1
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
161 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
162 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
163 #endif
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
164 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
165 ds18b20_state->num_sensors = num_devices;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 xSemaphoreGive(xSemaphoreDS18B20);
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
167 } else {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
168 ESP_LOGE(TAG, "Missed lock 1");
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
169 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 } else {
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
171
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
172 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
173 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
174
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
175 /*
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
176 * 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
177 */
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
178 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) {
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
179 ds18b20_state->num_sensors = num_devices;
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
180 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
181 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
182 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
183 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
184 }
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
185 xSemaphoreGive(xSemaphoreDS18B20);
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
186 } else {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
187 ESP_LOGE(TAG, "Missed lock 2");
10
d08c7466bb40 One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
188 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
189
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190 xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191 xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
194 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
195

mercurial