-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaluesupdate.py
266 lines (206 loc) · 11.6 KB
/
valuesupdate.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#WARNING!!! Run this file in Python3, NOT in Python2!!!
import paho.mqtt.client as mqtt # Import the MQTT library
import time # The time library is useful for delays
import datetime
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="raspberry",
database="HeatBatt"
)
SLOWER=0.6 #Damper to slow down the reaction?
HEATRES1_POWER = 1200 #This is the wattage for the first resistorbank (ON/OFF controlled)
HEATRES2_POWER = 1060 #This is the wattage for the 2nd resistorbank (ON/OFF controlled), it was measured with the banktest program
HEATRESC_POWER = 1200 #This is the wattage for the controlled resistorbank (triac phase controlled)
BatteryPower=HEATRES1_POWER + HEATRES2_POWER + HEATRESC_POWER #Power in Watts of the battery can take to charge: depending on the heating resistors
HeatRes1_Status = 0 #Can be 0 or 1200W (Depends on HEATRES1_POWER actually)
HeatRes2_Status = 0 #Can be 0 or 1200W (Depends on HEATRES2_POWER actually)
HeatResC_Status = 0 #Can be anything between 0 and 1200W (Depends on HEATRESC_POWER actually)
PushPower = 0
# Our "on message" event executes all this on every new message of the MQQT
# - First read and decode the power to and from the grid, from the MQTT message
# - Write these values (as string) into the database
# - Then check if the chargesystem is in auto or manual mode
# - If mode in 'auto', then increase or decrease the power going to the HeatBattery depending on power availibility
# - If in manual, do not change the percentage (it can be done through the webpage index.php)
#MessageFunction below is execute every time a message comes in.-----------------------------------------------------------------------------
def messageFunction (client, userdata, message):
#Decode MQTT message and write it to the database
global HeatRes1_Status
global HeatRes2_Status
global HeatResC_Status
global PushPower
topic = str(message.topic)
mssge = str(message.payload.decode("utf-8"))
PowerString = str(message.payload)
positionstartCONSUME = ( PowerString.find("svalue5"))
positionstartFEED = ( PowerString.find("svalue6"))
positionendCONSUME = (PowerString.find("svalue6"))
positionendFEED = (PowerString.find("unit"))
#print ("Consumption is: ", PowerString[positionstartCONSUME+12:positionendCONSUME-7])
ConsumptionString= (PowerString[positionstartCONSUME+12:positionendCONSUME-7])
#print ("Feed is: ", PowerString[positionstartFEED+12:positionendFEED-7])
FeedString=PowerString[positionstartFEED+12:positionendFEED-7]
#---------------------------------------------------------------------------------#
# FOR SIMULATION ONLY!!! #
#ConsumptionString = "0" #
#FeedString = "2500" #
#FeedSim = int(FeedString) - HeatRes1_Status - HeatRes2_Status - HeatResC_Status #
#print ("---", FeedSim) #
#FeedString = str(int(FeedSim)) #
#---------------------------------------------------------------------------------#
#Write the values to the database------------------------
mycursor = mydb.cursor()
#Write Grid Consumption in SQL
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = (ConsumptionString, "Grid_Consumption")
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = (FeedString, "Grid_Supply")
mycursor.execute(sql,val)
#----- #Read the modus for charging from the database to check for auto/man ------------------------------------------------------------*
mycursor.execute("SELECT Value FROM Settings WHERE Setting='modus'")
myresult = mycursor.fetchone()
txt=str(myresult)
#print (txt)
x=txt.find("auto")
#print (x)
if x >= 0:
print ("Now in automatic, so percentage following Power")
#----- #Calculate the resistorbanks to be puton or off or precentage
#From here calculate how the heating battery should charge: This calculations should only be done if modus is in 'auto'
Feedpower = int(FeedString) - int(ConsumptionString) #This is the Power that is actually coming/going really from/to the grid, in Watt
# A positive value for Feedpower means it is feeding to the grid. A negative value is consuming from the grid.
#Push Power is the Gross-power that would be left over if there was no charging at all of the battery.
#From that value it is decided how much charging will take place.
if (Feedpower > 50): #increment if more Power available
PushPower = PushPower +30
if (Feedpower > 500):
PushPower = PushPower +150
if (Feedpower <=0): #decrement if not enough power available
PushPower = PushPower -100
if (Feedpower < -500):
PushPower = PushPower - 500
if (PushPower >3600): #Limit the maximum
PushPower = 3600
if (PushPower < 0): #Limit the minimum
PushPower = 0
print ( " PushPower= ", PushPower, "Feedpower = ", Feedpower)
if PushPower >= (HEATRES1_POWER + HEATRES2_POWER + HEATRESC_POWER) : #Bigger than the maximum 3600W
HeatRes1_Status = HEATRES1_POWER
HeatRes2_Status = HEATRES2_POWER
HeatResC_Status = HEATRESC_POWER
#NOG WEGSCHRIJVEN NAAR DB!!!!!!!!!!!!!!!
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = (str(int(100)), "HeatResistorC") #Set to 100%
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("on", "HeatResistor1")
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("on", "HeatResistor2")
mycursor.execute(sql,val)
if (PushPower < (HEATRES1_POWER + HEATRES2_POWER + HEATRESC_POWER)) and (PushPower > (HEATRES1_POWER + HEATRES2_POWER)): #Between 2400 and 3600W left over of own consumption
HeatRes1_Status = HEATRES1_POWER
HeatRes2_Status = HEATRES2_POWER
HeatResC_Status =int( (PushPower - HEATRES1_POWER - HEATRES2_POWER) * SLOWER)
#NOG WEGSCHRIJVEN NAAR DB
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = (str(int((HeatResC_Status)/(HEATRES1_POWER/100))), "HeatResistorC") # to go from 1200W to 100%
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("on", "HeatResistor1")
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("on", "HeatResistor2")
mycursor.execute(sql,val)
if (PushPower <= (HEATRES1_POWER + HEATRES2_POWER)) and (PushPower > (HEATRES1_POWER)): #Between 1200 and 2400W left over of own consumption
HeatRes1_Status = HEATRES1_POWER
HeatRes2_Status = 0
HeatResC_Status = int((PushPower - HEATRES1_POWER)* SLOWER)
#NOG WEGSCHRIJVEN NAAR DB
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = (str(int((HeatResC_Status)/(HEATRESC_POWER/100))), "HeatResistorC") # to go from 1200W to 100%
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("on", "HeatResistor1")
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("off", "HeatResistor2")
mycursor.execute(sql,val)
if (PushPower < HEATRES1_POWER) or (Feedpower == 0): #Lower than 1200W left over of own consumption
HeatRes1_Status = 0
HeatRes2_Status = 0
if (PushPower >=0):
HeatResC_Status = int(PushPower * SLOWER)
else:
HeatResC_Status = 0
#NOG WEGSCHRIJVEN NAAR DB
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = (str(int((HeatResC_Status)/(HEATRESC_POWER/100))), "HeatResistorC") #to go from 1200W to 100%
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("off", "HeatResistor1")
mycursor.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("off", "HeatResistor2")
mycursor.execute(sql,val)
print ("R1", HeatRes1_Status, " R2:", HeatRes2_Status, " R3:", HeatResC_Status)
print (" ")
#From here existing to be cleaned up??????????
ChargePercentage = PushPower / BatteryPower * 100
if ChargePercentage>100:
ChargePercentage = 100
# print ("Chargepercentage", ChargePercentage)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = (str(int(ChargePercentage)), "Percentage")
mycursor.execute(sql,val)
# else x<0:
# print ("now in manual, so percentage not changing")
mydb.commit()
#client MQTT connectie maken met MQTT-brooker/server--------------------
ourClient = mqtt.Client("Zero_client_mqtt") # Create a MQTT client object
ourClient.connect("192.168.123.8", 1883) # Connect to the test MQTT broker
ourClient.subscribe('domoticz/out/Power') # Subscribe to the topic AC_unit
ourClient.on_message = messageFunction # Attach the messageFunction to subscription
ourClient.loop_start() # Start the MQTT client
# Main program loop
while(1):
time.sleep(2) # Sleep for a second or 2
#The 'timed' mode can be programmed here if the battery is not MQTT controlled.
mydb.commit()
mycursor2 = mydb.cursor()
mycursor2.execute("SELECT Value FROM Settings WHERE Setting='modus'")
myresult2 = mycursor2.fetchone()
txt=str(myresult2)
#print (txt)
if txt.find("timed")>0:
#print ("Now in 'timed', Power on certain times")
uur = (datetime.datetime.now())
print (uur.strftime("%H"))
if (int(uur.strftime("%H")) == 3 or int(uur.strftime("%H")) == 4):
print ("het is nu 3h of 4h")
#NOG WEGSCHRIJVEN NAAR DB
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("100", "HeatResistorC") #to go from 1200W to 100%
mycursor2.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("on", "HeatResistor1")
mycursor2.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("on", "HeatResistor2")
mycursor2.execute(sql,val)
else:
print ("Nu geen 3h of 4h")
#NOG WEGSCHRIJVEN NAAR DB
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("0", "HeatResistorC") #to go from 1200W to 100%
mycursor2.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("off", "HeatResistor1")
mycursor2.execute(sql,val)
sql = "UPDATE Settings SET Value = %s WHERE Setting = %s"
val = ("off", "HeatResistor2")
mycursor2.execute(sql,val)
mydb.commit()