Forwarding HTTP to MQTT

Intro

For enabling data acquisition via HTTP, just forward payloads of POST or PUT requests to the MQTT bus and let the downstream infrastructure MQTTKit handle all the rest.

Setup

This can be achieved by configuring a generic HTTP-to-MQTT forwarder application:

 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
50
51
52
53
54
55
56
57
58
; ------------------------------------------
; Family:   Protocol forwarder
; About:    Versatile protocol forwarder components for bridging the gap between
;           different data sinks, bus systems and serialization formats.
; ------------------------------------------
; Name:     http-to-mqtt forwarder
; Date:     June 2016
; About:    Suitable for data acquisition via HTTP POST/PUT requests.
; Channel:  Transport: HTTP, MQTT; Format: JSON, x-www-form-urlencoded
; See also: https://getkotori.org/docs/applications/forwarders/http-to-mqtt.html
;           https://getkotori.org/docs/handbook/acquisition/protocol/http.html
; ------------------------------------------
; Description:
;
;         - Listen to HTTP POST or PUT requests
;         - Receive payloads formatted as JSON or in urlencoded format
;         - Forward payloads to the MQTT bus
;
; Manual:   Please specify forwarding source and target parameters in uri format.
;           The MQTT topic path is derived from the http uri path by interpolating
;           the appropriate part of the context uri.
;
; Example:  In the example below, given the "address" part of the resource URI
;           is "testdrive/area-42/node-1", data sent to the full URI
;
;           /api/mqttkit-1/testdrive/area-42/node-1/data
;
;           will be republished to the MQTT topic
;
;                mqttkit-1/testdrive/area-42/node-1/data.json
;
; ------------------------------------------

[mqttkit-1.http-acquisition]
enable          = true

type            = application
application     = kotori.io.protocol.forwarder:boot

realm           = mqttkit-1
source          = http:/api/mqttkit-1/{address:.*}/{slot:(data|event)} [POST]
target          = mqtt:/mqttkit-1/{address}/{slot}.json


; ------------------------------------------------
;   Notes about "source" and "target" parameters
; ------------------------------------------------

; Note that the "netloc" part (i.e. for specifying hostname/port)
; in directives "source" and "target" are omitted from these uris.
;
; Kotori currently will only listen to the default http port and forward
; payloads to the default mqtt broker. Both are specified in the main
; configuration file, usually "/etc/kotori/kotori.ini".
;
; However, this might change in the future to enable spinning
; up HTTP listeners on arbitrary ports at runtime and to allow
; publishing messages to different MQTT brokers.

Tip

Also have a look at Data acquisition over HTTP for different ways to transmit telemetry data over HTTP.