Introduction: Alexa and Switch Operated Lamp
Runner Up in the
Voice Activated Challenge
The Amazon Echo is a great piece of kit! I love the idea of voice activated devices!
I wanted to make my own Alexa operated lamp, but keep the manual switch as an option.
I searched the web and found a WEMO emulator, which, having looked at other options, this seems the most simple way of getting Alexa to operate any device!
**** IMPORTANT NOTE: RESPECT MAINS ELECTRICITY - IT WILL KILL YOU ****
Step 1: The Harware
The component count is minimal thanks to the ESP2866-12e which does all of the work!
All of the components came from eBay.
1 x ESP2866 -12e (or any ESP8266 with at least 2 GPIO ports)
1 x 5v power supply
1 x 5v relay. I found these very small relays with 5v coil & 250v 3 Amp contacts on eBay
1 x 5v to 3v3 step down
1 x opto coupler (4N35 or equivalent)
1 x momentary action push to make switch
1 x suitable project box
The image shows a resistor which was not required!
Step 2: Putting It Together
I wanted the project to live in a compact enclosure so it wouldnt look too unsightly.
It simply replaces the lamps original switch with a relay and a 'soft' switch!
Its important to ensure that the mains side (the bit that will kill you) is installed as securely & safely as possible - I used a 'choc block' connector for safety and ease.
The relay coil connectors are pretty close together so take care when wiring. Keep the exposed 5v coil wires and mains connections as short as possible.
The mains feeds the 5v power supply. The neutral side of the mains goes direct to the lamp, whereas the live side goes via the normally open contacts of the relay.
The switch is wired one side to ground and the other to GPIO13 of the ESP8266. Any input will do but check the data sheet as some pins are multiplexed.
GPIO15 is wired to 0v !! I have a piece of wire attached to Reset & another to GPIO0. These are used for uploading the sketch and can be removed when complete.
Note that the ESP8266 devices operate at 3.3v
GPIO4 (again any GPIO will do) is used to set / reset the relay via an opto coupler. I used the opto coupler to minimise current drain on the ESP8266.and allow the 3.3v to switch the 5v relay coil.
I used double sided sticky pads to keep the components in place.
I drilled a couple of 2mm holes at each end of the enclosure for air flow. Not sure if it was nrcessary as very little heat is generated, but it made me feel better :-)
Step 3: The Sotfware
I downloaded the following from GitHub.
The files are all in one directory and the only file which needs modifying is the .ino file.
It shows how to operate multiple switches and is a great piece of software.
I used the Arduino IDE to upload the sketch to the ESP. There are lots of articles on the web of how this is done, just Google - Using Arduino IDE to program ESP8266. Its pretty straight forward and only has to be set up once.,
Note: To upload the sketch I used a standard FTDI usb to serial convertor. GPIO15 must be grounded - I have this permanently wired to 0v, GPIO0 must be held to 0v during reset. After reset, GPIO0 can be left floating. The sketch should now upload.
The parts in the sketch which need modifying are repalcing your routers SSID & Password and the the command you would like Alexa to respond to. Search for 'table lamp' and replace it with a command of your choice, e.g. 'bedside light' or 'ceiling fan'.
The software interacts with Alexa and switches GPIO4 high or low with the the commands table lamp on and table lamp off respectively. It also sets flags - rl1 and isr_ran.
Flag rl1 is used so the software knows the current state of the lamp in order for it to be toggled on or off via Alexa or the switch.
The switch generates an interrupt when GPIO13 is grounded. The interrupts are disabled, GPIO4 is toggled and flag rl1 is set / reset accordingly. Flag isr_ran is used in the main loop to re-enable interrupts after a short delay - this prevents switch bounce!
Imortant note :Ensure your choice of relay has capable contact ratings to deal with the device you are switching.
We have a be nice policy.
Please be positive and constructive.
Disadvantage of using ESP8266-01 & this circuit - on initial power up, both GPIO0 & GPIO2 must be high for normal boot ! The circuit holds GPIO2 sufficiently low to prevent a normal boot :-(.
Replacing the 56 ohm resitor with a 3mm LED solved this !!
CORRECTION: DO NOT have GPIO2 connected when uploading the sketch as it will fail !
I have tested with an ESP8266-01 using the circuit attached. Only mods to the program sketch are as follows:-
Interrupt is now GPIO0 (was 13)
relay is now on GPIO2 (was GPIO4)
so all references to these pins need changing in the code.
DO NOT have GPIO connected when uploading the sketch as it will fail !
I have replaced the opto coupler with a BC337 & a 56 ohm resistor. Any NPN transistor should suffice. I use a resistor to limit the current - it was the value I had available that worked. I'm sure there is a mathematical formula for calculating this value :-)
I had problems with the opto coupler in the hardware pictures. I had success with opto couplers labelled EL 4N35 528. I have built 3 of the switches using this component with no problems.
I tried building this over the weekend, based on the ESP8266-01. The code uploads well and runs, but Alexa does not discover the new device. I've been reading many posts about the differences between 1st and 2nd generation Alexa products. Seems like Amazon has made some changes with it's protocol making these projects much more challenging.
I'm wondering if there is a variant of this code that uses the Sinric model on GitHub, but includes your interrupt button and code? That's a great piece I'd love to see move forward.
Can you suggest changes to the sketch to adapt it to the ESP8266-01 package? I assume I would use GPIO0 and GPIO2. What would need modification in the sketch? Thanks.
You have one of the pins labeled “EN”. I’m not sure what that is. Thanks.
Hi, your assumption is correct- I have not tested this with the esp8266-01as long as you can set one of the GPIOs as an interrupt and the other as an output the code should work.
Just a note Re the opto coupler - I purchased 5 x 4N35 from the UK and the internal transistor would not turn on hard enough to enable the relay :-(. I will check the manufacture of the opto's which work and post the info tomorrow.
Looks like the exact project I have been looking for. Can you provide the schematic for using an ESP8266-01? I don't recognize some of the labels in the drawing to translate it to the 01. Thanks.
Hi, which labels do you not understand ?