Sat, 26 Oct 2019 14:05:17 +0200
Changed build system to idf.py
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | #define GPIO_DS18B20_BUS (CONFIG_ONE_WIRE_BUS) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | #define DS18B20_RESOLUTION (DS18B20_RESOLUTION_12_BIT) |
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 | 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 | |
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 | 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
|
28 | 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
|
29 | |
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 | void request_ds18b20(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | } |
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 | bool ready_ds18b20(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | if (xEventGroupGetBits(xEventGroupDS18B20) & TASK_DS18B20_REQUEST_DONE) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | return true; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | return false; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | } |
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 | * Task to read temperature sensors on request. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | void task_ds18b20(void *pvParameter) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | { |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
54 | int i, num_devices = 0; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | bool found = false; |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
56 | EventBits_t uxBits; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | ESP_LOGI(TAG, "Starting DS18B20 sensors"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | 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
|
60 | ds18b20_state->valid = false; |
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
61 | 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
|
62 | |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
63 | 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
|
64 | 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
|
65 | 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
|
66 | 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
|
67 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
69 | /* |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
70 | * 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
|
71 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | xEventGroupDS18B20 = xEventGroupCreate(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | /* |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
75 | * Initialize the one-wire bus. |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | */ |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
77 | 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
|
78 | 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
|
79 | owb_use_crc(owb, true); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | |
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 | * Task loop forever. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | while (1) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | 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
|
87 | if (uxBits & TASK_DS18B20_REQUEST_TEMPS) { |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
89 | ESP_LOGI(TAG, "Requested DS18B20 readings"); |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
90 | 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
|
91 | num_devices = 0; |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
92 | OneWireBus_SearchState search_state = {0}; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | found = false; |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
94 | owb_search_first(owb, &search_state, &found); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | while (found) { |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
96 | 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
|
97 | 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
|
98 | rom_code_s[16] = '\0'; |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
99 | #if 0 |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
100 | 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
|
101 | #endif |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
102 | device_rom_codes[num_devices] = search_state.rom_code; |
5 | 103 | 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
|
104 | ++num_devices; |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
105 | owb_search_next(owb, &search_state, &found); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
108 | if (num_devices) { |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
109 | /* |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
110 | * Create DS18B20 devices on the bus |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
111 | */ |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
112 | DS18B20_Info * devices[DS18B20_MAX] = {0}; |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
113 | 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
|
114 | 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
|
115 | devices[i] = ds18b20_info; |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
116 | |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
117 | if (num_devices == 1) { |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
118 | 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
|
119 | } else { |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
120 | 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
|
121 | } |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
122 | 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
|
123 | 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
|
124 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
126 | // 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
|
127 | ds18b20_convert_all(owb); |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
128 | ds18b20_wait_for_conversion(devices[0]); |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
129 | |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
130 | // Read the results immediatly after conversion. |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
131 | float readings[DS18B20_MAX] = { 0 }; |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
132 | DS18B20_ERROR errors[DS18B20_MAX] = { 0 }; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
134 | 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
|
135 | 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
|
136 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
137 | |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
138 | // Process the results with a locked semaphore |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
139 | if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
10
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
140 | ds18b20_state->valid = true; |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
141 | 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
|
142 | if (errors[i] != DS18B20_OK) { |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
143 | if (errors[i] == DS18B20_ERROR_CRC) |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
144 | 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
|
145 | else |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
146 | 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
|
147 | ds18b20_state->valid = false; |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
148 | 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
|
149 | } else { |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
150 | 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
|
151 | 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
|
152 | } |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
153 | #if 1 |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
154 | 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
|
155 | 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
|
156 | #endif |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
157 | } |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
158 | ds18b20_state->num_sensors = num_devices; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
159 | 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
|
160 | } 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
|
161 | 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
|
162 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
163 | } else { |
10
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 | 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
|
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 | /* |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
169 | * 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
|
170 | */ |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
171 | if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
172 | ds18b20_state->num_sensors = num_devices; |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
173 | 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
|
174 | 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
|
175 | 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
|
176 | 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
|
177 | } |
d08c7466bb40
One-wire bus can now handle multiple sensors.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
178 | 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
|
179 | } 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
|
180 | 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
|
181 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
182 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
183 | xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
184 | xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE); |
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 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
187 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
188 |