Python Forum

Full Version: GPIO high if network IP has good ping
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to ping an android phone on my network every 30 minutes and light one of two LEDs according to the ping result.  Can a python program do this?
Are the LEDs on the phone, or some other device?  You can't just run a script on an android phone, you'll have to create a full application, and that will require a fair amount of android knowledge.
The LEDs are on the breadboard attached to the raspberry pi. If the pi gets a good ping to the Android phone I want the LEDs to light up
That's easier (presuming you know the DNSname/IP of the phone).

There are modules that allow for pinging directly from python, but usually only privileged (root) accounts can construct the correct sockets.  Thats suggest that it can often be simpler to just run /usr/bin/ping in subprocess.run() or similar and grab the exit code.  

>>> import subprocess
>>> p = subprocess.run(['ping', '-c', '1', '5.4.3.2'], stdout=subprocess.DEVNULL)
>>> p.returncode
1
>>> p = subprocess.run(['ping', '-c', '1', 'localhost'], stdout=subprocess.DEVNULL)
>>> p.returncode
0