A previous blog showed how MQTT logs can be sent to Logentries for storage, analysis and how those logs can be to alert on potential MQTT security threats, as well as to store and visualize sensor data. This blog follows that by showing how to build a fully connected IoT system composed of the OwnTracks iOS app as an MQTT publisher, a Raspberry Pi with Mosquitto embedded as an MQTT messaging broker and Logentries as the log analytics platform used to collect, store and analyze data.
OwnTracks is an open source app that allows you to record your own location. It also allows you to share your location with other users. OwnTracks uses open protocols for communication so you can adjust your level of security and privacy. The app can be installed on iOS or Android and is easy to download. When your smart phone establishes that it has moved by a couple of meters, your phone sends (“publishes”) a location message to a HTTP or MQTT server. The MQTT server works as a broker, forwarding the message to interested parties (subscribers).
This blog post will demonstrate how to use Raspberry Pi and Mosquitto as MQTT broker for OwnTracks.
Subscribing to OwnTracks topics
Firstly we need to:
(i) Install OwnTracks on our smartphone. Reference their Scenarios page.
(ii) Set up Mosquitto running on Raspberry Pi as an MQTT broker. You should read our earlier blog: Logging Mosquitto Server logs (from Raspberry Pi) to Logentries. For instructions on how to install Mosquitto on Raspberry Pi and monitor the Mosquitto broker.
Now we can connect the OwnTracks phone and Raspberry Pi via WiFi (you could also use 3G).
- In OwnTracks settings, set Host to Raspberry Pi network card IP address, in our network it’s 10.0.0.5 and set Port to default for MQTT 8883. The user can adjust additional security settings. Security features of OwnTracks are broadly discussed in OwnTracks documentation on security.
- Open up a terminal on your Raspberry Pi device and run: mosquitto_sub –h 10.0.0.5 –p 8883 –v –t ‘owntracks/#’
- Above command will subscribe to all sub-topics that belong to main owntracks topic and display messages in the terminal. You can manually publish location information by clicking “share” button in the app.
The location information is structured in JSON format shown in the screenshot below and contains a lot of useful information about the device and its location. We will explain just a few of those values and a full list of possible payload types is available online.
batt – is the device’s battery level in percent. This can be very useful in a sensor management scenario, where remote sensor or device that sensors connect to are likely to run out of battery. Constant monitoring and real-time alerting can be very helpful. We will present, how to set up real-time alerts with Logentries in further sections of this blog.
**lat & lon **– latitude and longitude represented as a floating point number
**acc **– accuracy of the reported location in meters. The accuracy of location in the example above is equal to 30 meters, hence it’s very poor, but it improves with time and a bit of movement.
vel, alt, p – velocity, altitude and pressure.
Note on Firewall:
If you are going to test it at home or in a small office, it is very likely that you will have some form of router which connects the local network to the Internet and that your router has a firewall configured on it which will allow outgoing connections, but may block incoming connections. You will have to reconfigure your router to allow incoming TCP traffic on port 8883, which is the default port for MQTT broker.
Integration with Logentries
We now have OwnTracks connected to our Raspberry Pi using Mosquitto, so let’s set it up with Logentries to store all data collected and analyze sensor information. Just to give you a better idea of what can be achieved by combining OwnTracks native functionality and Logentries log analysis capabilities, we’ve configured a sample region in the application, and set its radius to 10 meters:
By setting a region we are able to show that it is possible to set up alerts, whenever the user enters or leaves the region defined by us. The app will publish additional element named “event” with payload set to either “enter” or “leave” and trigger it on every transition from outside of the region to the inside and vice-versa.
There are potentially hundreds of applications, where above functionality can be used. It could be a parent looking to find out where its child is, or law enforcement representative wanting to be alerted about persons most recent position, business owner locating nearest driver or evil boss willing to be notified every time you leave your office or even your own desk, which is possible, since OwnTracks can integrate with very accurate iBeacons. Sending OwnTracks data to Logentries not only provides the user with real-time alerts, but can be used to trackback historical location and behavior of particular user.
Forwarding MQTT subscribe messages
There are only a couple of tweaks required to forward MQTT subscribed data to Logentries.
First, we need to append messages to any text file, such as:
mosquitto_sub –h 10.0.0.5 –p 8883 –v –t ‘owntracks/#’ > /home/pi/Desktop/messages.txt
Then as per Logging Mosquitto Server logs (from Raspberry Pi) to Logentries, configure rsyslog server to send data to Logentries. This can be easily done by appending this piece of code to the end of rsyslog.conf file. Please remember to replace the token placeholder with your own Logentries token. In order to create your own token you need to register with Logentries.
$template Logentries,"<LOGENTRIES_TOKEN_HERE> %HOSTNAME% %syslogtag%%msg%\n" *.* @@data.logentries.com:80;Logentries $Modload imfile $InputFileName /home/pi/Desktop/messages.txt $InputFileTag OwnTracks $InputFileStateFile OwnTracks-file1 $InputFileSeverity info $InputFileFacility local7 $InputRunFileMonitor $InputFilePollInterval 10
The file at /home/pi/Desktop/messages.txt will now be followed and logs should appear in your Logentries account.
Get started with the OwnTracks community pack
We’ve developed the OwnTracks pack that can be installed from your Logentries dashboard, so that you don’t have to start your OwnTracks log analysis from scratch. This pack is equipped with alerts such as:
- Low Battery Level
- Entering geofence region
- Leaving geofence region
Additionally, this pack includes series of timeline graphs and bar charts used to analyze historical data such as:
- Battery Level
- Average Accuracy
- Average Velocity
OwnTracks pack can be downloaded it from here.
Logentries makes it easy to collect all of you IoT data into one central location for visualization, analysis, and alerting. Sign up for a free Logentries account to get started.