Google Home/Alexa/Internet Access solved

The star of the STICK family.

Moderators: simonB, nick, florent, Ben

alaistair
Posts: 2
Joined: Thu Dec 27, 2018 11:10 am

Google Home/Alexa/Internet Access solved

Post by alaistair »

Folks - I know a few folk have wanted to integrate their DE3 to Google Home, etc.
As this isn't currently supported - I looked into alternate ways of doing this.
So my solution was to have a small raspberry pi pc on my network - this would then run a small python script
The python script would create a mini webserver and listen on a web port for incoming requests.
I formatted the url so that you could have pagenumber and scene.
This would then send a TCP string to the DE3 to selec the desired scene.

I then configured my router to allow the resberry pi to receive incoming requests from the internet....

The using the service IFTTT I created triggers that would call the webhooks service to call my lights to change them.

Python code is below....
Feel free to reach out to me at [email protected] for more info/help

If I get enough interest, I can write up a fuller set of instructions....

# Python Script To Control stick Lights

# Load libraries
import time
import socket
from bottle import route, run, template

#IP Address of StickDE3
TCP_IP = '192.168.1.20'
#Port Number for Commands to StickDE3
TCP_PORT = 2431
BUFFER_SIZE = 1024
#See Programmers guide for message format. Should be preamble + scene (page*50+scene number) + postamble
BASEMESSAGE = "Stick_3Am\00\00\00\00\01\00\00\00\00\00\00\00\00\00\00"

# Handle http requests to the root address
@route('/')
def index():
return 'Go away.'

# Handle http requests to /stick
@route('/stick/<page:int>/<scene:int>')
def stick(page=0,scene=0):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Connect to Stick Controller
s.connect((TCP_IP, TCP_PORT))
#Create command string based on page*50 + scene
lightingcommand = bytearray(BASEMESSAGE,'ascii')
#insert the scene into bytes 10 and 11
lightingcommand[10]=(50*page+scene)%256
lightingcommand[11]=int((50*page+scene)/256)
print (lightingcommand)
#Send command to lighting controller
s.send(lightingcommand)
#Receive lighting controller response
#First clear last message received
data = s.recv(BUFFER_SIZE).decode()
#Wait for new scene to fire
time.sleep(1.5)
#Read current scene
data = s.recv(BUFFER_SIZE).decode()
s.close()
#Console debug
#break up response to current zone and scene
scenename=data[12:24]
zonename=data[25:37]
print (zonename, scenename)

#send response back to web service
return template("page {{page}}, scene {{scene}} selected\n{{zonename}} {{scenename}}", page=page, scene=scene, zonename=zonename, scenename=scenename)

#Listen to port 8000 for incoming requests
run(host='0.0.0.0', port=8000)