-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensornet.py
49 lines (40 loc) · 1.63 KB
/
sensornet.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
import time
from umqtt.robust import MQTTClient
import json
class SensorTransporter(MQTTClient):
def __init__(self, *args, **kwargs):
self.config = config = kwargs["config"]
self.client_id = config["client_id"]
self.sensor_name = config["sensor_name"]
self.broker = config["broker"]
self.topic = self.set_topic(config["topic"])
self.sensor = kwargs["sensor"]
super(SensorTransporter, self).__init__(self.client_id, self.broker, *args)
print("Transport settings: Broker: {0} Topic: {1}".format(self.broker, self.topic))
def set_topic(self, topic):
return '{}/{}'.format(topic or self.config.get('topic'),
self.client_id.decode('UTF-8'))
def knock_knock(self):
try:
self.connect()
except OSError:
time.sleep(5)
self.knock_knock()
else:
print("Saying Hello to %s" % self.broker)
self.publish("/debug", bytes("Hello from %s" % self.client_id, 'utf-8'))
self.disconnect()
def log_value(self, value=None):
if not value:
if 'ADC' in str(type(self.sensor)):
print type(self.sensor)
value = self.sensor.read()
else:
value = self.sensor.value() or 0
self.connect()
data = json.dumps({"sensor_name": self.sensor_name,
"sensor_pin": self.config['sensor_pin'],
"client_id": self.client_id,
"value":value,})
self.publish(self.topic, bytes(data, 'utf-8'))
self.disconnect()