MQTT - **SOLVED**

Discussion of the Meteohub software package

Moderator: Mattk

Post Reply
baobab
Junior Boarder
Junior Boarder
Posts: 25
Joined: Sat Dec 25, 2010 11:00 pm

MQTT - **SOLVED**

Post by baobab » Mon Nov 07, 2016 6:12 pm

I have built a fairly extensive network of various sensors. For these, I am using MQTT and OpenHAB. I am trying to now find a way to get the sensor readings into Meteohub for evaluating, archiving, etc. Has anyone developed a means to accomplish this through a plug-in script? Ideally I would use a python based plug-in script to accomplish this but I cannot get python to work on my Meteohub (Debian Lenny) as the installation is either broken or not there and I am hesitant to start messing with it.

Sensor----MQTT---->MQTT Broker-------MQTT------>Python Script--------->Meteohub

Thanks,

Baobab
Last edited by baobab on Sun Dec 18, 2016 9:57 pm, edited 1 time in total.

baobab
Junior Boarder
Junior Boarder
Posts: 25
Joined: Sat Dec 25, 2010 11:00 pm

Re: MQTT

Post by baobab » Sat Dec 17, 2016 8:04 pm

Here is how I solved the problem outlined in the original post. The goal is to get sensor data into Meteohub via MQTT mapped to the appropriate sensor (thx, datay, thbz, etc). The first part of the solution I used is specific to the iConnect version of Meteohub and was needed to overcome some issues that may not exist with other platforms. The python script, added as a plug-in, uses the paho-mqtt library to subscribe to a topic and then outputs the sensor value to stdout for meteohub to recognize it (thx y z, where y is temperature and z is humidity).

Paho-mqtt requires python 2.7 or better. Metoehub for iConnect is based on Debian Lenny which comes with python 2.5. I tried installing python 2.7 but found that the system partition on the usb stick was not large enough. The solution that was successful is as follows:

1 - using gparted, redistribute unallocated space from the data partition to the system partition on the usb stick. Make sure you back up your usb stick as an image before doing this.
2 - download the appropriate python version, compile, and install. I did this late at night after a couple rums so best I can say is to google - this was helpful: https://gist.github.com/lukaslundgren/2659457. I did have issues with openssl and other dependencies but was able to find workarounds or solutions.
3 - once python 2.7.x was working, I installed pip and then the paho-mqtt library - https://eclipse.org/paho/clients/python/
4 - write up python script that will be the plug in and install and test

Below is the python script that I am using as a plug-in. It should work on any meteohub installation that supports python 2.7.x assuming the paho-mqtt library is installed and working. Please feel free to use it if it is helpful. The script should be easy to modify to add additional sensor nodes and then based on msg.topic map to the desired sensors in meteohub.

Code: Select all

#!/usr/bin/env python

import paho.mqtt.client as mqtt
import datetime
import time
import sys

time.sleep(10)

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
        # Subscribing in on_connect() means that if we lose the connection and
        # reconnect then subscriptions will be renewed.
    client.subscribe("node1/temphum")
    
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    if msg.topic == 'node1/temphum':
        data = msg.payload
        pieces = data.split()
        temp = int(round(float(pieces[0])*10))
        hum = int(round(float(pieces[1])))
        temp = str(temp)
        hum = str(hum)
        time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        time = str(time)
        print "th10 " + temp + " " + hum
        print "data10 " + time
        sys.stdout.flush()
    
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("192.168.1.xyz", 1883, 60)

client.loop_forever()

Post Reply