DS18B20 in an array. OneWire.h spark-dallas-temperature/spark-dallas-temperature.h

Has anyone had luck in calling sensors in an array. I have found this snip for arduino, but i cannot compile for photon.

(i have included “spark-dallas-temperature/spark-dallas-temperature.h” in stead of “DallasTemperature.h”

It would help if we had some code to look at and error messages.
How did you include the library?

The libraries are included through the web IDE. The code is a relatively simple, but i am uncertain of what the compiler doesnt like (taken from arduino)

// 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"


//OneWire DS18x20 sensors on these wires
int oneWirePins[]={3,7};
const int oneWirePinsCount
      =sizeof(oneWirePins)/sizeof(int);
 
OneWire ds18x20[oneWirePinsCount];
DallasTemperature sensor[oneWirePinsCount];
 
void setup(void) {
  Serial.begin(9600);
  Serial.print("============Ready with ");
  Serial.print(oneWirePinsCount);
  Serial.println(" Sensors================");
 
  //Start up the library on all defined pins
  DeviceAddress deviceAddress;
  for (int i=0; i<oneWirePinsCount; i++) {;
    ds18x20[i].setPin(oneWirePins[i]);
    sensor[i].setOneWire(&ds18x20[i]);
    sensor[i].begin();
    if (sensor[i].getAddress(deviceAddress, 0)) 
      sensor[i].setResolution(deviceAddress, 12);
  }
}
 
void loop(void) {
  Serial.print("Requesting temperatures...");
  for (int i=0; i<oneWirePinsCount; i++) {
    sensor[i].requestTemperatures();
  }
  Serial.println("DONE");
 
  delay(1000);
  for (int i=0; i<oneWirePinsCount; i++) {
    float temperature=sensor[i].getTempCByIndex(0);
    Serial.print("Temperature for the sensor ");
    Serial.print(i);
    Serial.print(" is ");
    Serial.println(temperature);
  }
  Serial.println();
}

Usually the compiler tells you by means of error messages as @Moors7 already said

Can you post them too?

Does it say something about no constructor taking no arguments?

The errors are :

sensorarray.cpp:13:33: error: no matching function for call to 'OneWire::OneWire()'
 //OneWire DS18x20 sensors on these wires
                                 ^

sensorarray.cpp:13:33: note: candidates are:
In file included from spark-dallas-temperature/spark-dallas-temperature.h:27:0,
                 from sensorarray.cpp:2:
spark-dallas-temperature/../OneWire/OneWire.h:135:5: note: OneWire::OneWire(uint16_t)
     OneWire( uint16_t pin);
     ^
spark-dallas-temperature/../OneWire/OneWire.h:135:5: note:   candidate expects 1 argument, 0 provided
spark-dallas-temperature/../OneWire/OneWire.h:29:7: note: constexpr OneWire::OneWire(const OneWire&)
 class OneWire
       ^
spark-dallas-temperature/../OneWire/OneWire.h:29:7: note:   candidate expects 1 argument, 0 provided
spark-dallas-temperature/../OneWire/OneWire.h:29:7: note: constexpr OneWire::OneWire(OneWire&&)
spark-dallas-temperature/../OneWire/OneWire.h:29:7: note:   candidate expects 1 argument, 0 provided
sensorarray.cpp:14:42: error: no matching function for call to 'DallasTemperature::DallasTemperature()'
 int oneWirePins[]={3,7};
                                          ^

sensorarray.cpp:14:42: note: candidates are:
In file included from sensorarray.cpp:2:0:
spark-dallas-temperature/spark-dallas-temperature.h:74:3: note: DallasTemperature::DallasTemperature(OneWire*)
   DallasTemperature(OneWire*);
   ^
spark-dallas-temperature/spark-dallas-temperature.h:74:3: note:   candidate expects 1 argument, 0 provided
spark-dallas-temperature/spark-dallas-temperature.h:70:7: note: constexpr DallasTemperature::DallasTemperature(const DallasTemperature&)
 class DallasTemperature
       ^
spark-dallas-temperature/spark-dallas-temperature.h:70:7: note:   candidate expects 1 argument, 0 provided
spark-dallas-temperature/spark-dallas-temperature.h:70:7: note: constexpr DallasTemperature::DallasTemperature(DallasTemperature&&)
spark-dallas-temperature/spark-dallas-temperature.h:70:7: note:   candidate expects 1 argument, 0 provided
sensorarray.cpp: In function 'void setup()':
sensorarray.cpp:25:16: error: 'class OneWire' has no member named 'setPin'
   Serial.println(" Sensors================");
                ^

sensorarray.cpp:26:15: error: 'class DallasTemperature' has no member named 'setOneWire'
  
               ^

make[1]: *** [../build/target/user/platform-6sensorarray.o] Error 1
make: *** [user] Error 2
Error: Could not compile. Please review your code.
sensorarray.cpp:13:33: error: no matching function for call to 'OneWire::OneWire()'
 //OneWire DS18x20 sensors on these wires
                                 ^

sensorarray.cpp:13:33: note: candidates are:
In file included from spark-dallas-temperature/spark-dallas-temperature.h:27:0,
                 from sensorarray.cpp:2:
spark-dallas-temperature/../OneWire/OneWire.h:135:5: note: OneWire::OneWire(uint16_t)
     OneWire( uint16_t pin);
     ^
spark-dallas-temperature/../OneWire/OneWire.h:135:5: note:   candidate expects 1 argument, 0 provided
[/quote]

This exactly is what I meant with "no constructor taking no arguments".

OneWire ds18x20[oneWirePinsCount];

This will try to call the OneWire constructor that takes no arguments, since you are not passing any, but as the error message states, the only constructor found expects one argument.

Stated reather explicitly here


If you try this code, this at least builds

#include "OneWire/OneWire.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"

//OneWire DS18x20 sensors on these wires
int oneWirePins[]={D3,D7};
const int oneWirePinsCount
      =sizeof(oneWirePins)/sizeof(int);
 
OneWire ds18x20[oneWirePinsCount] { D3, D7 };
DallasTemperature sensor[oneWirePinsCount] { &ds18x20[0], &ds18x20[1] };
 
void setup(void) {
  Serial.begin(9600);
  Serial.print("============Ready with ");
  Serial.print(oneWirePinsCount);
  Serial.println(" Sensors================");
 
  //Start up the library on all defined pins
  DeviceAddress deviceAddress;
  for (int i=0; i<oneWirePinsCount; i++) {;
    //ds18x20[i].setPin(oneWirePins[i]);
    //sensor[i].setOneWire(&ds18x20[i]);
    sensor[i].begin();
    if (sensor[i].getAddress(deviceAddress, 0)) 
      sensor[i].setResolution(deviceAddress, 12);
  }
}
 
void loop(void) {
  Serial.print("Requesting temperatures...");
  for (int i=0; i<oneWirePinsCount; i++) {
    sensor[i].requestTemperatures();
  }
  Serial.println("DONE");
 
  delay(1000);
  for (int i=0; i<oneWirePinsCount; i++) {
    float temperature=sensor[i].getTempCByIndex(0);
    Serial.print("Temperature for the sensor ");
    Serial.print(i);
    Serial.print(" is ");
    Serial.println(temperature);
  }
  Serial.println();
}

or a bit shorter

#include "OneWire/OneWire.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"

const int oneWireCount = 2;
DallasTemperature sensor[oneWireCount] { new OneWire(D3), new OneWire(D7) };
 
void setup(void) {
  Serial.begin(9600);
  Serial.print("============Ready with ");
  Serial.print(oneWireCount);
  Serial.println(" Sensors================");
 
  //Start up the library on all defined pins
  DeviceAddress deviceAddress;
  for (int i=0; i<oneWireCount; i++) {;
    sensor[i].begin();
    if (sensor[i].getAddress(deviceAddress, 0)) 
      sensor[i].setResolution(deviceAddress, 12);
  }
}
 
void loop(void) {
  Serial.print("Requesting temperatures...");
  for (int i=0; i<oneWireCount; i++) {
    sensor[i].requestTemperatures();
  }
  Serial.println("DONE");
 
  delay(1000);
  for (int i=0; i<oneWireCount; i++) {
    float temperature=sensor[i].getTempCByIndex(0);
    Serial.print("Temperature for the sensor ");
    Serial.print(i);
    Serial.print(" is ");
    Serial.println(temperature);
  }
  Serial.println();
}

Is there any way to construct the code, in order to be able to make the sensor read something like:


for (int i=0, i< numberOfSensors, i++) {
    sensor[i].requestTemperatures();
}

???

i have been struggeling with it for quite a while… (being autononemous newbie is not always easy… ) :smiley:

Thanks ScruffR

I’ve just updated above post with a slightly shorter version.

Thank you ScruffR. Working on it… Just took a step up the learning curve. :slight_smile:

1 Like