Suppose you want to maintain the temperature of the machine. If the temperature of the machine goes beyond 50 degree Celsius, you want to be notified by e-mail. This exercise can be part of your home automation. An Internet connection is essential for this exercise. Your router to which you connected BeagleBone should have some kind of working Internet connection via fiber, WAN port, DSL RJ11, or GSM dongle.
We are going to use the nodemailer JavaScript library for sending an e-mail using a Gmail account. Let's install it by running the following command in the BeagleBone shell:
sudo npm install –g nodemailer
The source code and other information can be found here: https://github.com/nodemailer/nodemailer.
We are going to use a Gmail account to send e-mail. Even a local mail server can be set up on BeagleBone to send e-mails. But it is a learning curve itself. Google does not allow you to e-mail directly using its SMTP servers. You need to use some settings in order for Gmail to allow you send e-mail via a program. Google provides two types of authentication for Gmail login:
Create a setup the same as we did for temperature sensing using tmp36
in Chapter 5, Reading from Analog Components. Open Cloud9 and write the following program and save it as emailAlert.js
. Then replace the value of the variable gmailFrom
with your Gmail ID and gmailPasswd
with your Gmail password. Also, replace the value of variable emailTo
to the e-mail address where you want to receive e-mails. This can be a Gmail address as well. Please note here you are saving your password in plaintext. There is a security risk. It is better to create a temporary Gmail ID and use it for this exercise.
var b = require('bonescript'), var nodemailer = require('nodemailer'), var threshold = 50; var checkInterval = 4000; //4 seconds var gmailFrom = '[email protected]'; //gmail address var gmailPasswd = 'secretpasswd'; //gmail {app} password var emailTo = '[email protected]'; //email address where emails will be sent var temperature, volt; var timer = setTimeout(readVoltageLoop, checkInterval); function readVoltageLoop() { b.analogRead('P9_40',checkOverTemerature); } function checkOverTemerature(pinObj) { volt = pinObj.value * 1.8; temperature = (100 * volt ) - 50; //Celsius console.log("Temperature = ",temperature.toPrecision(3)); if (temperature > threshold) { shootEmail(); timer = setTimeout(readVoltageLoop, 3600000); // 1 hour wait } else { timer = setTimeout(readVoltageLoop, checkInterval); } } function shootEmail() { var transporter = nodemailer.createTransport( { service: 'gmail', auth: { user: gmailFrom, pass: gmailPasswd } }); var mailOptions = { from: 'Beaglebone <'+ gmailFrom + '>', //sender to: emailTo, //receivers subject: 'Overtemperature alert!! : '+ temperature, //Subject text: 'Current temperature : ' + temperature, //plaintext body html: '<p>Current temperature : </p> <b>'+ temperature + '</b>' //html body }; transporter.sendMail(mailOptions, sentMailHandler); function sentMailHandler(error, info) { if(error) console.log(error); else console.log('Message sent' + info.response); } }
The nodeMailer
variable has a preconfiguration for a Gmail SMTP service. We used it in our program to send e-mails. So, a Gmail account is needed for this exercise. But e-mails can be sent to any existing e-mail ID like [email protected]
. Once an e-mail is sent, you get a response like this from the Gmail SMTP server—Message sent250 2.0.0 OK 1429777906 wa4sm7408653pab.17 – gsmtp
.
This program includes bonescript
and nodemailer
libraries. Then we declared and initialized variables. We set a timer that will call callback the function readVoltageLoop()
after 4 seconds. Inside the readVoltageLoop()
function, there is a call to the analogRead()
function, which reads the sensor voltage and the callback function checkOverTemperature()
gets called. In this function, voltage is converted to degrees Celsius like we did in Chapter 5, Reading from Analog Components. Currently measured temperature is compared with threshold. If found to be greater, the function shootEmail()
is called and then we wait for an hour before measuring voltage again. This is done to avoid sending too many e-mails in a short time. There is a limit to how many e-mails you can send in a day by Gmail.
Inside the shootEmail()
function, we call the method createTransport()
from the nodemailer
library. We submitted the service provider (as Gmail), username and the password to this method. Then we filled mailOptions
with information about the e-mail itself. The e-mail recipient, subject, and content are all decided here. Finally, we call the method sendMail()
with the parameters mailOptions
filled in earlier and callback the function name. Inside the sendMailHandler()
callback function, we are printing an SMTP server response or error information if any.
You can visit the e-mail address specified in the variable emailTo
and check if you got that e-mail. If not, check the spam e-mails folder as well. This program can be combined with the push-button program in Chapter 4, Control LED by Push Button to allow us to send an e-mail by button press.
getaddrinfo ENOTFOUND
, then it means there is a problem with internet connectivity.Error: Invalid login code: 'EAUTH'
, then that indicates authentication was unsuccessful. Check your e-mail ID and app password.cert_not_valid
, then that means mostly there is a problem with the date/time. Check the current time using the command date
. Run command ntpdate –b-s –u pool.ntp.org
to update the current time.There are numerous JavaScript package modules available on https://www.npmjs.com/. Many of these packages provide APIs to do IoT fast and easy, for example, the node-dropbox
module allows you to create, delete, and download files and folders on Dropbox. There are also modules related to various online services like Facebook, Twitter, Google-spreadsheet, and many more. You can do an interesting project combining our physical computing projects with them. There is a tool called Node-RED that allows you to create various IoT scenarios visually by a few mouse clicks. Installation and testing on BeagleBone is documented at: http://nodered.org/docs/hardware/beagleboneblack.html. Node-RED allows combining various hardware platforms, IO pins, network protocols, data formats, online services, and storage together in a visual way to create complex IoT scenarios. More details can be found at: http://nodered.org/.
We end our JavaScript programming part here. Before moving to another language there is a need to know what is yet left to do. We did not do any bus programming like I2C and SPI. We will cover that in the Python programming part. For now, the bonescript
library does not have SPI implemented yet. It is work in progress. It might be implemented in near future. The source code of BoneScript can be downloaded from: https://github.com/jadonk/bonescript.
3.133.127.37