Skip to content

Commit 1f36edc

Browse files
authored
Merge pull request Manuel83#2 from mrillies/patch-2
Critical update
2 parents 1ad2f03 + 87efc8d commit 1f36edc

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# MQTT Plugin for CraftBeerPi 3.0
22

3-
This plugins allows to connect to an MQTT Message broker to receive sensor data and invoke actors.
3+
This plugins allows to connect to an MQTT Message broker to receive sensor data and invoke actors.
4+
5+
46

57
## Installation
68

@@ -31,3 +33,15 @@ The current version don't support username and password log for the mqtt broker
3133

3234
# MQTT Test Client
3335
A nice MQTT test client is mqtt.fx http://www.mqttfx.org/
36+
37+
38+
# Plugin config
39+
40+
## MQTT Sensor
41+
42+
- Enter the message topic
43+
- If the data in the payload is in a dictionary, specify the path in "Payload dictionary" with '.' seperators. EG
44+
- msg = { "Name":"MySensor", "Sensor": {"Value": 32 , "Type" : "1-wire"}
45+
- "Payload Dict" = Sensor.Value
46+
- If you data is raw eg (mosquitto_pub -d -t temperture -m 32), leave "Payload Dictionary" Blank
47+
- Enter prefered unit up to 3 chars

__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def off(self):
4141
@cbpi.sensor
4242
class MQTT_SENSOR(SensorActive):
4343
a_topic = Property.Text("Topic", configurable=True, default_value="", description="MQTT TOPIC")
44-
b_payload = Property.Text("Payload Dict", configurable=True, default_value="", description="Where to find msg in patload, leave blank for raw payload: EG msg = {"A":{"B": 32 }} A.B will return 32)
45-
c_unit = Property.Text("Unit", configurable=True, default_value="°C", description="Units to display")
44+
b_payload = Property.Text("Payload Dictioanry", configurable=True, default_value="", description="Where to find msg in patload, leave blank for raw payload")
45+
c_unit = Property.Text("Unit", configurable=True, default_value="", description="Units to display")
4646

4747
last_value = None
4848
def init(self):
@@ -51,23 +51,24 @@ def init(self):
5151
self.payload_text = None
5252
else:
5353
self.payload_text = self.b_payload.split('.')
54-
self.unit = self.c_unit
54+
self.unit = self.c_unit[0:3]
5555

5656
SensorActive.init(self)
5757
def on_message(client, userdata, msg):
5858

5959
try:
6060
print "payload " + msg.payload
6161
json_data = json.loads(msg.payload)
62-
print json_data
62+
#print json_data
6363
val = json_data
6464
if self.payload_text is not None:
6565
for key in self.payload_text:
66-
val = val.get(key, 0)
67-
print val
68-
q.put({"id": on_message.sensorid, "value": val})
69-
except:
70-
pass
66+
val = val.get(key, None)
67+
#print val
68+
if isinstance(val, (int, float, basestring)):
69+
q.put({"id": on_message.sensorid, "value": val})
70+
except Exception as e:
71+
print e
7172
on_message.sensorid = self.id
7273
self.api.cache["mqtt"].client.subscribe(self.topic)
7374
self.api.cache["mqtt"].client.message_callback_add(self.topic, on_message)
@@ -76,7 +77,7 @@ def on_message(client, userdata, msg):
7677
def get_value(self):
7778
return {"value": self.last_value, "unit": self.unit}
7879

79-
deg get_units(self):
80+
def get_unit(self):
8081
return self.unit
8182

8283
def stop(self):

0 commit comments

Comments
 (0)