Since I have been fiddling with Alexa I was able to get a light working with Wemo emulation. Both on the Raspberry Pi and on the ESP itself. I am mostly using the ESP with Fauxmo to act as physical devices. The Wemo emulation being done on the Pi is for running a bunch of scripts with MQTT or (hopefully in the future) gettin’ data from sensors and such. Still trying to find a way to get Alexa to read whatever I give her from MQTT, that would be righteous. But for now I have an automated keyboard light with Alexa.
(TLDR; Made a keyboard light on an ESP with a relay that emulates a Wemo plug and is voice activated by Alexa. Skip to the bottom for the code I used.
Any who, I replaced my old keyboard light switch made out of an old telephone biscuit jack with a toggle switch. I upgraded. I can now voice activate my keyboard light with Alexa. Man I’m lazy, and man that is cool. Not the lazy part the keyboard light. I do have to admit this was not my first attempt at this build. I tried two times before I finally got it right. The first two times I was trying to use 2N2222 and 2N3904 transistors and neither would work right for me. I was able to get it all working on the breadboard just fine but as soon as I transferred it to a PCB it failed. I think the problem is with the transistor. From my measurements it keeps leaking 12v back through the base and I don’t know enough about electronics to be able to figure it out yet, obviously, I tried twice.
So the third time I used the pre-made modules I have; 5v relay module. I put together a small PCB for the ESP and a DC-DC converter and added some pins to use jumper wires to attach to the relay. Soldered the power to a barrel jack and hooked up a toggle switch and connected it to the relay. So if I flip the switch it bypasses the relay and I get light manually. Always good to have a backup. The switch will work with or without the ESP plugged in. I plugged it in and bam! It worked. I gave Alexa a few commands and on and off the relay clicked. Beautiful.
Then……it failed, it started flickering the relay. It took me a minute to figure it out. I forgot the current limiting resistor on GPIO2 for the relay. Oops. That’s an easy fix luckily. The green jumper wire in the pictures goes to the pin header from GPIO2, so all I had to do was remove the jumper wire and replace it with a 1K ohm resistor. Easy. It was getting late so I turned it off and removed the ESP. The next day I go over to my computer and I can smell the lovely aroma of burnt electronics. Fuck. I look down and I can see the DC-DC converter sparking on the underside of the PCB. Turned out to be a bad solder job on my part. Since my liver transplant I have to take a shit load of pills, and some of these pills cause my hands to shake. Sometimes it’s not so bad and other times it’s ridiculous. I guess they were shaking more than I thought that night.
So I had to rebuild the whole thing. Again. Live and learn. This time I was sure to leave extra space in my solder routing just in case. The Mark IV has been up and running with zero problems for two days now. I think I worked out the kinks. And it is awesome to be able to sit down and tell Alexa to turn on my computer room and keyboard lights. Hell with Node-Red I could even WOL my computer!
Now behold, pictures…
Then the code used (basically the straight Fauxmo ESP sketch):
https://bitbucket.org/xoseperez/fauxmoesp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
/* * ESP8266-01 connected to a relay module * Uses FauxmoESP, to simulate a Weemo plug * Give Alexa the command with whatever name you assign */ #include <Arduino.h> #include <ESP8266WiFi.h> #include "fauxmoESP.h" #define SERIAL_BAUDRATE 9600 #define LED 2 #define WIFI_SSID "xxxxxx" #define WIFI_PASS "xxxxxx" fauxmoESP fauxmo; void wifiSetup() { // Set WIFI module to STA mode WiFi.mode(WIFI_STA); // Wifi MAC address byte mac[]= { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; // Connect Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASS); // Wait while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println(); // Connected! Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); } void setup() { // Init serial port and clean garbage Serial.begin(SERIAL_BAUDRATE); Serial.println(); Serial.println(); // Wifi wifiSetup(); // LED pinMode(LED, OUTPUT); digitalWrite(LED, LOW); // Fauxmo fauxmo.addDevice("keyboard light"); // fauxmo.addDevice("light two"); // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY // it's easier to match devices to action without having to compare strings. fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) { Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF"); digitalWrite(LED, state); }); } void loop() { // Since fauxmoESP 2.0 the library uses the "compatibility" mode by // default, this means that it uses WiFiUdp class instead of AsyncUDP. // The later requires the Arduino Core for ESP8266 staging version // whilst the former works fine with current stable 2.3.0 version. // But, since it's not "async" anymore we have to manually poll for UDP // packets fauxmo.handle(); static unsigned long last = millis(); if (millis() - last > 5000) { last = millis(); Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap()); } } |
Update 03-09-2019: Originally I was using 1ft waterproofed LED strips that came prewired. I was using something similar to these. After about a year or so of use the LEDs started burning out. Also, the lights were not very bright and they had a slight yellow tint to them. Overall I wasn’t satisfied with them. I ordered some other LED lights off Amazon for wiring up my gun safe with LEDs, these LED strips are much much better and far cheaper – they just are not waterproof (I don’t need waterproofed LEDs under my desk).
Discover more from Its_All.Lost
Subscribe to get the latest posts sent to your email.