forked from switchdoclabs/SDL_Pi_SunTrackerTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SunVoltage.py
108 lines (75 loc) · 2.5 KB
/
SunVoltage.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
# power graph generation
# filename: powersupplygraph.py
# Version 1.3
#
#
import sys
import time
import RPi.GPIO as GPIO
import gc
from datetime import datetime
from datetime import timedelta
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
from matplotlib import pyplot
from matplotlib import dates
import pylab
import MySQLdb as mdb
def sunvoltagesupplygraph(source,days,delay,start,end):
print("sunvoltage running now")
# now we have to get the data, stuff it in the graph
print("trying database")
db = mdb.connect('localhost', 'root', 'password', 'SunTracker');
cursor = db.cursor()
query = "SELECT timestamp, deviceid, currentDegrees, battery_load_voltage, battery_current, solarcell_load_voltage, solarcell_current, id FROM SolarPowerData where id > %i AND id < %i " % (start, end)
print "query=", query
cursor.execute(query)
result = cursor.fetchall()
t = [] # time
s = [] # rotating panel voltage
u = [] # stationary panel voltage
for record in result:
deviceid = record[1]
if (deviceid == 0):
# convert to PST
#UTCObject = datetime.strptime(record[0],"%Y-%m-%d %H:%M:%S")
UTCObject = record[0]
TimePST = UTCObject - timedelta(seconds=7*60*60)
#TimeStringPST = TimePST.strftime('%Y-%m-%d %H:%M:%S')
t.append(TimePST)
s.append(record[5])
else:
u.append(record[5])
print ("count of t=",len(t))
fds = dates.date2num(t) # converted
# matplotlib date format object
hfmt = dates.DateFormatter('%m/%d-%H')
fig = pyplot.figure()
fig.set_facecolor('white')
ax = fig.add_subplot(111,axisbg = 'white')
ax.vlines(fds, -200.0, 1000.0,colors='w')
ax.xaxis.set_major_locator(dates.HourLocator(interval=1))
ax.xaxis.set_major_formatter(hfmt)
ax.set_ylim(bottom = -200.0)
pyplot.xticks(rotation='45')
pyplot.subplots_adjust(bottom=.3)
pylab.plot(t, s, color='b',label="Sun Tracked Voltage",linestyle="-",marker=".")
pylab.plot(t, u, color='r',label="Fixed Voltage",linestyle="-",marker=".")
pylab.xlabel("Hours")
pylab.ylabel("Volts V")
pylab.legend(loc='lower center')
pylab.axis([min(t), max(t), 0, max(s)+1])
pylab.figtext(.5, .05, ("SunTracker %s" % days),fontsize=18,ha='center')
pylab.grid(True)
pyplot.show()
pyplot.savefig("/var/www/SunVoltage-%i.png" % start,facecolor=fig.get_facecolor())
cursor.close()
db.close()
del cursor
del db
fig.clf()
pyplot.close()
pylab.close()
gc.collect()
print "finished now"