Created
February 16, 2016 09:03
-
-
Save k0d/63ca7c7dfbdaa2b7bf87 to your computer and use it in GitHub Desktop.
Code to find a TellStick Net and listen for events from it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright (C) 2016 Telldus Technologies AB. All rights reserved. | |
import socket, re, sys | |
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) | |
UDPSock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1) | |
UDPSock.setblocking(1) | |
UDPSock.settimeout(3) | |
UDPSock.bind( ('', 42314) ) | |
print "Autodiscover TellStick Net..." | |
UDPSock.sendto("D", ('255.255.255.255',30303)) | |
p = re.compile('(.+):(.+):(.+):(.+)') | |
ip = None | |
while 1: | |
try: | |
(buf, (ip, port)) = UDPSock.recvfrom(2048) | |
except socket.error, msg: | |
break | |
m = p.match(buf) | |
print "Found %s on ip %s firmware version %s" % (m.group(1), ip, m.group(4)) | |
if (m.group(4) == '17'): | |
print " This has firmware 17, lets use it" | |
break | |
else: | |
print " Not using correct firmware" | |
ip = None | |
if (ip == None): | |
print "No TellStick Net with correct firmware found" | |
sys.exit(1) | |
print "Ask the TellStick Net to send received data to our IP. (%s)" % ip | |
msg = "B:reglistener" | |
UDPSock.sendto(msg,(ip, 42314)) | |
while True: | |
try: | |
data, addr = UDPSock.recvfrom(1024) # buffer size is 1024 bytes | |
print "Received:", data | |
except socket.timeout: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment