DS1820 Temperatursensor am Raspberry Pi

DS1820 Temperatursensor am Raspberry Pi auslesen.

Inbetriebnahme

Die notwendigen 1-Wire Kernelmodule müssen geladen werden. Für das automatische laden beim Booten eintragen in /etc/modules-load.d/raspberrypi.conf:

w1-gpio
w1-therm

In /boot/config.txt:

dtoverlay=w1-gpio,gpiopin=18

Die Angaben sind für GPIO-18 und keine parasitäre Spannungsversorgung.

Den Messwert auslesen:

cd /sys/bus/w1/devices
cd 28-02151541c5ff
cat w1-slave

Das gibt eine Temperatur von 31.125°C aus:

f2 01 55 00 7f ff 0c 10 69 : crc=69 YES
f2 01 55 00 7f ff 0c 10 69 t=31125

Links

Java Test Programm

Download: Java-Source

Quellcode:

package net.oberguru.wetterfrosch;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * Class DS1820
 * 
 * Reads the temperature from a DS1B20 sensor.
 * Needs the w1-gpio und w1-therm kernel driver modules. 
 * 
 * @author Christian Paul
 *
 */
public class DS1820 {

    /* path to search for devices in filesystem */
    private static String devicesPath = "/sys/bus/w1/devices";

    /* file of the measured values */
    private static String valueFile = "w1_slave";

    /* id of sensor */
    private String id = null;

    /**
     * Get the sensor id
     * @return sensor id
     */
    public String getId() {
       return id;
    }

    /**
     * Set the sensor id
     * @param id sensor id
     */
    public void setId(String id) {
       this.id = id;
    }

    /**
     * Get the temperature
     * @return the current temperature in Celsius
     */
    public double read() {
       // if id is null, search for sensor and take the first
       if (getId()==null) {
         List<String> ids = DS1820.list();
         if (ids.isEmpty())
          return Double.MAX_VALUE;
         else
          id = ids.get(0);
       }
       // read sensor
       return DS1820.read(getId());
    }

    /**
     * Read sensor and returns temperature
     * @param sensorId the id of the sensor
     * @return the temperature
     */
    public static double read(String sensorId) {
       Path path = FileSystems.getDefault().getPath(devicesPath, sensorId, valueFile);
       List<String> lines;

       int attempts = 3;
       boolean crcOK = false;

       while (attempts > 0) {
         try {
          lines = Files.readAllLines(path);
          for(String line: lines) {
              if (line.endsWith("YES"))
                 crcOK = true;
              else if (line.matches(".*t=[0-9-]+") && crcOK) 
                 return Integer.valueOf(line.substring(line.indexOf("=")+1))/1000.0;
          }
         } catch (Exception e) {
          e.printStackTrace();
         }
         attempts--;
       }

       return Double.MAX_VALUE;
    }

    /** 
     * List the id's of all available sensors.
     * = all subdirectories found in /sys/bus/w1/devices/
     * @return list of sensors
     */
    public static List<String> list() {
       List<String> list = new ArrayList<String>();
       File searchPath = new File(devicesPath);
       if (searchPath.listFiles()!=null) {
         for (File f: searchPath.listFiles()) {
          if (f.isDirectory() && !f.getName().startsWith("w1_bus_master")) 
              list.add(f.getName());
         }
       }
       return list;
    }

    /**
     * Start 
     * @param args
     */
    public static void main(String[] args) {
       System.out.println("DS1820 Temperature Sensor Tool");
       System.out.println("------------------------------");
       if (args.length == 0) {
         for(String id: DS1820.list()) {
          System.out.println("  found sensor: " + id);
          System.out.print("  read sensor " + id + " ...");
          double t = DS1820.read(id);
          System.out.println(" --> temp: " + t);
         }
       }
       else {
         System.out.print("  read sensor " + args[0] + " ...");
         double t = DS1820.read(args[0]);
         System.out.println(" --> temp: " + t);
       }
       System.out.println();
    }

}