HomeGenie Forum

General Category => General Discussion => Topic started by: xefil on January 23, 2015, 08:43:47 AM

Title: MQTT Broker
Post by: xefil on January 23, 2015, 08:43:47 AM
Hello,

I've activated the MQTT from HG. I see it's contacting correctly the MQTT server. are there examples on how use the values it should collect from the MQTT server?

Thanks

Simon
Title: Re: MQTT Broker
Post by: Wibo on January 23, 2015, 06:53:40 PM
Hi Simon,

Have no idea what you're trying to accomplish, there's not much info.

I have 2 Homegenie servers talking to each other by using mqtt.
I use the Moquitto mqtt broker which runs on a win7 box.

If this is what you want I can provide you with info how to setup.

regards,
Wibo.
Title: Re: MQTT Broker
Post by: xefil on January 25, 2015, 03:06:01 AM
Hello Wibo,

I'm using an arduino as an mqtt broker and would like that HG server could contact the arduino to read some values and sensors the arduino is exposing.
Do you have an idea how to do that?

Thanks!

Simon
Title: Re: MQTT Broker
Post by: Gene on January 25, 2015, 10:12:08 AM
Hi xefil,

just look at the MQTT client program source code that you find in Automation->Interconnections section.

Cheers,
g.
Title: Re: MQTT Broker
Post by: Gene on January 25, 2015, 10:15:43 AM
A simple example:

Code: [Select]
var server = "192.168.12.34";
var port = 1883;
var username = "pippo";
var password = "pluto";
var clientid = "homegenie";

// connect to MQTT broker using username and password)
Net.MqttService(server, port, username, password, clientid);
// or connect without credentials
// Net.MqttService(server, port, clientid);

// subscribe a topic and recevice messages (<mpayload>)
Net.Subscribe(topic, (mtopic, mpayload) => {
   Program.Notify("MQTT client message", mpayload);
});

Program.GoBackground();

g.
Title: Re: MQTT Broker
Post by: bkenobi on May 25, 2015, 08:20:48 AM
I'm just starting to look into MQTT.  I've gotten Mosquitto set up and receiving messages.  I created a script with your code and compiled, but it has errors.  HG complains that "topic" is undefined.

Quote
Error
×
Line 13, Column 15 (Code):
    The name `topic' does not exist in the current context

Line 13, Column 5 (Code):
    The type arguments for method `System.ObservableExtensions.Subscribe(this System.IObservable, System.Action, System.Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly
[/quote

Any suggestions?
Title: Re: MQTT Broker
Post by: Gene on May 25, 2015, 04:50:01 PM
the code it's missing a line...just add the following at the beginning:

Code: [Select]
var topic = "test/topic";

Cheers,
g.
Title: Re: MQTT Broker
Post by: bkenobi on May 25, 2015, 05:16:59 PM
That did it.  I was pretty sure that was the issue, but I didn't want to potentially spend a week trying to get a test code running.  Thanks!
Title: Re: MQTT Broker
Post by: bkenobi on May 25, 2015, 07:01:06 PM
Perhaps I'm expecting something that is unrealistic.  I have Mosquitto set up and running on the RPi:
Code: [Select]
pi@raspberrypi ~ $ ps -ef|grep mosq && netstat -tln | grep 1883
108       3516     1  0 May24 ?        00:00:05 /usr/sbin/mosquitto -c /etc/mos uitto/mosquitto.conf
pi       11004 10817  0 09:22 pts/0    00:00:00 grep --color=auto mosq
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN
If I send a packet from the RPi command line, I can see the incoming message:
sent message:
Code: [Select]
pi@raspberrypi ~ $ mosquitto_pub -d -t hello/world -m "Message To Send"
Received CONNACK
Sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (15 bytes))

received message:
Code: [Select]
pi@raspberrypi ~ $ mosquitto_sub -h raspberrypi -d -t hello/world
Received CONNACK
Received SUBACK
Subscribed (mid: 1): 0
Sending PINGREQ
Received PINGRESP
Received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (15 bytes))
Message To Send
Sending PINGREQ
Received PINGRESP

