Receiving a Twilio SMS

Twilio

 

I use Node-Red for a lot of things and I have recently wanted to send notifications via SMS instead of an email, and I wanted to be able to receive SMS messages as well. I have had issues with the email node. It doesn’t always send for me (I’m using Google’s SMTP) and I don’t have a loud email notification on my phone. Because tons of emails thats why. But text messages on my phone have a very loud notification. I also wanted to be able to send messages to my Node-Red server, doing that via email is cumbersome. So texting it is. Plus sending/receiving an SMS is way cooler than a boring email. I know I can send texts to my phone via email (on most carriers) but that still requires using email and the email node. Don’t want to.

Enter Twilio. I have used Twilio before in the past with their free service tier and never got around to actually using it. The free service lets you do all the same stuff (I think) as the paid service but all the texts have a “This message was sent by Twilio – ” tag attached before every message. So I think thats why I never used it. Well I looked into it again and this time I just upgraded my account right away. Not fucking around. I just threw down $20 bucks. You buy a phone number for $1 per month and then you pay for all the messages you send. The cost per message (at this time) is somewhere around $0.0075 cents! Sweet! I figure I should be able to get a years worth of my number rental and a few messages off that $20 bucks.

So lets use Node-Red to send and receive an SMS with Twilio, it’s why you’re here right? Not for my ramblings.

To send an SMS from Node-Red is dead simple. Just install the Twilio node. It gives you a new output node. Just send you data to the node and off the SMS goes.

To be able to receive a message from Twilio your Node-Red needs to be accessible from the outside world. Twilio needs to be able to see the XML we are going to generate. In my searches for doing this I got a fucking headache. I found a few blogs out there with info on this but nothing concrete. It was actually pretty simple, at least for me. I have also already done this before in a previous post. I happen to run Apache for my web server, which is already setup and open to the world, DNS already setup. All I had to do was create a subdomain (not required) and point a the new vhost to my Node-Red server/install (via ProxyPass). Once this was done Twilio could access my Node-Red served pages. I also have (but I don’t think is required?) SSL already setup. I used Let’s Encrypt on my Apache server. However you do it is fine, you just need to be able to see at least 1-2 Node-Red generated pages from the web. I honestly found this to be one of the more pain in the ass tasks to deal with. Good luck.

Update: added a new post about reverse-proxy/SSL

 

Twilio Flow

The switch node checks msg.Body with   msg.req.query.Body for an incoming command. If the switch matches a command it routes it to a function node. If it matches a command it also checks that command for an ON or OFF and outputs the results as a 1 or 0 for you to do with what you like. So you would text your Twilio number “mySwitch on” or “mySwitch off” and bam! Otherwise it passes it off to a different function node to process the message further. That node checks the incoming message for a global. By this I mean you text your Twilio number “read mySensor” and this function node sees the “read” command and then checks for a sensor name (mySensor). It matches this name against a global you have saved and spits back the results after being XML’d. So when you text “read mySensor” it checks the mySensor global and texts you back with “mySensor: yourvalue”. Awesome.

Tip: the switch node is case sensitive, so be careful. The function nodes change everything to caps I assume to simplify stuff I dunno. I didn’t write it. Also when you change the sensor name make sure to edit the substr to match the character limit of your sensor name. Example: WEMO would be 4, MYSWITCH is 8 characters. Same thing if you change the READ query.

Here’s the flow, I included a testing global for you as well:

This is where I got the information on the Twilio node (from the creator it seems) and how to receive messages. But I found it about as clear as mud. (Note: the json2xml node doesn’t exist anymore. Just use the XML node now.)
http://blog.thiseldo.co.uk/?p=1090

Here is the original flow on flows.node.red.org, but its a little outdated.
http://flows.nodered.org/flow/d65e0c5e4f5fef767be2

 

Some other pages I found helpful along the way:
http://flows.nodered.org/flow/bce112d484f93a8c282a
https://groups.google.com/d/msg/node-red/YBVKXhJLI90/gUh3T-O6BAAJ
https://www.thethingsnetwork.org/forum/t/parse-data-with-node-red/835/9
http://noderedguide.com/node-red-lecture-3-basic-nodes-and-flows/
https://github.com/Leonidas-from-XIV/node-xml2js/issues/87
https://www.twilio.com/docs/api/twiml/sms/your_response

IVR with Twilio and Node-Red example flow
http://flows.nodered.org/flow/637b5f6128a8d423503f

 

Alexa, turn on Red Alert!

Red Alert

I love Star Trek (any good SciFi really) and who doesn’t? I also have an Amazon echo and I have been making my home smarter and adding automation where I can, very SciFi’ish yeah? The other day I thought to myself how cool would it be if I could activate a red alert”? Well I do have the Dot and I do have Philips Hue bulbs. I do have a home server and I do have Raspberry Pies. So I decided I wanted to have this feature and set out to get it done. As it turns out I am not the only one that wanted to be able to do this. After Googling I found a few other people that went through a similar process. Although I have not seen anyone do it the way I did. The ones I found mostly accomplished it with a Google Home Assistant and some used Node-Red. Hey, I have Node-Red. I guess the Home Assistant can play audio files. I have found a few pages on Alexa doing this recently but I have not gotten into making skills, yet. All the pages I saw also seemed to rely on an outside service of some kind (minus the voice assistant). They pulled the audio from the web or used IFTTT (which I hate) to do something. I don’t want that. I like to be as self contained as possible. Here’s what I did.

I have my home server setup with NR and it takes it all the MQTT in the house and does all the NR handling for the house. Then I have a Raspberry Pi (that also sits on top of the server), this has a temperature sensor on it and it handles the audio portion of the red alert. Since it is sitting there I also have it monitor the server, and the server monitor the Pi.

 

