#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged TO GPIO 4
#define ONE_WIRE_BUS 12
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Number of temperature devices found
int numberOfDevices;
// We'll use this variable to store a found device address
DeviceAddress tempDeviceAddress;
DeviceAddress sensor1 = { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFB };
DeviceAddress sensor2 = { 0x28, 0xFF, 0xB4, 0x6, 0x33, 0x17, 0x3, 0x4B };
const int POMPA = 38;
bool PompaState = false;
void setup(){
// start serial port
Serial.begin(115200);
pinMode(POMPA, OUTPUT);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
void loop(){
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
float Termo = sensors.getTempC(sensor1);
float Acqua = sensors.getTempC(sensor2);
Acqua = 40.00;
float Diff = Termo - Acqua;
if (Diff>1.0 & !PompaState){
PompaState = true;
Serial.println("Pompa ON");
}
if (Diff<0.0 & PompaState){
PompaState = false;
Serial.println("Pompa OFF");
}
Serial.print(" Termovettore : ");
Serial.print(Termo);
Serial.print(" - Acqua: ");
Serial.print(Acqua);
Serial.print(" - Pompa: ");
Serial.println(PompaState);
digitalWrite(POMPA, PompaState);
delay(1000);
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++){
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}