Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #121 from foogod/ds18b20-updates
DS18B20 API Improvements
- Loading branch information
Showing
6 changed files
with
862 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,78 @@ | ||
| /* ds18b20 - Retrieves temperature from ds18b20 sensors and print it out. | ||
| /* ds18b20_onewire.c - Retrieves readings from one or more DS18B20 temperature | ||
| * sensors, and prints the results to stdout. | ||
| * | ||
| * This sample code is in the public domain., | ||
| */ | ||
| #include "espressif/esp_common.h" | ||
| #include "esp/uart.h" | ||
|
|
||
| #include "FreeRTOS.h" | ||
| #include "task.h" | ||
| #include "timers.h" | ||
| #include "queue.h" | ||
| #include "esp/uart.h" | ||
|
|
||
| // DS18B20 driver | ||
| #include "ds18b20/ds18b20.h" | ||
| // Onewire init | ||
| #include "onewire/onewire.h" | ||
|
|
||
| void print_temperature(void *pvParameters) | ||
| { | ||
| int delay = 500; | ||
| uint8_t amount = 0; | ||
| // Declare amount of sensors | ||
| uint8_t sensors = 2; | ||
| ds_sensor_t t[sensors]; | ||
|
|
||
| // Use GPIO 13 as one wire pin. | ||
| uint8_t GPIO_FOR_ONE_WIRE = 13; | ||
|
|
||
| onewire_init(GPIO_FOR_ONE_WIRE); | ||
| #define SENSOR_GPIO 13 | ||
| #define MAX_SENSORS 8 | ||
| #define RESCAN_INTERVAL 8 | ||
| #define LOOP_DELAY_MS 250 | ||
|
|
||
| void print_temperature(void *pvParameters) { | ||
| ds18b20_addr_t addrs[MAX_SENSORS]; | ||
| float temps[MAX_SENSORS]; | ||
| int sensor_count; | ||
|
|
||
| // There is no special initialization required before using the ds18b20 | ||
| // routines. However, we make sure that the internal pull-up resistor is | ||
| // enabled on the GPIO pin so that one can connect up a sensor without | ||
| // needing an external pull-up (Note: The internal (~47k) pull-ups of the | ||
| // ESP8266 do appear to work, at least for simple setups (one or two sensors | ||
| // connected with short leads), but do not technically meet the pull-up | ||
| // requirements from the DS18B20 datasheet and may not always be reliable. | ||
| // For a real application, a proper 4.7k external pull-up resistor is | ||
| // recommended instead!) | ||
|
|
||
| gpio_set_pullup(SENSOR_GPIO, true, true); | ||
|
|
||
| while(1) { | ||
| // Search all DS18B20, return its amount and feed 't' structure with result data. | ||
| amount = ds18b20_read_all(GPIO_FOR_ONE_WIRE, t); | ||
| // Every RESCAN_INTERVAL samples, check to see if the sensors connected | ||
| // to our bus have changed. | ||
| sensor_count = ds18b20_scan_devices(SENSOR_GPIO, addrs, MAX_SENSORS); | ||
|
|
||
| if (amount < sensors){ | ||
| printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount); | ||
| } | ||
| if (sensor_count < 1) { | ||
| printf("\nNo sensors detected!\n"); | ||
| } else { | ||
| printf("\n%d sensors detected:\n", sensor_count); | ||
| // If there were more sensors found than we have space to handle, | ||
| // just report the first MAX_SENSORS.. | ||
| if (sensor_count > MAX_SENSORS) sensor_count = MAX_SENSORS; | ||
|
|
||
| for (int i = 0; i < amount; ++i) | ||
| { | ||
| int intpart = (int)t[i].value; | ||
| int fraction = (int)((t[i].value - intpart) * 100); | ||
| // Multiple "" here is just to satisfy compiler and don`t raise 'hex escape sequence out of range' warning. | ||
| printf("Sensor %d report: %d.%02d ""\xC2""\xB0""C\n",t[i].id, intpart, fraction); | ||
| // Do a number of temperature samples, and print the results. | ||
| for (int i = 0; i < RESCAN_INTERVAL; i++) { | ||
| ds18b20_measure_and_read_multi(SENSOR_GPIO, addrs, sensor_count, temps); | ||
| for (int j = 0; j < sensor_count; j++) { | ||
| // The DS18B20 address is a 64-bit integer, but newlib-nano | ||
| // printf does not support printing 64-bit values, so we | ||
| // split it up into two 32-bit integers and print them | ||
| // back-to-back to make it look like one big hex number. | ||
| uint32_t addr0 = addrs[j] >> 32; | ||
| uint32_t addr1 = addrs[j]; | ||
| float temp_c = temps[j]; | ||
| float temp_f = (temp_c * 1.8) + 32; | ||
| printf(" Sensor %08x%08x reports %f deg C (%f deg F)\n", addr0, addr1, temp_c, temp_f); | ||
| } | ||
| printf("\n"); | ||
|
|
||
| // Wait for a little bit between each sample (note that the | ||
| // ds18b20_measure_and_read_multi operation already takes at | ||
| // least 750ms to run, so this is on top of that delay). | ||
| vTaskDelay(LOOP_DELAY_MS / portTICK_RATE_MS); | ||
| } | ||
| } | ||
| printf("\n"); | ||
| vTaskDelay(delay / portTICK_RATE_MS); | ||
| } | ||
| } | ||
|
|
||
| void user_init(void) | ||
| { | ||
| void user_init(void) { | ||
| uart_set_baud(0, 115200); | ||
|
|
||
| printf("SDK version:%s\n", sdk_system_get_sdk_version()); | ||
|
|
||
| xTaskCreate(&print_temperature, (signed char *)"print_temperature", 256, NULL, 2, NULL); | ||
| } | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.