Skip to content

Instantly share code, notes, and snippets.

@blaukon
Created March 9, 2020 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blaukon/1e6c4c9ad6ae98ad62545e1659555ecd to your computer and use it in GitHub Desktop.
Save blaukon/1e6c4c9ad6ae98ad62545e1659555ecd to your computer and use it in GitHub Desktop.
python script - "presence" - detection MagicMirror
from os import popen,system
from time import sleep
import subprocess
import os
from datetime import datetime, timedelta
import sys
import threading
from threading import Thread
ip= ["10.0.0.81", "10.0.0.32"]
def now():
return datetime.now().strftime("%d.%m.%Y %H:%M:%S")
def getTemp():
response = subprocess.Popen("vcgencmd measure_temp", shell=True, stdout=subprocess.PIPE)# > /dev/null 2>&1
output = response.stdout.read()
response.stdout.close()
response.wait()
temp = output[-7:-3]
return temp
def findThread(id):
from time import sleep
#print "searching",ip[id]
global foundPerson
t = threading.currentThread()
for i in range(27):
response = os.system("ping -w 2 -c 1 " + ip[id] + "> /dev/null 2>&1")
#print response, ip[id]
if response == 0:
print " FOUND DEVICE ->", ip[id]," - ",now()
foundPerson = ip[id]
break
if foundPerson != 0:
#print " a thread found device, search for", ip[id],"stopped"
break
sleep(1)
def findDeviceThread(ip):
global foundPerson
foundPerson = 0
for i in xrange(len(ip)):
findDeviceThread = Thread( target=findThread, args=([i]) )
findDeviceThread.start()
print "controller started ",now()
system('vcgencmd display_power 1')
wait = 60
stick = 1
while True:
findDeviceThread(ip)
sleep(wait)
if foundPerson != 0:
status=1
if stick==1:
sleep(300)
else:
status=0
if status==1 and stick == 0:
system('vcgencmd display_power 1')
stick=1
print " display on",now()
elif status==0 and stick == 1:
system('vcgencmd display_power 0')
stick=0
print " display off",now()
if float(getTemp()) > 80:
print "oh - TOO HOT - restarting"
os.system("sudo reboot")
@iwashere13
Copy link

An Idea: What about making the IP address non-static by usage of the individual MAC addresses instead?
Is there not a possibility to search the IP address by using the result of arp -a ? Whith doing so you you could ping against theme even though they may change.

e.g. https://stackoverflow.com/questions/56022026/get-local-ip-address-from-a-known-mac-address-in-python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment