-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolar_Control.py
More file actions
executable file
·429 lines (363 loc) · 10.5 KB
/
Solar_Control.py
File metadata and controls
executable file
·429 lines (363 loc) · 10.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# -*- coding:utf-8 -*-
#Created By Hower & Tom
#---------------------------import modules------------------------------
import RPi.GPIO as io
import time
import smbus
import math
import threading
import numpy as np
import sys
#----------------------------Port Config--------------------------------
# Set to the GPIO port of main relay
relay = 4
# Set to the GPIO port of the switch button
switchButton = 20
# Set to the GPIO port of the manual/auto mode button
manualAutoSwitch = 21
# Set to true if you also have a relay that turns off the inverter when unused
inverterRelayEnabled = False
# Set the port of inverter relay
inverterRelay = 26
# Set GPIO ports of dual 74HC595 display
# DS/DIO
displayDS = 17
# SHCP/SCK
displaySHCP = 22
# STCP/RCK
displaySTCP = 27
# Calibration data:
# Calculate adda using: divisor / multiplier * addaValue
addaDivisor = 12.6
addaMultiplier = 112
# Set to True to enable auto controlling of fan
fanEnabled = False
# Set to the GPIO where the relay controlling the fan is connected
fanPort = 19
# Set to True if you want to use LED in addition to the 8-digits display
ledEnabled = True
# LED representing whether manual mode is on
manualModeLED = 16
# LED representing whether the solar power is used
solarEnergyLED = 12
# LED representing whether the grid/city power is used
gridIndicator = 26
#----------------------------Initializing-------------------------------
#Setup I2C
address = 0x48
A0 = 0x40
A1 = 0x41
A2 = 0x42
A3 = 0x43
bus = smbus.SMBus(1)
#Setup BCM
io.setmode(io.BCM)
#Relay
io.setup(relay, io.OUT)
#Actual Switch Button
io.setup(switchButton,io.IN)
#Manual/Auto Swith Button
io.setup(manualAutoSwitch,io.IN)
#inverter Relay
if inverterRelayEnabled == True:
io.setup(inverterRelay,io.OUT)
#Setup Display
DS = displayDS #DIO
SHCP = displaySHCP #SCK
STCP = displaySTCP #RCK
io.setup(DS, io.OUT)
io.setup(STCP, io.OUT)
io.setup(SHCP, io.OUT)
io.output(STCP, False)
io.output(SHCP, False)
# Setup LEDs
if ledEnabled == True:
io.setup(manualModeLED, io.OUT)
io.setup(solarEnergyLED, io.OUT)
io.setup(gridIndicator, io.OUT)
#Initialize Variable
city = 0
isCity = 5
volt = 100
isManual = False
solar = True
#set up array for moving average
movingAverage = []
for i in range(0,30):
bus.write_byte(address,A0)
value = bus.read_byte(address)
volt1 = round(12.4 / 207 * value, 2)
movingAverage.append(volt1)
#Initializing Fan Control
def cpu_temp():
with open("/sys/class/thermal/thermal_zone0/temp", 'r') as f:
return float(f.read())/1000
#-------------------------------Display---------------------------------
def setBitData(data):
# Send bit
io.output(DS, data)
# Activate Shift Register
io.output(SHCP, False)
io.output(SHCP, True)
# Preset function to display digits (1 - 9),
# with optionn for dot point
def showDigit(num, showDotPoint):
if (num == 0) :
setBitData(not showDotPoint) # DP
setBitData(True) # G
setBitData(False) # F
setBitData(False) # E
setBitData(False) # D
setBitData(False) # C
setBitData(False) # B
setBitData(False) # A
elif (num == 1) :
setBitData(not showDotPoint)
setBitData(True)
setBitData(True)
setBitData(True)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(True)
elif (num == 2) :
setBitData(not showDotPoint)
setBitData(False)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(True)
setBitData(False)
setBitData(False)
elif (num == 3) :
setBitData(not showDotPoint)
setBitData(False)
setBitData(True)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
elif (num == 4) :
setBitData(not showDotPoint)
setBitData(False)
setBitData(False)
setBitData(True)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(True)
elif (num == 5) :
setBitData(not showDotPoint)
setBitData(False)
setBitData(False)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(True)
setBitData(False)
elif (num == 6) :
setBitData(not showDotPoint)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(True)
setBitData(False)
elif (num == 7) :
setBitData(not showDotPoint)
setBitData(True)
setBitData(True)
setBitData(True)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(False)
elif (num == 8) :
setBitData(not showDotPoint)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
elif (num == 9) :
setBitData(not showDotPoint)
setBitData(False)
setBitData(False)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(False)
elif (num == 10) :
# Letter C
setBitData(not showDotPoint) # DP
setBitData(True) # G
setBitData(False) # F
setBitData(False) # E
setBitData(False) # D
setBitData(True) # C
setBitData(True) # B
setBitData(False) # A
elif (num == 11) :
#Letter A
setBitData(not showDotPoint)
setBitData(False)
setBitData(False)
setBitData(False)
setBitData(True)
setBitData(False)
setBitData(False)
setBitData(False)
def showLocation(location):
for x in range(0,8):
if x == location:
setBitData(True)
else:
setBitData(False)
def showDigitWithLocation(num, showDotPoint, location):
#io.output(STCP, False)
showLocation(location)
showDigit(num, showDotPoint)
# Activate register lock to push bits into register
io.output(STCP, True)
io.output(STCP, False)
def displayVoltage():
global volt
global isCity
while True:
voltMultiplied = round(volt*100, 0)
digits = 4
divisor = 1000
if voltMultiplied < 1000:
digits = 3
divisor = 100
for i in range(1, digits + 1):
digit = voltMultiplied % 10
#print(digit)
voltMultiplied = math.floor(voltMultiplied / 10)
#print(voltMultiplied)
showDot = False
if i == 3:
showDot = True
showDigitWithLocation(digit, showDot, i - 1)
time.sleep(0.001)
showDigitWithLocation(isCity, False, 7)
#showDigitWithLocation(isManual,False,5)
if isManual == 0:
showDigitWithLocation(11,False,5)
else:
showDigitWithLocation(0,False,5)
#-----------------------Multi Treading For Display----------------------
bgThread = threading.Thread(target = displayVoltage, args = ())
bgThread.start()
#---------------------Read Voltage with I2C-----------------------------
def readVoltage():
global volt
global movingAverage
global addaDivisor
global addaMultiplier
while True:
bus.write_byte(address,A0)
value = bus.read_byte(address)
volt1 = round(addaDivisor / addaMultiplier * value, 2)
movingAverage.append(volt1)
del movingAverage[0]
volt = np.average(movingAverage)
time.sleep(0.3)
#-----------------------Multi Treading For ADDA-------------------------
bgThread2 = threading.Thread(target = readVoltage, args = ())
bgThread2.start()
#-----------------------Controlling Reverter Relay----------------------
def switchRelay():
global solar
# Allows switching on first run
# This variable prevents io.output from being called repeatedly
previousSolar = not solar
while True:
if solar == True:
if previousSolar == False:
if inverterRelayEnabled == True:
io.output(inverterRelay, False)
time.sleep(3.0)
io.output(relay,False)
else:
if previousSolar == True:
if inverterRelayEnabled == True:
io.output(inverterRelay, True)
io.output(relay,True)
previousSolar = solar
time.sleep(0.3)
#-----------------------Multi Treading For Relay----------------------
bgThread3 = threading.Thread(target = switchRelay, args = ())
bgThread3.start()
#-----------------------Multi Treading For Fan----------------------
def fan():
global fanPort
# close air fan first
io.setup(fanPort, io.OUT)
is_close = True
while True:
temp = cpu_temp()
if is_close:
if temp > 50.0:
#print time.ctime(), temp, 'open air fan'
io.output(channel, 1)
is_close = False
else:
if temp < 40.0:
#print time.ctime(), temp, 'close air fan'
print('fan close')
io.output(channel, 0)
is_close = True
time.sleep(2.0)
#print time.ctime(), temp
print(temp)
if fanEnabled is True:
bgThread4 = threading.Thread(target = fan, args = ())
bgThread4.start()
#-----------------------------Main Thread------------------------------
buttonOn = False
solar = False
manualOn = False
while True:
if city == 0:
isCity = 5
else:
isCity = 10
#---Manual switch
button = io.input(manualAutoSwitch)
if button == 1 and manualOn == False:
isManual = not isManual
manualOn = True
if button == 0:
manualOn = False
previousManual = manualOn
#---Switching
if isManual == True:
button = io.input(switchButton)
if button == 1 and buttonOn == False:
solar = not solar
buttonOn = True
if button == 0:
buttonOn = False
if volt < 11.0:
#Force city power when battery is dead
on = False
else:
#Auto
if volt >= 12.6:
solar = True
if volt <= 11.2:
solar = False
city = not solar
previousOn = solar
#LEDs
if ledEnabled == True:
io.output(manualModeLED, isManual)
io.output(solarEnergyLED, solar)
io.output(gridIndicator, city)
time.sleep(0.1)