forked from CliveCarrington/RPi_Temp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccPowerUsage.py
107 lines (85 loc) · 2.51 KB
/
ccPowerUsage.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
#*****************************************************
#
# ccPowerUsage
#
# Clive Carrington
# Version 1.1
# 11th December 2021
#
# History:
# 0.1. Initial version, copied from ccDataReceiver
# 1.1. Recoded to use Python3
# Definitions
logRoot = "log"
# Definitions for MySQL boolean entries (using short Int)
SQL_On = 0
SQL_Off = 1
SQL_Unk = 99
# Imports
import datetime
import sys
import serial
from heatingToAWS import sendPowerMeasurement, sendTemperature
import xml.etree.ElementTree as ET
# Will eventually use the same source for all SQP routines!
# from heatingToMySQL_AWS import sendHeatingData
#*********************************************************
#
# Set of routines dealing with creating and amending the output record
#
# constructOutputRecord creates a CSV string from the current record
# updateField(Name, Value)
#
#**********************************************************
def openInputChannel():
inputHandle = serial.Serial('/dev/ttyUSB0', 57600, timeout=15)
return inputHandle
# Start of Main()
# *******************************
#
# Example of Main routine using new functions
#
#*********************************
def main_CHroutine():
# Open up the input channel
inputChannel = openInputChannel()
# SET up the main loop to receive data
while inputChannel.isOpen() :
print("Waiting for a line")
each_line_binary = inputChannel.readline()
each_line = each_line_binary.decode('uft-8')
print("Received a line, checking it")
if each_line != "":
print ("Received a line")
print (each_line)
root = ET.fromstring(each_line)
sensor = -1
recordValid = False
for child in root:
#print(child.tag, child.text)
if child.tag == "tmpr":
airingCupboardTemp = child.text
if child.tag == "sensor":
if child.text != "0":
break
if child.tag == "hist":
break
if child.tag == "sensor" and child.text != "0":
break
if child.tag == "ch1":
#print list(child.itertext())
houseTotal = list(child.itertext())[0]
recordValid = True
if child.tag == "ch3":
solarPower = list(child.itertext())[0]
if child.tag == "ch2":
waterHeating = list(child.itertext())[0]
if recordValid:
print("Temp is {}, house is {}, solar is {} and water heating is {}."\
.format(airingCupboardTemp, houseTotal, solarPower, waterHeating))
sendPowerMeasurement(0,houseTotal, waterHeating, solarPower)
sendTemperature("Airing Cupboard", airingCupboardTemp)
inputchannel.close()
return 0
if __name__ == '__main__':
main_CHroutine()