Python Forum
GPIO high if network IP has good ping - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: GPIO high if network IP has good ping (/thread-30240.html)



GPIO high if network IP has good ping - duckredbeard - Oct-12-2020

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?


RE: GPIO high if network IP has good ping - bowlofred - Oct-12-2020

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.


RE: GPIO high if network IP has good ping - duckredbeard - Oct-12-2020

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


RE: GPIO high if network IP has good ping - bowlofred - Oct-12-2020

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