Skip to content

Commit

Permalink
Merge pull request #2 from mrillies/patch-2
Browse files Browse the repository at this point in the history
Critical update
  • Loading branch information
Manuel83 authored Sep 18, 2017
2 parents 1ad2f03 + 87efc8d commit 1f36edc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# MQTT Plugin for CraftBeerPi 3.0

This plugins allows to connect to an MQTT Message broker to receive sensor data and invoke actors.
This plugins allows to connect to an MQTT Message broker to receive sensor data and invoke actors.



## Installation

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

# MQTT Test Client
A nice MQTT test client is mqtt.fx http://www.mqttfx.org/


# Plugin config

## MQTT Sensor

- Enter the message topic
- If the data in the payload is in a dictionary, specify the path in "Payload dictionary" with '.' seperators. EG
- msg = { "Name":"MySensor", "Sensor": {"Value": 32 , "Type" : "1-wire"}
- "Payload Dict" = Sensor.Value
- If you data is raw eg (mosquitto_pub -d -t temperture -m 32), leave "Payload Dictionary" Blank
- Enter prefered unit up to 3 chars
21 changes: 11 additions & 10 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def off(self):
@cbpi.sensor
class MQTT_SENSOR(SensorActive):
a_topic = Property.Text("Topic", configurable=True, default_value="", description="MQTT TOPIC")
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)
c_unit = Property.Text("Unit", configurable=True, default_value="°C", description="Units to display")
b_payload = Property.Text("Payload Dictioanry", configurable=True, default_value="", description="Where to find msg in patload, leave blank for raw payload")
c_unit = Property.Text("Unit", configurable=True, default_value="", description="Units to display")

last_value = None
def init(self):
Expand All @@ -51,23 +51,24 @@ def init(self):
self.payload_text = None
else:
self.payload_text = self.b_payload.split('.')
self.unit = self.c_unit
self.unit = self.c_unit[0:3]

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

try:
print "payload " + msg.payload
json_data = json.loads(msg.payload)
print json_data
#print json_data
val = json_data
if self.payload_text is not None:
for key in self.payload_text:
val = val.get(key, 0)
print val
q.put({"id": on_message.sensorid, "value": val})
except:
pass
val = val.get(key, None)
#print val
if isinstance(val, (int, float, basestring)):
q.put({"id": on_message.sensorid, "value": val})
except Exception as e:
print e
on_message.sensorid = self.id
self.api.cache["mqtt"].client.subscribe(self.topic)
self.api.cache["mqtt"].client.message_callback_add(self.topic, on_message)
Expand All @@ -76,7 +77,7 @@ def on_message(client, userdata, msg):
def get_value(self):
return {"value": self.last_value, "unit": self.unit}

deg get_units(self):
def get_unit(self):
return self.unit

def stop(self):
Expand Down

0 comments on commit 1f36edc

Please sign in to comment.