MQTT integration

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.
Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

MQTT integration

Post by Andrex » Thursday 18 February 2016 12:33

Hi guys,
I'm going crazy!
I'm starting in these days with Domoticz so I'm not a master, but I've looked all around and I can't find a way to have him to talk with MQTT.
I've created a "MQTT Client Gateway with LAN interface" and in the log I see that it connects.
I've created the dummy hardware and a virtual Switch. But how can I tell Domoticz to react to certain MQTT command that he receives?
How can I tell the switch to send a command in MQTT to switch on/off a device?
Thanks in advance!
Andrea

daandamhuis
Posts: 23
Joined: Tuesday 19 January 2016 11:47
Target OS: Raspberry Pi
Domoticz version: 2.4608
Location: 's-Hertogenbosch
Contact:

Re: MQTT integration

Post by daandamhuis » Thursday 18 February 2016 15:18

Hi Andrex!

Did you read the Wiki about MQTT? http://domoticz.com/wiki/MQTT
RPi Model 2: Domoticz Server (Beta)
RPi Model B+: Smart Meter Slave (Stable)
Particle Photon (2x): Prototyping, Controlling LEDS and gathering Sensor data for Domoticz
Arduino Uno R3: with CC2500 Shield for Philips LivingColor Lights 1st Gen.

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Thursday 18 February 2016 18:18

Hi daandamhuis!
I've read that wiki and I've seen this:

Code: Select all

Communication from and to Domoticz works via JSON. Default MQTT topics of the Domoticz for incoming and outcoming messages are:

  domoticz/in
  domoticz/out
But where should I put that direction?
I've also seen the examples like this one for Domoticz to MQTT:

Code: Select all

{
 "idx" : 5,
 "name" : "Internal Temperature",
 "id" : "00080A",
 "unit" : 1 
 "dtype" : "Temp",
 "stype" : "TFA 30.3133",
 "nvalue" : 0,
 "svalue1" : "41.2",
 "Battery" : 100,
 "RSSI" : 12,
}
and this one for MQTT to Domoticz:

Code: Select all

{
 "idx" : 7,
 "nvalue" : 0,
 "svalue" : "90;2975.00"
}
But where do I put this code???
Thanks a lot!

User avatar
nayr
Posts: 430
Joined: Tuesday 11 November 2014 19:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: MQTT integration

Post by nayr » Thursday 18 February 2016 21:30

use a mqtt client library to connect to Mosquitto and subscribe to domoticz/out..

check out my node-domoticz-mqtt module, all the code is written you just have to make it do something.
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Thursday 18 February 2016 22:14

But how do I subscribe to domoticz in/out?
Sorry for the silly question but...where do I see your module?

User avatar
nayr
Posts: 430
Joined: Tuesday 11 November 2014 19:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: MQTT integration

Post by nayr » Thursday 18 February 2016 22:19

thats part of the MQTT protocol, you have to write your own client to to implement the protocol.. most popular programing languages have MQTT Client libraries that have most of the protocol work done for you..

https://www.npmjs.com/package/node-domoticz-mqtt

this is for implementing MQTT with JavaScript, it was written against Domoticz's JSON API so you dont even have to make the json.. just call the functions you need.
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Friday 19 February 2016 19:15

Nayr, thanks again for your patience answering me, but I really don't know where to use that code!

pepijn
Posts: 512
Joined: Friday 12 July 2013 13:19
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: MQTT integration

Post by pepijn » Friday 19 February 2016 21:08

What do you want to achieve using MQTT

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Saturday 20 February 2016 8:37

Hi Pepijn,
I want to change the state of some switch when receiving certain messages and send some messages when the switches are pressed

pepijn
Posts: 512
Joined: Friday 12 July 2013 13:19
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

MQTT integration

Post by pepijn » Saturday 20 February 2016 8:43

Domoticz only allows Domoticz/in and Domoticz/out topics with specific message formats as described above. So there are 2 options:
1) your device or application should publish (and subscribe) messages according the Domoticz standard
2) you use something in the middle that translates your messages to Domoticz and back. I use Node-RED for this matter, it's part of the Domoticz SD card

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Saturday 20 February 2016 8:48

For that purpose I've installed nodered on the Raspberry Pi that manage the relays

pepijn
Posts: 512
Joined: Friday 12 July 2013 13:19
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: MQTT integration

Post by pepijn » Saturday 20 February 2016 8:50

And you build a flow to do this translation?

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Saturday 20 February 2016 8:57

Well...I still have to do that (read the GPIO status and send it, and react on messages coming from Domoticz), but first I need to send those messages from Domoticz and be able to use the one that nodered will send, and this is an obscure part for me!

pepijn
Posts: 512
Joined: Friday 12 July 2013 13:19
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: MQTT integration

Post by pepijn » Saturday 20 February 2016 9:03

You also have to build a flow for this as well. There are some samples on the wiki but some basic knowledge on node-red is really needed. So yo might start reading on nodered.org

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Saturday 20 February 2016 9:06

I'm doing that, but I need info on the Domoticz side to use that flow and to send something back

pepijn
Posts: 512
Joined: Friday 12 July 2013 13:19
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: MQTT integration

Post by pepijn » Saturday 20 February 2016 9:15

This is described in the wiki https://www.domoticz.com/wiki/MQTT

To set a switch in Domoticz publish a message to MQTT to the domoticz/in topic with the following message payload:

Code: Select all

{"command": "switchlight", "idx": 2450, "switchcmd": "On", "level": 100 }
Domoticz will publish every device update to domoticz/out so you have to filter the messages you need and translate them to the destination message format,

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Saturday 20 February 2016 9:29

But how do I tell Domoticz to listen on port 1883? And how do I tell Domoticz the IP where to send the send the updates?

pepijn
Posts: 512
Joined: Friday 12 July 2013 13:19
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: MQTT integration

Post by pepijn » Saturday 20 February 2016 10:05

Add the MQTT hardware on the device settings

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Saturday 20 February 2016 11:17

You mean the "MQTT Client Gateway with LAN interface" on the Hardware section?
Because i don't see any setting in the device section

Andrex
Posts: 64
Joined: Thursday 18 February 2016 10:11
Target OS: Linux
Domoticz version:
Contact:

Re: MQTT integration

Post by Andrex » Saturday 20 February 2016 12:11

I got Domoticz to talk to Node-RED (i still have to "translate" the message passed to turn on/off GPIO)
and right now I got Node-RED to talk to Domoticz too!!!
Thanks!

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests