forked from switchdoclabs/SDL_Pi_DataLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINA3221Functions.py
More file actions
220 lines (162 loc) · 7.5 KB
/
INA3221Functions.py
File metadata and controls
220 lines (162 loc) · 7.5 KB
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
######################################
#
# readINA3221Data and buildINA3221Graph
#
#
######################################
import gc
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
from matplotlib import pyplot
from matplotlib import dates
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import pylab
from datetime import datetime
import pymysql as mdb
import SDL_Pi_INA3221
# the three channels of the INA3221 named for SunAirPlus Solar Power Controller channels (www.switchdoc.com)
LIPO_BATTERY_CHANNEL = 1
SOLAR_CELL_CHANNEL = 2
OUTPUT_CHANNEL = 3
INA3221tableName = 'INA3221Table'
#initialize the ina3221 board
ina3221 = SDL_Pi_INA3221.SDL_Pi_INA3221(addr=0x40)
def readINA3221Data(password):
print('readINA3221Data - The time is: %s' % datetime.now())
# open database
con = mdb.connect('localhost', 'root', password, 'DataLogger' )
cur = con.cursor()
print("------------------------------")
shuntvoltage1 = 0
busvoltage1 = 0
current_mA1 = 0
loadvoltage1 = 0
busvoltage1 = ina3221.getBusVoltage_V(LIPO_BATTERY_CHANNEL)
print( "------------------------------")
shuntvoltage1 = ina3221.getShuntVoltage_mV(LIPO_BATTERY_CHANNEL)
# minus is to get the "sense" right. - means the battery is charging, + that it is discharging
current_mA1 = ina3221.getCurrent_mA(LIPO_BATTERY_CHANNEL)
loadvoltage1 = busvoltage1 + (shuntvoltage1 / 1000)
# set Label
# myLabel = "LIPO_Battery"
myLabel = ""
print( "(Channel 1) %s Bus Voltage 1: %3.2f V " % (myLabel, busvoltage1))
print( "(Channel 1) %s Shunt Voltage 1: %3.2f mV " % (myLabel, shuntvoltage1))
print( "(Channel 1) %s Load Voltage 1: %3.2f V" % (myLabel, loadvoltage1))
print( "(Channel 1) %s Current 1: %3.2f mA" % (myLabel, current_mA1))
print
shuntvoltage2 = 0
busvoltage2 = 0
current_mA2 = 0
loadvoltage2 = 0
busvoltage2 = ina3221.getBusVoltage_V(SOLAR_CELL_CHANNEL)
shuntvoltage2 = ina3221.getShuntVoltage_mV(SOLAR_CELL_CHANNEL)
# "-" removed for this demo program
current_mA2 = ina3221.getCurrent_mA(SOLAR_CELL_CHANNEL)
loadvoltage2 = busvoltage2 + (shuntvoltage2 / 1000)
# set Label
# myLabel = "Solar Cell"
myLabel = ""
print ("(Channel 2) %s Bus Voltage 2: %3.2f V " % (myLabel, busvoltage2))
print ("(Channel 2) %s Shunt Voltage 2: %3.2f mV " % (myLabel, shuntvoltage2))
print ("(Channel 2) %s Load Voltage 2: %3.2f V" % (myLabel, loadvoltage2))
print ("(Channel 2) %s Current 2: %3.2f mA" % (myLabel, current_mA2))
print
shuntvoltage3 = 0
busvoltage3 = 0
current_mA3 = 0
loadvoltage3 = 0
busvoltage3 = ina3221.getBusVoltage_V(OUTPUT_CHANNEL)
shuntvoltage3 = ina3221.getShuntVoltage_mV(OUTPUT_CHANNEL)
current_mA3 = ina3221.getCurrent_mA(OUTPUT_CHANNEL)
loadvoltage3 = busvoltage3 + (shuntvoltage3 / 1000)
# set Label
# myLabel = "Output"
myLabel = ""
print( "(Channel 3) %s Bus Voltage 3: %3.2f V " % (myLabel, busvoltage3))
print( "(Channel 3) %s Shunt Voltage 3: %3.2f mV " % (myLabel, shuntvoltage3))
print( "(Channel 3) %s Load Voltage 3: %3.2f V" % (myLabel, loadvoltage3))
print( "(Channel 3) %s Current 3: %3.2f mA" % (myLabel, current_mA3))
print
#
# Now put the data in MySQL
#
# Put record in MySQL
print( "writing SQLdata ")
# write record
deviceid = 0
query = 'INSERT INTO '+INA3221tableName+('(timestamp, deviceid, channel1_load_voltage, channel1_current, channel2_load_voltage, channel2_current, channel3_load_voltage, channel3_current) VALUES(UTC_TIMESTAMP(), %i, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f)' %( deviceid, busvoltage1, current_mA1, busvoltage2, current_mA2, busvoltage3, current_mA3))
print("query=%s" % query)
cur.execute(query)
con.commit()
# INA3221 graph building routine
def buildINA3221Graph(password, myGraphSampleCount):
print('buildINA3221Graph - The time is: %s' % datetime.now())
# open database
con1 = mdb.connect('localhost', 'root', password, 'DataLogger' )
# now we have to get the data, stuff it in the graph
mycursor = con1.cursor()
print( myGraphSampleCount)
query = '(SELECT timestamp, deviceid, channel1_load_voltage, channel1_current, channel2_load_voltage, channel2_current, channel3_load_voltage, channel3_current, id FROM '+INA3221tableName+' ORDER BY id DESC LIMIT '+ str(myGraphSampleCount) + ') ORDER BY id ASC'
print("query=", query)
try:
mycursor.execute(query)
result = mycursor.fetchall()
print("---------")
except:
print("3---------")
e=sys.exc_info()[0]
print("Error: %s" % e)
print (result[0])
t = [] # time
u = [] # channel 1 - Current
averageCurrent = 0.0
currentCount = 0
for record in result:
t.append(record[0])
u.append(record[3])
averageCurrent = averageCurrent+record[3]
currentCount=currentCount+1
averageCurrent = averageCurrent/currentCount
print ("count of t=",len(t))
#x1 = [datetime.strptime(d, '%Y-%m-%d %H:%M:%S',) for d in t]
x1 = [d for d in t]
fds = dates.date2num(x1) # converted
# matplotlib date format object
hfmt = dates.DateFormatter('%H:%M:%S')
#hfmt = dates.DateFormatter('%m/%d-%H')
fig = pyplot.figure()
fig.set_facecolor('white')
ax = fig.add_subplot(111,facecolor = 'white')
ax.vlines(fds, -200.0, 1000.0,colors='w')
#ax.xaxis.set_major_locator(dates.MinuteLocator(interval=1))
ax.xaxis.set_major_formatter(hfmt)
ax.set_ylim(bottom = 0.0)
pyplot.xticks(rotation='45')
pyplot.subplots_adjust(bottom=.3)
pylab.plot(fds, u, color='r',label="OurWeather Current",linestyle="-",marker=".")
pylab.xlabel("Seconds")
pylab.ylabel("Current mA")
pylab.legend(loc='lower center')
print("-----")
print(max(u))
print("-----")
pylab.axis([min(fds), max(fds), 0, max(u)+20])
pylab.figtext(.5, .05, ("Average Current %6.2fmA\n%s") %(averageCurrent, datetime.now()),fontsize=18,ha='center')
pylab.grid(True)
pyplot.show()
pyplot.savefig("/var/www/html/INA3221DataLoggerGraph.png", facecolor=fig.get_facecolor())
mycursor.close()
con1.close()
fig.clf()
pyplot.close()
pylab.close()
gc.collect()
print( "------INA3221Graph finished now")
######################################
#
# readINA3221Data and buildINA3221Graph
#
#
######################################