1 """
2 @author: Stefan Lehmann
3
4 """
5
6 import os
7
8 W1_DEVICE_DIRECTORY = "/sys/bus/w1/devices"
9
11 """
12 Class for accessing the onewire bus of the Raspberry Pi
13
14 @type devices: list
15 @ivar devices: list of the connected devices, the index is the device id
16 """
19
21 """
22 Return the filename of the given sensor.
23 @rtype: str
24 """
25 path_ = os.path.join(W1_DEVICE_DIRECTORY, self.devices[device_id])
26 path_ = os.path.join(path_, "w1_slave")
27 return path_
28
30 """
31 Return the temperature of the sensor with the given id.
32 @type device_id: int
33 @param device_id: sensor id in the device list
34 @rtype: float
35
36 """
37
38 with open(self.filename(device_id)) as f:
39 text = f.read()
40 second_line = text.split("\n")[1]
41 temperature_data = second_line.split(" ")[9]
42 temperature = float(temperature_data[2:]) / 1000.0
43 return temperature
44
45 os.system('sudo modprobe w1-gpio')
46 os.system('sudo modprobe w1-therm')
47