Skip to main content

Connecting SenseCAP to Twilio via Node-RED

SenseCAP K1100 - The Sensor Prototype Kit represents Seeed Studio concentrating the essence of LoRa® communication on technology and edge intelligence products, for the easiest deploying and mastering of LoRa® and IoT applications.

pir

Upgradable to Industrial Sensors

With the SenseCAP S2110 controller and S2100 data logger, you can easily turn the Grove into a LoRaWAN® sensor. Seeed not only helps you with prototyping but also offers you the possibility to expand your project with the SenseCAP series of robust industrial sensors.

The IP66 housing, Bluetooth configuration, compatibility with the global LoRaWAN® network, built-in 19 Ah battery, and powerful support from APP make the SenseCAP S210x the best choice for industrial applications. The series includes sensors for soil moisture, air temperature and humidity, light intensity, CO2, EC, and an 8-in-1 weather station. Try the latest SenseCAP S210x for your next successful industrial project.

SenseCAP Industrial Sensor
S2100
Data Logger
S2101
Air Temp & Humidity
S2102
Light
S2103
Air Temp & Humidity & CO2
S2104
Soil Moisture & Temp
S2105
Soil Moisture & Temp & EC
S2110
LoRaWAN® Controller
S2120
8-in-1 Weather Station

Twilio

Twilio is a customer engagement platform used by hundreds of thousands of businesses and more than ten million developers worldwide to build unique, personalized experiences for their customers.

Twilio known for democratizing channels like voice, text, chat, video, and email through APIs, making it easy for every organization to build meaningful interactions with customers on the channels they prefer.

pir

This section will use the SenseCAP K1100 kit as well as the SenseCAP console, Node-RED, to complete the task of triggering a Twilio message push under certain conditions.

If you haven't installed or don't know what Node-RED is, please refer to Node-RED & SenseCAP Tutorials.

Create MQTT Node

Step 1. Start Node-RED

Start Node-RED by typing the command node-red in the terminal and open a browser and enter the address http://localhost:1880 in the address bar to access the editor of Node-RED.

Step 2. Create MQTT Node

We use Network -> mqtt in node and configure mqtt in the format of the SenseCAP API as requested in the previous tutorial.

  • Server: openstream.api.sensecap.seeed.cc

  • Port: 1883

  • Protocol: MQTT V3.1.1

  • Client ID: The format is org-<Organization ID>-<Random ID>

    • <Orgnization ID> Your organization ID. We have obtained it in the Get the SenseCAP API.
    • <Random ID> uses your own randomly generated numbers or lowercase letters.
  • Topic Format: /device_sensor_data/<OrgID>/<DeviceEUI>/<Channel>/<Reserved>/<MeasurementID>

OrgIDYour organization ID. We have obtained it in the Get the SenseCAP API.
DevEUIUnique identification of sensor devices. This information can be found on the sticker on the back of the Grove - Wio E5, as well as in the SenseCAP console device.
ChannelA physical interface on the device to which the sensor is connected. For the K1100 kit, the default value here is 1.
ReservedReserved Fields.
MeasurementIDMeasured value ID. This ID can be found in the Measurement IDs section of the SenseCAP documentation

Step 3. Validate MQTT nodes

Once configured, please click the Deploy button in the top right corner to check if the configuration was successful. If it is filled in correctly, then the word Connected will be displayed.

Configure Twilio

Step 1. Register or login to Twilio

If you have already registered with Twilio, then please login on the Twilio website.

If you do not use or have registered with Twilio, then please complete your registration and login here.

Step 2. Get the necessary information

We can try using a free service that sends SMS messages to the mobile phone we verified during registration.

Click on Get a trial phone number on the main screen.

Twilio will then automatically generate a Twilio phone number for you.

At the bottom of the main screen, under Account Info, you will find what we need to build a connection to Twilio.

Please note down the Account SID, Auth Token and My Twilio phone number, which we will use later in Node RED.

Configure Node-RED

