mikelo,
Be sure that you are using both the Onewire lib and the spark-dallas-temprature (since you are using ds18b20) the latter has some higher level functions like getDeviceCount() that will make things much easier.
It is a bit of a paradox with the ds18b20. You can get the temperature by address or by index (0,1,2 …) but the problem is you can’t ever count on the indexes being the same, and this only happens at setup. What most people do is run code like below to find the addresses then set the values in a variable of DeviceAddress.
Something like this:
// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;
insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
(From at the multi example Misternetwork talked about to get the temperature by address.)
I am working on a solution that will store the addresses in EEPROM so if a new devices is added (even during runtime) it’s address will be added to the EEPROM and the location in EEPROM will map to your known devices every time.
// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"
// This #include statement was automatically added by the Particle IDE.
#include "OneWire/OneWire.h"
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
//globals
int deviceCount;
DeviceAddress tempDeviceAddress, deviceOne, deviceTwo, deviceThree, deviceFour; // We'll use this variable to store a found device address
//Prototypes
void printAddress(DeviceAddress deviceAddress);
void setup() {
Serial.begin(9600);
sensor.begin();
}
void loop() {
/* Get and display the device count
* this will not change if you add or remove sensors once you are in the loop.
* if you want the count to be dynamic you need to add sensor.begin() to
* the loop in addtion to the setup
*/
deviceCount = sensor.getDeviceCount();
Serial.print("\nNumber of devices: ");
Serial.println(deviceCount);
delay(1000);
/* now print the addresses for the count.
* I am going to store them in the same variable
* tempDeviceAddress and just reassign it. I am getting
* the address by method1: byindex
* if you want to store them seperately you need
* to pass a variable like deviceOne, deviceTwo ...
* or use an array
*/
for( int i = 0; i < deviceCount; i++) {
sensor.getAddress(tempDeviceAddress, i);
Serial.print("\nthe address for device ");
Serial.print(i);
Serial.print(" is: ");
printAddress(tempDeviceAddress);
}
delay(5000);
}
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++)
{
Serial.print(" ");
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
You can use something like this: sensor.getAddress(deviceOne, 0)
to assign the first sensor found to the variable deviceOne of type DeviceAddress