forked from sailoog/openplotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1w.py
75 lines (65 loc) · 2.03 KB
/
1w.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# This file is part of Openplotter.
# Copyright (C) 2015 by sailoog <https://github.com/sailoog/openplotter>
#
# Openplotter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
# Openplotter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Openplotter. If not, see <http://www.gnu.org/licenses/>.
import socket, pynmea2, time
from w1thermsensor import W1ThermSensor
from classes.conf import Conf
conf=Conf()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sensors_list=eval(conf.get('1W', 'DS18B20'))
sensors=[]
for index,item in enumerate(sensors_list):
try:
type=W1ThermSensor.THERM_SENSOR_DS18B20
for sensor in W1ThermSensor.get_available_sensors():
if item[3] == sensor.id:
type = sensor.type
sensors.append(W1ThermSensor(type, item[3]))
except Exception,e:
sensors_list[index][5]='0'
print str(e)
while True:
time.sleep(0.01)
temp=''
list_tmp=[]
ib=0
c=0
try:
for i in sensors_list:
if i[5]=='1':
if i[2]=='C': unit=W1ThermSensor.DEGREES_C
if i[2]=='F': unit=W1ThermSensor.DEGREES_F
if i[2]=='K': unit=W1ThermSensor.KELVIN
temp=sensors[ib].get_temperature(unit)
temp=round(temp,1)
list_tmp.append('C')
list_tmp.append(str(temp))
list_tmp.append(i[2])
list_tmp.append(i[4])
ib=ib+1
c=c+1
if c==3:
xdr = pynmea2.XDR('OS', 'XDR', (list_tmp))
xdr1=str(xdr)
xdr2=xdr1+"\r\n"
sock.sendto(xdr2, ('127.0.0.1', 10110))
list_tmp=[]
c=0
if c>0:
xdr = pynmea2.XDR('OS', 'XDR', (list_tmp))
xdr1=str(xdr)
xdr2=xdr1+"\r\n"
sock.sendto(xdr2, ('127.0.0.1', 10110))
except Exception,e: print str(e)