However, when the message is sent/received successfully, I do not see anything on the web interface for HG.  Do I need to configure the MQTT Network module to include a user/pass for some reason?  I see that HG is able to connect in the log:
Code: [Select]
2015-05-25T09:30:22.6032000-07:00 HomeAutomation.HomeGenie.Automation 77 Automation Program Program.Notification {"Title":"MQTT","Message":"Connecting to 192.168.0.200..."}
2015-05-25T09:30:22.9033450-07:00 HomeAutomation.HomeGenie.Automation 77 Automation Program Program.Notification {"Title":"MQTT","Message":"Connected!"}

Any thoughts?
Title: Re: MQTT Broker
Post by: Gene on May 25, 2015, 08:11:58 PM
did you already try:

Code: [Select]
var topic = "hello/world";
or
Code: [Select]
var topic = "#";

?

Cheers,
g.
Title: Re: MQTT Broker
Post by: bkenobi on May 25, 2015, 08:35:42 PM
I tried
Code: [Select]
var topic = "hello/world",I also tried using "test/topic" in the sent message.  I will try using the "#" wild card.
Title: Re: MQTT Broker
Post by: bkenobi on May 25, 2015, 10:46:57 PM
I tried using # but didn't see anything.  So I tested if HG was really connecting to the server and found that it may not be.  I set up an X10 switch to publish to the MQTT broker, but I didn't see anything at the broker nor did HG pop up any message or add activity to the log indicating that it tried to send anything.  I was running 484 so I updated to the new release you just posted.  I'll try again and hopefully things will work better.
Title: Re: MQTT Broker
Post by: bkenobi on May 26, 2015, 06:55:10 AM
I updated to 488 and I still can't see any traffic with MQTT.  I know the broker is working as I've been able to test it with both the RPi and the example Arduino code with my Uno + Eth shield.  I'm pretty impressed by how fast MQTT is, so I'm really hoping I can get HG to see packets.
Title: Re: MQTT Broker
Post by: bkenobi on June 02, 2015, 04:43:43 PM
If anyone has any suggestions on how to get the MQTT client working in HG, I'd really appreciate the suggestions.  I have an Arduino client providing data to the RPi MQTT broker, but no matter how I set up the scripts in HG, I can't seem to connect to the MQTT broker.  HG claims it connects, but I see nothing on the broker nor does the HG client see anything the broker is receiving.  Obviously something isn't working but I'm at a loss as to what.
Title: Re: MQTT Broker
Post by: kevin1 on June 03, 2015, 01:08:51 PM

Does this thread help:
http://www.homegenie.it/forum/index.php?topic=758.0 (http://www.homegenie.it/forum/index.php?topic=758.0)

If not, what is the difference between the MQTT broker and what is done in this other thread?  I have an Adafruit Huzzah ESP8266 WiFi board coming in the mail and need to figure out how to communicate with it via HG.
Title: Re: MQTT Broker
Post by: bkenobi on June 03, 2015, 03:39:27 PM
I was searching for that thread the other day, thanks for the link.

I was able to get communication from the broker last night.  I reviewed the MQTT code in the Interconnects automation group and found the issue.  The code is looking for a message with a topic split into several parts (4 are called out) delineated by '/'.  My messages are only using a single word at the moment, so they failed the check.  I added a Program.Notify to see what traffic was coming in and saw every message the Arduino published.

At this point, I need to make sensor items to store the incoming data and get plotting set up.  I was originally thinking of uploading the data to WU or Pachube, but they appear flakey so I'm not sure it's worth the effort.
Title: Re: MQTT Broker
Post by: Gene on June 03, 2015, 04:07:11 PM
Perhaps the mosquitto MQTT broker service is not configured properly to accept remote connections.
See http://manpages.ubuntu.com/manpages/trusty/man5/mosquitto.conf.5.html (http://manpages.ubuntu.com/manpages/trusty/man5/mosquitto.conf.5.html) (bind_address and  allow_anonymous).

