Automated Keyboard Light with Alexa

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…

View Post

ESP-01, MQTT and an OLED (SSD1306)

OLED ScreenIt took me about a week to get this going. There are still a few kinks that need to be ironed out, or worked around. But I wanted to get this out there before I forgot about it. I could not find a sketch for using an Arduino or ESP with an OLED and MQTT. Not one that didn’t just display data from a connected sensor., that is not what I wanted. I wanted to be able to send messages to the OLED screen via MQTT. I spent quite a while looking for it and I couldn’t find anything. About a week or two ago I finally broke the wall that was keeping me from sending and receiving MQTT message on an ESP. So I took that code and the working code from the Adafruit OLED sketch and made a baby.

I was able to produce a sketch that will simply display any text received via MQTT. Perfect. I plan on using this cobbled together with Node-Red. That way I can have one screen and send multiple sensor readings to it with minimal coding and parts. There are a few things I still need to figure out. For example I can’t get the screen to clear. Perhaps thats what the OLED reset pin was for? But my OLED only has four pins; SDA, SCL, Vcc and Ground. No reset. What to do? I think that is actually the only thing I need to work out. The Adafruit code will wrap your text to new lines, so make a note of that. I have gotten around clearing the screen by injecting spaces via MQTT. Its a little more effort in Node-Red for now until I get that down. But it works. It doesn’t matter how you get there as long as you get there right?

I Program the ESP via the Arduino IDE, if you don’t know how to set that up take a stroll down Google lane. Tons of help there on that topic. As it stands in the code below, once powered up the OLED should flash the Adafruit logo until a message is received then it will display the message until a new one is received. Simple. The default topic is “inTopic”, and the default OLED font size is 1. I have tried a font size of 2 but no higher. I also stumbled upon this library here. Much smaller and designed just for text. May try that in the future, but if it ain’t broke don’t fix it.

Items Required:
(1) ESP-01 (mine is a standard ESP-01 from eBay)
(1) OLED I2C Screen (I used a 0.96″ 4pin OLED also off eBay)

IDE Library Requirements:
Wire
Adafruit GFX
Adafruit SSD1306
ESP8266WiFi
PubSubClient

Some places that helped
https://learn.adafruit.com/monochrome-oled-breakouts/arduino-library-and-examples
https://github.com/adafruit/Adafruit-GFX-Library
https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts
https://learn.adafruit.com/adafruit-gfx-graphics-library/graphics-primitives
https://github.com/knolleary/pubsubclient

This was the tutorial that helped me get what I have going. I couldn’t get the OLED to work quite right until I came across this post. I used it as the base for what I have.
http://randomnerdtutorials.com/guide-for-oled-display-with-arduino/

This page helped but not until I specified the SDA, SCL pins for the ESP. I couldn’t get the display to work properly and do what I wanted.
http://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/

And the code (below): [See the comments for updated code]
View Post