SmartThings Community

[Release] Host Pinger (IP based Online State / Presence)

SmartThings Host Pinger

Using an EXE or Python script running on any computer (Windows, Mac or Linux) - Ping any IP, host name or URL and pass this to a switch in SmartThings!

Code and Program Location:

Requirements

1. Application to Ping Hosts (running on any PC connected to the internet.)

>> Option A - STHostPinger.exe - Application (Windows or Mac&Linux via Mono) - config.config needs to be configured with details as described below and can be running on any PC connected to the internet.

>> Option B - PingerPy - Python Script - Cross platform script based on python, which also provides a web interface to configure the settings, you will need to download Python to use this.

>> Option C - Arduino / ESP8266 Device - Use a small piece of Arduino/ESP8266 compatible hardware such as the Wemos D1 Mini, which can be purchased for only a couple of £/$ powered by USB.

>> Option D - Bash Script - For use in Unix shells, ideal for Raspberry Pi.

2. Host Pinger SmartApp - This passes the output of the exe to the custom device types.

3. Host Ping Device Type (optional) - This device type revieves the online/offline event and translates this in to an on/off switch or presence sensor.

How To Install:

1. Install the Smart App and Custom Device Type

Go to your IDE, then…

My SmartApps:

A. Create New SmartApp

B. Select “From Code”

C. Paste App source code with the code from the file SmartApp_HostPinger.txt.

D. Save and publish the app for yourself.

E. Enable OAuth in IDE by going in to IDE > My Smart Apps > [App Name] > App Settings > Enable OAuth.

F. Get the Token and API Endpoint values via one of the below methods:

  • EASY OPTION: Enable debugging, open live logging in IDE and then open the app again and press done and values will be returned in Live Logging.
  • Open the SmartApp and click API Information (this will involve manually typing the codes)

My Device Handlers (optional):

A. Create New Device Handler

B. Select “From Code”

C. Paste App source code with the code from the file DeviceType_HostPinger.txt.

D. Save and publish the device for yourself.

2. Configure Your Chosen Polling Application (Option A / B / C)

>> Option A: STHostPinger.exe & config.config

(Mac and Linux users can launch this exe by downloading Mono from the Mono-Project and running “mono STHostPinger.exe”)

Linux mono install instructions: http://www.mono-project.com/download/stable/#download-lin-raspbian Also make sure you follow the instructions about certificates otherwise it will error when trying to send the https request.

Help for running on startup and in the background using mono (Post 143: Thanks RobbieCrash): [Release] Host Pinger (IP based Online State / Presence)

A. Download the exe and config.config file.

B. Open the config.config file.

C. In config/smartThingsEndpoints fill in your API token and add the APP ID to the endpoint urls from the previous section.

ENTITY accessToken “XXXXXX FROM SMARTTHINGS XXXXXXXXX”
ENTITY appId “XXXXXX FROM SMARTTHINGS XXXXXXXXX”
ENTITY ide “https://graph-eu01-euwest1.api.smartthings.com

D. Be sure to also check that your IDE URL matches the URL in config.config, if you have the URL from the app then this should be correct, if you were unable to get this from the app then you willl need to copy from IDE, it’ll be something like “graph-na02-useast1.api.smartthings.com

E. Configure all of the IPs / Hosts or Addresses you want to ping.

item HOST=“google.com
item HOST=“192.168.1.1”
item HOST=“SERVER1”

G. The polling interval, timeout and debugging can also be configured to your liking or leave as the default values.

H. Run The EXE

You can now run the EXE and and this should push the updates to your ST Hub via the cloud.

If anything obvious isn’t working you will receive errors in the console, you can enable extra debugging by setting to 2 in config.config, or have no updates by setting to 0.

To run this on mono you will need to download mono for Mac, or for Linux install “mono-complete”, the standard run time doesn’t contain everything you need.

On Linux you can run “mono STHostPinger.exe” or “mono STHostPinger.exe &” to run in the background (“jobs” will show running instances and “kill $1” will kill process 1)

In windows you can make this a tray icon using:
http://rbtray.sourceforge.net/

>> Option B: PingerPy - Python Script

This has been developed by @CurlyTailed_Buffalo and I have linked directly to his post below for reference, you will need to download and install Python if you don’t already have it and detailed instructions are available on the GitHub page:

>> Option C: HostPinger ESP8266 Edition

Using the Arduino IDE, load the Arduino sketch, amend the settings with your details and upload to an ESP8266 device such as the Wemos D1 Mini, which can be purchased for only a couple of £/$ and powered from a standard USB socket.

>> Option D: Pinger Bash Script

This has been developed by @camedia and I have linked directly to his post below, this should probably be the most simple way to get a Raspberry Pi or any Unix device running.

3. Configure the Smart App

Configuration should be self explanatory however come back here with any questions.

Add a new device give it the same name you have specified as the host in config.config, you can open the device later and change to a different name if you like!

4. Errors / Debugging

EXE - SendGetRequest: the remote server returned and error: (403) Forbidden
There is an issue with the URL, App ID or token in config.config re-check section 2.D

No error in the EXE and no “Event” present in the App
This is because SmartThings is not reciving an event, this is usually because the App ID or Token are incorrect, if you re-install the app these values will change and need to be setup again.

"Event" in the app, but hasn’t triggered the switch to change.
This is likely to that the Child Device has been configured with a name not matching the host in config.config.

