MQTT, MQTTitude & OwnTracks

Sheesh. 9 days is like a flash in the pan with my hectic life!

Hahahah. Gotta keep up man! Lol

Cheers,
Jay.

Check yer Facebook messages. I think i messaged u.

Cheers,
Jay.

Like this?
You just need to look for type=waypoints instead of location mentioned below.

rule "Mqtt Postion XXX"
when 
	Item mqttPositionXXXRaw received update
then
    val String json = (mqttPositionXXXRaw.state as StringType).toString
    val String type = transform("JSONPATH", "$._type", json)
    if (type == "location") {
		val String lat  = transform("JSONPATH", "$.lat", json)
		val String lon  = transform("JSONPATH", "$.lon", json)
		val String acc  = transform("JSONPATH", "$.acc", json)
		val String batt = transform("JSONPATH", "$.batt", json)
		val String tst = transform("JSONPATH", "$.tst", json)
		
		mqttXXXLatitude.postUpdate(lat)
		mqttXXXLongitude.postUpdate(lon)
		Location_XXX.postUpdate(new PointType(lat + "," + lon))
		mqttXXXAccuracy.postUpdate(acc)
		mqttXXXBattery.postUpdate(batt)
		var DateTime time_tmp_D = new DateTime(Long::parseLong(tst) * 1000L)
    	mqttXXXTimeSt.postUpdate(new DateTimeType(time_tmp_D.toString))
    }
    logInfo("geofencing.rules", "Position update - XXX (" + mqttPositionXXXRaw.state + ")")
end

Hi, i wanted to install mqtt and owntracks, but it doesn´t work.

I´m on latest OH2 Snapshot version. I use a android Phone with Owntracks installed and a debian server with OH2 and mosquitto running on it.

I configured the mqtt.cfg inside openhab.

But i can´t connect with owntracks to mosquitto.

Can anybody help me?

Settings in owntracks on phone:
host: 192.168.102.23 (ip from debian-server)
Port: 1883

Username/pswd are set inside mosquitto and are entered in owntracks.

I don´t get a connection inside my local network. With dyndns and portforwarding of 1883/8883 tcp to debian server the same problem when outside and made changes to owntracks.

Is there a way to check, where i made some wrong settings? First i want to get the connection owntracks-mqtt running, after that i will start with OH2 settings.

There is a nice tutorial here:

http://owntracks.org/booklet/guide/broker/#mqtt-broker

Also worth reading:

I did the things again, but still no luck.

I get error:connection refused inside putty. I tried my ip, localhost and 127.0.0.1.

I don´t get a logfile. I removed the logfile some time ago and there will not be created a new one? No idea, how i can check where i have a wrong configuration? I even removed the mosqutiio.conf file and restarted the server, but nothing helps.

I get the following inside my shell:


root@debian:~# /etc/init.d/mosquitto status
● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
   Loaded: loaded (/etc/init.d/mosquitto)
   Active: active (exited) since Mi 2017-05-03 14:27:48 CEST; 5s ago
  Process: 3318 ExecStop=/etc/init.d/mosquitto stop (code=exited, status=0/SUCCESS)
  Process: 3915 ExecStart=/etc/init.d/mosquitto start (code=exited, status=0/SUCCESS)

Mai 03 14:27:48 debian systemd[1]: Starting LSB: mosquitto MQTT v3.1 message broker...
Mai 03 14:27:48 debian mosquitto[3915]: Starting network daemon:: mosquitto.
Mai 03 14:27:48 debian systemd[1]: Started LSB: mosquitto MQTT v3.1 message broker.

So i think it is running?

But with this two commands:

netstat -an |grep 1883

netstat -an |grep 8883

I don´t get any output.

Here is the mosquitto deirectory. Is this ok? The rights for conf and password file are 644.

And here is my mosquitto.conf:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

listener 1883 # Unverschlüsselte Verbindung. Sofern auf der Firewall nicht durchgeschleust nur für's heimische Netzwerk
listener 8883 # Verschlüsselte Verbindung für Clients über Port 8883 (kann in der Firewall durchgereicht werden)
retry_interval 20
user mqttuser
password_file /etc/mosquitto/passwd

Now i removed all password/username things and it is working with port 1883. No authentication.

Next thing is to change to port 8883 and after that i want to use authentication with username/password.

So what do i have to do, if i want to listen at 1883 and 8883?

This is working only on 1883:

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

