-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
_P025_ADS1x15.py
147 lines (136 loc) · 4.79 KB
/
_P025_ADS1x15.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
#!/usr/bin/env python3
#############################################################################
#################### ADS1015/ADS1115 plugin for RPIEasy #####################
#############################################################################
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import lib.lib_adsrouter as ADS #import Adafruit_ADS1x15 as ADS
import gpios
class Plugin(plugin.PluginProto):
PLUGIN_ID = 25
PLUGIN_NAME = "Analog input - ADS1x15"
PLUGIN_VALUENAME1 = "Analog"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_I2C
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
self.ports = 0
self.readinprogress = 0
self.valuecount = 1
self.senddataoption = True
self.timeroption = True
self.timeroptional = True
self.formulaoption = True
self.adc = None
self._nextdataservetime = 0
self.lastread = 0
self.samples = 3
self.preread = self.samples*1000 # 3 * 1 sec
self.TARR = []
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.TARR = []
if self.enabled:
try:
i2cl = self.i2c
except:
i2cl = -1
try:
i2cport = gpios.HWPorts.geti2clist()
if i2cl==-1:
i2cl = int(i2cport[0])
except:
i2cport = []
if len(i2cport)>0 and i2cl>-1:
if self.interval>2:
nextr = self.interval-2
else:
nextr = self.interval
self._lastdataservetime = rpieTime.millis()-(nextr*1000)
self.preread = self.samples*1000
self.ports = str(self.taskdevicepluginconfig[3])
if int(self.taskdevicepluginconfig[0]) in [10,11]:
try:
self.adc = ADS.request_ads_device(int(self.taskdevicepluginconfig[1]),i2cl,int(self.taskdevicepluginconfig[0]))
self.initialized = self.adc.initialized
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"ADS can not be initialized! "+str(e))
self.initialized = False
else:
self.initialized = False
def webform_load(self): # create html page for settings
choice1 = self.taskdevicepluginconfig[0]
options = ["ADS1015","ADS1115"]
optionvalues = [10,11]
webserver.addFormSelector("Type","plugin_025_type",2,options,optionvalues,None,int(choice1))
choice2 = self.taskdevicepluginconfig[1]
options = ["0x48","0x49","0x4A","0x4B"]
optionvalues = [0x48,0x49,0x4a,0x4b]
webserver.addFormSelector("Address","plugin_025_addr",4,options,optionvalues,None,int(choice2))
webserver.addFormNote("Enable <a href='pinout'>I2C bus</a> first, than <a href='i2cscanner'>search for the used address</a>!")
choice3 = self.taskdevicepluginconfig[2]
options = ["2/3","1","2","4","8","16"]
optionvalues = [(2/3),1,2,4,8,16]
webserver.addFormSelector("Gain","plugin_025_gain",len(optionvalues),options,optionvalues,None,float(choice3))
choice4 = self.taskdevicepluginconfig[3]
options = ["A0","A1","A2","A3"]
optionvalues = [0,1,2,3]
webserver.addFormSelector("Analog pin","plugin_025_apin",4,options,optionvalues,None,int(choice4))
webserver.addFormCheckBox("Oversampling","plugin_025_over",self.timer1s)
return True
def webform_save(self,params): # process settings post reply
par = webserver.arg("plugin_025_type",params)
if par == "":
par = 0
self.taskdevicepluginconfig[0] = int(par)
par = webserver.arg("plugin_025_addr",params)
if par == "":
par = 0
self.taskdevicepluginconfig[1] = int(par)
par = webserver.arg("plugin_025_gain",params)
if par == "":
par = 1
self.taskdevicepluginconfig[2] = float(par)
par = webserver.arg("plugin_025_apin",params)
if par == "":
par = 0
self.taskdevicepluginconfig[3] = int(par)
if (webserver.arg("plugin_025_over",params)=="on"):
self.timer1s = True
else:
self.timer1s = False
self.plugin_init()
return True
def plugin_read(self): # deal with data processing at specified time interval
result = False
if self.initialized and self.enabled:
self.p025_get_value()
if len(self.TARR)>0:
self.set_value(1,(sum(self.TARR) / float(len(self.TARR))),False)
self.plugin_senddata()
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"ADS1x15 read failed!")
result = True
self.TARR = []
self._lastdataservetime = rpieTime.millis()
self._nextdataservetime = self._lastdataservetime + (self.interval*1000)
return result
def timer_once_per_second(self):
if self.initialized and self.enabled:
if self._nextdataservetime-rpieTime.millis()<=self.preread:
self.p025_get_value()
return self.timer1s
def p025_get_value(self):
val = -1
try:
val = self.adc.ADread(self.taskdevicepluginconfig[3],self.taskdevicepluginconfig[2])
except Exception as e:
val = -1
if val != -1:
self.TARR.append(val)