Multiple DS18B20 with Photon

Since you will have a lot of logic to control the valves and such, it would seem best to move the code to get the temperatures to another file. I think something like below should give you close to what you want. The temperatures are put into 3 arrays, one for each bus, rather than one, but you probably could get them in to one array if you really want that. I only have 3 sensors, so I’m simulating 3 sets of sensors. Anyway, here’s what I came up with.

The Dallas.h file,

#include "Particle.h"

void getTemperatures(float temps[], int tempsCount, int pin, int select);

The Dallas.cpp file,

#include "Dallas.h"
#include "OneWire/OneWire.h"

double celsius, fahrenheit;

byte addrs0[3][8] = {{0x28, 0x1B, 0x1C, 0xE3, 0x03, 0x0, 0x0, 0xC5}, {0x28, 0x8, 0x56, 0xE3, 0x3, 0x0, 0x0, 0x93}, {0x28, 0xD, 0xD3, 0xE2, 0x3, 0x0, 0x0, 0xEE}};
byte addrs1[3][8] = {{0x28, 0x1B, 0x1C, 0xE3, 0x03, 0x0, 0x0, 0xC5}, {0x28, 0x8, 0x56, 0xE3, 0x3, 0x0, 0x0, 0x93}, {0x28, 0xD, 0xD3, 0xE2, 0x3, 0x0, 0x0, 0xEE}};
byte addrs2[3][8] = {{0x28, 0x1B, 0x1C, 0xE3, 0x03, 0x0, 0x0, 0xC5}, {0x28, 0x8, 0x56, 0xE3, 0x3, 0x0, 0x0, 0x93}, {0x28, 0xD, 0xD3, 0xE2, 0x3, 0x0, 0x0, 0xEE}};


void getTemperatures(float temps[], int tempsCount, int pin, int select) {
    OneWire ds = OneWire(pin);
    ds.reset();            
    ds.skip();          // Make all devices start the temperature conversion
    ds.write(0x44, 1);  // tell it to start a conversion, with parasite power on at the end (pass 0 for second argument if using powered mode)

    delay(1000);       //  wait 1 sec for conversion
    ds.reset();
  
    for (int i=0; i<tempsCount; i++) {
        switch (select) {
            case 0:
                ds.select(addrs0[i]);
                break;
            case 1:
                ds.select(addrs1[i]);
                break;
            case 2:
                ds.select(addrs2[i]);
                break;
        }
        
        ds.write(0xBE,0);   
    
        byte data0 = ds.read();
        byte data1 = ds.read();
        ds.reset();
      
        int16_t raw = (data1 << 8) | data0;
    
        celsius = (float)raw * 0.0625;
        fahrenheit = celsius * 1.8 + 32.0;
        temps[i] = fahrenheit;
  }
}

The main .ino file,

// This #include statement was automatically added by the Particle IDE.
#include "Dallas.h"

float T1, T2, T3, T4, T5, T6, T7, T8, T9;
float* temps1[] = {&T1, &T2, &T3}; // group sensors by which pin they're connected to
float* temps2[] = {&T4, &T5, &T6};
float* temps3[] = {&T7, &T8, &T9};

void setup() {
    Serial.begin(9600);
    delay(3000);
}


void loop() {
    getTemperatures(*temps1, 3, D4, 0); // the last argument is used to select which array of addresses is used in the Dallas.cpp file
    getTemperatures(*temps2, 3, D4, 1);
    getTemperatures(*temps3, 3, D4, 2);
    
   if (T7>50) {
       Serial.printf(" T1 = %.1f  T2 = %.1f  T3 = %.1f", T1, T2, T3);
       Serial.println();
       Serial.printf(" T4 = %.1f  T5 = %.1f  T6 = %.1f", T4, T5, T6);
       Serial.println();
       Serial.printf(" T7 = %.1f  T8 = %.1f  T9 = %.1f", T7, T8, T9);
       Serial.println();
       Serial.println();
   }
   
    delay(10000);
}

The if(T7>50) block is just there to test whether I was getting the correct readings, and to show that you can use your variable names directly to get the temperatures.

1 Like