Skip to content

Instantly share code, notes, and snippets.

@drewbaumann
Created August 24, 2015 06:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewbaumann/9556972a10165cfd2916 to your computer and use it in GitHub Desktop.
Save drewbaumann/9556972a10165cfd2916 to your computer and use it in GitHub Desktop.
import socket
import struct
import binascii
from time import sleep
from phue import Bridge
b = Bridge('192.168.1.247') # Enter bridge IP here.
turnOn = {'transitiontime' : 1, 'hue' : 34112, 'sat' :254, 'on' : True, 'bri' : 254}
turnOff = {'on' : False}
number = 0
def start():
global number
number += 1
def toggleAll():
if (number % 2) == 0:
print "turn off all lights"
for _ in range(3):
b.set_group( [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], turnOff)
sleep(1)
else:
print "turn on all lights"
for _ in range(3):
b.set_group( [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], turnOn)
sleep(1)
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW,
socket.htons(0x0806))
MAC = '74c246214e50'
while True:
packet = rawSocket.recvfrom(2048)
ethernet_header = packet[0][0:14]
ethernet_detailed = struct.unpack("!6s6s2s", ethernet_header)
arp_header = packet[0][14:42]
arp_detailed = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_header)
# skip non-ARP packets
ethertype = ethernet_detailed[2]
if ethertype != '\x08\x06':
continue
source_mac = binascii.hexlify(arp_detailed[5])
dest_ip = socket.inet_ntoa(arp_detailed[8])
if source_mac == MAC and dest_ip != "192.168.1.1":
print "Dash button pressed!, IP = " + dest_ip
start()
toggleAll()
@drewbaumann
Copy link
Author

I needed to modify line 29 & 30 due to different socket info and 50 prevents the action from being fired more than once.

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