Live Logging - java.lang.NullPointerException: Cannot get property ‘authorities’ on null object @ line xx
You have not enabled OAuth in the SmartApp

Running using Mono - Failed to SendGetRequest: Error getting response stream (Write: The authentication or decryption has failed.)
This is a certificates issue using mono, follow this link:
http://www.mono-project.com/download/stable/#download-lin-raspbian

10 Likes

Do you mind if I look into jacking your device code into my universal device dth?

Update:
Well that’s not going to work, your sa is creating a standard virtual switch child device. If you allowed the user to select a switch in addition or in lieu of creating one, they could use any switch, including my uDTH…

My specific use case being to ping a static dhcp lease for all our mobile devices, then map each of these via uDTH as adjunct presence sensors…

4 Likes

Now THAT is an awesome idea. Been trying to get it done for a year now…

1 Like

Yes please.

Not clean but you can do this with core still, connecting ops switch to a udth…?

Yea, amongst other apps as well… interested in the clean route here since I have enough issues as it is with all my virtual zone apps and devices…
Who knows, maybe we’ll have a good weekend with the db tweeks they rolled out…

3 Likes

Agreed! I would finally be able to stop replying on the ST presence sensor in my wife’s car.

1 Like

Easy enough to do… I was purposefully trying to avoid it so that you didn’t manually have to create a virtual switch then add in to the smart app, then you could report presence based on the switch state, but I’ll add presence to my DT as that’s a good idea.

But I’ll create a child app so you can assign to a switch also… in terms of anyone wanting to jack code, then help yourself!

Edit: will your mobile devices sustain wifi though and you may need to consider that pings may increase power consumption, I’ll give it a go with my phone later… see how reliable ping is for mobile

1 Like

Awesome! Let us know when the updated code is there and I will be happy to test! Also, it would be cool if you can program in some time out to avoid false positives. For example, if someone’s phone is either rebooted or out of range with a dead Wifi spot, you don’t want ST to think it left the area.

Finally, you might want to consider setting up your Repo to allow others to sync to it for updates. It’s the best way to get everyone on the latest code:

http://docs.smartthings.com/en/latest/tools-and-ide/github-integration.html

@Mike_Maxwell and everyone else - Code updated with changes :slight_smile:

1.1 Added child app and triggering of external switches & added presence to the Device Type

@Mbhforum - Yes can add a configurable value to ignore any offline for X seconds…

I’ll take a look at GitHub integration, but as I’m based in the EU the EU IDE doesn’t have this option, so (assuming I can integrate?!)

Edit: not sure I can implement the delay where using the device type though as would need to use a runIn on the parent that could be over written by another event… will need to have a think… alternatively rulemachine / core could handle this easily…

Ahh I just read the installation notes and didn’t realize you need a Windows host up and running this .exe at all times. I have a Windows machine, but it it’s a laptop that is usually powered down. I only have a Mac that is up at all times. Is there any solution for Mac or better yet, I am running a router with DD-WRT? (so I can have a script just ping constantly).

I had something like this running on my tablet using Tasker as the pinging device. It worked fairly reliably. The phones would show as connected several minutes after the presence sensors arrival. (On average) The main issues was the I phones would drop off the network when they went to sleep / idle and sometimes the pings would not get responded to by any phone. So you had to account for that. Possibly just my setup / configuration. I am very interested in this and hopefully getting @bridaus “reliable presence” to work with more than 2 inputs and leverage them all together. Could possibly be a actual reliable presence by leveraging a 2 of three or four inputs as the trigger.

I’m out for the rest of the day, but if memory serves me right there is already something that can do this via DD-WRT.

I don’t own a Mac, but my brother does and actually programs in C#, he said he will see if he can recompile later :wink:

Awesome :slight_smile: There is currently a script for WW-DRT, but the problem is it examines MAC address by doing an ARP lookup. When you have multiple access points in the house that are non-WW-DRT, then the problem becomes as you roam around in the house, you will disconnect from your WW-DRT router. Ping solves this as you never leave the network :slight_smile: I tried it and it ended up being unreliable for me.

1 Like

So as a Mac user you can just install Mono apparently, he’s checked and it launches fine:

http://www.mono-project.com/

Once installed at the console you can just navigate to the folder and type “mono STHostPinger.exe”.

If mono is is not recognised as a command then the below link will help:

If you could give it a go, and let me know how you get it on, if you have any additional instructions let me know and i’ll add to the original post!

It’s working perfect on my Mac! nice job!!!

2 Likes

I just realized you cannot use this as a reliable method for iPhone presence detection b/c iOS puts it’s wifi to sleep when the phone is not in use :frowning: I realized this as my iPhone kept bouncing online and offline. Amazing how many work arounds I have tried from Life360, WW-DRT, ST presence fobs, native ST mobile presence, and nothing has been rock solid.

Same thing I saw. Was hoping it was just my setup. Guess not. My android would stay connected but probably because I have the toggle to NOT turn off WiFi when asleep enabled.

I missed that. iOS most likely does this to conserve battery life.

IBeacons are pretty solid, maybe because they’re so simple, it’s just that the range is limited. You can set up a region with multiple Ibeacons, but then you significantly increase the device cost.

I’m happy with mine, but I was looking for a small detection area. They wouldn’t work if you wanted the garage door to start opening while you were half a block away. :sunglasses: