2

I want to read temperature from DS18B20 sensors. Sensors are connected and works fine. How can I read temperature using Pi4J? Is there API for this?

  • Why don't you just poll the /sys/bus/device/... node? Do you need to check the temperature 100000 times a second? – goldilocks Dec 29 '15 at 19:26
  • Would you care to elaborate @goldilocks what connection does a 100KHz sample rate got to do with Pi4J which seems to be a java I/O library the for Raspberry Pi and a device presented in the sysfs file-system area by the kernel? – SlySven Dec 29 '15 at 20:39
  • 1
    here is an excellent java example github.com/marcandreuf/sunfounder-sensors-raspi-4j/blob/master/… – mikebertiean Apr 21 '16 at 20:00
2

Pi4J has released a 1-Wire API in version 1.1. It's no longer a SNAPSHOT version.

W1Master master = new W1Master();
List<W1Device> w1Devices = master.getDevices(TmpDS18B20DeviceType.FAMILY_CODE);
for (W1Device device : w1Devices) {
    //this line is enought if you want to read the temperature
    System.out.println("Temperature: " + ((TemperatureSensor) device).getTemperature());
    //returns the temperature as double rounded to one decimal place after the point

    try {
        System.out.println("1-Wire ID: " + device.getId() +  " value: " + device.getValue());
        //returns the ID of the Sensor and the  full text of the virtual file
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Maven Dependencies:

<dependencies>
    <dependency>
        <groupId>com.pi4j</groupId>
        <artifactId>pi4j-core</artifactId>
        <version>1.1</version>
        <exclusions>
            <exclusion>
                <groupId>com.pi4j</groupId>
                <artifactId>pi4j-native</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- used for the TemperatureSensor interface -->
    <dependency>
        <groupId>com.pi4j</groupId>
        <artifactId>pi4j-device</artifactId>
        <version>1.1</version>
    </dependency>
</dependencies>
1

The API is open the file /sys/bus/w1/devices/28-00*/w1_slave, read the file, close the file, and then parse the results. Do that as often as needed but any faster than once every four seconds is pretty pointless.

Here is some Python code you can adapt.

#!/usr/bin/env python

import glob
import time

# Typical reading
# 73 01 4b 46 7f ff 0d 10 41 : crc=41 YES
# 73 01 4b 46 7f ff 0d 10 41 t=23187

while True:

   for sensor in glob.glob("/sys/bus/w1/devices/28-00*/w1_slave"):
      id = sensor.split("/")[5]

      try:
         f = open(sensor, "r")
         data = f.read()
         f.close()
         if "YES" in data:
            (discard, sep, reading) = data.partition(' t=')
            t = float(reading) / 1000.0
            print("{} {:.1f}".format(id, t))
         else:
            print("999.9")

      except:
         pass

   time.sleep(3.0)
  • I know how to read file. I just thought that there is some function which gets sensor id and returns temperature. That's all. – mariusz2108 Dec 30 '15 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.