-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
_P206_PZEM016.py
228 lines (218 loc) · 8.02 KB
/
_P206_PZEM016.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
#############################################################################
##################### PZEM016/004 plugin for RPIEasy ########################
#############################################################################
#
# Plugin for the USB-RS485 PZEM-016 device reading
#
# Available commands:
# PZEMADDRESS,<currentaddress>,<newaddress> - change address of pzem device
# PZEMRESET,<address> - PZEM reset energy at address
#
# Copyright (C) 2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import time
import lib.lib_pzem as uPZEM
import lib.lib_serial as rpiSerial
class Plugin(plugin.PluginProto):
PLUGIN_ID = 206
PLUGIN_NAME = "Energy (AC) - PZEM016/004 Modbus (TESTING)"
PLUGIN_VALUENAME1 = "Volt"
PLUGIN_VALUENAME2 = "Amper"
PLUGIN_VALUENAME3 = "Watt"
PLUGIN_VALUENAME4 = "Wh"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_SER
self.vtype = rpieGlobals.SENSOR_TYPE_QUAD
self.valuecount = 4
self.senddataoption = True
self.recdataoption = False
self.timeroption = True
self.timeroptional = False
self.formulaoption = True
self.pzem = None
self.readinprogress=0
self.initialized=False
def webform_load(self): # create html page for settings
choice1 = self.stripstring(self.taskdevicepluginconfig[0])
options = rpiSerial.serial_portlist()
if len(options)>0:
webserver.addHtml("<tr><td>Serial Device:<td>")
webserver.addSelector_Head("p206_addr",False)
for o in range(len(options)):
options[o] = self.stripstring(options[o])
webserver.addSelector_Item(options[o],options[o],(str(options[o])==str(choice1)),False)
webserver.addSelector_Foot()
webserver.addFormNote("Address of the USB-RS485/232 converter")
else:
webserver.addFormNote("No serial ports found")
webserver.addFormNumericBox("Slave address","p206_saddr",self.taskdevicepluginconfig[1],1,248)
webserver.addFormNote("Default address is 1. Use 'pzemaddress,[currentaddress],[newaddress]' command to change it. (broadcast=248)")
options = ["Fast PZEM016", "Slow PZEM004"]
optionvalues = [0, 1]
webserver.addFormSelector("Device type","plugin_206_slow",len(options),options,optionvalues,None,self.taskdevicepluginconfig[6])
if self.taskname=="":
choice1 = 0
choice2 = 1
choice3 = 3
choice4 = 5
else:
choice1 = self.taskdevicepluginconfig[2]
choice2 = self.taskdevicepluginconfig[3]
choice3 = self.taskdevicepluginconfig[4]
choice4 = self.taskdevicepluginconfig[5]
options = ["None", "Volt","Amper","Watt", "Wh","Hz","PwrFact"]
optionvalues = [-1, 0, 1, 3, 5, 7, 8]
webserver.addFormSelector("Indicator1","plugin_206_ind0",len(options),options,optionvalues,None,choice1)
webserver.addFormSelector("Indicator2","plugin_206_ind1",len(options),options,optionvalues,None,choice2)
webserver.addFormSelector("Indicator3","plugin_206_ind2",len(options),options,optionvalues,None,choice3)
webserver.addFormSelector("Indicator4","plugin_206_ind3",len(options),options,optionvalues,None,choice4)
# webserver.addFormCheckBox("Increase timeout for slow PZEM004 compatibility","plugin_206_slow",self.taskdevicepluginconfig[6])
return True
def webform_save(self,params): # process settings post reply
paddr = self.taskdevicepluginconfig[1]
ninit = False
par = webserver.arg("p206_saddr",params)
try:
self.taskdevicepluginconfig[1] = int(par)
except:
self.taskdevicepluginconfig[1] = 1
try:
if int(paddr) != int(self.taskdevicepluginconfig[1]):
ninit = True
except:
ninit = True
try:
self.taskdevicepluginconfig[6] = int(webserver.arg("plugin_206_slow",params))
except:
self.taskdevicepluginconfig[6] = 0
try:
self.taskdevicepluginconfig[0] = self.stripstring(webserver.arg("p206_addr",params))
for v in range(0,4):
par = webserver.arg("plugin_206_ind"+str(v),params)
if par == "":
par = -1
else:
par=int(par)
if str(self.taskdevicepluginconfig[v+2])!=str(par):
self.uservar[v] = 0
self.taskdevicepluginconfig[v+2] = par
if int(par)>0 and self.valuecount!=v+1:
self.valuecount = (v+1)
if self.valuecount == 1:
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
elif self.valuecount == 2:
self.vtype = rpieGlobals.SENSOR_TYPE_DUAL
elif self.valuecount == 3:
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
elif self.valuecount == 4:
self.vtype = rpieGlobals.SENSOR_TYPE_QUAD
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,+str(e))
if ninit:
self.plugin_init()
return True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.taskdevicepluginconfig[0] = self.stripstring(self.taskdevicepluginconfig[0])
self.readinprogress=0
self.initialized=False
if self.valuecount == 1:
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
elif self.valuecount == 2:
self.vtype = rpieGlobals.SENSOR_TYPE_DUAL
elif self.valuecount == 3:
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
elif self.valuecount == 4:
self.vtype = rpieGlobals.SENSOR_TYPE_QUAD
if self.enabled and self.taskdevicepluginconfig[0]!="" and self.taskdevicepluginconfig[0]!="0":
self.ports = str(self.taskdevicepluginconfig[0])+"/"+str(self.taskdevicepluginconfig[1])
if self.taskdevicepluginconfig[6]:
timeout = 3
else:
timeout = 0.1
try:
if timeout>1:
self.pzem = uPZEM.request_pzem4_device(self.taskdevicepluginconfig[0],self.taskdevicepluginconfig[1],timeout)
else:
self.pzem = uPZEM.request_pzem_device(self.taskdevicepluginconfig[0],self.taskdevicepluginconfig[1],timeout)
if self.pzem != None and self.pzem.initialized:
if timeout>1:
sl = "slow "
else:
sl = ""
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"PZEM "+sl+"initialized at: "+str(self.taskdevicepluginconfig[0])+" / " +str(self.taskdevicepluginconfig[1]))
self.initialized=True
self.readinprogress = 0
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PZEM init failed at address "+str(self.taskdevicepluginconfig[1]))
except Exception as e:
self.pzem = None
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PZEM init error: "+str(e))
else:
self.ports = ""
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
for v in range(0,4):
vtype = int(self.taskdevicepluginconfig[v+2])
if vtype != -1:
try:
c = 10
while self.pzem.busy and c>0:
time.sleep(0.05)
c=c-1
value = self.pzem.read_value(vtype)
except:
value = None
if value != None:
self.set_value(v+1,value,False)
self.plugin_senddata()
self._lastdataservetime = rpieTime.millis()
result = True
self.readinprogress = 0
return result
def plugin_write(self,cmd):
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0][:4] != "pzem":
return False
if cmdarr[0] == "pzemaddress":
try:
ca = int(cmdarr[1].strip())
na = int(cmdarr[2].strip())
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Parameter error: "+str(e))
return False
tpzem = uPZEM.request_pzem_device(self.taskdevicepluginconfig[0],ca)
if tpzem:
res = tpzem.changeAddress(na)
else:
res = False
elif cmdarr[0] == "pzemreset":
try:
ca = int(cmdarr[1].strip())
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Parameter error: "+str(e))
return False
tpzem = uPZEM.request_pzem_device(self.taskdevicepluginconfig[0],ca)
if tpzem:
res = tpzem.resetenergy()
if res==False:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PZEM reset command failed!")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PZEM device not found!")
res = False
return res
def stripstring(self,tstr):
sentence = str(tstr)
sentence = ''.join(sentence.split())
return sentence