From c805d9c30611b3d23d026bc1f72cc5d9abee732d Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Wed, 7 Sep 2016 12:39:40 +0200 Subject: [PATCH] add example for otr.publish() --- doc/HOOKS.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/HOOKS.md b/doc/HOOKS.md index c8383f4..0b5ee16 100644 --- a/doc/HOOKS.md +++ b/doc/HOOKS.md @@ -100,4 +100,27 @@ otr_publish(topic, payload, qos, retain) `topic` and `payload` are mandatory and must be strings. `qos` and `retain` are optional and specify the QoS for publishing as well as a retain flag; `qos` must be specified if `retain` is to be. +```lua +JSON = (loadfile "JSON.lua")() -- http://regex.info/blog/lua/json + +function otr_init() +end + +function otr_exit() +end + +function otr_hook(topic, _type, data) + -- republish partial messages to a different topic + local d = {} + d['_type'] = data['_type'] + d['lat'] = data['lat'] + d['lon'] = data['lon'] + d['fromLua'] = true + + local payload = JSON:encode(d) + + otr.publish("topic/1", payload, 1, 0) +end +``` + This function allows your code to publish via MQTT from the Recorder, using the same MQTT connection (including TLS, authentication, etc) as the Recorder uses.