SMS NOTIFICATIONS

How to send SMS from my IoT Server?

IoT Server gives you power to create simple function. Those simple functions can be combinated to create more complex functions and at the end full application. One of the function we will go through here is the SMS alerts.

20dfb1f882e097d4ca82399a84098aa57c0be2a8d0a6e9ca51e8352dadfa3f79

Pre-requisite

  • An IoT Server
  • A SMS gateway provider

The dataflow

31dee594a522198c9d615d3bb8c31b08aa0416fcc8ee86f5cdfc67999ca8bb44

This dataflow is a 3 steps dataflow:

  1. Trigger: the trigger can be whatever you want. In our example above it is an inject node but it could be another application like Alert for example,
  2. Request creation: Using a function node we are creating the request to send to our SMS gateway provider. You can see content of this function node below,
  3. Request sending: With an https request node we push the request to our SMS gateway prodiver. Find the settings below

Request Creation:

As you can see in this example we are using Mailjet as a SMS gateway provider. Mailjet is expecting a payload containing the recipient number, the sender and the text. In the header we also add an Authorization token coming from the account and linked to a wallet. Details about their API can be found here

msg.number = "+1 (123) 456-7890"
const number = Math.floor(Math.random() *100);
const content = `This is the content of my SMS. And I can add a variable here: ${number}`;

msg.headers = { 'Authorization': "Bearer XXXXXXXXX", "content-type": "application/json"};
msg.payload = '{ "From": "Braincube", "To": "'+msg.number+'", "Text": "'+ content + '"}';
msg.url = "https://api.mailjet.com/v4/sms-send";
msg.method = "POST";

return msg;

Request sending:

Our https request node is running with a really light configuration has we made the choice to use the msg to send the URL and the method.

85a488fe5e57bf355f705dc0002499d33ec7f25d647cba97b82355f8cf47e050

The output

Output of our HTTP REQUEST node is a JSON msg with some information such as:

  • statusCode: is my request sent, received and valid? Here we have a 200 so everything worked as expected
  • payload: some information sent by the server we contacted. Here mailjet answer with more details about the SMS. Sender, Receiver, Price, ...
  • headers: Headers of the answer coming from our https request,
  • Others: others informations we have added in our msg before the https node
{
    "_msgid":"83631446.1a9f58",
    "payload":{
        "ID":"a6e673f8-3840-4c43-9263-3cbe2e632576",
        "From":"Braincube",
        "To":"+1 (123) 456-7890",
        "Status":{
            "Code":1,
            "Name":"sent_pending",
            "Description":"Message is being sent"},
        "Cost":{
            "Value":0.042,
            "Currency":"EUR"},
        "CreationTS":1680616319,
        "Text":"This is the content of my SMS. And I can add a variable here: 49",
        "SmsCount":1},
    "topic":"",
    "timestamp":1680616318357,
    "_emitterId":"95acfeb7.67f67",
    "number":"+1 (123) 456-7890",
    "headers":{
        "content-length":"315",
        "content-type":"application/json; charset=UTF-8",
        "x-mj-request-guid":"11b3f13c-6e56-4613-9925-e881ae4f5c61",
        "date":"Tue, 04 Apr 2023 13:51:59 GMT",
        "connection":"close",
        "x-node-red-request-node":"5cde3f7c"},
    "url":"https://api.mailjet.com/v4/sms-send",
    "method":"POST",
    "statusCode":200,
    "responseUrl":"https://api.mailjet.com/v4/sms-send",
    "redirectList":[]
}

Implementation example

Here is an example of how we can push Alerts into an SMS:

ecb1b4da52f990141a55ae3c59ae79414d4d52f0017017f20fadcdd19f221a0d

In this flow the Alert application will output a message each time an alert is triggered and we are using this nessage to trigger our SMS Alerts. From the example above we just changed the content of our SMS to inform an operator about this alert.

const alertDefinition = msg.payload.alertDefinition;
const currentValue = msg.payload.triggerValue;
const alertDefinitionText = `Alert is: ${alertDefinition.condition.aggregator.count} ${alertDefinition.condition.aggregator.unit} ${alertDefinition.condition.aggregator.type} ${alertDefinition.condition.comparator} ${alertDefinition.condition.value}`;
const alertValueText = `Current value is: ${currentValue}`;
const name = msg.payload.name;
const extra = msg.payload.extra;

msg.number = "+1 (123) 456-7890"
const content = `${name}\\n${alertDefinitionText}\\n${alertValueText}\\n${extra}`

Was this article helpful?

Powered by Zendesk