Set Email and SMS Notifications that are Triggered by Sensor Values

Email Notifications

There is an email output node in Node-RED editor which is very convenient for sending email notifications. We will connect this node to IoT In node to set email notifications that are triggered by sensor values our devices publishes. Scroll to social section on the nodes catalog and select email out node.

 

Email out node

For the beginning, you can attach an inject node to it to verify that you are receiving emails.

 

Simple flow with email node

Configure email out node. I am using my Gmail account. Put the address you want to send message to, and UserID and password of your account you want to send message from.

 

Edit email out node

At first, I was having troubles receiving emails, but after searching a bit for a problem, I found that it should work if I set my Gmail password to 8 characters or less (the problem is that the node for some reason reduces password you enter to 8 characters), and it worked. Also, you will might have to turn on "Access to less secure apps" on your Gmail account.

If you are able to receive emails after you click on inject node, you can now replace inject node with IoT In node. We won’t connect them directly, because we don’t want to send an email each time a payload from our devices is sent. Instead, we will put a switch node to analyze particular parameter and decide whether an email should be sent or not.

 

Flow with IoT and email nodes

I used the same payload format as in the previous tutorial.

Switch node is used to route messages based on property values. It receives whole payload and we set the rule that if the temperature value is higher than 30, then it sends payload to the first output, otherwise, it blocks it. You can set multiple rules in one switch node.

 

Edit switch node

Don’t forget to edit IoT node properly (explained in the 3rd part).

Before email node, put a function node to form a message to be sent with temperature value and device ID.

var temperature = msg.payload.d.temperature;

var Name = msg.payload.d.Name;

msg.payload = "Temperature at device " + Name + " is " + temperature + " degrees!";

return msg;

If you want to limit the number of emails sent per certain period of time, you can put delay node before the email node.

You can do all the same steps for humidity.

 

SMS Notifications with Twilio Service

What if the situation is more urgent, let’s say temperature rises above 40 and we want to send an SMS notification? Fortunately, there is a Twilio node that allows us to do that easily. Twilio is a service that gives developers possibility to programmatically make and receive phone calls and send and receive text messages using its web service APIs. Twilio's services are accessed over HTTP and are billed based on usage. Go to https://www.twilio.com and sign up. In the console, click on the # to see your number you can send messages from. To use your Twillio account from Node-RED, you will need ACCOUNT SID and AUTH TOKEN.

We will extend the flow we have just built. Add a new rule in the switch node.

 

Add a new rule to switch node


Copy the function node for message creation and if you want, you can add a delay node to limit the number of sent messages.

 

Extended flow with Twilio node

 

Edit delay node

Click on the Twilio node and then click Add new Twilio-api. Fill these fields with your Twilio number, account SID and token you can find on your Twilio account. Then, add a number you want to send messages to. If you use a trial account, you can only send messages to one registered number. Deploy your app and that’s it, when the temperature rises above 40 degrees, you should receive an SMS notifications.

You have seen how you can quickly and easily set email and SMS notifications in Node-RED. You can modify these flows and payloads to make your own notification systems.   

Lessons of this Series

Online computer science courses to jumpstart your future.
WP Engine Managed WordPress Hosting

Trending

Newsletter

Subscribe to our newsletter for good news, sent out every month.

Tags