-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpubScribe.py
354 lines (252 loc) · 9.14 KB
/
pubScribe.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
"""
Copyright(C) 2021, BrucesHobbies
All Rights Reserved
AUTHOR: BrucesHobbies
DATE: 3/01/2021
REVISION HISTORY
DATE AUTHOR CHANGES
yyyy/mm/dd --------------- ------------------------------------------------
2021/04/14 BrucesHobbies Added support for Antonio's variable tone buzzer
OVERVIEW:
Place holder for alert and publish-subscribe
LICENSE:
This program code and documentation are for personal private use only.
No commercial use of this code is allowed without prior written consent.
This program is free for you to inspect, study, and modify for your
personal private use.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
--- if MQTT is used, basic commands for install of MQTT ---
sudo apt-get install mosquitto # local broker
sudo apt-get install mosquitto-clients # local publish-subscribe
sudo systemctl enable mosquitto # Enable the broker and allow it to auto-start after reboot
sudo systemctl status mosquito # Check status
hostname -I
ifconfig | grep inet | grep broadcast
sudo pip3 install paho-mqtt
--- if INFLUXDB is used, basic commands for install of INFLUXDB are found on web pages ---
--- Plan to add SQL ---
"""
import os
import sys
import time
import datetime
#
# USER CONFIGURATION SECTION
# Select one or more options to enable
#
CSV_FILE_ENABLED = 1
EMAIL_SMS_ENABLED = 1
IP_PORT_ENABLED = 0 # Future
# MQTT
MQTT_ENABLED = 0
MQTT_HOST = "localhost"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
# INFLUX_DB
INFLUX_DB_ENABLED = 0
INFLUX_HOST = "192.168.100.11"
INFLUX_PORT = 8086 # default port
INFLUX_USER = "rpi" # requires write access
INFLUX_PASSWORD = "rpi"
INFLUX_DBNAME = "sensor_data"
# BUZZER
BUZZER_ENABLED = 0
buzzerPIN = 18 # Customize based on your wiring
# --- END USER CONFIGURATION ---
if MQTT_ENABLED :
import paho.mqtt.client as mqtt
mqttClient = mqtt.Client()
if EMAIL_SMS_ENABLED :
import sendEmail
if INFLUX_DB_ENABLED :
from influxdb import InfluxDBClient
if BUZZER_ENABLED :
import RPi.GPIO as GPIO
from threading import Timer
def connectPubScribe() :
global mqttClient
global influxClient
if MQTT_ENABLED :
mqttClient.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
if EMAIL_SMS_ENABLED :
sendEmail.loadJsonFile()
# sendStatus("pubScribe.py", " Program start")
if INFLUX_DB_ENABLED :
influxClient = InfluxDBClient(INFLUX_HOST, INFLUX_PORT, INFLUX_USER, INFLUX_PASSWORD, INFLUX_DBNAME)
if BUZZER_ENABLED :
# GPIO.setwarnings(False) # Remove warning message
GPIO.setmode(GPIO.BCM) # Set the pin mode to BOARD mode
GPIO.setup(buzzerPIN, GPIO.OUT) # Buzzer is output mode
return
def disconnectPubScribe() :
if MQTT_ENABLED :
mqttClient.disconnect()
if BUZZER_ENABLED :
GPIO.cleanup()
return
def attachFunction() :
return
# Destinations
MQTT = 'MQTT'
CSV_FILE = 'CSV_FILE'
EMAIL_SMS = 'EMAIL_SMS'
INFLUX_DB = 'INFLUX_DB'
BUZZER = 'BUZZER'
#
# Publish data record
# dest: [MQTT, CSV_FILE, EMAIL_SMS, INFLUX_DB]
# topic: 'topic/subtopic', 'topic/subtopic/alert', or etc.
# data: dict, list, or str
#
def pubRecord(dest, topic, data, hdr="") :
# print("DEST: ", dest, " TOPIC: ", topic, " DATA: ", data, " HDR: ", hdr)
if MQTT_ENABLED and (MQTT in dest) :
if not isinstance(data,str) :
msg = json.dumps(data)
else :
msg = data
mqttClient.publish(topic, msg)
if CSV_FILE_ENABLED and (CSV_FILE in dest) :
writeCsv(topic, data, hdr)
if EMAIL_SMS_ENABLED and (EMAIL_SMS in dest) :
if not isinstance(data, str) :
msg = str(data)
# if not isinstance(data,str) :
# msg = json.dumps(data, indent=4)
else :
msg = data
upperTopic = topic.upper()
if 'ALERT' in upperTopic :
sendAlert(topic, msg)
elif 'STATUS' in upperTopic :
sendStatus(topic, msg)
if INFLUX_DB_ENABLED and (INFLUX_DB in dest) :
if not isinstance(data,str) :
msg = json.dumps(data)
else :
msg = data
influxClient.write_points(msg)
if BUZZER_ENABLED and (BUZZER in dest) :
buzzerOn(data)
return
#
# CSV files
#
topicFmtStr = {} # format string for data records in a topic's csv file
topicFiles = {} # Dictionary of csv files that exist
#
# Enables custom format strings per topic when writting csv files
#
def addTopicFmtStr(topic, fmtStr) :
topicFmtStr[topic] = fmtStr
#
# For new files, create a header row using dict or from hdr
#
def addTopicFileHeaders(filename, topic, data, hdr="") :
result = ""
if not (topic in topicFiles) :
topicFiles[topic] = hdr
if not os.path.isfile(filename) :
# If csv log file does not exist, write header
result = 'UNIX time (s),DateTime,'
if isinstance(data, dict) :
# print("dict: ", data)
result += ",".join("{}".format(k) for k in data) # keys
# print(result)
else :
# print("Else: ", hdr)
result += hdr
# print(result)
result += '\n'
return result
#
# Append data to CSV file
#
def writeCsv(topic, data, hdr="") :
filename = topic.replace('/','_') + ".csv"
# print("Filename: ", filename)
s = addTopicFileHeaders(filename, topic, data, hdr)
s += str(round(time.time())) + "," + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S,')
if isinstance(data, dict) :
s += ",".join("{}".format(v) for k, v in data.items()) # values
elif isinstance(data, list) :
if topic in topicFmtStr :
s += topicFmtStr[topic].format(*data)
else :
s += ('{},'*len(data)).format(*data)
elif isinstance(data, str) :
s += data
else :
print("Type not supported")
# write interval data to csv file
with open(filename, "a") as csvFile :
csvFile.write(s + '\n')
csvFile.close()
#
# EMAIL SMS
#
#
# Send alert via email to another email or as SMS text
#
def sendAlert(subj, msg) :
msg = time.strftime("%a, %d %b %Y %H:%M:%S \n", time.localtime()) + msg
sendEmail.send_mail(sendEmail.ALERT_USERID, subj, msg)
#
# Send status via email to another email or as SMS text
#
def sendStatus(subj, msg) :
msg = time.strftime("%a, %d %b %Y %H:%M:%S \n", time.localtime()) + msg
sendEmail.send_mail(sendEmail.STATUS_USERID, subj, msg)
#
# Buzzer On
#
buzzer = []
def buzzerOn(data) :
global buzzer
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S '),"Buzzer On", " F: ", data.get('Frequency', 700), " DC: ", data.get('Dutycycle', 10), " Duration(s): ", data.get('Duration',10))
buzzer = GPIO.PWM(buzzerPIN, data.get('Frequency', 700)) # Default is 700 Hz
buzzer.start(data.get('Dutycycle', 10)) # Default is 10%
Timer(data.get('Duration',10), buzzerOff).start() # Default is 10 seconds
#
# Buzzer Off
#
def buzzerOff() :
global buzzer
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S '), "Buzzer Off")
buzzer.stop()
#
# Test / debug
#
if __name__ == '__main__':
print("\nPress CTRL+C to exit...\n")
connectPubScribe()
topic = "Sensor"
dictVar = {"Current": 6.4,"Power": 3.2,"Energy": 4.5,"PF": 0.95}
pubRecord(CSV_FILE, topic, dictVar)
topic = "Sensor/Alert"
listVar = [ 6.4, 3.2, 4.5, 0.95]
pubRecord(CSV_FILE, topic, listVar,"Col1,Col2,Col3,Col4")
fmtStr = "{:.1f},{:.2f},{:.3f},{:.4f}"
addTopicFmtStr(topic, fmtStr)
pubRecord(CSV_FILE, topic, listVar, "Col1,Col2,Col3,Col4")
topic = "Sensor/topic"
sVar = '6.4, 3.2, 4.5, 0.95'
pubRecord(CSV_FILE, topic, sVar, "Col1,Col2,Col3,Col4")
# Test buzzer
if BUZZER_ENABLED :
pubRecord(BUZZER, "", {'Frequency': 500, 'Dutycycle': 20, 'Duration': 20})
time.sleep(60)
pubRecord(BUZZER, "", {})
time.sleep(60)
pubRecord(BUZZER, "", {'Frequency': 900, 'Dutycycle': 30, 'Duration': 40})
time.sleep(60)
disconnectPubScribe()