Introduction: Amazon Echo Controlled IR Remote
The Amazon Echo system can control a lot of aspects of a smart home, but a smart outlet can only turn off and on. Many devices do not instantly turn on by simple being plugged in and require additional steps, such as pressing buttons on a remote or the physical device to power on or get the desired settings.
In this guide, a Raspberry Pi Zero W will be configured to act as a smart home device that can be controlled by Amazon Echo, and send any desired IR commands to a device when requested to power on or off.
In this specific case, the Pi will be configured to learn the IR commands of a remote provided with a "ClassicFlame 23II310GRA 23" Infrared Quartz Fireplace Insert". An IR LED will then be used to send out the IR commands on demand, and finally the Pi configured to emulate a Philips Hue device that can be control be Echo.
Step 1: Materials
Required:
- Raspberry Pi Zero W
- 4 GB or greater Micro SDHC Class 10 memory card (16 GB Example)
- MicroUSB
- 1 IR LED
- MicroUSB Power Adapter (2.1 amps or higher recommended)
- IR LED
- IR Receiver VS/1838B
- 100 ohm resistor
- Misc. wire
Recommended:
- Headphone Jack
- 1/8 inch headphone/audio wire
- 2N2222 NPN Transistor
- 1k ohm resistor
- Rapsberry Pi Zero Case
To complete the initial configuration of a Raspberry Pi Zero W, a few additional peripherals will be required, but will not be in use full time by the completed project
- Mini HDMI to HDMI Adapter: Used to connect Pi Zero W to a TV or monitor with a full sized HDMI cable
- USB OTG Cable: Used to convert from micro-USB to full sized USB port(s) for connecting a keyboard and/or mouse
- HDMI Cable: Used to connect to TV or monitor along with an adapter to mini HDMI
The first two items as well as a case are included various Pi Zero starter kits, such as: MakerSpot Mega Kit
Step 2: Setup Raspberry Pi
The Raspberry Pi website has an excellent walkthrough for setting up Raspbian Stretch OS on a Raspberry Pi. I recommend the Lite version for a smaller footprint if this is only project running on the Pi. If you wish to have more Operating System options in the future, or a more simple setup, following the instructions for NOOBS will get you up and running in no time. This guide is based on Raspbian, which is included with NOOBS
https://www.raspberrypi.org/learning/software-guide/
Once Raspbian is running, enable SSH to allow remote connections to the device without needing a monitor/keyboard/mouse to be connected directly to the Pi. If you would like to optionally have remote access to the GUI, you can also enable VNC access
https://www.raspberrypi.org/documentation/remote-access/ssh/
It is also highly recommended you set a static IP on the wireless network configuration so it does not change over time. It is possible the IP may not change if new devices are not regularly connected to the wireless network, but configuring it as static will ensure it does not.
http://www.circuitbasics.com/how-to-set-up-a-static-ip-on-the-raspberry-pi/
Step 3: Configuring LIRC
The following steps are highly based on the excellent guides found here:
http://alexba.in/blog/2013/01/06/setting-up-lirc-on-the-raspberrypi/
https://camp.isaax.io/en/isaax-examples/ir-control-via-lirc-on-raspberry-pi-zero-w
This guide is based on Rasbian Stretch and lirc 0.9.4c-9
Other guides may be based on earlier versions of and mention a hardware.conf file that is no longer needed with this version and higher. I've also found that it is no longer necessary on my systems to modify the /etc/modules file
At this time, additional changes will be needed for kernel version 4.19 that are not documented here currently. Please ensure you are on 4.14 for this set of instructions
The Linux Infrared Remote Control (LIRC) library is used to handle receiving IR commands through the receiver module, saving them to a file, and then sending the commands when desired through the IR LED.
The first step is to record the IR signals from our existing remote using the IR Receiver and saving them to a file. The IR Receiver is only needed initially to learn the IR signals and then could be removed, so a temporary connection could be used.
Connect the IR Receiver to the Raspberry Pi. Use the attached picture to identify the VCC, GND, and Signal pins. Using a breadboard, hookup wires, or creative bending of the pins to the following connections
VCC connects to 5 volt pin
GND to a ground pin
Signal to Pin 23
Power on and connect to the Raspberry Pi through either by opening the terminal on the local device, or creating an SSH connection using a program such as Putty for Windows.
The following lines can be copied and pasted into the SSH session. They will add/modify lines to various configuration files required for lirc to operate.
sudo apt update sudo apt install -y lirc echo dtoverlay=lirc-rpi,gpio_in_pin=23,gpio_out_pin=22 | sudo tee -a /boot/config.txt sudo sed -i '/driver = */cdriver = default' /etc/lirc/lirc_options.conf sudo sed -i '/device = */cdevice = /dev/lirc0' /etc/lirc/lirc_options.conf sudo shutdown -r now
Wait for the device to reboot and log in again.
Optional: Test IR Receiver
Mount the LIRC device to confirm any input can be received. You may need to restart the Pi after this test is completed to make it available for later steps.
sudo systemctl stop lircd mode2 -d /dev/lirc0
Aim an IR remote control at the receiver and press a button and ensure data appears on the screen
CTRL+C to stop
At this point, the LIRC program is installed and we are able to view IR information.
Step 4: Capturing IR Codes
A remote profile can be downloaded from the LIRC website
http://lirc-remotes.sourceforge.net/remotes-table.html
and placed in /etc/lirc/lircd.confg.d, or a custom profile can be created with your own remote.
During this process, you will enter the name of the key/button you are recording. Only valid names are allowed, so run the following command to view all available names
irrecord --list-namespace
Example: I used the name KEY_POWER when I recorded the Power button on my remote and KEY_TIME when recording the timer button.
If more than a few keys are being recorded, I recommend documenting the key names used and what button they map to, as there may not be a perfect name for the button being recorded. This will make it easier to reference in the future.
Generate a Remote Configuration file
Create a new remote control configuration file (using /dev/lirc0)
sudo systemctl stop lircd cd ~ irrecord -d /dev/lirc0
If you encounter an error saying unable to decode data or something went wrong, press CTRL+C to cancel and run the last command as
irrecord -f -d /dev/lirc0
this will force the record to run in raw mode and should allow the process to continue
Follow the directions on the screen. Once the system is initialized, enter the name of the remote you are recording (the resulting file will be based on this name), then enter the key name you are going to record, and then press the button on the remote while pointing it at the receiver until prompted for the next button name. Repeat this step for each button on the remote you wish to record.
Copy over your new configuration file(s) to the lircd.conf.d directory and start lirc again
sudo cp ~/*.lircd.conf /etc/lirc/lircd.conf.d sudo systemctl start lircd
At this point, remote codes have been recorded to a file.
Step 5: Headphone Jack (Optional)
To make running wires and modifying the Raspberry Pi more modular, I hot glued a 1/8 inch headphone audio jack to the case and connected wires to the jack. Headphone wires with the matching plug were used to connect the IR LED, so this wire could be routed to an inconspicuous locate to point at the IR receiver of the device I wanted to connect, but could easily be unplugged from the Pi without needing to remove all of the wires.
This is purely optional, but has come in handy.
Step 6: Connecting IR LED (Quick)
Connecting the IR Emitting LED to the Raspberry Pi can be done multiple ways. This step shows the quick way I connected it, but which I found out later can exceed the current limit on the Pi's pins. So far I have not run into any problems, but a more ideal way of connecting is described in the next step
Calculate the resistor needed for your IR LED. http://ledcalculator.net/ can assist with determining the proper resistor value if you have all specifications of your LED. In this case, the voltage of pin 22 is 3.3 volts, the LED voltage drop is 1.2 volts, current rating is 20 ma, and 1 LED was used, resulting in a value of 110 ohm resistor needed. I used a single 100 ohm resistor.
Note: It was later brought to my attention that the max current of all pins at any given time is 16 ma, so this configuration could exceed that. A better configuration with a transistor and 5 volt supply is described in the next step, but after several weeks of running in this configuration, I have not encountered any problems yet.
Pin 22 on the Raspberry Pi will be connected to the anode of the IR LED, which is the longer leg by default.
The shorter pin of the LED connects to the resistor and then to the ground pin. I cut off most of the wire on the resistor and soldered it directly to a ground pin and to the ground wire going to the LED.
Step 7: Connecting IR LED (correct Method)
To properly connect the LED without exceeding the draw limit of the Raspberry Pi, connect the LEDs to the 5 volt supply with appropriate resistor, connect the cathode pin to the collector pin of a 2N2222 resistor, connect the Emitter pin of the transistor to ground, and connect pin 22 of the Pi to a 1K ohm resistor to the base pin of the transistor. This allow a very small current from pin 22 to connect the LED to ground, completing the circuit without over drawing the Pi.
In my example, I wired up 2 IR LEDs, so I could control ambient lighting as well as the electric fireplace.
Step 8: Testing Sending IR Commands
To send an IR command, the program irsend is used.
Syntax: irsend
Example: irsend SEND_ONCE Spectrafire KEY_POWER
This sends the power button command from the Spectrafire remote once. Replace Spectrafire with whatever you named your remote. Repeat with other key names used when recording the file.
At this stage, you are able to send any commands previously recorded using the IR LED connected to the Raspberry Pi.
Step 9: Installing Ha-bridge
To allow the Echo to be able to control our device, we will emulate a Philips Hue bulb using ha-bridge. Once configured, the Echo will be able to detect this device and send power on/off commands to it.
The website for ha-bridge clearly outlines the process for the current version and is highly recommended to review.
https://github.com/bwssytems/ha-bridge
Copy and paste the following lines into an SSH session to install and configure HA Bridge. (It may be necessary to run the first line itself, then paste the remaining lines
sudo apt install -y oracle-java8-jdk mkdir /home/pi/habridge cd /home/pi/habridge/ rm ha-bridge-*.jar -f wget $(curl -s https://api.github.com/repos/bwssytems/ha-bridge/releases/latest | grep 'browser_' | cut -d\" -f4) mv ha-bridge-*.jar ha-bridge.jar echo [Unit] | sudo tee /etc/systemd/system/habridge.service echo Description=HA Bridge | sudo tee -a /etc/systemd/system/habridge.service echo Wants=network.target | sudo tee -a /etc/systemd/system/habridge.service echo After=network.target | sudo tee -a /etc/systemd/system/habridge.service echo | sudo tee -a /etc/systemd/system/habridge.service echo [Service] | sudo tee -a /etc/systemd/system/habridge.service echo Type=simple | sudo tee -a /etc/systemd/system/habridge.service echo WorkingDirectory=/home/pi/habridge | sudo tee -a /etc/systemd/system/habridge.service echo ExecStart=/usr/bin/java -jar -Dconfig.file=/home/pi/habridge/data/habridge.config /home/pi/habridge/ha-bridge.jar | sudo tee -a /etc/systemd/system/habridge.service echo | sudo tee -a /etc/systemd/system/habridge.service echo [Install] | sudo tee -a /etc/systemd/system/habridge.service echo WantedBy=multi-user.target | sudo tee -a /etc/systemd/system/habridge.service #Reload System Control and start service, setting to enable at startup sudo systemctl daemon-reload sudo systemctl start habridge.service sudo systemctl enable habridge.service
Step 10: Emulating a Philips Hue Bulb
With ha-bridge running, open a web browser and enter the IP address of the Raspberry Pi, and the interface for ha-bridge should appear.
Click the Add/Edit link at the top of the page
Name: Enter the name you want to use when speaking commands
At the section labeled "On Items" set the type "Execute Command/Script/Program and enter the command in the Target Item box. If multiple commands are desired, click the Add button to save the current line and enter another command. It is also possible to set a delay and repeat a command a certain number of times. In this case, the power button needed pressed first, then the Timer button pressed 3 times to set the auto-off timer for 3 hours.
Repeat the same idea for the "Off Items" area, clicking Add when finished.
At the top of the page, click "Add Bridge Device" to save it as a new item, or Update Bridge Device if modifying an existing one.
Aim the IR LED at the device. On the Bridge Devices page, click the Test ON or Test OFF button to verify it is acting as desired.
Step 11: Connecting to Amazon Echo
The last step is to allow the Amazon Echo to communicate with this device. Note: Both devices must be the same network.
Option 1) say "Alexa, discover smart home devices"
Option 2) Open the Alexa app, tap on Menu>Smart Home and click the "Discover Devices" link
After a few moment, the device should be recognized.
Speak, "Alexa, turn the bedroom fireplace on" and verify that the device turns on as expected. Replace Bedroom Fireplace with whatever you name your device in ha-bridge. Repeat the process to turn off the device.
If you haven't mounted the IR LED yet, find an inconspicuous place to mount it while allowing it to point in the general direction of the IR receiver in the device. You may need to move it around to point at different areas to find the best location.