Skip to content

Instantly share code, notes, and snippets.

@k0d
Created February 16, 2016 09:03
Show Gist options
  • Save k0d/63ca7c7dfbdaa2b7bf87 to your computer and use it in GitHub Desktop.
Save k0d/63ca7c7dfbdaa2b7bf87 to your computer and use it in GitHub Desktop.
Code to find a TellStick Net and listen for events from it.
#!/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