Step 1. Download Twilio Paletts

Click on the upper-right menu bar and select Settings.

Search and install node-red-node-twilio in the Paletts -> Install.

Add twilio out from the storage bar on the left,double-click it to enter the configuration page, then click the edit button to edit twilio out node.

Step 2. Edit twilio out node

Please fill in the corresponding fields with the information we obtained in the previous configuration of Twilio. Then just click the Add button in the top right corner.

Then, please fill in the mobile phone number you have registered with Twilio.

note

Please note that you need to add the prefix "+ country code" to the mobile phone number you fill in here.

Step 3. Configure the function node

The content of the SMS can be defined in the function block, and the trigger conditions can be that the data reported by the PaaS platform triggers certain rules, all of which can be customized in the function building block.

Drag out the function node from the function bar on the left, double-click it to enter the edit page, then copy the code to On Message.

For example, in this section, when a sensor value is received, the sensor value is sent to the phone and the code can be written like this.

{
var payload = msg.payload;
var topic = msg.topic;
var strs = topic.split("/");
var length = strs.length
if (length >= 2) {
var measurementId = strs[length - 1]
var value = payload.value
if (measurementId == 4100) {
msg.payload = "CO2:" + value
} else if (measurementId == 4103) {
msg.payload = "soilmoisture:" + value
} else if (measurementId == 4150) {
msg.payload = "accelX:" + value
} else if (measurementId == 4151) {
msg.payload = "accelY:" + value
} else if (measurementId == 4152) {
msg.payload = "accelZ:" + value
} else if (measurementId == 4192) {
msg.payload = "sound:" + value
} else if (measurementId == 4193) {
msg.payload = "light:" + value
} else if (measurementId == 4195) {
msg.payload = "tvoc:" + value
} else if (measurementId == 4097) {
msg.payload = "temperature:" + value
} else if (measurementId == 4098) {
msg.payload = "humidity:" + value
} else if (measurementId == 4175) {
msg.payload = "AIdetection_1:" + value
} else if (measurementId == 4176) {
msg.payload = "AIdetection_2:" + value
} else if (measurementId == 4177) {
msg.payload = "AIdetection_3:" + value
} else if (measurementId == 4178) {
msg.payload = "AIdetection_4:" + value
} else if (measurementId == 4179) {
msg.payload = "AIdetection_5:" + value
} else if (measurementId == 4180) {
msg.payload = "AIdetection_6:" + value
} else if (measurementId == 4181) {
msg.payload = "AIdetection_7:" + value
} else if (measurementId == 4182) {
msg.payload = "AIdetection_8:" + value
} else if (measurementId == 4183) {
msg.payload = "AIdetection_9:" + value
} else if (measurementId == 4184) {
msg.payload = "AIdetection_10:" + value
}
}
return msg;
}
note

Please keep an eye on your account balance, the code above will send a different sensor SMS every five minutes. This can quickly drain your balance if you use the above code directly! We recommend programming for a particular sensor rather than sending each sensor value once.

Step 4. Deploy

Then we connect all the nodes and click on the Deploy button and if everything is set up correctly you will be able to see the mqtt in node showing connected.

If you want to see the logging information of the data, you can add a debug node after the function node.

Once the Wio Terminal starts powering up and working and starts sending data to SenseCAP PaaS server, then we can check the data on Node-RED debug log.

If all goes well, you will also receive a text message from Twilio for you with the sensor's data values.

Troubleshoot

Q1: Why can't I find the Palette in Node-RED?

A: If you cannot find the Palette in the settings, check your terminal for an error message when you start Node-RED.

The most common scenario is that your npm version is too old to start the Palette editor.

If your situation is as described above, run Powershell as administrator and enter the following command to upgrade npm.

npm install -g npm

Then just restart Node-RED.

Tech Support & Product Discussion

Thank you for choosing our products! We are here to provide you with different support to ensure that your experience with our products is as smooth as possible. We offer several communication channels to cater to different preferences and needs.

Loading Comments...