1

I'm looking to control Phillips hue lights from a piece of software that's not capable of sending RESTful messages so I'm attempting to write a bit of middleware that will interpret at TCP message and pass on the appropriate request to the bulbs. The socket server comes from an example I found on BinaryTides.

I'm using qhue for all thing Phillips, however my issue is with python interpreting the message. When I start the socket server and connect to it from another laptop using netcat I can't seem to get it to activate either of the desired colour changes.

Typing Red or Green should result in a qhue message being sent, but I always end up with "UNKOWN COMMAND" outcome.

# Socket server in python using select function

import socket, select, requests
from qhue import Bridge

if __name__ == "__main__":

CONNECTION_LIST = []    # list of socket clients
RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
PORT = 9999

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this has no effect, why ?
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("0.0.0.0", PORT))
server_socket.listen(10)

# Add server socket to the list of readable connections
CONNECTION_LIST.append(server_socket)

# qhue setup
hueBridge = "172.16.17.73"
hueUsername = "hLaqm-dJZKabcdeTz1QwyMHqiLnzlFl3KKKM2x4o"
b = Bridge(hueBridge, hueUsername)
green = 25500
red = 0

print "Chat server started on port " + str(PORT)

while 1:
    # Get the list sockets which are ready to be read through select
    read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])

    for sock in read_sockets:

        #New connection
        if sock == server_socket:
            # Handle the case in which there is a new connection recieved through server_socket
            sockfd, addr = server_socket.accept()
            CONNECTION_LIST.append(sockfd)
            print "Client (%s, %s) connected" % addr

        #Some incoming message from a client
        else:
            # Data recieved from client, process it
            try:
                #In Windows, sometimes when a TCP program closes abruptly,
                # a "Connection reset by peer" exception will be thrown
                data = sock.recv(RECV_BUFFER)
                # echo back the client message
                if data:
                    sock.send('OK ... ' + data)
                    print data
                    if str(data) is "Red" :
                        b.lights[1].state(bri=254, hue=Red)
                    elif str(data) is "Green" :
                        b.lights[1].state(bri=254, hue=green)
                    else:
                        print "UNKNOWN COMMAND"



            # client disconnected, so remove from socket list
            except:
                broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                print "Client (%s, %s) is offline" % addr
                sock.close()
                CONNECTION_LIST.remove(sock)
                continue

server_socket.close()

UPDATE:

I've written a simple script that does the same thing based on raw_input which works as desired. So it seem like the issue lies in the way that the socket server reads and stores data. (Script below works)

from qhue import Bridge

# qhue setup
hueBridge = "172.16.17.73"
hueUsername = "hLaqm-dJZKwMwO9Tz1QwyMHqiLnzlFl3KKKM2x4o"
b = Bridge(hueBridge, hueUsername)
green = 25653
red = 65527

while 1:

Colour = raw_input("Pick a Colour > ")

if str(Colour) == "Red":
    b.lights[2].state(bri=254, hue=red)
elif str(Colour) == "Green":
    b.lights[2].state(bri=254, hue=green)
else:
    print "Unkown Colour"
RichPorter
  • 87
  • 8

0 Answers0