-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
49 lines (35 loc) · 1.46 KB
/
client.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
#!/usr/bin/env python
# Copyright (C) 2016 Synapse Wireless, Inc.
"""Data Collector MQTT Client script for use with Example 1"""
from __future__ import print_function
import logging
from synapse_data_collector_client.simple_client import simple_data_collector_client
LOG = logging.getLogger(__name__)
# TODO: Replace with your SNAP Thing Services credentials
STS_USER = "username"
STS_PASS = "password"
def print_poll_results(poll):
"""Print out the poll results.
Prints out the data for each successful node.
Prints out error codes for each failed node.
"""
for node, data in poll['successful'].items():
# Parse the CSV string from the node
poll_counter, temp_dC, ps_mV = [int(v) for v in data.split(b',')]
# Print out the data, converting to more common units
print("{} {} [{}]: Temperature = {} degC, Power Supply Voltage = {} V".format(poll['timestamp'], node, poll_counter, temp_dC * 0.1, ps_mV * 0.001))
for node, err_code in poll['failed'].items():
# For failed nodes, just print out the error code.
print("{} {}: ERROR {}".format(poll['timestamp'], node, err_code))
if __name__ == '__main__':
logging.basicConfig()
client = simple_data_collector_client(
poll_cb=print_poll_results,
metrics_cb=print,
status_cb=print,
mqtt_user=STS_USER,
mqtt_pass=STS_PASS,
topic="SCDC"
)
print("Polling until CTRL-C is pressed")
client.loop_forever()