Tuesday 30 May 2017

The ESP8266 Alarm Can Now Send Emails

Here's the result:

An email alert as seen in my inbox:

The code is here. The credentials and recipient email address are defined in email.h at the moment, so changing settings requires a reflashing at the moment.

The ESP8266 does a DNS lookup for gmail's SMTP servers, then connects with SSL, logs into an email address I set up specifically for this device and sends off an email. I have only tested this with gmail, where I enabled the option to allow "less secure apps" and also created an app-specific password (I don't know if other email services provide this) - which is a necessary precaution, as I haven't implemented certificate verification yet.

If you ever want/need to debug SMTP over TLS/SSL, as I had to do a lot of, here's a useful command to run on Linux if you have OpenSSL installed (you most likely do):

openssl s_client -crlf -connect <server>:<port>

It lets you talk directly to the server in plaintext from a terminal, just as if you were using a telnet terminal to talk over an unencrypted connection. The only thing to note is, do not send the character 'R' as the OpenSSL terminal intercepts this and interprets it as a command to renegotiate. This caught me up for a long time when I was trying to send "RCPT TO: ..." as you do for SMTP; lowercase is a better choice it seems.

Here's a screenshot of sending an email with SMTP, as I thought would be a good idea to figure out how to do by hand before implementing it on the ESP8266. The lines with numbers 1-7 are steps/commands that I sent. The authentication string (step 2) is just the username concatenated with the password, all encoded in base 64.

Some additions to the web page interface: 

The page now looks like this before any sensors are triggered:


The additions are the new options to arm/disarm the alarm and time stamping. Pressing these buttons achieves the same thing as pressing the wireless key fob which came with the original alarm base station, which this alarm system now listens for.

After some sensors have been detected:

 A printed case:

"But it looked alright on the computer"

Definitely only a first revision enclosure. I designed it a bit small, with walls too thin as well, so it doesn't quite close properly, and the overlap/interlocking bits are tiny (can't really see them in the photos).
Also - oops: I tried polishing the printed plastic with an ABS plastic and acetone solvent solution... but it was white ABS dissolved in the acetone.

Conclusion:

In all, seemed like it would be an easy feature to add; SMTP is simple after all, but to get this one working reliably was a challenge with the complexity of needing SSL on the ESP8266. So if you are looking to get SSL working on the ESP8266 and need more working examples take a look at the code.

No comments:

Post a Comment