OctoPrint and Email Notifier woes

Email Notifier Woes

Update 3-22-19: It appears that the plugin is broken, dead, and abandoned by its creator. I currently know of no other way to send notifications besides Twilio or IFTTT.

I have OctoPrint (via OctoPi) setup and I wanted a way to be notified when my print was done without constantly walking into the garage. So I searched the plugins one day and found the Email Notifier.

After I installed it I could not get it to work for the life of me. I followed the instructions of adding my username and password to the keyring but it always produced an error NameError: global name ‘keyring’ is not defined”. Tried some fixes here and there but nothing had worked. So I bailed.

I tried to install the SMS Notifier (Twilio) instead, and that seems to have broken my plugin installation manager. Great. Now whenever I try to install a plugin after I tried to install the SMS Notifier I get the error message about pip not understanding the output or some shit. Great. I have flashed an SD card 2x and tried this twice, it always causes this issue. Damn you SMS Notifier. I have not looked at their Github for a fix yet, so… (Update: it did not break my plugin manager, that was another issue.)

I went back to the Email Notifier, and this time I got it working. I spent some more time digging on the net and found the solution to the issue I had of the keyring not defined. Then… I ran into a different error of yagmail not wanting to send mail via Google because of some reason (it didn’t say exactly) just gave me the generic error code of “send-mail: Authorization failed 534 5.7.14”. Looking that up had no help. But… I did find the answers to my problems and now my Email Notifier is up and running!

I get the error message of there being a new pip version but I have ignored this so far. These are the steps I took to get it working, YMMV.

First make sure that your Google account is setup to allow less secure sign-ins.
https://support.google.com/accounts/answer/6010255

Then SSH into your OctoPrint and update yagmail then install keyring.

Install yagmail (its probably already installed but hey)
/home/pi/oprint/bin/pip install yagmail[all] --upgrade /home/pi/oprint/bin/pip install keyring --no-deps

Start OctoPrint Python
/home/pi/oprint/bin/python

then

(control+z to exit)

Reboot the Raspberry Pi for all the changes to take effect. I tried just restarting OctoPrint but that didn’t work. After the reboot I ran into another issue (of course).

“sending failed. Reason :: send() got an unexpected keyword argument “validate_email”

Modify the file init.py (__init.py__ is how it showed on my system with ls ) found in /home/pi/oprint/lib/python2.7/site-packages/octoprint_emailnotifier and change the line:
mailer.send(to=emails, subject=subject, contents=body, validate_email=False)

to
mailer.send(to=emails, subject=subject, contents=body)

(here is the fix https://github.com/anoved/OctoPrint-EmailNotifier/issues/41#issuecomment-315799836)

Places I found my answers:

https://github.com/anoved/OctoPrint-EmailNotifier/issues/8#issuecomment-187017056
https://github.com/anoved/OctoPrint-EmailNotifier/issues/27#issuecomment-295525220
https://github.com/anoved/OctoPrint-EmailNotifier/issues/27#issuecomment-302890618

I got a 3D printer!!

YES! I finally got one (about a month or two ago), been wanting one for a long time. I did a gig and got some extra funds (mainly, I was able to finally talk the wife into it). So yes, I got a 3D printer!! I did about three days of research and Googling. I had thought I found one and I was about to buy it, then of course, I found another one that caught my eye, it happened to be on sale, so I ended up doing research on that one too. It turned out to be a lot better. Metal body not acrylic. So I ended up with that one. It is a Monoprice Maker Select V2.1, a rebrand of the Wanhao Duplicator i3. A great machine, and very popular which was a factor in my decision. Great community support. Worked great right out of the box, only took about six screws or so to assemble. I did order the MOSFET board to offload the heat bed amperage. Since the board has cheap skinny Chinese traces it is prone to overheat and melt down. I would like to avoid that. The MOSFET does this.

First thing printed…a liver hahah due to my transplant. Then a replacement knob for our Kitchen Aid Mixer. I have tried to print useful things and not dumb trinkets. I don’t want to fill my desk and shelves with plastic knick-knacks. After I printed a few pieces I went straight into printing printer upgrades. The Z-braces were the first thing I printed and it is the best upgrade I have done so far. It shows immediate improvement on your printed items. Of course, now there is a rear Z-brace kit, which I would have done but I do not want to reprint everything and redo the printer. It is solid so as is, so I am leaving it as is.

So far the printer is awesome. I have many plans for it. I  can now print prototypes for my projects and custom boxes for my electronics, this is perfect! I have already started to dabble in creating my own custom “things”.

Using Amazon Polly with Node-Red

Polly Diagram

Amazon Polly and Node-Red. This was far easier to get going than any Alexa skill I tried to do. I just came across this the other day on Scargill’s Tech blog.

Amazon Polly is a service that turns text into lifelike speech. … With Amazon Polly, you only pay for the number of characters you convert to speech, and you can save and replay Amazon Polly’s generated speech.”

Basically you feed Polly SSML text and it returns it in spoken form as an MP3 (or other format). Sweet, I can give my server/Node-Red a voice! Not sure my wife will like this but it will actually server a valid purpose. I was admitted to the hospital (again) for a short three-day stay recently and while I was admitted (and loaded up on drugs) my server closet reached my maximum set temperature… and issued the text alert 187 times. Now I can have the server annunciate that the closet needs to be opened. I could add my wife’s number to the SMS list but that will fail, lol. Simply saving the MP3 on the server and having it be played back works just great.

I am sure you can tap into the API many other ways, and use Polly many other ways, but the simplest way was my path. I just logged into the Amazon Polly Developer console and slapped some text in the input box. It spit out an MP3 and I saved it. The catch here is that the free tier of the Polly program only lasts 12 months, then you get charged. Its super cheap though, $4 per 1 million characters. The free tier is 4 million per month (for 12 months). From what I have read Polly will search the cloud to see if the file already exists. If it does it pulls it down, if it doesn’t it creates it (and counts agains the character limit) and pulls it down. Is this for “the cloud” or your account? Dunno. Still pretty cheap.

I have circumvented the whole thing (for now). I don’t really plan on (or have the need to) generating Polly files on the fly. What works for me is as I said, just slapping a ton of phrases I think I’ll need into Polly and saving the files for later. I’m cool with that.

But, there is a node for NR that will generate Polly files for you on the fly, and/or pull down the ones already generated. I mentioned I found Polly on Scargill’s blog, he also used NR to play with Polly. He had to do it the hard way. Three days ago the node was made. Definitely check out his blog even if you just use the node. Its all good info.

Install node-red-contrib-polly-tts

https://flows.nodered.org/node/node-red-contrib-polly-tts
https://www.npmjs.com/package/node-red-contrib-polly-tts

npm install node-red-contrib-polly-tts

The node looks pretty simply to use (I have not used it). You need to add your Amazon Polly API credentials to the node. The input gets TTS’d and the file saved. If the text has already been converted no Polly call is made and just the file served. So it looks like it stores all the Polly MP3s for you locally. Sweet.

 

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