forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P514_Dallas_USB.py
166 lines (156 loc) · 5.55 KB
/
_P514_Dallas_USB.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
#############################################################################
################### Dallas onewire USB plugin for RPIEasy ###################
#############################################################################
#
# Based on pydigitemp and pyserial
#
# https://www.instructables.com/id/Quick-Digital-Thermometer-Using-Cheap-USB-to-TTL-C/
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import serial
import serial.tools.list_ports
import digitemp.master
import digitemp.device
class Plugin(plugin.PluginProto):
PLUGIN_ID = 514
PLUGIN_NAME = "Environment - DS1820 USB"
PLUGIN_VALUENAME1 = "Temperature"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_SER
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
self.readinprogress = 0
self.valuecount = 1
self.senddataoption = True
self.timeroption = True
self.timeroptional = False
self.formulaoption = True
self.sensor = None
self.bus = None
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.initialized = False
try:
if self.bus is not None:
self.bus.close()
self.bus._unlock()
except:
pass
if self.enabled or enableplugin:
if (str(self.taskdevicepluginconfig[0]) != "0") and (str(self.taskdevicepluginconfig[0]).strip() != ""):
try:
self.bus = digitemp.master.UART_Adapter(str(self.taskdevicepluginconfig[0]))
except:
pass
if (str(self.taskdevicepluginconfig[1]) != "0") and (str(self.taskdevicepluginconfig[1]).strip() != ""):
try:
if self.taskdevicepluginconfig[1].startswith("10"):
self.sensor = digitemp.device.DS1820(self.bus, rom=str(self.taskdevicepluginconfig[1]))
self.initialized = True
elif self.taskdevicepluginconfig[1].startswith("28"):
self.sensor = digitemp.device.DS18B20(self.bus, rom=str(self.taskdevicepluginconfig[1]))
self.initialized = True
elif self.taskdevicepluginconfig[1].startswith("22"):
self.sensor = digitemp.device.DS1822(self.bus, rom=str(self.taskdevicepluginconfig[1]))
self.initialized = True
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"DS Type uknown for "+self.taskdevicepluginconfig[1])
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"USB-Dallas "+str(e))
self.initialized = False
if self.initialized==False:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"USB-Dallas device can not be initialized!")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"USB-Dallas device initialized!")
def webform_load(self):
choice0 = self.taskdevicepluginconfig[0]
options0 = self.serial_portlist()
if len(options0)>0:
webserver.addHtml("<tr><td>Serial Device:<td>")
webserver.addSelector_Head("p514_addr",False)
for o in range(len(options0)):
webserver.addSelector_Item(options0[o],options0[o],(str(options0[o])==str(choice0)),False)
webserver.addSelector_Foot()
choice1 = self.taskdevicepluginconfig[1]
options1 = self.find_dsb_devices()
if len(options1)>0:
webserver.addHtml("<tr><td>Device Address:<td>")
webserver.addSelector_Head("p514_id",True)
for o in range(len(options1)):
webserver.addSelector_Item(options1[o],options1[o],(str(options1[o])==str(choice1)),False)
webserver.addSelector_Foot()
else:
webserver.addFormNote("No DS18B20 found on bus!")
else:
webserver.addFormNote("No serial port found!")
webserver.addFormNote("You have to connect the Ds18B20 through an USB-Serial adapter!")
return True
def webform_save(self,params):
par = str(webserver.arg("p514_addr",params))
p1 = str(self.taskdevicepluginconfig[0])
self.taskdevicepluginconfig[0] = par
par = str(webserver.arg("p514_id",params))
p2 = str(self.taskdevicepluginconfig[1])
self.taskdevicepluginconfig[1] = str(par)
if p1 != str(self.taskdevicepluginconfig[0]) or p2 != str(self.taskdevicepluginconfig[1]):
self.plugin_init()
return True
def plugin_read(self): # deal with data processing at specified time interval
result = False
if self.initialized and self.readinprogress==0 and self.enabled:
self.readinprogress = 1
try:
succ, temp = self.read_temperature()
if succ:
self.set_value(1,temp,True)
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Dallas read error!")
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Dallas read error! "+str(e))
self.enabled = False
self._lastdataservetime = rpieTime.millis()
result = True
self.readinprogress = 0
return result
def serial_portlist(self):
ports = []
try:
for port in serial.tools.list_ports.comports():
ports.append(str(port.device))
except:
pass
return ports
def find_dsb_devices(self):
self.readinprogress = 1
try:
if self.bus is not None:
self.bus.close()
self.bus_unlock()
self.initialized = False
except:
pass
rlist = []
try:
if str(self.taskdevicepluginconfig[0])!="0" and str(self.taskdevicepluginconfig[0]).strip()!="":
self.bus = digitemp.master.UART_Adapter(str(self.taskdevicepluginconfig[0]))
rlist = digitemp.device.AddressableDevice(self.bus).get_connected_ROMs()
self.plugin_init()
except Exception as e:
rlist = []
self.readinprogress = 0
return rlist
def read_temperature(self):
try:
if self.bus.uart.is_open == False:
self.plugin_init()
return True, float(self.sensor.get_temperature())
except Exception as e:
pass
return False, 0