more G-Labs products

Author Topic: MQTT Broker  (Read 12194 times)

January 23, 2015, 08:43:47 AM
Read 12194 times

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
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

January 23, 2015, 06:53:40 PM
Reply #1

Wibo

  • ***
  • Information
  • Full Member
  • Posts: 95
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.

January 25, 2015, 03:06:01 AM
Reply #2

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
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

January 25, 2015, 10:12:08 AM
Reply #3

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi xefil,

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

Cheers,
g.

January 25, 2015, 10:15:43 AM
Reply #4

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.
« Last Edit: January 25, 2015, 10:19:07 AM by Gene »

May 25, 2015, 08:20:48 AM
Reply #5

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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?

May 25, 2015, 04:50:01 PM
Reply #6

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
the code it's missing a line...just add the following at the beginning:

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

Cheers,
g.

May 25, 2015, 05:16:59 PM
Reply #7

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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!

May 25, 2015, 07:01:06 PM
Reply #8

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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?

May 25, 2015, 08:11:58 PM
Reply #9

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
did you already try:

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

?

Cheers,
g.

May 25, 2015, 08:35:42 PM
Reply #10

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

May 25, 2015, 10:46:57 PM
Reply #11

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

May 26, 2015, 06:55:10 AM
Reply #12

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

June 02, 2015, 04:43:43 PM
Reply #13

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

June 03, 2015, 01:08:51 PM
Reply #14

kevin1

  • *****
  • Information
  • Hero Member
  • Posts: 330

Does this thread help:
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.