#include <OneWire.h>
#include <DallasTemperature.h>
//One extra library:
#include <Arduino_FreeRTOS.h>
//Connect ds18b20 to Arduino pin D12:
#define ONE_WIRE_BUS 4
//Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
//Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
//Initialization of sensor & serial communication:
void setup() {
Serial.begin(9600);
sensors.begin();
xTaskCreate(DS18B20, "ds18b20", 100, NULL, 0, NULL);
}
//Without a while loop, the program will show the results in serial monitor only once!
static void DS18B20(void* pvParameters) {
while(true){
//Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
//Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println(sensors.getTempCByIndex(0));
Serial.print("Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
//The following line demark the last serial iteration from the next one!
Serial.println("-------------------------");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
//No need for 'loop' function here!
void loop() {}