retry_interval 20

log_type error
log_type warning
log_type notice
log_type information

connection_messages true
log_timestamp true

And the strange thing is:

This is not working on 1883 and 8883:

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

listener 1883 
listener 8883 
retry_interval 20

log_type error
log_type warning
log_type notice
log_type information

connection_messages true
log_timestamp true

Try:

port 1883
listener 8883

To properly configure TLS, see also: Tls - OwnTracks Booklet

I only added only this two lines to my config, no more other changes. Owntracks stopped working on my phone, i didn´t get a connection anymore (same config on phone).

If i only add port 1883 and listener 8883 and phone still the same like before, it should normaly still work with old settings and on standard port 1883?

Yes, it should work…
port 1883 is the default listener
Also check the conf.d subdir not to have any other configs there (not likely)
On the phone set 1883 and disable tls and auth for the beggining

I use OT clients with only auth (no tls)

I didn´t change config on the phone. Only added the two lines into the conf-file. There is no second conf-file.

TLS and auth are off.

So i think i will stay at 1883 and use only auth.

Do you also use only 1883? Is it possible to use 8883 without tls or is this the same like 1883?

Another question: I started testing with my two phones. After that i changed the names in owntracks app on both phones. Now i still get the last locations of the old phone-names. There are now 4 friends in it. I already removed the mosquitto.db, but after it is build new, there are still 4 friends in it. Do you know where it is stored and why the 4 friends are still there?

I use only 1883
I think that you can use 8883 without tls in parallel to 1883… but why would you do this?
I will give you more info soon…(also how to clear old stuff from OT :slight_smile: )
The latest version of ot client on android includes the option to clear a friend.
Driving atm and using discourse mobile client to post so it’s not easy to find the info.

Thanks, i have found it. I removed the friends.

1 Like

In the /etc/mosquitto/mosquitto.conf you set the following:

port 1883
<networking options>
listener 8883
<networking options>
listener <other port>
<networking options>

If you don’t define any <networking options>, then you won’t enable TLS (or other networking stuff)

A working example with the default listener on 1883, an extra listener on 8883 with TLS enabled and an extra listener without TLS on 9883 with protocol websockets enabled would look like:

port 1883
listener 8883
certfile /etc/encryption/blah/blah/cert.pem
cafile /etc/encryption/blah/blah/chain.pem
keyfile /etc/encryption/blah/blah/privkey.pem
listener 9883
protocol websockets

Source: MQTT Mosquitto broker with SSL/TLS transport security – Primal Cortex's Weblog

Before this option on the mobile client app (OT), you could do the following:

Send an empty (-n) MQTT message to the topic and this would clear the friend:

mosquitto_pub -t owntracks/owntracks/<old_device_name> -r -n -u <username> -P <password>

After you have your MQTT Broker configured, your OT mobile clients connected and reporting their location history, your openHAB2 items configured, etc… check out also the awesome Owntracks Recorder web app :slight_smile:

Thanks, i will have a look on it.

MQTTitude-binding is only for using waypoints/areas? So i can check if someone is at home, at work, …

Which binding do i have to use when i want to get the location and the street name and so on? Maybe show it on google maps.

Only MQTT-Binding?

The MQTTitude binding is basically going to give you a Presence Detection Solution. It will turn on a switch item when the location of a friend falls within the pre-defined geo-fenced region. You can then use this event (switch was turned on) to trigger a rule and perform other events from OH2.

I don’t use it personally (I have another presence detection solution with the Unifi binding) but I think that the answer to your question is yes.

btw: OwnTracks is the new name of MQTTitude but the binding name was never updated to OwnTracks :slight_smile:

Yes, only the MQTT Binding (note: not the street name… see json payload info below).
Since the OT mobile clients will directly publish their locations on MQTT topics, you could use only the MQTT binding to communicate to OH2 the required info (subscribe to the topic and pull the data from there).

Now… what to do with the data (the json payload published in the topic) and how to use it to display for example a google map or the street name where a friend is on your sitemap… that’s another journey :slight_smile: (but it’s possible)

Here is an example json payload coming from OT Android mobile client app (v1.1.7):

{
  "_type": "location" 
  "tid":"S6", 
  "acc":"12", 
  "batt":"59", 
  "conn":"m", 
  "doze":"false", 
  "lat": "47.056987", 
  "lon": "10.623918", 
  "tst": "1389104761"
}