What I would also suggest is to uninstall the mosquitto server and use HomeGenie MQTT Service plugin instead:

- download the add-on from https://github.com/genielabs/MIG-MqttBroker/raw/master/MIG-MqttBroker.zip (https://github.com/genielabs/MIG-MqttBroker/raw/master/MIG-MqttBroker.zip)
- load the add-on from the "install add-on" screen in homegenie (see attacched screenshot)

Cheers,
g.
Title: Re: MQTT Broker
Post by: bkenobi on June 03, 2015, 04:51:34 PM
I can give that a try.  Was this the result of the work Wibo put in a few months ago as far as determining the best MQTT broker for HG on RPi?  I thought Mosquitto was what he suggested which is why I went that route thus far.
Title: Re: MQTT Broker
Post by: Gene on June 03, 2015, 07:30:22 PM
I've not experienced any problem using mosquitto as broker for hg clients under Ubuntu. Never tried it on a raspberry pi.
Few months ago hg was not providing any "add-on" funcionality, so the purpose of this plugin is to provide an easy integrated way for adding MQTT broker  to HG.

Cheers,
g.

Title: Re: MQTT Broker
Post by: bkenobi on June 04, 2015, 06:54:17 AM
I was able to get HG to receive MQTT data from my Arduino using the Mosquitto broker by copying the built in script from the Interconnect group.  I'll have to spend some time figuring out the best method to use the data, but I think the hard part is done.  Thanks!
Title: Re: MQTT Broker
Post by: Wibo on June 05, 2015, 09:23:04 PM
Hi bkenobi,

I used mosquitto broker because it seemed a good broker and it proofed to be an extreme stable broker under windows, that's the only platform I tested.

My search was not about a broker, a stable dotNet library to communicate with the broker was my problem.

My search ended up with RabbitMQ which also has an extremely stable broker.
It also has a very stable dotNet library for accessing the broker, that's why I'm using it.

Really busy building hardware and software at home and at work, so that's why I'm not posting very often.

Regards,
Wibo.
Title: Re: MQTT Broker
Post by: bkenobi on June 06, 2015, 12:30:45 AM
Thanks for clarifying.

I think the issue I was having is that the Interconnect script uses a different call than the code posted by Gene in this topic.  It appears that "Net.Subscribe" does not work in the code provided for whatever reason.  However, using "MqttClient.Subscribe" does work.
Title: Re: MQTT Broker
Post by: bkenobi on June 07, 2015, 04:53:16 PM

Does this thread help:
[url]http://www.homegenie.it/forum/index.php?topic=758.0[/url] ([url]http://www.homegenie.it/forum/index.php?topic=758.0[/url])

If not, what is the difference between the MQTT broker and what is done in this other thread?  I have an Adafruit Huzzah ESP8266 WiFi board coming in the mail and need to figure out how to communicate with it via HG.


