I am very new to this stuff. Trying to see if anyone has accomplished what I am looking to do.

I would like to have a ping to my android phone running on my Raspberry Pi. When it gets a response I want the Pi to run a command (turn on lights etc). My goal is to have the lights turn on as soon as I get home and my android phone is ping-able. Then eventually I want to create something that does the same after ping is lost for an hour.

I have seen people do similar things for external URLs with code below. Any ideas?

import socket
from urllib2 import urlopen, URLError, HTTPError

socket.setdefaulttimeout( 23 )  # timeout in seconds

url = 'http://google.com/'
try :
    response = urlopen( url )
except HTTPError, e:
    print 'The server couldn\'t fulfill the request. Reason:', str(e.code)
except URLError, e:
    print 'We failed to reach a server. Reason:', str(e.reason)
else :
    html = response.read()
    print 'got response!'
    # do something, turn the light on/off or whatever
  • 2
    your question has nothing to do with the RPi ... it is a Python programming problem – jsotola Oct 2 '19 at 23:16
  • Yeah that should be obvious... Yet I am running it on a Pi. More info the better. – ar550n1c Oct 3 '19 at 19:07
  • why don't you run this code on a Mac, then you can engage Apple technical support – jsotola Oct 3 '19 at 22:08

The following Python will ping an IP address on your network:

import os

ip = ''

if os.system('ping -c 1 '+ip) == 0:
    print('not found')
| improve this answer | |
  • Thanks I will test that out! – ar550n1c Oct 3 '19 at 19:08
  • Thanks! I think that is close to what I am looking for. Been trying to accomplish this a few different ways. When I tried it earlier I At first used Cron to run this script periodically (ping -c 1 &> /dev/null && /var/www/rfoutlet/codesend 98682 -l 173 -p 0 || /var/www/rfoutlet/codesend 87862 -l 173 -p 0). Goal is to have the main light turn on when I get home, and then off as soon as I leave. My setup is based on a No-Internet concept. I don't want to rely on a connection when I could just automate things in different ways without it. – ar550n1c Oct 7 '19 at 15:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.