TNS
VOXPOP
Will JavaScript type annotations kill TypeScript?
The creators of Svelte and Turbo 8 both dropped TS recently saying that "it's not worth it".
Yes: If JavaScript gets type annotations then there's no reason for TypeScript to exist.
0%
No: TypeScript remains the best language for structuring large enterprise applications.
0%
TBD: The existing user base and its corpensource owner means that TypeScript isn’t likely to reach EOL without a putting up a fight.
0%
I hope they both die. I mean, if you really need strong types in the browser then you could leverage WASM and use a real programming language.
0%
I don’t know and I don’t care.
0%
Edge Computing / Tech Life

Off-The-Shelf Hacker: Control Your Home Projects with Amazon Alexa

Mar 30th, 2019 6:00am by
Featued image for: Off-The-Shelf Hacker: Control Your Home Projects with Amazon Alexa

One of the great things about working with modern microcontrollers is that you can massively alter device behavior by simply modding the firmware and maybe adding a circuit or two.

We’ve been hacking a NodeMCU WiFi unit, a little 5-volt power supply, a passive infrared sensor (PIR) and a relay board into a backyard sensor light. This project uses MQTT messaging to control the light and possibly post a notice that an “intruder” has passed in front of the PIR sensor. The hardware and firmware work very well and I’ll soon cram the whole works into a standard plastic electrical box and install it at the back corner of my house.

I recently ran across an article about FauxmoESP, an Arduino library that works with the Amazon Alexa voice services. A little over an hour later, I was delighted to say “Alexa, turn on the dining room light” and poof, on went the dining room light. We’ll revert back to the yard sensor firmware shortly.

Let’s look at the details of this “temporary” exploratory build.

Add a Socket

Modifying the backyard light sensor project to try the Alexa library was easy and fast.

I wanted to keep the Alexa test simple, so I just unplugged the PIR sensor and set it aside on my desk. I then soldered some 18 gauge wire across the A/C input to the 5-volt power supply, so I could power a socket for the dining room light. The socket is wired in series with the relay normally-open (NO) contacts and the A/C line. Energize the relay, via the D1 general purpose input/output (GPIO) pin on the NodeMCU microcontroller and the light turns on. The D1 pin corresponds to GPIO5 in the code.

A/C outlet wires added to bottom of the input on the 5-volt power supply

Be extra careful when working with 120-volt A/C power and live bare contacts. An electrical shock is painful and possibly very bad for your health.

The contacts on the relay board are rated at 10 amps at 120-volts A/C. The hanging dining room light I used for the test uses three 7.5-watt LED bulbs. When the hardware gets moved out to the backyard and back under MQTT control, I’ll use two 150-watt incandescent outdoor floodlight bulbs. That’s 300-watts, which is below the capacity of the relay contacts. I may opt for a pair of 250-watt equivalent (32-watt actual) LED bulbs from Lowes. The $35 per bulb price is a little steep though. That would also be, at 64 watts, well within the capacity of the relay switch contacts. I’ll need the 18 gauge wire to hook the yard sensor device up to the floodlights.

That’s it for the hardware mods, now let’s look at the firmware.

Using the FauxmoESP Library

The FauxmoESP library is bundled into the last couple of Arduino IDE versions. I used the Arduino IDE version 1.8.7. 3.1.0 was the version of FauxmoESP.

Bring up the Arduino IDE and click “Include Library” under the “Sketch” tab. Slide over and click “Manage Libraries.” Type “fauxmo” into the search box and hit enter.

The FauxmoESP library should show up. Click the “install” button to use it in your sketches. Close down the “Manage Library” window to get back to the main Arduino IDE screen.

I used the fauxmoesp-basic sketch as the basis for my test. Choose “Examples” under the “File” main tab. Look way down in the “custom libraries” section for the fauxmoesp examples.

Much of the sketch was left unchanged. I changed the value of the “#define LED_BLUE” to 16 because that is the pin controlling one of the blue onboard LEDs. The value of the “#define LED_GREEN was changed to 5 to reflect NodeMCU pin that controls the relay board. A little further down in the code, the “#define ID_GREEN” was changed to “dining room light.” The words in this section correspond to what you will actually say when you ask Alexa to turn something on or off.

The code is pretty straightforward. I left the unused “lamps” so readers will have some context for my minimal changes.


Bringing up the example script also loads a file called “credentials.h.”  You should see a file tab in the main Arduino IDE with that name. Click on the tab and change the SSID and PASS entries to match your local network WiFi access point values before trying to compile the code.

Compilation and Upload Challenges

A couple of areas slowed progress a bit.

First, there was an “Async” compile error. A quick trip to the internet revealed that the FauxmoESP library is dependent on the Async TCP library for the ESP8266. Go to the Async TCP library GitHub page and click the “Download” button at the top right. Select the “Download Zip” button and save the “ESPAsyncTCP-master.zip” file to a local directory. Again go to “Include Library” under the “Sketch” tab and click on “Add Zip Library.” Enter the location of the downloaded “ESPAsync” zip file and hit return. That fixes the compilation problem.

Next, the resultant binary file simply wouldn’t upload. I again searched the Web and found that you could just change the “IwIP variant” parameter, under the “Tools” tab from “v2 – low bandwidth” to “v2 – high bandwidth” and everything will work properly. After this change, the file uploaded to the NodeMCU device without any issues. Easy.

Overall, the whole process, including troubleshooting took about an hour and a half. Google and the other search engines are still your friends when trying to run down some obscure one-off problem.

Results And What’s Next

I’ve had the NodeMCU, relay board and power supply running on my dining room table for a few days and looks very reliable. Of course, you also have to have a working Alexa device on your local network and have it paired with the NodeMCU board. My Alexa Echo Dot is a generation 2 device. With everything plugged in I asked, “Alexa, find my devices” and waited for her to complete the discovery routine.

Note that I changed the “green lamp” to “dining room light” in the one define line. If you request that Alexa to “turn on the dining room lamp,” she will ask if you mean “dining room light,” which I found thoughtful. I replied “yes” and the light came on.

Interesting, after a few days, my wife even started using voice commands to turn on the light. Nice.

You can also control the NodeMCU device using the Alexa Android app, both over WiFi and 4/5G networks. The buttons and voice control work great remotely using the app.

I’ll revert back to the MQTT backyard sensor light firmware after finishing this story and put the gadget out in the yard for testing, soon. Time to order a new batch of NodeMCU boards.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.