IoTFlows
  • Welcome to IoTFlows Docs
  • IoTFlows
  • Console
  • REST API
  • Device Management & Secure Remote Access
    • 1. Connect a device to the cloud or create a virtual device
    • 2. SSH to your device over the internet
    • 3. Transfer files to your device over the internet
    • 4. Remotely access Node-RED on your device
    • 5. Create public links for local web-apps
  • Real-Time Data Streams, Alerts, and Actions
    • Create a Device API Key
    • Publish and subscribe to real-time messages with Node-RED
    • Creating an Alert Channel
    • Publish an Alert with Node-RED
    • Define and execute an Action in Node-RED
    • Python real-time SDK
    • Javascript real-time SDK
  • Cloud Node-RED Servers
    • Create a secure Cloud Node-RED Server in five minutes
    • Subscribe to data streams with a Cloud Node-RED Server
    • Integrate data streams to Node-RED dashboard
    • Integrate data streams to Slack
  • IOTFLOWS OVERVIEW
    • IoTFlows Roles and Permissions
    • Adding Team Members
  • Example Projects
    • Raspberry Pi as Gateway
      • Arduino to Raspberry Pi Serial Communication for Ultrasonic Sensor
    • Getting Started with Node-Red Dashboard
      • Installing Node-Red Dashboard
      • Accessing Node-Red Dashboard
      • 1. Creating a Page
      • 2. Adding Gauge Nodes
      • 3. Changing Dashboard Themes
      • 4. Adding Data to Gauge Nodes
      • 5. Adding a Chart Node
      • 6. Adding Data to Chart Node
      • 7. Adding a Template Node
      • 8. Adding Data to Template Node
      • 9. Adding Switch Node
      • 10. Adding Logic to Switch Node
      • 11. Adding a Slider Node
      • 12. Adding a Color Picker Node
      • 13. Adding a Numeric Node
      • 14. Adding a Button Node
      • 15. Adding Logic to a Button Node
      • Node-Red Dashboard Code
    • Publishing Data Streams from Python
    • Ultrasonic Sensor on Raspberry Pi Node-Red
  • General
    • Downloading IoTFlows Node-Red Nodes
    • Publishing Alert from Node-Red Manual
    • Publishing Data Stream from Node-Red Manual
Powered by GitBook
On this page
  • Connect Ultrasonic Sensor
  • Sensing with Node-Red
  • Publish Data to IoTFlows
  • Publishing an Alert

Was this helpful?

  1. Example Projects

Ultrasonic Sensor on Raspberry Pi Node-Red

PreviousPublishing Data Streams from PythonNextDownloading IoTFlows Node-Red Nodes

Last updated 4 years ago

Was this helpful?

In this example, we will show you how to

  1. Connect an HC-SR04 Ultrasonic sensor to your Raspberry Pi

  2. Sensing through Node-Red

  3. Publish data to IoTFlows console

  4. Publish Alert to IoTFlows Alert Channel

To complete this project you will need:

Hardware:

  • HC-SR04 Ultrasonic sensor

  • Raspberry Pi

  • Jumper Wires

Software:

  • IoTFlows Platform

  • Node-Red

Connect Ultrasonic Sensor

The HC-SR04 Ultrasonic sensor has 4 pins: ground (GND), Echo Pulse Output (ECHO), Trigger Pulse Input (TRIG), and 5V Supply (Vcc). As shown below

We will use the jumper wires to connect these 4 pins to the Raspberry pins GPIO 5V [Pin 2], GPIO GND [Pin 6], GPIO 23 [Pin 16], GPIO 24 [Pin 18].

  1. Plug four of your male to female jumper wires into the pins on the HC-SR04.

  2. Plug the Vcc jumper wire into GPIO 5V [Pin 2] of the Pi.

  3. Plug the GND jumper wire into GPIO GND [Pin 6] of the Pi.

  4. Plug the TRIG jumper wire into GPIO 23 [Pin 16] of the Pi.

  5. Plug the ECHO jumper wire into GPIO 24 [Pin 18] of the Pi.

