]>
Commit | Line | Data |
---|---|---|
ec56fabb MF |
1 | import piplates.DAQCplate as DAQC |
2 | import time | |
3 | from decimal import Decimal | |
68ed5ad8 MF |
4 | import os |
5 | import paho.mqtt.client as mqtt #import the client1 | |
6 | from prometheus_client import start_http_server, Summary, Gauge | |
7 | ||
aad4ecbc | 8 | #DAQC.setDOUTbit(0,0) |
ec56fabb MF |
9 | #thermistor reading function |
10 | def compressor(state=None): | |
11 | if state==None: | |
12 | return DAQC.getDOUTbyte(0) | |
13 | if state==True: | |
14 | DAQC.setDOUTbit(0,0) | |
15 | if state==False: | |
16 | DAQC.clrDOUTbit(0,0) | |
17 | ||
18 | def temp_get(volts,supply_voltage=5): | |
aad4ecbc | 19 | divider_resistor=1500 |
ec56fabb MF |
20 | ohms = Decimal(divider_resistor*supply_voltage/volts-divider_resistor) #calculate the ohms of the thermisttor |
21 | #IVT oem NTC | |
22 | a = Decimal(1.298022762e-3) | |
23 | b = Decimal(2.365068126e-4) | |
24 | c = Decimal(0.9305914923e-7) | |
25 | #Steinhart Hart Equation | |
26 | # T = 1/(a + b[ln(ohm)] + c[ln(ohm)]^3) | |
27 | temp = 1/(a + b*ohms.ln() + c*ohms.ln()**3) | |
28 | tempc = temp - Decimal(273.15) #K to C | |
29 | return tempc | |
30 | ||
68ed5ad8 MF |
31 | def log(fp, data): |
32 | print(*data,sep=',', file=fp) | |
33 | fp.flush() | |
34 | ||
35 | fp = open("/var/tmp/heatpump.csv","w+") | |
36 | start_http_server(8000) | |
37 | running_g = Gauge('heatpump_running', 'Is the compressor running?') | |
38 | top_g = Gauge('heatpump_toptemp', 'Temperature at top of tank.') | |
39 | return_g = Gauge('heatpump_returntemp', 'Temperature at inlet sensor.') | |
40 | gas_g = Gauge('heatpump_hottemp', 'Temperature at Hot Gas side sensor.') | |
ec56fabb MF |
41 | while(True): |
42 | vv_retur=temp_get(DAQC.getADC(0,0),DAQC.getADC(0,8)) | |
a45ebe4b | 43 | vv_top=temp_get(DAQC.getADC(0,2),DAQC.getADC(0,8)) |
ec56fabb MF |
44 | hetgas=temp_get(DAQC.getADC(0,1),DAQC.getADC(0,8)) |
45 | print("VV retur:",vv_retur) | |
a45ebe4b | 46 | print("VV top:",vv_top) |
ec56fabb MF |
47 | print("Hetgas:",hetgas) |
48 | print("Kompressor:",compressor()) | |
68ed5ad8 MF |
49 | log(fp, [vv_retur,vv_top,hetgas,str(compressor()[0])]) |
50 | broker_address="monitor.tranquillity.se" | |
51 | client = mqtt.Client("P1") #create new instance | |
52 | client.connect(broker_address) #connect to broker | |
53 | client.publish("heatpump/running",str(compressor()[0]), retain=True)#publish | |
54 | client.publish("heatpump/toptemp",str(vv_top), retain=True)#publish | |
55 | client.publish("heatpump/returntemp",str(vv_retur), retain=True)#publish | |
56 | client.publish("heatpump/hottemp",str(hetgas), retain=True)#publish | |
57 | running_g.set(compressor()[0]) | |
58 | top_g.set(vv_top) | |
59 | return_g.set(vv_retur) | |
60 | gas_g.set(hetgas) | |
61 | if vv_top < 47: | |
ec56fabb | 62 | compressor(True) |
68ed5ad8 MF |
63 | print("Toptemp under 47. Startar kompressor") |
64 | if vv_top > 51: | |
ec56fabb | 65 | compressor(False) |
68ed5ad8 | 66 | print("Toptemp över 51. Stänger av kompressor") |
ec56fabb MF |
67 | # import pdb;pdb.set_trace() |
68 | time.sleep(1) | |
69 |