65 lines
1.3 KiB
C++
65 lines
1.3 KiB
C++
#include <OneWire.h>
|
||
#include <DallasTemperature.h>
|
||
|
||
#define ONE_WIRE_BUS 8 // Pin, kde je připojen DS18B20
|
||
|
||
OneWire oneWire(ONE_WIRE_BUS);
|
||
DallasTemperature sensors(&oneWire);
|
||
|
||
DeviceAddress sensorAddress;
|
||
bool sensorFound = false;
|
||
|
||
void printAddress(DeviceAddress deviceAddress) {
|
||
for (uint8_t i = 0; i < 8; i++) {
|
||
if (deviceAddress[i] < 16) Serial.print("0");
|
||
Serial.print(deviceAddress[i], HEX);
|
||
}
|
||
}
|
||
|
||
void setup() {
|
||
pinMode(8, INPUT_PULLUP);
|
||
Serial.begin(115200);
|
||
delay(2000);
|
||
|
||
Serial.println("Hledám DS18B20...");
|
||
|
||
sensors.begin();
|
||
|
||
if (sensors.getDeviceCount() == 0) {
|
||
Serial.println("Žádné čidlo nenalezeno!");
|
||
return;
|
||
}
|
||
|
||
if (sensors.getAddress(sensorAddress, 0)) {
|
||
sensorFound = true;
|
||
Serial.print("Čidlo nalezeno, adresa: ");
|
||
printAddress(sensorAddress);
|
||
Serial.println();
|
||
|
||
sensors.setResolution(sensorAddress, 12); // 9–12 bitů
|
||
} else {
|
||
Serial.println("Nepodařilo se získat adresu čidla.");
|
||
}
|
||
}
|
||
|
||
void loop() {
|
||
|
||
if (!sensorFound) {
|
||
delay(2000);
|
||
return;
|
||
}
|
||
|
||
sensors.requestTemperatures();
|
||
|
||
float temperature = sensors.getTempC(sensorAddress);
|
||
|
||
if (temperature == DEVICE_DISCONNECTED_C) {
|
||
Serial.println("Čidlo odpojeno!");
|
||
} else {
|
||
Serial.print("Teplota: ");
|
||
Serial.print(temperature);
|
||
Serial.println(" °C");
|
||
}
|
||
|
||
delay(2000);
|
||
} |