1 """
2 Usage
3 =====
4 List devices connected to the onewire bus (W1-Bus).
5 >>> w1 = W1Bus()
6 >>> print w1.devices
7 ['10-0008028d5c8a', 'w1_bus_master1']
8
9 Read temperature value of a connected device identified by the list index.
10 >>> w1.read_value(0)
11 29.312
12
13 @author: Stefan Lehmann
14 @version: 1.0.0
15 @summary: Access temperature sensors connected to the onewire bus.
16
17 """
18
19
20 import os
21
22
23 W1_DEVICE_DIRECTORY = "/sys/bus/w1/devices"
24
25
27 """
28 Class for accessing the onewire bus of the Raspberry Pi
29
30 @type devices: list
31 @ivar devices: list of the connected devices, the index is the device id
32 """
34 self.devices = []
35 self.refresh()
36
38 """
39 Return the filename of the given sensor.
40 @rtype: str
41 """
42 path_ = os.path.join(W1_DEVICE_DIRECTORY, self.devices[index])
43 path_ = os.path.join(path_, "w1_slave")
44
45 return path_
46
48 """
49 Return the temperature of the sensor with the given index.
50 @type index: int
51 @param index: sensor index in the device list
52 @rtype: float
53
54 """
55
56 with open(self.filename(index)) as f:
57 text = f.read()
58 second_line = text.split("\n")[1]
59 temperature_data = second_line.split(" ")[9]
60 temperature = float(temperature_data[2:]) / 1000.0
61 return temperature
62
68
69 os.system('sudo modprobe w1-gpio')
70 os.system('sudo modprobe w1-therm')
71