Monday 24 July 2017

Controlling mains AC or DC via raspberry Pi and Alexa

Controlling mains AC or DC via Raspberry Pi via Alexa

Background

I have LED strip lights, lighting the floating wall, and want to control them via Alexa. The raspberry pi will be doing the actual switching.

There is a decent library which emulates a belkin wemo switch called fauxmo and Alexa can control Belkin's Wemo switches.

Requirements:

  • Raspberry Pi (I will be using a 3 model b)
  • Opto relays (I Bought mine from Amazon UK https://www.amazon.co.uk/gp/product/B06XK6HCQC/ref=oh_aui_search_detailpage?ie=UTF8&psc=1 )
  • Amazon echo or echo dot 
  • Wires and a breadboard

Wiring

  •  Install N00bs, following the raspberry pi guide here: https://www.raspberrypi.org/documentation/installation/noobs.md
  • Attach everything (I am using a breadboard), according to this pinout (raspberry pi 2/3 specific)  

So:
GND on the relay -> GND on the raspberry Pi
IN1,2,3,4 -> GPIO 2,3,4,17
VCC -> 5v power.

As of writing this, n00bs, has all the pre-reqs pretty much installed already, but you will need to double check, using this excellent guide from adafruit ( https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-gpio )

Testing

Write some test code to check it all works (this will need to be run as root)
Change the GPIO numbers according to your setup.

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# init list with pin numbers
pinList = [2, 3, 4, 20, 21]

# loop through pins and set mode and state to 'high'

for i in pinList:
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, GPIO.LOW)
    print i

print "ALL SET LOW"
# time to sleep between operations in the main loop

SleepTimeL =3
time.sleep(SleepTimeL);

# main loop

try:
  time.sleep(SleepTimeL);
  GPIO.output(2, GPIO.HIGH)
  print "TWO"
  time.sleep(SleepTimeL);
  GPIO.output(3, GPIO.HIGH)
  print "THREE"
  time.sleep(SleepTimeL);
  GPIO.output(4, GPIO.HIGH)
  print "FOUR"
  time.sleep(SleepTimeL);
  GPIO.output(20, GPIO.HIGH)
  print "20"
  time.sleep(SleepTimeL);
  GPIO.output(21, GPIO.HIGH)
  print "21"
  time.sleep(SleepTimeL);
  print "Good bye!"
  for i in pinList:
    GPIO.output(i, GPIO.LOW)
  #  GPIO.cleanup()

# End program cleanly with keyboard
except KeyboardInterrupt:
  print "  Quit"

  # Reset GPIO settings
  GPIO.cleanup()

Alexa integration

pip install fauxmo

Check the code out in my repo: https://bitbucket.org/c240amg/raspberry-pi-alexa/src/