-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththingspeak1.py
executable file
·312 lines (255 loc) · 9.81 KB
/
thingspeak1.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/python3
import paho.mqtt.client as mqtt
import http.client
import urllib
import logging
import configparser
import datetime
import time
import json
config_fname = 'thingspeak1.cfg'
config = configparser.ConfigParser()
config.read(config_fname)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", "THINGSPEAKAPIKEY": "N6BP4CYM039RFCNY"}
updateList = []
#*******************************************************************
class gatewayMessage:
"""Defines a class to hold message objects"""
def __init__(self, config):
"""This instantiates the message """
# Prefefine instance variables
self.title = ''
self.base_topic = ''
self.field1_topic = ''
self.field2_topic = ''
self.field3_topic = ''
self.field4_topic = ''
self.field5_topic = ''
self.field6_topic = ''
self.field7_topic = ''
self.field8_topic = ''
self.subtopic = ''
self.api_key = ''
self.jmsg = {}
def DecodeTopic(self, inpTopic):
"""This decodes an input topic """
self.config_found = 0
myList = inpTopic.split("/")
print(myList)
for sec in config.sections():
if sec == 'mqtt' or sec == 'thingspeak' or sec == 'general':
continue
if config['mqtt'].get('top_topic') + '/' + myList[1] == config[sec].get('base_topic',""):
self.config_found = 1
self.title = sec
self.base_topic = config[sec].get('base_topic', '')
self.field1_topic = config[sec].get('field1_topic', '')
self.field2_topic = config[sec].get('field2_topic', '')
self.field3_topic = config[sec].get('field3_topic', '')
self.field4_topic = config[sec].get('field4_topic', '')
self.field5_topic = config[sec].get('field5_topic', '')
self.field6_topic = config[sec].get('field6_topic', '')
self.field7_topic = config[sec].get('field7_topic', '')
self.field8_topic = config[sec].get('field8_topic', '')
self.action = myList[3]
self.subtopic = myList[4]
self.api_key = config[sec].get('write_key', '')
#config.set(sec,'active', '1')
#print(config_fname)
#with open('thingspeak.cfg', 'w') as f:
# config.write(f, True)
# print('Config file updated')
if not self.config_found:
logging.warning("Config not found, topic : {}".format(inpTopic))
def DecodeMsg(self, inpMsg):
"""Decodes an input msg"""
self.value = 0
str_inp = inpMsg.decode('UTF-8')
logging.info(str_inp)
if str_inp.startswith('{'): # the payload is a JSON message
self.jmsg = json.loads(str_inp)
print("JSON message")
else: # not JSON
print("Not a JSON message")
if self.config_found == 1:
self.value = float(str_inp)
self.jmsg[self.subtopic] = self.value
if self.subtopic == self.mqtt_val1:
self.float1 = self.value
if self.subtopic == self.mqtt_val2:
self.float2 = self.value
return self.value
#def DecodeMqtt(self, mqttMsg):
# """Populates structure for mqtt message input"""
# self.DecodeTopic(mqttMsg.topic)
# self.DecodeMsg(mqttMsg.payload)
#def SerialOutput(self):
# outStr = "P,{0:d},{1:d},{2:d},{3},{4:d},{5:d},{6},{7}\n".format(self.node, self.device,
# self.instance, self.action, self.result, self.req_ID, self.float1, self.float2)
# return outStr
#*******************************************************************
class thingspeakUpdate:
"""Class to build up a whole TS update from a number of mqtt messages """
def __init__(self, inp_Base_Topic, inp_API_Key):
self.init_dt = datetime.datetime.now()
self.data_dict = {}
self.field1 = ''
self.field2 = ''
self.field3 = ''
self.field4 = ''
self.field5 = ''
self.field6 = ''
self.field7 = ''
self.field8 = ''
self.base_topic = inp_Base_Topic
self.api_key = inp_API_Key
self.complete = 0
# **************************************************************
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
logging.info("Connected to mqtt server")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
#client.subscribe("homeassistant/#")
#logging.info("Subscribed to homeassistant/#")
for sec in config.sections():
if sec == 'mqtt' or sec == 'thingspeak' or sec == 'general':
continue
sTopic = config[sec].get('base_topic') + '/I/#'
client.subscribe(sTopic)
logging.info("Subscribed to " + sTopic)
# **************************************************************
# The callback for when a MQTT message is received from the server.
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload))
jStr = msg.payload.decode("utf-8")
# instantiate & populate the message rec
gMsg = gatewayMessage(config)
gMsg.DecodeTopic(msg.topic)
gMsg.DecodeMsg(msg.payload)
logging.info("MQTT message received {} : {}".format(msg.topic, msg.payload))
# don't bother with errors or rubbish mqtt messages
if gMsg.config_found == 0:
return
# look for an update record
uD_found = 0
nCnt=0
for uD in updateList:
if gMsg.base_topic == uD.base_topic:
curr_uD = uD
uD_found = 1
break
else:
nCnt += nCnt
# add one to the list if it doesn't exist
if not uD_found:
updateList.append(thingspeakUpdate(gMsg.base_topic, gMsg.api_key))
curr_uD = updateList[-1]
nCnt = len(updateList)-1
for k, v in gMsg.jmsg.items():
curr_uD.data_dict[k] = v
# update the relevant field in the update record
if gMsg.subtopic == gMsg.field1_topic:
curr_uD.field1 = jStr
elif gMsg.subtopic == gMsg.field2_topic:
curr_uD.field2 = jStr
elif gMsg.subtopic == gMsg.field3_topic:
curr_uD.field3 = jStr
elif gMsg.subtopic == gMsg.field4_topic:
curr_uD.field4 = jStr
elif gMsg.subtopic == gMsg.field5_topic:
curr_uD.field5 = jStr
elif gMsg.subtopic == gMsg.field6_topic:
curr_uD.field6 = jStr
elif gMsg.subtopic == gMsg.field7_topic:
curr_uD.field7 = jStr
elif gMsg.subtopic == gMsg.field8_topic:
curr_uD.field8 = jStr
# work out if we have all the fields
curr_uD.complete = 1
if gMsg.field1_topic != '' and curr_uD.field1 == '':
curr_uD.complete = 0
if gMsg.field2_topic != '' and curr_uD.field2 == '':
curr_uD.complete = 0
if gMsg.field3_topic != '' and curr_uD.field3 == '':
curr_uD.complete = 0
if gMsg.field4_topic != '' and curr_uD.field4 == '':
curr_uD.complete = 0
if gMsg.field5_topic != '' and curr_uD.field5 == '':
curr_uD.complete = 0
if gMsg.field6_topic != '' and curr_uD.field6 == '':
curr_uD.complete = 0
if gMsg.field7_topic != '' and curr_uD.field7 == '':
curr_uD.complete = 0
if gMsg.field8_topic != '' and curr_uD.field8 == '':
curr_uD.complete = 0
# ****************************************************************
def main_program():
"""This program subscribes to the mqtt server and publishes specfic
data items to thingspeak channels
"""
time_cntr = time.time()
# set up logging
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(filename='thingspeak1a.log',
level=config["general"].getint("loglevel"),
format=FORMAT)
logging.info('Thingspeak starting')
# Connect to the mqtt server
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(config['mqtt'].get('ip_address','127.0.0.1'),
config['mqtt'].getint('port', 1883),
config['mqtt'].getint('timeout',60))
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_start()
while True:
time.sleep(1)
if time_cntr + 60 < (time.time()):
time_cntr = time.time()
config.read(config_fname)
print("Re-read config file")
nCnt=0
for uD in updateList:
# if it's taken longer than 5 seconds, send the update anyway
timeDiff = datetime.datetime.now() - uD.init_dt
if timeDiff.seconds > 5:
logging.info("Timeout - sending update anyway")
uD.complete = 1
if uD.complete == 1:
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain",
"THINGSPEAKAPIKEY": uD.api_key}
params = "field1={}".format(uD.field1)
if uD.field2 != '':
params = params + "&field2={}".format(uD.field2)
if uD.field3 != '':
params = params + "&field3={}".format(uD.field3)
if uD.field4 != '':
params = params + "&field4={}".format(uD.field4)
if uD.field5 != '':
params = params + "&field5={}".format(uD.field5)
if uD.field6 != '':
params = params + "&field6={}".format(uD.field6)
if uD.field7 != '':
params = params + "&field7={}".format(uD.field7)
if uD.field8 != '':
params = params + "&field8={}".format(uD.field8)
print(params)
try:
conn = http.client.HTTPConnection(config['thingspeak'].get('url'))
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
logging.info("HTTP send success")
conn.close()
updateList.pop(nCnt)
except:
logging.warning("HTTP send error {}:{}".format(headers, params))
else:
nCnt += 1
# Only run the program if called directly
if __name__ == "__main__":
main_program()