-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.py
127 lines (94 loc) · 4.78 KB
/
mqtt.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
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"
)
# "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
#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 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"))
#debug: print ("Consumption is: ", PowerString[positionstartCONSUME+12:positionendCONSUME-7])
ConsumptionString= (PowerString[positionstartCONSUME+12:positionendCONSUME-7])
#debug: print ("Feed is: ", PowerString[positionstartFEED+12:positionendFEED-7])
FeedString=PowerString[positionstartFEED+12:positionendFEED-7]
#Write the values to the database------------------------
mycursor = mydb.cursor()
#Write Grid Consumption in SQL
sql = "UPDATE settings SET setting_value = %s WHERE setting_name = %s"
val = (ConsumptionString, "Grid_Consumption")
mycursor.execute(sql,val)
sql = "UPDATE settings SET setting_value = %s WHERE setting_name = %s"
val = (FeedString, "Grid_Supply")
mycursor.execute(sql,val)
mydb.commit()
#Realtime power is currently:
realpwr = int(FeedString) - int(ConsumptionString)
#print (realpwr)
# Here start the calculation of the overall percentage (when in auto) that can be used to calculate the individual outputs.
# start reading database of maximum possible power per resistorbank
#----- #Read the modus first, to see if in auto or not_percentage to do calculations.
mycursor = mydb.cursor()
mycursor.execute("SELECT setting_value FROM settings WHERE setting_name='modus'")
myresult = mycursor.fetchone()
txt=str(myresult)
y=txt.find("auto")
if (y>=0):
mycursor.execute("SELECT setting_value FROM settings WHERE setting_name='Bank1_MAX_power'")
myresult = mycursor.fetchone()
txt=str(myresult)
bank1_max_power=int(txt[2:6])
mycursor.execute("SELECT setting_value FROM settings WHERE setting_name='Bank2_MAX_power'")
myresult = mycursor.fetchone()
txt=str(myresult)
bank2_max_power=int(txt[2:6])
mycursor.execute("SELECT setting_value FROM settings WHERE setting_name='Bank3_MAX_power'")
myresult = mycursor.fetchone()
txt=str(myresult)
bank3_max_power=int(txt[2:6])
#print (bank1_max_power,bank2_max_power, bank3_max_power)
#Extract previous percentage from DB to calculate new percentage:
mycursor.execute("SELECT setting_value FROM settings WHERE setting_name='percentage'")
myresult = mycursor.fetchone()
#convert from string to integer
num = ""
for c in myresult:
if c.isdigit():
num = num + c
#print (myresult)
lstprct = num
#print (lstprct)
ttlpwr = bank1_max_power + bank2_max_power + bank3_max_power
#Calculation of the new percentage to be used!
ttlprct = (realpwr / ttlpwr)*100 + int(lstprct) #Previous power needs to be added (what was already on before last update): It could also be negative in case of consumption
#set limits:
if ttlprct > 100:
#print ("test", ttlprct)
ttlprct = 100
if ttlprct < 0:
#print ("test", ttlprct)
ttlprct = 0
sql = "UPDATE settings SET setting_value = %s WHERE setting_name = %s"
val = (str(int(ttlprct)), "percentage")
mycursor.execute(sql,val)
mydb.commit()
#client MQTT connectie maken met MQTT-brooker/server--------------------
ourClient = mqtt.Client("HeatBattV2_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