-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed-emoncms.py
189 lines (142 loc) · 6.4 KB
/
feed-emoncms.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
#! /usr/bin/python
# Simple script for pooling set of power MCP39F5xx meters and feeding reading to EMONCMS server.
# List of sensors with MCP39F521 and their type. Allowed types are: "1phase" and "3pahase"
sensors = {
"MainPower" :["192.168.1.178","3phase"],
"ServerRoom" :["192.168.1.174","1phase"],
"Lights" :["192.168.1.175","1phase"],
}
# IP address of EMONCMS server
emoncms_server_ip = "192.168.1.254"
# API key for EMONCMS server
emoncms_api_key = "712e90ebae780d638d89ad1axxxxxxx"
debug = 0
#-------------------------------------------------------------------------------
emoncms_url = 'http://' + emoncms_server_ip + '/emoncms/input/post.json?apikey=' + emoncms_api_key + '&node='
import urllib, json, time
def log(str):
now = time.time()
localtime = time.localtime(now)
milliseconds = '%03d' % int((now - int(now)) * 1000)
timestamp = time.strftime('%H:%M:%S', localtime) + "." + milliseconds
print timestamp + " " + str
def get_decode_json_from_url(url):
try:
response = urllib.urlopen(url + "/json")
raw_data = response.read()
if (debug): print(raw_data)
except:
log('ERROR: Can\'t get data from: ' + url)
return False
try:
data = json.loads(raw_data)
if (debug): print(data)
return data
except:
log( 'ERROR: Can\'t decode data from: ' + url)
print(raw_data)
return False
def read_values_from_json(data, line):
Voltage = data['power-and-energy'][line]['Voltage']
Current = data['power-and-energy'][line]['Current']
Frequency = data['power-and-energy'][line]['Frequency']
Active_Power = data['power-and-energy'][line]['Active Pwr']
Reactive_Power = data['power-and-energy'][line]['Reactive Pwr']
Apparent_Power = data['power-and-energy'][line]['Apparent Pwr']
Power_Factor = data['power-and-energy'][line]['Power Factor']
Import_Active_Energy = data['power-and-energy'][line]['Import Active Eng']
Export_Active_Energy = data['power-and-energy'][line]['Export Active Eng']
Import_Reactive_Energy = data['power-and-energy'][line]['Import Reactive Eng']
Export_Reactive_Energy = data['power-and-energy'][line]['Export Reactive Eng']
Free_Memory = data['power-and-energy']['Resources']['Free Mem']
if (debug):
print "Line: " + line
print "\n Voltage : " + str(Voltage)
print " Current : " + str(Current)
print " Frequency : " + str(Frequency)
print "\n--Power--------"
print " Active Power : " + str(Active_Power)
print " Reactive Power : " + str(Reactive_Power)
print " Apparent Power : " + str(Apparent_Power)
print " Power Factor : " + str(Power_Factor)
print "\n--Energy-------"
print " Import Active Energy : " + str(Import_Active_Energy)
print " Export Active Energy : " + str(Export_Active_Energy)
print " Import Reactive Energy : " + str(Import_Reactive_Energy)
print " Export Reactive Energy : " + str(Export_Reactive_Energy)
print "\n--Resouces-----"
print " Free Memory : " + str(Free_Memory)
return [ \
Voltage, \
Current, \
Frequency, \
Active_Power, \
Reactive_Power, \
Apparent_Power, \
Power_Factor, \
Import_Active_Energy, \
Export_Active_Energy, \
Import_Reactive_Energy, \
Export_Reactive_Energy, \
Free_Memory \
]
def send_to_emoncms_node(node, values, emoncms_url):
emoncms_url = emoncms_url + node + "&json={"
emoncms_url = emoncms_url + "Voltage:" + str(values[0])
emoncms_url = emoncms_url + ",Current:" + str(values[1])
emoncms_url = emoncms_url + ",Frequency:" + str(values[2])
emoncms_url = emoncms_url + ",Active_Power:" + str(values[3])
emoncms_url = emoncms_url + ",Reactive_Power:" + str(values[4])
emoncms_url = emoncms_url + ",Apparent_Power:" + str(values[5])
emoncms_url = emoncms_url + ",Power_Factor:" + str(values[6])
emoncms_url = emoncms_url + ",Import_Active_Energy:" + str(values[7])
emoncms_url = emoncms_url + ",Export_Active_Energy:" + str(values[8])
emoncms_url = emoncms_url + ",Import_Reactive_Energy:" + str(values[9])
emoncms_url = emoncms_url + ",Export_Ractive_Energy:" + str(values[10])
emoncms_url = emoncms_url + ",Free_mamory:" + str(values[11])
emoncms_url = emoncms_url + "}"
if (debug): print "Emocms feed URL:\n" + emoncms_url + "\n"
try:
response = urllib.urlopen(emoncms_url)
rc = response.read()
if (debug): print(rc)
except:
log("ERROR: Sending to emoncms fialed.\nURL was:" + emoncms_url + "\nAnswer was:"+ rc + "\n")
for name in sensors.iterkeys():
if (debug): print "Sensor:" + name
params = sensors[name]
ip = params[0]
sensor_type = params[1]
if (debug):
print "IP:" + ip
print "Type:" + sensor_type
if (sensor_type == "3phase"):
url = "http://" + ip
data = get_decode_json_from_url(url)
if (data):
line = "R_Line"
node = name + "-R-Line"
if (debug): print("--Feeding " + node)
values = read_values_from_json(data, line)
send_to_emoncms_node(node, values, emoncms_url)
line = "S_Line"
node = name + "-S-Line"
if (debug): print("--Feeding " + node)
values = read_values_from_json(data, line)
send_to_emoncms_node(node, values, emoncms_url)
line = "T_Line"
node = name + "-T-Line"
if (debug): print("--Feeding " + node)
values = read_values_from_json(data, line)
send_to_emoncms_node(node, values, emoncms_url)
elif (sensor_type == "1phase"):
url = "http://" + ip
data = get_decode_json_from_url(url)
if (data):
line = "Line"
node = name
if (debug): print("--Feeding " + node)
values = read_values_from_json(data, line)
send_to_emoncms_node(node, values, emoncms_url)
else:
print "ERROR: Unknown sensor type!"