-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathINA219Functions.py
More file actions
213 lines (152 loc) · 6.38 KB
/
INA219Functions.py
File metadata and controls
213 lines (152 loc) · 6.38 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
from __future__ import print_function
######################################
#
# readINA219Data and buildINA219Graph
#
#
######################################
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
import matplotlib.dates as mdates
from datetime import datetime
import pymysql as mdb
import sys
from ina219 import INA219
from ina219 import DeviceRangeError
SHUNT_OHMS = 0.1
INA219tableName = 'INA219Table'
#initialize the ina3221 board
ina219 = INA219(SHUNT_OHMS,address=0x45)
ina219.configure()
def readINA219Data(username, password):
print('readINA219Data - The time is: %s' % datetime.now())
# open database
con = mdb.connect('localhost', username, password, 'DataLogger' )
cur = con.cursor()
print("------------------------------")
voltage = ina219.voltage()
current = ina219.current()
power = ina219.power()
print("------------------------------")
# minus is to get the "sense" right. - means the battery is charging, + that it is discharging
# set Label
myLabel = ""
print("%s Voltage : %3.2f V" % (myLabel, voltage))
print("%s Current : %3.2f mA" % (myLabel, current))
print("%s Power : %3.2f mW" % (myLabel, power))
print()
#
# Now put the data in MySQL
#
# Put record in MySQL
print("writing SQLdata ")
# write record
deviceid = 0
query = 'INSERT INTO '+INA219tableName+('(TimeStamp, Voltage, Current, Power) VALUES(UTC_TIMESTAMP(3), %.3f, %.3f, %.3f )' %( voltage, current, power ))
print("query=%s" % query)
cur.execute(query)
con.commit()
# INA219 graph building routine
def strftime_ms(datetime_obj):
y,m,d,H,M,S = datetime_obj.timetuple()[:6]
ms = timedelta(microseconds = round(datetime_obj.microsecond/1000.0)*1000)
ms_date = datetime(y,m,d,H,M,S) + ms
return ms_date.strftime('%M:%S.%f')[:-3]
def buildINA219Graph(username, password, myGraphSampleCount):
print('buildINA219Graph - The time is: %s' % datetime.now())
# open database
con1 = mdb.connect('localhost', username, password, 'DataLogger' )
# now we have to get the data, stuff it in the graph
mycursor = con1.cursor()
print(myGraphSampleCount)
query = '(SELECT timestamp, voltage, current, power, id FROM '+INA219tableName+' 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 Current
v = [] # channel Voltage
averageCurrent = 0.0
currentCount = 0
peakCurrent = 0.0
minimumVoltage = 6.0
for record in result:
t.append(record[0])
v.append(record[1])
u.append(record[2])
averageCurrent = averageCurrent+record[2]
if (record[2] > peakCurrent):
peakCurrent = record[2]
if (record[1] > 4.75):
if (record[1] < minimumVoltage):
minimumVoltage = record[1]
currentCount=currentCount+1
averageCurrent = averageCurrent/currentCount
''
print(("count of t=",len(t)))
x1 = t
# matplotlib date format object
hfmt = dates.DateFormatter('%M:%S')
#hfmt = dates.DateFormatter('%m/%d-%H')
fig = pyplot.figure()
fig.set_facecolor('white')
#ax = fig.add_subplot(111,axisbg = 'white')
ax1 = fig.add_subplot(211)
ax1.xaxis.set_major_formatter(hfmt)
ax1.plot(x1, u, '-', color='r', label = 'current')
ax1.grid()
ax2 = fig.add_subplot(212)
ax2.xaxis.set_major_formatter(hfmt)
ax2.plot(x1, v, '-', color='b', label = 'voltage')
ax2.grid()
ax1.legend(loc=0)
ax2.legend(loc=0)
ax1.set_ylabel("Current mA")
ax2.set_ylabel("Voltage V")
ax2.set_ylim(4.5, 5.5)
ax1.set_ylim(0,1400)
#ax2.xaxis.set_tick_params(rotation=45)
#pyplot.xticks(rotation='45')
pyplot.subplots_adjust(bottom=.3)
name = "Pi3B+ Turn On Current SmallPS w/Cap "
pylab.figtext(.5, .05, ("%s\nAverage Current %6.2fmA\nPeak Current %6.2fmA\nMinimum Voltage %6.2fV\n%s UTC") %(name,averageCurrent, peakCurrent, minimumVoltage, x1[0]),fontsize=12,ha='center')
pyplot.show()
pyplot.savefig("/var/www/html/INA219DataLoggerGraph.png", facecolor=fig.get_facecolor())
mycursor.close()
con1.close()
fig.clf()
pyplot.close()
pylab.close()
gc.collect()
print("------INA219Graph finished now")
'''
# plot
pyplot.plot(t,u)
# beautify the x-labels
pyplot.gcf().autofmt_xdate()
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/INA219DataLoggerGraph.png")
pyplot.close()
'''
######################################
#
# readINA219Data and buildINA219Graph
#
#
######################################