Add DS18B20 temperature sensor support
This commit is contained in:
@@ -1,10 +1,87 @@
|
||||
#include <Arduino.h>
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "sensors.h"
|
||||
|
||||
SensorData sensors;
|
||||
|
||||
OneWire oneWire(ONEWIRE_PIN);
|
||||
DallasTemperature ds18b20(&oneWire);
|
||||
|
||||
DeviceAddress sensorAddresses[4];
|
||||
int sensorCount = 0;
|
||||
|
||||
float cToF(float c) {
|
||||
return (c * 9.0 / 5.0) + 32.0;
|
||||
}
|
||||
|
||||
bool validTempC(float tempC) {
|
||||
return tempC != DEVICE_DISCONNECTED_C && tempC > -55 && tempC < 125;
|
||||
}
|
||||
|
||||
void printAddress(DeviceAddress address) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (address[i] < 16) Serial.print("0");
|
||||
Serial.print(address[i], HEX);
|
||||
if (i < 7) Serial.print(":");
|
||||
}
|
||||
}
|
||||
|
||||
void printSensorAddresses() {
|
||||
Serial.println("DS18B20 Sensors Found:");
|
||||
|
||||
for (int i = 0; i < sensorCount; i++) {
|
||||
Serial.print("Sensor ");
|
||||
Serial.print(i);
|
||||
Serial.print(": ");
|
||||
printAddress(sensorAddresses[i]);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
if (sensorCount == 0) {
|
||||
Serial.println("No DS18B20 sensors found.");
|
||||
}
|
||||
}
|
||||
|
||||
void initSensors() {
|
||||
ds18b20.begin();
|
||||
sensorCount = ds18b20.getDeviceCount();
|
||||
|
||||
if (sensorCount > 4) {
|
||||
sensorCount = 4;
|
||||
}
|
||||
|
||||
for (int i = 0; i < sensorCount; i++) {
|
||||
ds18b20.getAddress(sensorAddresses[i], i);
|
||||
}
|
||||
|
||||
printSensorAddresses();
|
||||
}
|
||||
|
||||
void updateSensors() {
|
||||
// Placeholder until DS18B20 support is added
|
||||
ds18b20.requestTemperatures();
|
||||
|
||||
float tempsF[4] = {-127, -127, -127, -127};
|
||||
bool online[4] = {false, false, false, false};
|
||||
|
||||
for (int i = 0; i < sensorCount; i++) {
|
||||
float tempC = ds18b20.getTempC(sensorAddresses[i]);
|
||||
|
||||
if (validTempC(tempC)) {
|
||||
tempsF[i] = cToF(tempC);
|
||||
online[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
sensors.fridgeZone1 = tempsF[0];
|
||||
sensors.fridgeZone2 = tempsF[1];
|
||||
sensors.rearSeat = tempsF[2];
|
||||
sensors.outsideAir = tempsF[3];
|
||||
|
||||
sensors.fridgeZone1Online = online[0];
|
||||
sensors.fridgeZone2Online = online[1];
|
||||
sensors.rearSeatOnline = online[2];
|
||||
sensors.outsideAirOnline = online[3];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user