Thanks for posting that link.  I hadn't looked at the code before, but it pretty much confirms what I thought.  I just need to create a virtual module to save the incoming data to.  Then, I use the built-in plotting capability to track different items (temp, rain, etc) as needed.
Title: Re: MQTT Broker
Post by: bkenobi on June 07, 2015, 05:26:56 PM
I now have my power meter and weather station sending data that HG receives via the MQTT broker I have running on the RPi (Mosquitto).  I have data saved to virtual module sensors (thanks to CptJack's script) and I can see the data if I look at the parameters under the module settings.  I now have 2 questions:

http://www.homegenie.it/forum/index.php?topic=758.msg4497#msg4497 (http://www.homegenie.it/forum/index.php?topic=758.msg4497#msg4497)
Title: Re: MQTT Broker
Post by: sintei on June 09, 2015, 09:53:11 PM
Hello,

So I tried downloading the latest homegenie.
Went into Programs, interconnections and enabled mqtt network.
Set the parameters:
Unique ID for this client: Hometest
MQTT server address: broker.mqtt-dashboard.com  (this is a free test broker over the internet)
Serverport: 1883
Topic: /mqttspy/test/

Homegenie says it is connected.
In Eventghost and another subsription/publishing software I can see stuff that I publish between Eventghost and the other software.
but from Homegenie, nothing happens.
I have enabled Publish over MQTT at the device in the homegenie dashboard.
Do you guys have any things I can do to troubleshoot?
Title: Re: MQTT Broker
Post by: bkenobi on June 10, 2015, 01:32:59 AM
I just went though this.  The code is probably set up and working correctly, but the script is designed to connect one HG installation to another.  So, the script is looking for a specific topic/message combination.  I modified the code slightly (added a Program.Notify) to post any messages that it saw come in on the subscribed topic and found it was connecting as it said.  Next, just add some code to parse the topic/message as you need.
Title: Re: MQTT Broker
Post by: sintei on June 10, 2015, 12:18:56 PM
Yes, but firstly I want to publish using mqtt from HG.
Do the HG need to subscribe to a specific topic if it wants to publish to one?
I mean, basic setttings should mean the devices publish to selected topic.
I apologize for not really understanding. I just learned some python to be able to write scripts in Eventghost and to tackle a new software (HG) is making my learning curve very steep as I do not know where to look instantly.
I'll go in HG later tonight and see if I can begin to understand what happens in the background.

If you could, could you please publish your code for interconnections/mqtt broker?
Cheers.
Title: Re: MQTT Broker
Post by: bkenobi on June 10, 2015, 03:44:39 PM
When I started liiokking into MQTT (only 2 weeks ago now, so I'm no expert), the first thing I tried was to set HG to publish the status of a couple test switches.  I subscribed to all topics with a RPi client and changed the state of the switches.  I was never able to get traffic to the broker.  But, that was not a goal for me so I didn't try for very long and did no other debugging.

My code for receiving is derived from the MySensors code that was shared earlier in this topic.  The only difference is specific to my sensors really and wouldn't help over what was posted there.
Title: Re: MQTT Broker
Post by: sintei on June 10, 2015, 11:10:10 PM
Gene, is the broker only supposed to work with other HG boxes?
I looked at the code in the mqtt script (from what I understand) and i can't figure out why it won't publish info to the brokers.
What is the topic set by HG?
cheers.
Title: Re: MQTT Broker
Post by: sintei on June 11, 2015, 05:20:55 PM
OK, so I set up an own broker and could now see that HG changes the topic depending on the event.
So for instance if I turn on lights in one room this is the topic:
hometest/HomeAutomation.ZWave/5/event

And for another light/room:
hometest/HomeAutomation.ZWave/4/event

The first "hometest" is HG identifier in the line and the "5" or "4" is the ID of the z-wave switch.
the datapayload given from HG:
{"Name":"Status.Level","Value":"0","Description":"","FieldType":"","UpdateTime":"2015-06-11T15:09:53.8443472Z","NeedsUpdate":false}

Basically, it does not matter what topic you input in HG, it does nothing and HG selects according to above rule and only uses HG Identifier.

Now.. I have to understand how you guys got HG to act when you send a mqtt message into HG and make the switch turn on /off.
Will check your codes later and see if I can work it out.
Title: Re: MQTT Broker
Post by: sintei on June 11, 2015, 10:51:19 PM
OK, so I got HG to recognize the incoming messages and also make the switches in my lights based on what I published.
I know it is probably very simple for you guys, but I do not know programming language and really have to learn the hard way.
Anyways, here is a snippet of code on how I got it to work.
The module is named lounge (my zwave switch)
I publish "loungeON" or respective "loungeOFF" on the topic "MyMQTT/" (standard by importing CPTjacks scripts)

Net.Subscribe(topic, (mtopic, mpayload) => {
var zwave1 = Modules.WithName("lounge");
if (mpayload == "loungeON")
{
zwave1.On();
return;
}
if (mpayload == "loungeOFF")
{
zwave1.Off();
return;
}

Hope it helps any beginners like me.
Title: Re: MQTT Broker
Post by: bkenobi on June 11, 2015, 11:36:44 PM
Yup, that's pretty much all that you need.  Glad you got it working!
Title: Re: MQTT Broker
Post by: jjennings308 on September 25, 2015, 09:04:11 PM
I am new to MQTT.  Is it possible to send a message for a dimmer signal?  I have MQTT working between two HG units and Off and ON will work, but the dimmer signal from the remote unit has no effect on the local unit.
Title: Re: MQTT Broker
Post by: djatie on January 13, 2017, 10:36:57 PM
sory this stupid question, im new mqtt user too.

is this mqtt broker on hg just for connecting 2 HG box? or i can do more? cos i just see on sytem setting just show mqtt port, no other setting.

i have one esp8266, i have flash with some scrip were it can control relay to on off.
i must log to esp8266 wifi then go to tiny web server on this esp8266, than manual on/off it from there.

i have plan to control it with HG,
if i want to do that, i must install mosquito mqtt broker on my raspi, then modificate some script on esp8266 to send receive payload with mqtt , over same wifi.

then make some script on hg were it will cek esp8266 status or send command to on off gpio.

its true like wat i think or i have another option to do that.
pls need guide, and sample. thanks before

:D

Title: Re: MQTT Broker
Post by: bkenobi on January 13, 2017, 10:43:52 PM
HG can be triggered by MQTT messages if you set up code to do it. I have a script that watches for weather and power info and adds the data to the statistics monitor so I can track those items from HG. It would be possible to trigger events based on MQTT packets if you want.

I use Mosquitto, but the one Gene built in will work just fine. You don't have to install an external broker.
Title: Re: MQTT Broker
Post by: djatie on January 14, 2017, 02:44:00 PM
Nice share pls  ;D
Title: Re: MQTT Broker
Post by: bkenobi on January 15, 2017, 03:02:18 AM
https://github.com/bkenobi/HomeGenie_Apps/blob/master/MQTT_Sensors.hgx
Title: Re: MQTT Broker
Post by: djatie on January 19, 2017, 06:43:49 PM
i activate original mqtt network from hg.
try to use demo model were it publish with mqtt

on android phone i use app mqtt iot to see what message publish.
its show like the attachement.

hem..
surrender for this cool facility :(
hahahaha dont understand how to use this
Title: Re: MQTT Broker
Post by: bkenobi on January 19, 2017, 07:43:04 PM
that format is used for HG to communicate with other HG boxes. If you want to use MQTT for tour own purposes, look at my code and see how I set it up. It's basically a modified version of the old built in HG example code but allows me to send data from sensors to my HG box. That data can be used for tracking of to perform actions.

If I had a light sensor in a room and a set of motorized shades, it would be really easy to make the shades open/close depending on sun light.
Title: Re: MQTT Broker
Post by: raptorjr on January 19, 2017, 09:59:14 PM
Excuse me for asking a stupid question. What are the benefits of using MQTT?
Title: Re: MQTT Broker
Post by: bkenobi on January 19, 2017, 11:07:34 PM
MQTT is just a transport protocol that allows you to transmit from one system without worrying about who's currently listening. On the other end, anot her system(s) can be listening without worrying if anyone is sending. The only real requirement is that the broker is alive. How/if you implement it into a specific design is up to you. It's a nice option if you can't trust a communications IMO because the sender and receiver don't rely on each other so if a message gets lost or one goes down it won't kill/hang the other.

You can do some reading for other uses, but I think it's really useful for sensor communication.
Title: Re: MQTT Broker
Post by: djatie on January 23, 2017, 10:09:01 PM
DONE now i can on off my gpio use mqtt client.

Any one have sucsess integrating sonoff with homegenie?
Title: Re: MQTT Broker
Post by: bkenobi on January 23, 2017, 11:43:13 PM
sonoff is based on ESP8266 meaning WiFi. I'd imagine the that you'd need to call a URL from HG to turn on/off the module. I haven't looked into these, but there were a couple threads I was watching at the X10 forum about them.