IMPORTANT: The sensor output signal (ECHO) on the HC-SR04 is rated at 5V. However, the input pin on the Raspberry Pi GPIO is rated at 3.3V. Sending a 5V signal into that unprotected 3.3V input port could damage your GPIO pins, which is something we want to avoid! We recommend you to use a small voltage divider circuit, to lower the sensor output voltage to something our Raspberry Pi can handle. For simplicity, we will skip this step.

You are done! You've connected your HC-SR04 Ultrasonic sensor to your Raspberry Pi.

Sensing with Node-Red

Now that we've hooked our Ultrasonic sensor to our Pi, we need to program Node-Red to detect distance.

In your Node-Red console, make sure you have the node for a Raspberry Pi to use a SRF04 or SRF05 range finder

node-red-node-pisrf

After installation is complete, you will see the following rpi srf node in your console

Drag and drop the rpi srf node in your canvas and configure it as follows

Click Done and that is it! you can also add a debug node to view the incoming data on your Node-Red console.

Click on the debug node and select node status (32 characters) for the values to be displayed below that node

Publish Data to IoTFlows

Create a Data Stream

In your device, create a data stream for your Ultrasonic Sensor. For more details on how to create a data stream go to Creating a Data Stream.

We will label the data stream the following:

  • Data Stream Name: Ultrasonic

  • Data Stream Description: Ultrasonic Sensor example

  • Data Stream Units: cm

Integrate Data Stream Node

Double-click on the datastream out node and click on the edit button for the server

Here we will add the client that will be used to publish data into our table. If you do not have a client set up or need more information on setting up a client go to Creating a Device Client.

Type in your Client Id and Password, and click Add.

Your client should now be displayed as your server.

Next, we will click the refresh button in the Topic row and select our Ultrasonic sensor

Click Done.

Next, connect your Pi SRF node to your datastream out node and click Deploy

Now your data stream node is connected and we are receiving data into our IoTFlows data streams table, as shown below.

Publishing an Alert

Creating Alert Logic

In order to trigger an alert, we will set up a simple function that tells us when the Ultrasonic sensor reaches a value of over 350.

First, we set up a function node to determine if the value has reached over 350. We will name it "is > 350?"

if(msg.payload > 350)
{
    return msg;    
}

Next, we will set up a delay node in order to not get spammed by messages when that value is reached and stayed there continuously.

Next, we will configure the delay node, this is a personal preference so adjust parameters to what fits you best.

This will limit the number of messages we receive to just 1 message per minute and it will ignore the rest.

Click Done.

Configuring Alert Message

Next, we will set up another function node to alert us that the value has reached over 350. We will name this "Ultrasonic Alert Message"

msg.payload = 
{
    "severity_level": "MINOR",
    "subject": "Distance over reached",
    "description":  "Ultrasonic sensor distance is over 350"
};
return msg;

Click Done.

Integrate Alert Channel Node

Double-click on the alert channel out node and similar to the data streams node, we will double click on the edit button for the Server and configure our Client Id and Password. If you do not have a client set up or need more information on setting up a client go to Creating a Device Client.

Next, we will click the refresh button in the Topic and select the Alert Channel that we will like to publish to.

Once everything is complete, click Done.

Next, we will connect all the nodes and click Deploy

As you can see, the alert channel out node is now connected.

We can now go to our Alert Channel in the IoTFlows platform and view the alert message

Next, we will publish data into our IoTFlows data streams table. To do this we will Node-Red IoTFlows nodes, if you do not have these installed, go to for installation details.

Now that we have a data stream created for our Ultrasonic sensor, we will drag and drop the datastream out node into our canvas. If you do not have this installed, go to for installation details.

Next, we will publish an Alert into our IoTFlows Alerts table and send an email and text message after this alert is triggered. To do this we will use the Node-Red IoTFlows nodes, if you do not have these installed, go to for installation details.

Now that we have everything set up to publish our alert, we will drag and drop the alert channel out node into our canvas. If you do not have this installed, go to for installation details.

If you would like to receive text messages or email with these alert messages, turn on your notifications in your Manage Alert Channel Notifications. For more details on configuring notifications, go to .

IoTFlows Node-Red Nodes
IoTFlows Node-Red Nodes
IoTFlows Node-Red Nodes
IoTFlows Node-Red Nodes
Manage Alert Channel Notifications