Capture IOT sensor’s data — MQTT Protocol + RabbitMQ as MQTT Broker

Ritresh Girdhar
4 min readMay 7, 2020
Photo by Joshua Sortino on Unsplash.

Introduction

Purpose of this article is to give you an overview of how to capture events from MQTT enabled IOT sensors/devices and monitors it via ELK stack.

After capturing events, you could either store it in event-stores or in time-series database for further processing.

Here I am taking a very basic example of capturing an event i.e temperature change city wise.

MQTT Broker — RabbitMQ

MQTT Broker is a pre-requisite to capture MQTT events from IOT devices. Here I am using RabbitMQ as MQTT Broker

Check this before selecting MQTT Broker and for basic understanding of RabbitMQ watch this.

MQTT Client — Mosquitto MQTT publisher

To simulate actual behaviour of IOT device’s event generation I am using Mosquitto MQTT publisher.

Installation

  1. Install RabbitMQ Server.
  2. Install mosquitto package.
  3. Set up ELK on your machine.

Make sure to enable mqtt plugin in rabbitmq using below command:

rabbitmq-plugins enable rabbitmq_mqtt

Create a Queue

Bind exchange to queue

Publish a sample message

Check whether the message is received or not

Publish message from Mosquitto MQTT publisher

Let’s try to send some messages from Mosquitto publisher using the below command :

mosquitto_pub -h 127.0.0.1 -t weather.mumbai -m “{“temperature”:{“min”:21,”max”:29,”unit”:”celsius”},”time”:1568881230}” -u guest -P guest -p 1883 -d

Check whether the message is received or not in RabbitMQ Queue

How To Install Elasticsearch, Logstash, and Kibana (ELK Stack)

Start Logstash Server

Use the below configuration which will read weather.* events from RabbitMQ queue and will dump the same into weather index in Elastic Search.

input {
rabbitmq {
host => “localhost”
port => 15672
heartbeat => 30
durable => true
exchange => “amq.topic”
exchange_type => “topic”
key => “weather.*”
}
}
output {
elasticsearch {
hosts => “localhost:9200”
index => “weather”
}
stdout {}
}

Paste the above configuration in logstash-rabbitmq.conf file

Run Logstash server

logstash -f <path>/logstash-rabbitmq.conf

Once logstash started successfully, you will be able to see logstash queue created and binded with weather* events in RabbitMQ

Let’s publish some test messages via RabbitMQ GUI

Kibana Dashboard

Let’s check Kibana whether it is receiving any events under weather-index or not.

As you could see in below Kibana search that Elastic-Search received some events in weather-mumbai index via logstash.

Publish some message from Mosquitto MQTT publisher

$ mosquitto_pub -h 127.0.0.1 -t weather.mumbai -m ‘{“temperature”:{“min”:21,”max”:32,”unit”:”celsius”},”timestamp”:”2019–09–19T18:59:00"}’ -u guest -P guest -p 1883 -d

Finally you could see your messages in kibana, now you can configure Kibana Dashboards and create charts for ex: compare last 3 month temperature change etc.

--

--

Ritresh Girdhar

Father || Coder || Engineer || Learner || Reader || Writer || Silent Observer