Smart Home Automation Forum

Full Version: Multiple DS18B20 Temperature sensor at the same GPIO pin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

On Kincony A8 controller, I made a code for reading multiple temperature sensor with one GPIO(14) pin (DS18B20 type). I made the reading on the second core of the ESP32's processor, to don't make any delay in the normal program cycle.
In the code is managed the multiple reading to can assign every DS18B20 temperature sensor to a fix variable by unique code of the DS18B20.

Here I will attach a short code about it. I didn't uploaded all of the code because there is a lot of other things in it too. Maybe if you want to use, need to do some other implementations too. But for a start it is perfect.

Code:
#include <DS18B20.h>
#include <Preferences.h>
DS18B20 ds(14);
struct DS18B20_tempSensor {
  uint8_t temperatureID;
  uint8_t address[8];
  float value;
};
DS18B20_tempSensor dsTemp[16];
struct Parameters {
  uint32_t  magic;
  byte temperatureAddress[16][8];
};
Parameters paramLocal;
const Parameters paramDefault = {
  0x626C6E6B
};
float temperature[16];
void setup() {
  loadParameters();
  xTaskCreatePinnedToCore(
      Task1code, /* Function to implement the task */
      "Task1", /* Name of the task */
      10000,  /* Stack size in words */
      NULL,  /* Task input parameter */
      0,  /* Priority of the task */
      &Task1,  /* Task handle. */
      0); /* Core where the task should run */
}
void loop() {
 
}
void Task1code( void * parameter) {
  for(;;) {
    while(ds.selectNext()) {
     
      uint8_t address[8];
      ds.getAddress(address);
      bool found = false;
      for(int i = 0; i < 16; i++) {
        if(memcmp(dsTemp[i].address, address, 8) == 0) {
          found = true;
          dsTemp[i].value = ds.getTempC();
          break;
        }
      }
      if(!found) {
        for(int i = 0; i < 16; i++) {
          bool empty = true;
          for(int j = 0; j < 8; j++) {
            if(dsTemp[i].address[j] != 0) {
              empty = false;
              break;
            }
          }
          if(empty) {
            ds.setResolution(12);
            bool match = false;
            for(int j = 0; j < 16; j++) {
              if(memcmp(paramLocal.temperatureAddress[j], address, 8) == 0) {
                match = true;
                dsTemp[i].temperatureID = j + 1;
                break;
              }
            }
            if(!match) {
              dsTemp[i].temperatureID = 99;
            }
            for(int j = 0; j < 8; j++) {
              dsTemp[i].address[j] = address[j];
            }
            dsTemp[i].value = ds.getTempC();
            break;
          }
        }
      }
    }
  }
}
bool loadParameters() {
  Preferences prefs;
  if (prefs.begin("esp32", true)) { // read-only
    memset(&paramLocal, 0, sizeof(paramLocal));
    prefs.getBytes("parameters", &paramLocal, sizeof(paramLocal));
    if (paramLocal.magic != paramDefault.magic) {
      Serial.print(F("Using default parameters."));
      paramLocal = paramDefault;
    }
    return true;
  } else {
    Serial.print(F("Parameters read failed"));
    return false;
  }
}

bool saveParameters() {
  Preferences prefs;
  if (prefs.begin("esp32", false)) { // writeable
    prefs.putBytes("parameters", &paramLocal, sizeof(paramLocal));
    Serial.print(F("Parameters stored to flash"));
    return true;
  } else {
    Serial.print(F("Parameters write failed"));
    return false;
  }
}
thanks for your effort.