Ik heb het multi sensor voorbeeld geprobeerd te begrijpen. Dit heb ik er van gemaakt voor 3 fysieke sensoren:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 13 on the Arduino
#define ONE_WIRE_BUS 11
#define TEMPERATURE_PRECISION 12 // 9-12 bit resolutie
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire myOneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&myOneWire);
// arrays to hold device addresses
DeviceAddress ledThermometer, waterThermometer, buitenThermometer, thermo4;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// assign address manually. the addresses below will need 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 };
//DeviceAddress ledThermometer = { 0x28, 0xFC, 0xB3, 0x46, 0x92, 0x3, 0x2, 0x77 }; //Device 0 Address: 28FCB34692030277
DeviceAddress(ledThermometer) = { 0x28, 0xFC, 0xB3, 0x46, 0x92, 0x3, 0x2, 0x77 }; //Device 0 Address: 28FCB34692030277
DeviceAddress(waterThermometer) = { 0x28, 0x42, 0xEF, 0x46, 0x92, 0x15, 0x2, 0x91 };
DeviceAddress(buitenThermometer)= { 0x28, 0xEB, 0xDD, 0x46, 0x92, 0x15, 0x2, 0x5C };
DeviceAddress(thermo4)= { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 }; // fake adres
// search for devices on the bus and assign based on an index. ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
//
// method 1: by index
//if (!sensors.getAddress(ledThermometer, 0)) Serial.println("Unable to find address for Device 0");
//if (!sensors.getAddress(waterThermometer, 1)) Serial.println("Unable to find address for Device 1");
//if (!sensors.getAddress(buitenThermometer, 2)) Serial.println("Unable to find address for Device 2");
//if (!sensors.getAddress(thermo4, 3)) Serial.println("Unable to find address for Device 3");
// 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()
myOneWire.reset_search();
// assigns the first address found to ledThermometer
if (!myOneWire.search(ledThermometer)) Serial.println("Unable to find address for ledThermometer");
// assigns the seconds address found to waterThermometer
if (!myOneWire.search(waterThermometer)) Serial.println("Unable to find address for waterThermometer");
if (!myOneWire.search(buitenThermometer)) Serial.println("Unable to find address for buitenThermometer");
if (!myOneWire.search(thermo4)) Serial.println("Unable to find address for thermo4");
// show the addresses we found on the bus
Serial.print("1e gevonden device Address: ");
printAddress(ledThermometer);
Serial.println();
Serial.print("2e gevonden device Address: ");
printAddress(waterThermometer);
Serial.println();
Serial.print("3e gevonden device Address: ");
printAddress(buitenThermometer);
Serial.println();
Serial.print("4e gevonden device Address: "); // deze bestaat fysiek niet
printAddress(thermo4);
Serial.println();
// set the resolution
sensors.setResolution(ledThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(waterThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(buitenThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(thermo4, TEMPERATURE_PRECISION);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(ledThermometer), DEC);
Serial.println();
Serial.print("Device 1 Resolution: ");
Serial.print(sensors.getResolution(waterThermometer), DEC);
Serial.println();
Serial.print("Device 2 Resolution: ");
Serial.print(sensors.getResolution(buitenThermometer), DEC);
Serial.println();
Serial.print("Device 3 Resolution: ");
Serial.print(sensors.getResolution(thermo4), DEC);
Serial.println();
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
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);
//Serial.print(" Temp F: ");
//Serial.print(DallasTemperature::toFahrenheit(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)
{
Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
Serial.println();
}
void loop(void)
{
myOneWire.reset_search();
// 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
printData(ledThermometer);
printData(waterThermometer);
printData(buitenThermometer);
printData(thermo4);
}
thermo4 bestaat fysiek niet.
Dit levert de volgende output in de serial monitor:
Dallas Temperature IC Control Library Demo
Locating devices...Found 3 devices.
Parasite power is: OFF
Unable to find address for thermo4
1e gevonden device Address: 28FCB34692030277
2e gevonden device Address: 2842EF4692150291
3e gevonden device Address: 28EBDD469215025C
4e gevonden device Address: 28EBDD469215025C
Device 0 Resolution: 12
Device 1 Resolution: 12
Device 2 Resolution: 12
Device 3 Resolution: 12
Requesting temperatures...DONE
Device Address: 0000000000000000 Temp C: -127.00
Device Address: 0000000000000000 Temp C: -127.00
Device Address: 0000000000000000 Temp C: -127.00
Device Address: 0000000000000000 Temp C: -127.00
Requesting temperatures...DONE
Device Address: 0000000000000000 Temp C: -127.00
Device Address: 0000000000000000 Temp C: -127.00
Device Address: 0000000000000000 Temp C: -127.00
Device Address: 0000000000000000 Temp C: -127.00
Requesting temperatures...DONE
Inderdaad: 3 gevonden: klopt
unable to find adres 4: dat klopt. Die 4e is er niet.
4e adres = identiek aan 3e adres, dat klopt niet.. waarom wordt waarde 3e adres hier getoond?
1e 3 adressen in de serial monitor kloppen neem ik aan: die heb ik in de code opgenomen, maar deed ik dat goed? Ik betwijfel het.
De loop werk ook helemaal niet
Het begint een beetje trial and error te worden voor me. Als ik het originele voorbeeld draai, zonder dat ik "mijn" adreswaardes verwerk in de code: dan geeft de loop wel keurige sensor waardes weer.. schiet mij maar lek.
Wat ik uiteindelijk graag zou willen: van de sensoren die ik heb het serienummer\adres bepalen. Labeltje aan de sensor met het adres. Sensoren monteren, In de sketch wil ik dan sensor(adres) "koppelen" aan de juiste van de 4 meetfuncties.