Graylog2 is a great product. It might not be the most sophisticated one when it comes to log aggregation and interrogation, but it does its job very well. After employing Graylog2 to collect logs from all my Docker containers I thought that it would be absolutely fantastic to be able to manage logs from the IoT devices as well.
Installing the right input plugin
Before we can push stuff into Graylog2 we need to teach it the protocol of IoT devices - MQTT. Luckily enough there is a ready-made and supported input plugin just for that. To install it simply download it to the plugin
folder in your Graylog2 installation and restart Graylog2. Done.
The next thing is to add an input for MQTT. Select System/Inputs
and then create an input by selecting MQTT TCP (GEFL)
from the drop-down list of available connectors. You need to specify the MQTT server and queue which the connector will subscribe to. In the example below I used system/logs
so if you would go for a different name you need to adjust the code accordingly. And last but not least you need to start the input by pressing the Start input
button. Done.
Programming in Lua
Lua and the NodeMCU environment is a beautiful thing. It is simple, powerful and it just rocks. For our little project we'll need to build ourselves a version with bmp085
, cjson
, wifi
and mqtt
modules. Then we need to download it to the module and we're all set to start coding in Lua.
The code here initializes WiFi connection, once the connection is established connects to the MQTT server, initializes the BMP085
sensor (here we're using pins D2-sda
and D1-scl
as per the documentation) and sets up a recurring alarm every second to read the sensor and send GEFL-formatted logs if MQTT has been connected. Easy.
Transferring files to NodeMCU
There are many great ways to send Lua files to the module. I like to use the one written in NodeJS called nodemcu-tool
$ npm install -g nodemcu-tool
$ nodemcu-tool upload init.lua
$ nodemcu-tool terminal
Now press the reset button and your NodeMCU should inform you of 3 things:
- That it connected to WiFi network and has an IP address
- That it has connected to the MQTT server
- That it is sending logs to MQTT server
Happy logging!
No comments:
Post a Comment