MQTT – IoT protocol test – flower pot moisture web monitoring

In this article I want to describe a testing of one of the many protocols dedicated to the Internet of Things / Web of Things – MQTT protocol and some of the services (hosting platform and mqtt broker) which can be helpful in creating IoT-based solutions. The idea was to create an IoT-like weekend-long project for soil moisture monitoring in a flower pot through the Web.

1. What I used

  • Raspberry Pi as a mqtt client working on Node.JS
  • Arduino with soil moisture sensor (because RPi doesn’t have Analog Input)
  • Open Shift – nice platform that I used for Node.JS server (up to 3 gears for free)
  • CloudMQTT – nice MQTT Broker (up to 10 connections for free)

  • For publishing and subscribing tests very helpful was MQTT.fx tool.

    2. MQTT
    In a few words, MQTT (Message Queue Telemetry Transport) is a very lightweight message protocol invented by IBM using TCP/IP. Communication is based on publish/subscribe scheme where MQTT broker takes care of message transportation.

    Broker can provide three types of QoS (Quality of Service) such as:
    at most one message
    at least one message
    exactly one message

    Once you are connected to the specified broker, you can publish message (eg.: humidity value) with a topic given by you (eg.: home/flowerpot/humidity) and any other devices connected to the same broker will get the message immediately if they just subscribe the same topic (home/flowerpot/humidity). It can’t be simpler. The client can, of course, subscribe the same topic it publishes. Just think how it is easy to create chat application with MQTT. Maybe that’s why Facebook Messenger uses it. To get more info check:

    3. Architecture
    Above you can see the image of the communication architecture for my solution. I think it is clear enough, however, I will describe it in a few words. Raspberry Pi (connected to home router) runs Node.JS script which connects the MQTT Broker on specified port. Necessary data for connection with MQTT broker such as port, hostname, username and password is given in ‘Control Panel’ of CloudMQTT after creating the instance. Node.JS script also communicates with Arduino by serialport. When the data (soil humidity) comes from the Arduino’s serial port it is published to the MQTT Broker. Next, the broker emits the data for subscribers to get it. When the client (web browser) connects to the http server (on OpenShift platform), the actual value of the soil’s humidity is emitted by Node.JS’ WebSocket.

    4. Sample code

    Example code for Node.JS mqtt client:

    connection to MQTT Broker and publishing with Node.JS mqtt (Raspberry Pi side):

    var mqtt = require('mqtt');
    var client = mqtt.connect('mqtt://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}');
    var humidity = 0;

    client.on('connect', function() {
    // publish a message to a topic: home/flowerpot/humidity
    client.publish('home/flowerpot/humidity', humidity, function() {
    //client.end(); // you can close the connection when published

    connection to MQTT Broker and subscribing wiht Node.JS mqtt (http server side):

    var mqtt = require('mqtt');
    var client = mqtt.connect('mqtt://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}');

    client.subscribe('home/flowerpot/humidity', function() {
    client.on('message', function(topic, message, packet) {
    console.log("received '" + message + "' on '" + topic + "'");
    // do something with message, for eg.: emit via WebSocket to the connected clients

    5. Results

    Client-side is just a simple animation created with HTML Canvas. Data is being sent from the server in realtime via WebSocket. The reaction (flower pot animation) is almost immediate. Here is the website: However, I don’t know how long it will be available :-)

    MQTT is a great protocol for exchanging small messages. It fits IoT needs very well as devices are powered from batteries. From the energy consumption point of view, the lower overhead means that sending a message takes less time, and yet, time is the energy. The sooner the device goes into sleep mode the better for its battery lifetime.

    Posted in IoT, Raspberry Pi | Tagged , , , , , | Leave a comment

    Raspberry Pi + Pocketsphinx + Dimmer = Voice Dimmer

    In the last couple of days I was playing with voice recognition on Raspberry Pi. I have tried a library called Pocketsphinx written in C:
    With a medium knowledge of C language it can be easily adopted to custom application. In my case I created an application which controls a lamp. Some time ago I built a light dimmer based on Atmel’s AVR Atmega32U2 microcontroller working also as a USB CDC, so it can be connected to Pi as a serial device. As you can see on a scheme below, it is a simple dimmer which allows to control 3 bulps separately.


    As a microphone I used webcam (Logitech 9000 Pro).

    Here is a nice tutorial I followed:
    This also might be helpful:

    As it is written in the tutorial after installation you can run sample recognition with: ‘./pocketsphinx_continuous’.

    Without any params it will be run with 16000Hz samplerate, if you want higher rate you have to increase ‘nfft’ param properly. To disable logs you can redirect output to /dev/null with ‘logfn’ param. So 48kHz without logs will be:
    ./pocketsphinx_continuous -nfft 2048 -samprate 48000 -logfn /dev/null

    If you want to create your own C/C++ application using Pocketsphinx a good way to start is to use code from continous.c file that can be found under:
    To compile it with gcc or g++ you have to put necessary libraries and includes as it is showed below:
    gcc continuous.c -I/usr/local/include -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -L/usr/local/lib -lpocketsphinx -lsphinxbase -lsphinxad

    Posted in Electronics, Raspberry Pi | Tagged , , , | 5 Comments

    Property get vs get-like function

    I was wondering if there are any differences between using property getter and simple get function.. well there’s no difference, but lets see

    public class PropVsFun
        private int _x;
        public int x { get { return _x; } set { _x = value; } }
        public int getX() { return _x; }
        public int pX;
        public PropVsFun(int X)
            _x = X;
            pX = X;
    void test()
        PropVsFun pvsf = new PropVsFun(10);
        int a, b, c;
        // Property
        a = pvsf.x;
        // Function get
        b = pvsf.getX();
        // Variable
        c = pvsf.pX;

    There is no difference between IL codes of property get and get function as we see below:

    IL_0009: callvirt instance int32 CodeEfficiencyTests.PropVsFun::get_x()
    IL_000e: pop
    IL_000f: ldloc.0
    IL_0010: callvirt instance int32 CodeEfficiencyTests.PropVsFun::getX()
    IL_0015: pop
    IL_0016: ldloc.0
    IL_0017: ldfld int32 CodeEfficiencyTests.PropVsFun::pX
    IL_001c: pop
    IL_001d: ret

    Posted in C# | Tagged , | Leave a comment

    Initialization and access performance tests

    I was wondering many times whether to use structure or class, how I should initialize structure, and ofcourse about array vs list accessibility issue. There is a lot of tests on the web, however I decided to manage some sort of them by myself and put the results here.

    Continue reading

    Posted in C# | Tagged , | 1 Comment