Show HN: Email controlled gate opener in 20 lines of shell
acha.ninja
acha.ninja
The title suggests that it is all standard Unix tools plus 20 lines shell, and that all you need is a working mail server.
However, that setup requires in addition:
1) a command line tool "wsemail" for email handling, which is ~100 lines of Go code. (by the same author)
2) Cross-Compiling that tool from source for the Raspberry Pi.
And even that tool also doesn't handle email itself, instead it accesses:
3) a separate web service "websocket.email" (by the same author)
4) Requesting an API token for that web service.
If this was really about minimalism (as the title suggests), I would have expected polling a mail account via POP3, or opening a persistent IMAP connection to the mail server, all handled locally by the Raspberry Pi.
My take on it is that you only need to write 20 lines of code to do an email triggered action because I spent some time configuring the tedious parts. At the same time, I thought it would be fun to show people that the real world and linux shell are not so far from interacting with each other.
If you spend 100 lines on a separate Go program, you could as well make it a generic IMAP client (using some Go libraries) rather than making it a HTTP/Websocket client that is highly specialiced to a single web service.
- Connecting to IMAP via openssl: https://stackoverflow.com/questions/27134018
- Or via curl: https://debian-administration.org/article/726/Performing_IMA...
- Or talking POP3 via telnet: https://www.shellhacks.com/retrieve-email-pop3-server-comman...
- Or get POP3 messages via pure Bash and a little Perl: https://gist.github.com/xionluhnis/4712075
That last one really is quite interesting, since it looks like you could abandon Perl for parsing entirely if your goal was "just check for the 'open sesame' string anywhere in the email blob", after which (if my reading of the gist is correct) you'd be using pure Bash and shell builtins, and it wouldn't be much longer than 20ln.
https://www.stavros.io/posts/how-remote-control-rf-devices-r...
I also designed a PCB for it, and have uploaded code for sending arbitrary IR/RF codes: https://www.stavros.io/posts/building-cheap-home-sensorcontr...
Yet another also, I'm working on a side project where makers can showcase their projects, and I'd love it if you considered it for this or your other writeups:
- Internet connectivity is restored via my ISP.
- My phone charges so I can send an email.
- My mail relay unclogs/restarts/gets fixed.
But seriously, it is pretty neat as a proof of concept!
I just wouldn't want to spend a lot of time counting on something like this. I've bloodied my fingers trying to pull out the cotter pin on the ranch gate where I used to work plenty of times, because the RF opener wasn't working.
One extra idea here would be to integrate this with IFTTT or something similar where you tell it to send an e-mail automatically when you reach a certain location. (not sure how accurate this would be)
or to create some sort of macro on your phone to use a voice command to send a pre-set e-mail.
Still, since we’re going with the “do everything over the web!” route—
With JMAP, you’ll be able to use plain old HTTP Event Source to listen for emails if you want (http://jmap.io/spec-core.html#push) without the need for a third-party service, though that will just tell you “there’s a new message” and you’ll have to fetch it through a normal JMAP request (getMessages, that would be).
This will still probably be out of scope for pure shell scripting, but it won’t require much code, just a decent HTTP library. I haven’t really thought about it—maybe you could string something together with cURL and a bit of piping.
(I work for FastMail and we’re sponsoring JMAP development. It’s coming along nicely, and I believe we plan to take our JMAP implementation live fairly soon. I’m sure we’d love to hear about people making toys like this with it!)
At some point it makes sense to setup your own servers though. It's up to the individual to decide. I just wanted a way to write some unit tests, had to setup an email server, then made my test interface public.
Why?! Why would you run a separate mail server of that device?
All you need is IMAP (or POP3), to connect the device to an existing mail server.
POP3 is a polling protocol, so you can’t realistically use it for anything real-time. For IMAP, your server probably supports IDLE, and you’ll need to use a client that supports IDLE as well. I’m not familiar with what’s available in IMAP client libraries; people put most of their email client effort into user agents.
Everything IoT should run its own mail server, just for the fun of it. Bonus marks if it causes an RCE vulnerability. (But I’d say you’re pretty safe from that with Dovecot.)
But seriously, email servers aren’t actually that scary. I wish more people ran their own, and more developers could comfortably work with them. Especially as an endpoint for something like this, running your own email server is perfectly feasible and reasonable—it’ll only accept messages to one mailbox, which will be immediately processed and discarded, so there’s no storage. It’s simply a C&C module.
Maybe ngrok will work with smtp too.
I find the thought using email for IOT pub sub hilarious no matter how you do it.
And you need to setup DynDNS or similar.
Having a persistent IMAP connection sounds much less troublesome to me.