Package rpitemp :: Module rpitemp
[hide private]
[frames] | no frames]

Source Code for Module rpitemp.rpitemp

 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   
26 -class W1Bus():
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 """
33 - def __init__(self):
34 self.devices = [] 35 self.refresh()
36
37 - def filename(self, index):
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
47 - def read_value(self, index):
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
63 - def refresh(self):
64 """ 65 Refresh W1 device list. 66 """ 67 self.devices = os.listdir(W1_DEVICE_DIRECTORY)
68 #start kernel modules for onewire bus 69 os.system('sudo modprobe w1-gpio') 70 os.system('sudo modprobe w1-therm') 71