forked from switchdoclabs/SDL_Pi_SunTrackerTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SunTrackerDatabase.py
248 lines (166 loc) · 8.25 KB
/
SunTrackerDatabase.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#
# gathers data and writes to database
#
#
import MySQLdb as mdb
import Image
import ImageDraw
import ImageFont
import datetime
# /*---------------------------------------------------------------------*/
TCA9545_CONFIG_BUS0 = (0x01) # 1 = enable, 0 = disable
TCA9545_CONFIG_BUS1 = (0x02) # 1 = enable, 0 = disable
TCA9545_CONFIG_BUS2 = (0x04) # 1 = enable, 0 = disable
TCA9545_CONFIG_BUS3 = (0x08) # 1 = enable, 0 = disable
#/*=========================================================================*/
GAIN = 2/3
def writeDataToDatabase(currentSteps, currentDegrees, disp, i2cMux, adc, SunAirPlus0, SunAirPlus1):
#disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
#font = ImageFont.truetype('Minecraftia.ttf', 8)
# Load default font.
font = ImageFont.load_default()
x=0
top = 2
# turn on bus 1
i2cMux.write_control_register(TCA9545_CONFIG_BUS1)
# 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
# Write two lines of text.
nowPST = datetime.datetime.now() - datetime.timedelta(seconds=7*60*60);
print "two step at currentTime PST = %s" % nowPST.strftime("%Y-%m-%d %H:%M:%S");
draw.text((x, top), '%s' % (nowPST.strftime("%Y-%m-%d %H:%M:%S")), font=font, fill=255)
draw.text((x, top+10), '---------------', font=font, fill=255)
# gather data from SunAirPlus
print "gathering data";
print "-----------SunAirPlus 0--------------"
shuntvoltage1 = 0
busvoltage1 = 0
current_mA1 = 0
loadvoltage1 = 0
busvoltage1 = SunAirPlus0.getBusVoltage_V(LIPO_BATTERY_CHANNEL)
shuntvoltage1 = SunAirPlus0.getShuntVoltage_mV(LIPO_BATTERY_CHANNEL)
# minus is to get the "sense" right. - means the battery is charging, + that it is discharging
current_mA1 = SunAirPlus0.getCurrent_mA(LIPO_BATTERY_CHANNEL)
loadvoltage1 = busvoltage1 + (shuntvoltage1 / 1000)
print "LIPO_Battery Bus Voltage: %3.2f V " % busvoltage1
print "LIPO_Battery Load Voltage: %3.2f V" % loadvoltage1
print "LIPO_Battery Current 1: %3.2f mA" % current_mA1
print
shuntvoltage2 = 0
busvoltage2 = 0
current_mA2 = 0
loadvoltage2 = 0
busvoltage2 = SunAirPlus0.getBusVoltage_V(SOLAR_CELL_CHANNEL)
current_mA2 = -SunAirPlus0.getCurrent_mA(SOLAR_CELL_CHANNEL)
loadvoltage2 = busvoltage2 + (shuntvoltage2 / 1000)
print "Solar Cell Bus Voltage 2: %3.2f V " % busvoltage2
print "Solar Cell Load Voltage 2: %3.2f V" % loadvoltage2
print "Solar Cell Current 2: %3.2f mA" % current_mA2
print
shuntvoltage3 = 0
busvoltage3 = 0
current_mA3 = 0
loadvoltage3 = 0
busvoltage3 = SunAirPlus0.getBusVoltage_V(OUTPUT_CHANNEL)
current_mA3 = SunAirPlus0.getCurrent_mA(OUTPUT_CHANNEL)
loadvoltage3 = busvoltage3 + (shuntvoltage3 / 1000)
print "Output Bus Voltage 3: %3.2f V " % busvoltage3
print "Output Load Voltage 3: %3.2f V" % loadvoltage3
print "Output Current 3: %3.2f mA" % current_mA3
print
# Write two lines of text.
draw.text((x, top+20), 'STS=%3.1fV %3.1fmA' % (busvoltage2,current_mA2), font=font, fill=255)
draw.text((x, top+30), 'STB=%3.1fV %3.1fmA' % (busvoltage1,current_mA1), font=font, fill=255)
i2cMux.write_control_register(TCA9545_CONFIG_BUS3)
# read in the solar panel voltage
#resistor_voltage0 = adc.read_adc(0, gain=GAIN)*0.0001875
#resistor_current0 = resistor_voltage0/10.0;
#power_delivered0 = resistor_voltage0 * resistor_current0;
#print "Resistor_voltage0 = %5.3fV Resistor_current0_mA = %5.3fmA Power_Delivered=%5.3fW" % (resistor_voltage0 , resistor_current0*1000, power_delivered0);
# Put record in MySQL
print "writing data 0";
# open database
con = mdb.connect('localhost', 'root', 'password', 'SunTracker');
# you must create a Cursor object. It will let
# you execute all the queries you need
cur = con.cursor()
# write record
deviceid = 0
query = 'INSERT INTO SolarPowerData(timestamp, deviceid, currentSteps, currentDegrees, battery_load_voltage, battery_current, solarcell_load_voltage, solarcell_current, output_load_voltage, output_current) VALUES(UTC_TIMESTAMP(), %i, %i, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f)' %( deviceid, currentSteps, currentDegrees, busvoltage1, current_mA1, busvoltage2, current_mA2, busvoltage3, current_mA3)
print("query=%s" % query)
cur.execute(query)
# now fetch other data and save
i2cMux.write_control_register(TCA9545_CONFIG_BUS2)
# gather data from SunAirPlus
print "gathering data";
print "-----------SunAirPlus 1--------------"
shuntvoltage1 = 0
busvoltage1 = 0
current_mA1 = 0
loadvoltage1 = 0
busvoltage1 = SunAirPlus0.getBusVoltage_V(LIPO_BATTERY_CHANNEL)
shuntvoltage1 = SunAirPlus0.getShuntVoltage_mV(LIPO_BATTERY_CHANNEL)
# minus is to get the "sense" right. - means the battery is charging, + that it is discharging
current_mA1 = SunAirPlus0.getCurrent_mA(LIPO_BATTERY_CHANNEL)
loadvoltage1 = busvoltage1 + (shuntvoltage1 / 1000)
print "LIPO_Battery Bus Voltage: %3.2f V " % busvoltage1
print "LIPO_Battery Load Voltage: %3.2f V" % loadvoltage1
print "LIPO_Battery Current 1: %3.2f mA" % current_mA1
print
shuntvoltage2 = 0
busvoltage2 = 0
current_mA2 = 0
loadvoltage2 = 0
busvoltage2 = SunAirPlus0.getBusVoltage_V(SOLAR_CELL_CHANNEL)
current_mA2 = -SunAirPlus0.getCurrent_mA(SOLAR_CELL_CHANNEL)
loadvoltage2 = busvoltage2 + (shuntvoltage2 / 1000)
print "Solar Cell Bus Voltage 2: %3.2f V " % busvoltage2
print "Solar Cell Load Voltage 2: %3.2f V" % loadvoltage2
print "Solar Cell Current 2: %3.2f mA" % current_mA2
print
shuntvoltage3 = 0
busvoltage3 = 0
current_mA3 = 0
loadvoltage3 = 0
busvoltage3 = SunAirPlus0.getBusVoltage_V(OUTPUT_CHANNEL)
current_mA3 = SunAirPlus0.getCurrent_mA(OUTPUT_CHANNEL)
loadvoltage3 = busvoltage3 + (shuntvoltage3 / 1000)
print "Output Bus Voltage 3: %3.2f V " % busvoltage3
print "Output Load Voltage 3: %3.2f V" % loadvoltage3
print "Output Current 3: %3.2f mA" % current_mA3
print
i2cMux.write_control_register(TCA9545_CONFIG_BUS3)
# read in the solar panel voltage
#resistor_voltage1 = adc.read_adc(1, gain=GAIN)*0.0001875
#resistor_current1 = resistor_voltage1/10.0;
#power_delivered1 = resistor_voltage1 * resistor_current0;
#print "Resistor_voltage1 = %5.3fV Resistor_current1_mA = %5.3fmA Power_Delivered=%5.3fW" % (resistor_voltage1 , resistor_current1*1000, power_delivered1);
# Put record in MySQL
print "writing data 1";
# write record
deviceid = 1
query = 'INSERT INTO SolarPowerData(timestamp, deviceid, currentSteps, currentDegrees, battery_load_voltage, battery_current, solarcell_load_voltage, solarcell_current, output_load_voltage, output_current) VALUES(UTC_TIMESTAMP(), %i, %i, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f)' %( deviceid, currentSteps, currentDegrees, busvoltage1, current_mA1, busvoltage2, current_mA2, busvoltage3, current_mA3)
print("query=%s" % query)
cur.execute(query)
# Write two lines of text.
draw.text((x, top+40), 'FXS=%3.1fV %3.1fmA' % (busvoltage2,current_mA2), font=font, fill=255)
draw.text((x, top+50), 'FXB=%3.1fV %3.1fmA' % (busvoltage1,current_mA1), font=font, fill=255)
# Display image.
disp.image(image)
disp.display()
# close database
con.commit()
i2cMux.write_control_register(TCA9545_CONFIG_BUS0)
return