One wire
- 20111218 - Needed to read multiple DS18B20 Temperature sensors
- Define Known sensors with names, it will also find the rest.
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 10
#define TEMPERATURE_PRECISION 9
// 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);
typedef struct {
char name[20+1];
DeviceAddress Address;
byte found; //
} Thermometer_type; // Thermometer;
int ThermometerCount = 2; // Set to predefined number, add discovered here, after fixed.
Thermometer_type Thermometers[8];
Thermometer_type ThermometersStatic[]= {
"1st Pool temp in" , { 0x28, 0x71, 0x48, 0x57, 0x3, 0x0, 0x0, 0xDC } , false ,
"2nd Pool temp out" ,{ 0x28, 0xFB, 0x4A, 0x57, 0x3, 0x0, 0x0, 0x7A } , false
}; // arrays to hold device addresses
void setup(void)
{
// start serial port
Serial.begin(9600);
delay(5000);
setupTherm();
};
void loop(void)
{
loopTerm();
}
void setupTherm(void)
{
Serial.println("Dallas Temperature IC Control Library Demo");
//strcpy (Thermometers[0].name , "Test" );
// Start up the library
Serial.print("Locating devices...");
sensors.begin();
ThermometerCount = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Found ");
Serial.print(ThermometerCount);
Serial.println(" devices.");
delay(2000);
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
//PES
for (int i=0 ; i < ThermometerCount ; i++) {
Thermometers[i].found = false;
if (!sensors.getAddress(Thermometers[i].Address, i)) {
Serial.print("Unable to find address for Device ");Serial.println(i);
//ThermometerCount = i;
delay(5000);
}
else {
strcpy(Thermometers[i].name , "No Named defined.");
boolean found; //start true and test each byte
for (int ii = 0; ii < (sizeof(ThermometersStatic)/sizeof(Thermometer_type)); ii++) {
found = true;
for ( int iii = 0; iii < 8; iii++ ) {
found = found && (ThermometersStatic[ii].Address[iii] == Thermometers[i].Address[iii] );
};
if (found) {
strcpy(Thermometers[i].name , ThermometersStatic[ii].name);
Thermometers[i].found = true; //start true and test each byte
};
};
Serial.print("Device "); Serial.print(i);Serial.print(" Address: ");
printAddress(Thermometers[i].Address);
Serial.print(" ");
sensors.setResolution(Thermometers[i].Address, TEMPERATURE_PRECISION);
Serial.print(" Resolution: ");Serial.print(TEMPERATURE_PRECISION);
if (Thermometers[i].found) { Serial.print(" FOUND MATCH!"); };
Serial.print(" ");
Serial.print(Thermometers[i].name);
Serial.println();
}
}
delay(5000);
// assign address manually. the addresses below will beed to be changed
// to valid device addresses on your bus. device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
//outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
//
// method 1: by index
////if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
// method 2: search()
// search() looks for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are no devices,
// or you have already retrieved all of them. It might be a good idea to
// check the CRC to make sure you didn't get garbage. The order is
// deterministic. You will always get the same devices in the same order
//
// Must be called before search()
//oneWire.reset_search();
// assigns the first address found to insideThermometer
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
// assigns the seconds address found to outsideThermometer
//if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
// show the addresses we found on the bus
// set the resolution to 9 bit
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
//Serial.print(" 0x");
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
}
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
Serial.print("Resolution: ");
Serial.print(sensors.getResolution(deviceAddress));
Serial.println();
}
// main function to print information about a device
void printData(DeviceAddress deviceAddress,char* name)
{
Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
Serial.print(" ");
Serial.print(name);
Serial.println();
}
void loopTerm(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
// print the device information
for (int i=0 ; i < ThermometerCount ; i++) {
printData(Thermometers[i].Address , Thermometers[i].name );
}
}...
