diff --git a/README.md b/README.md index 2373e17..7b6a57b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/__init__.py b/__init__.py index 05ad5ca..65d73e5 100644 --- a/__init__.py +++ b/__init__.py @@ -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): @@ -51,7 +51,7 @@ 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): @@ -59,15 +59,16 @@ 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) @@ -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):