Prüfen, ob bestimmter DS18x20 angeschlossen ist

Hallo zusammen,

habe eine kurze Frage. Ich habe mehrere DS18s20 angeschlossen und möchte diese direkt abfragen.
--> Das funktioniert so weit <--

Wenn ich einen bestimmten Sensor anspreche, dieser aber nicht angeschlossen ist, bekomme ich -0,5°C zurück.

Wie kann ich prüfen, ob dieser spezielle Sensor angeschlossen ist, damit ich diesen ignorieren kann?

Danke und Gruß, Matze

Sieh dir mal die Funktion getTemp(byIndex) an.

Wo soll die sich denn verstecken? :astonished:

Auf OneWire Arduino Library, connecting 1-wire devices (DS18S20, etc) to Teensy gibt es jedenfalls keinen Hinweis...

Is glaub ich aus der DallasTemperature.h lib

Ja, immer mit der DallasTemperature.h

Mit diesem Sketch kannst Du DS..... suchen:

#include <DallasTemperature.h>
#include <OneWire.h>
#include <SoftwareSerial.h>


// LCD=======================================================
//Bibliothek initialisieren


/* DS18S20 Temperaturchip i/o */

OneWire  ds(17);  // on pin 9
#define MAX_DS1820_SENSORS 17
byte addr[MAX_DS1820_SENSORS][8];
void setup(void) 
{
  Serial.begin(9600);
  if (!ds.search(addr[0])) 
  {
  
    Serial.println("No more addresses.");
    ds.reset_search();
    delay(250);
    return;
  }
  if ( !ds.search(addr[1])) 
  {
    
    Serial.println("No more addresses.");
    ds.reset_search();
    delay(250);
    return;
  }
}
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
char buf[20];

void loop(void) 
{
  byte i, sensor;
  byte present = 0;
  byte data[12];

  for (sensor=0;sensor<MAX_DS1820_SENSORS;sensor++)
  {
    if ( OneWire::crc8( addr[sensor], 7) != addr[sensor][7]) 
    {

      Serial.println("CRC is not valid");
      return;
    }

   if ( addr[sensor][0] != 0x10) 
   {

      Serial.println("Device is not a DS18S20 family device.");
      return;
    }

    ds.reset();
    ds.select(addr[sensor]);
    ds.write(0x44,1);         // start conversion, with parasite power on at the end

    delay(1000);     // maybe 750ms is enough, maybe not
    // we might do a ds.depower() here, but the reset will take care of it.

    present = ds.reset();
    ds.select(addr[sensor]);    
    ds.write(0xBE);         // Read Scratchpad

    for ( i = 0; i < 9; i++) 
    {           // we need 9 bytes
      data[i] = ds.read();
    }

    LowByte = data[0];
    HighByte = data[1];
    TReading = (HighByte << 8) + LowByte;
    SignBit = TReading & 0x8000;  // test most sig bit
    if (SignBit) // negative
    {
      TReading = (TReading ^ 0xffff) + 1; // 2's comp
    }
    Tc_100 = (TReading*100/2);    

    Whole = Tc_100 / 100;  // separate off the whole and fractional portions
    Fract = Tc_100 % 100;

    sprintf(buf, "%d:%c%d.%d\337C     ",sensor,SignBit ? '-' : '+', Whole, Fract < 10 ? 0 : Fract);


    Serial.println(buf);
  }
}

Mit diesem die IDs auslesen und anzeigen:

#include <OneWire.h>
#include <DallasTemperature.h> 
OneWire ow(10); 
 
void setup(void) 
{
  Serial.begin(9600);
  lookUpSensors();
}
 
void lookUpSensors()
{
  byte address[8];
  int i=0;
  byte ok = 0, tmp = 0;
 
  Serial.println("--Suche gestartet--");
  while (ow.search(address))
  {
    tmp = 0;
    //0x10 = DS18S20
    if (address[0] == 0x10)
    {
      Serial.print("Device is a DS18S20 or DS1820: ");
      tmp = 1;
    } 
    else
    {
      //0x28 = DS18B20
      if (address[0] == 0x28)
      {
        Serial.print("Device is a DS18B20          : ");
        tmp = 1;
      }
    }
    //display the address, if tmp is ok
    if (tmp == 1)
    {
      if (OneWire::crc8(address, 7) != address[7])
      {
        Serial.println("but it doesn't have a valid CRC!");
      } 
      else
      {
        //all is ok, display it
        for (i=0;i<8;i++)
        {
          if (address[i] < 9)
          {
            Serial.print("0");
          }
          Serial.print("0x");
          Serial.print(address[i],HEX);
          if (i<7)
          {
            Serial.print(", ");
          }
        }
        Serial.println("");
        ok = 1;
      }
    }//end if tmp
  }//end while
  if (ok == 0)
  {
    Serial.println("Keine Sensoren gefunden");
  }
  Serial.println("--Suche beendet--");
}
 
void loop(void) 

{
  //do nothing :)
 // delay(1000);
 // setup();

}

Und mit diesem einzelne DS.... am Bus byIndex

#include <OneWire.h>
#include <DallasTemperature.h>

//Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

//Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

//Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup(void)
{
// start serial port
 Serial.begin(9600);
 //"Dallas Temperature IC Control Library Demo“);

//Start up the library
//sensors.begin();
}


void loop(void)
{
 Serial.println();
 Serial.println();
 Serial.println("DS1b20_1 Sketch");
 Serial.println();
sensors.requestTemperatures(); //to issue a global temperature
//request to all devices on the bus
Serial.println("Requesting temperatures");
sensors.requestTemperatures(); //Send the command to get temperatures
Serial.print("Temperature for Device 1 is: ");
Serial.println(sensors.getTempCByIndex(0));
delay(750);
Serial.println("DONE");
sensors.requestTemperatures(); //Send the command to get temperatures
Serial.print("Temperature for Device 2 is: ");
Serial.println(sensors.getTempCByIndex(1));
delay(750);
Serial.println("DONE");
sensors.requestTemperatures(); //Send the command to get temperatures
Serial.print("Temperature for Device 3 is: ");
Serial.println(sensors.getTempCByIndex(2));
delay(750);
Serial.println("DONE");
sensors.requestTemperatures(); //Send the command to get temperatures
Serial.print("Temperature for Device 4 is: ");
Serial.println(sensors.getTempCByIndex(3));
delay(750);
Serial.println("DONE");
sensors.requestTemperatures(); //Send the command to get temperatures
Serial.print("Temperature for Device 5 is: ");
Serial.println(sensors.getTempCByIndex(4));
delay(750);
Serial.println("DONE");
delay(3000);
//Why „byIndex“? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
}

Wobei 0 der 1. am Bus ist, 1 der 2. usw.