Yes I have a server and a Pi, yes they both also run NR. Why do I not use just the server instead of the Pi? The server doesn’t have a sound card and I don’t have an extra one. So yeah.

 

Flow 1
Flow 1

This flow is where I make the color changes. I use the Wemo Emulator node to create a device Alexa can discover, that also allows me to choose my own trigger word. That node gives a 1 or a 0 (on or off). I pass that to a function that contains the hue bulb color and activates the alert pulsing, this all flows to the hue bulb and out via MQTT. In the flow I only have one hue bulb connected. I have since connected the red alert to all the bulbs I have (currently 4 colored ones).

Flow 2
Flow 2

Here is where I activate the audio. I initially tried omxplayer. I found a shell script that looped the audio but it gave me issues when trying to kill the process. It only worked the first time. The processes didn’t die completely. I want to be able to stop the red alert also. So I continued searching. I came across a post in the NR Google groups and they had created a flow for playing a sound off of motion detection. I was able to take the kill (killall, duh) command he used to stop my flow. I also used the player (mpg123) he used because it has looping options builtin. Sweetness.

I am now able to tell Alexa to turn on a red alert and have her stop it as well. This doesn’t used IFTTT or rely on any other outside source (besides Alexa). It does not require an Alexa skill either. I have future plans to make a custom skill for this so I can change the phrase from “turn on red alert” to something more comfortable like “activate red alert”.

Onward to the flows!

Flow for the red alert lights:

Flow for the red alert audio:

(You may notice that the flows are different from the images, I cleaned up the flow before exporting and pasting the code.)

UPDATE 8/29/2017: I added updated code/flows so that you can disable the Red Alert lights. Previously a “stop node red” command would silence the sounds but not cancel the flashing lights. With the new update the sounds stop and the lights stop flashing and turn white. Still working on getting the lights to default to white after the Red Alert times out. 

 

UPDATED RED ALERT STOP FLOW 8/29/2017

Stop Red Alert

 

Some pages I found helpful.

Where I got the red alert sound
http://trekcore.com/audio/

Play Ambient audio on motion detection
https://groups.google.com/forum/#!searchin/node-red/audio|sort:relevance/node-red/vwQq8Plk0Zg/6DV5ZYMRCAAJ

The code for the ambient audio
https://github.com/natcl/exporail_video_player

Google Home Assitant and an RPi with video
https://www.youtube.com/watch?v=7j3QQlc_efY

Static IP on a Raspberry Pi running Jessie

Raspberry Pi

I know this is out there already in the Googlesphere, this post is more for me in the future than it is for you. I have a few Raspberry Pies and I like to run static IPs. It just makes things easier and I like having my network “all fancy”.

I went to setup a static IP the other day and it wasn’t working. I rebooted a few times until I figured out something was up. In my Googling I found out that the standard linux way of assigning a static IP address won’t work with the new version of Jessie for a Raspberry Pi. They made some updates and the old configuration now gets ignored. So editing the interfaces at  /etc/network/interfaces is a no go. I mean you can, but it’s pointless. Not to worry the new way is just as easy as before it is just located in a different file now. Now you need to edit  /etc/dhcpcd.conf, and the way you set the address is slightly different.

The old way of doing things:

And the new way of doing things:

It has become a little easier if you ask me, you just need to know where the new file to edit is.

All thanks to this page:
https://www.jeffgeerling.com/blog/2016/setting-static-ip-address-raspbian-jessie-lite-on-raspberry-pi

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

Alexa Skills

Amazon Echo Dot

I purchased an Amazon Echo Dot a couple weeks back. In the past I had stated that I would never put one in my house, and generally had a distain for voice activated things. I had done a little bit of reading on the Dot and I have been getting further into home automated things. I finally had a light bulb moment where I now understand all the coding. I am now dangerous. Haha.

Logically the next step was integrating Alexa with my custom build devices. All of my devices work off an ESP or Raspberry Pi (or will). I found Fauxmo Wemo emulation which is fantastic. That covers about 60% of my use, a simple off and on. But for things like my temperature sensors, hyrgrometers and water lever sensors I need to be able to pull data and have Alexa read it back. Like “Alexa, ask server for the temperature.” Or “Alexa, ask the garden for an update.” All of my devices communicate via MQTT. I started with Node-Red without Alexa so this is the direction I went. I still think it was the best route to go. All I need is an Alexa skill to read back different inbound MQTT topics (for different devices). I can’t believe there isn’t a skill already out there to read back MQTT data or homebrew temperature sensors. Or is there and I haven’t found it yet? The majority of the available Alexa skills fucking suck. There are very few actually useful skills to use. The majority of the skills I have seen are garbage. One off skills you would use once or twice then forget about. More Alexa joke skills than there needs to be.

So I started looking into making my own skill. This is what I have found to get started with creating a skill.

  • You need an Amazon AWS Developer account
  • You need access Lambda
  • You must subscribe to basic free tier of service
  • You must subscribe to Amazon EC2, which is only free for one year.

So is this a catch with using custom skills and Alexa? Will my skills then start costing me money after 12 months. If I cancel after 12 months do I lose all my skills? I’m thinking yes, I will bet you Amazon is planning on cashing in on this later? Or is it already that late in the Echo game that I am just being unfoundedly paranoid?

I have not yet created a skill. Javascript is not my forte and I haven’t sat down to dig in to this yet.

 

Some info that I found hard to find:
https://forums.developer.amazon.com/articles/45945/where-do-i-find-the-alexa-skills-kit-trigger-event.html

Great walk-through on creating a new skill:
https://www.youtube.com/watch?v=zt9WdE5kR6g