MQTT support in OpenNetHome

Functionality and usage of the OpenNethome Server
Jocke.g
Posts: 20
Joined: Tue Jan 05, 2016 9:06 pm

MQTT support in OpenNetHome

Post by Jocke.g »

We made a pull request some days ago, making OpenNetHome listen to MQTT.

MQTT is perfect for connecting home made sensors and other equipment to OpenNetHome, for example arduino-compatible ESP8266 (https://www.m.nu/esp8266-c-126_257.html)

You can either install a local MQTT-broker, like https://mosquitto.org/, or use one in the cloud for testing, like test.mosquitto.org.

There is four new Homeitems.
MQTTCommandPort
This is the item which actually listens to MQTT. Here you can specify the address to your MQTT broker, and the topic on which OpenNetHome subscribes.

MqttHygrometer
A moisture sensor, listening on a specific MQTT topic.

MqttThermometer
A temperaturesensor, listening on a specific MQTT topic.

MqttRemapButton
Listens to a specific topic, and can execute six different actions depending on the message received.

Hope you enjoy it!

ps.
Is it possible to get a account to the wiki, to add some documentation for these homeitems?

//Jocke
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: MQTT support in OpenNetHome

Post by stefangsbb »

Thanks for a really great contribution! I have been a bit slow in taking the pull request due to lack of time, but I have started now. I have been running in to some weird issues with Intellij and the MQTT library you are using, and I am currently trying to sort them out...
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: MQTT support in OpenNetHome

Post by stefangsbb »

So, Now I have merged the pull request with the suggested changes. I have also given you an account on the Wiki so you can add some documentation. Could you verify that the Items still work after my merge, since I did refactor some code in the MqttClient-Item?
Jocke.g
Posts: 20
Joined: Tue Jan 05, 2016 9:06 pm

Re: MQTT support in OpenNetHome

Post by Jocke.g »

Merge seems to not work perfect. I cant get the MQTTClient to connect anymore. Log says "Failed to connect to MQTT Server", so probably some exception is thrown on connect.

The renaming seems to not be complete also. There is still MQTTCommandPort on some pages.

//Jocke
Jocke.g
Posts: 20
Joined: Tue Jan 05, 2016 9:06 pm

Re: MQTT support in OpenNetHome

Post by Jocke.g »

Seems to be our mistake. There is some kind of sudo-related error. We have probably just debuged with sudo, and now it does not work on my real machine.

We will have a closer look and try to fix it when there is time. Or if someone else comes first with a fix, its welcome.

Stacktrace:

Code: Select all

2016.08.12 17:02:12.891 WARNING:Failed to connect to MQTT Server (MqttClient 18, TID138)
MqttException (0)
        at org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:80)
        at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:286)
        at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
        at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:224)
        at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:136)
        at nu.nethome.home.items.net.MqttClient.activate(MqttClient.java:106)
        at nu.nethome.home.impl.LocalHomeItemProxy.callAction(LocalHomeItemProxy.java:97)
        at nu.nethome.home.items.web.servergui.EditItemPage.updateAttributes(EditItemPage.java:617)
        at nu.nethome.home.items.web.servergui.EditItemPage.processAttributeUpdateAction(EditItemPage.java:284)
        at nu.nethome.home.items.web.servergui.EditItemPage.performActionsAndRedirect(EditItemPage.java:264)
        at nu.nethome.home.items.web.servergui.EditItemPage.printPage(EditItemPage.java:149)
        at nu.nethome.home.items.web.servergui.HomeGUI.service(HomeGUI.java:232)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502)
        at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:363)
        at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
        at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
        at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
        at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
        at org.mortbay.jetty.Server.handle(Server.java:324)
        at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:534)
        at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:879)
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:741)
        at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:213)
        at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:403)
        at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
        at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:522)
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: MQTT support in OpenNetHome

Post by stefangsbb »

Ok, I took your merge with the correction of the name and the other correction - thanks! Regarding the persistance exception, a quick google turned up this StackOverflow with instructions on how to use in memory storage: http://stackoverflow.com/questions/1349 ... ate-client.
Since I don't have a working setup with MQTT to test with, I don't want to do the change, but perhaps you can try it and get back if it seems to solve the problem?
krambriw
Posts: 86
Joined: Sun Jan 04, 2015 8:48 am

Re: MQTT support in OpenNetHome

Post by krambriw »

Code: Select all

event,Mqtt_Message,Direction,In,Mqtt.Message,just a test,Mqtt.Topic,MyHome/RPI,Value,
I can see that MQTT events are received in ONH (result above when monitoring with the TCP port and a telnet session). Can you also publish to a MQTT broker from ONH?
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: MQTT support in OpenNetHome

Post by stefangsbb »

Not yet. I have received this contribution with MQTT-Support and it can currently receive events and present values or react to messages, net send messages. I have had some ideas about a MQTT-Monitor Item that you would attach to any Item (like a thermometer or a rain gauge) and it would publish the value of the Item on MQTT. Maybe you have some comments if that would be a good idea?
krambriw
Posts: 86
Joined: Sun Jan 04, 2015 8:48 am

Re: MQTT support in OpenNetHome

Post by krambriw »

I think that is a good idea! Also if you can make it attachable to lamp items and other items so that when you switch the state of the device in ONH, this can be published to the outside via MQTT. In EventGhost, I have implemented support for publishing MQTT messages and the following parameters/settings I think is necessary to support:

MQTT Broker ip
MQTT Broker port (normally 1883)
Topic (to publish in)
Message
QOS (0,1 and 2)
Retain (True/False)

And then in your case the link to the actual item and it's various states plus whatever is needed for ONH

This is for an non-encrypted connection to the MQTT broker. It is also possible with a secure connection using TLS, encryption of payloads etc but the above could be a good start I think

Best regards, Walter

http://www.hivemq.com/blog/mqtt-essentials/
Jocke.g
Posts: 20
Joined: Tue Jan 05, 2016 9:06 pm

Re: MQTT support in OpenNetHome

Post by Jocke.g »

Fix for the persistence exception was also in the name correction that Patrik sent. We disabled persistence for the client. I have tested most of it now, and it works good.

MQTT-Monitor sounds like a really good idea! I have thought about something similair too. My first idea of the MQTT-implementation was that all homeitems should have an field for optional MQTT-topic. Messages received on the topic should update the item, and updates from e.g hardware should also be sent out on MQTT. But then I realized it would cause a loop, and we did this implementation instead. So MQTT-Monitor would complement perfect!

//Jocke
Post Reply