How to Build an IoT Sensor Project with Node-RED, InfluxDB, and Raspberry Pi

Navigate to:

Imagine you need to build a real-time IoT monitoring project—quickly, easily, and with reliability in mind. Does that feel overwhelming? If so, you’re not alone. Many people find IoT projects daunting because they involve many moving parts. From setting up hardware like Raspberry Pi and sensors to configuring wired or wireless connections, every step presents its own challenges. Then, you need to collect and store real-time data in a time-series database, like InfluxDB, and query that data for monitoring and analytics.

One way to do this is by using a good deal of coding and configuration with various libraries and SDKs. But what if you could set up a complete IoT system without writing a single line of code? That’s where Node-RED comes in. In this article, we’ll walk you through building a simple IoT project using Raspberry Pi and sensors, storing the data in InfluxDB Cloud, and visualizing it in a Node-RED dashboard. Along the way, you’ll learn key concepts and principles that can be applied to any real-time IoT data project.

Hardware Setup

  1. Raspberry Pi (3/4/5 or newer) with peripherals: We’ll be using a Raspberry Pi running Raspbian OS, which will act as the brain of our project. Ensure that it is set up and configured for remote development via SSH. Here is the official guide for setting up your Pi for development.
  2. GPIO HAT (optional): This add-on simplifies working with the Raspberry Pi’s GPIO pins, though it’s optional for this project.
  3. Breadboard: Prototyping board to create our circuit and lay wires and components.
  4. Ultrasonic sensor: We’ll use an ultrasonic sensor to measure distance and set a threshold value that, when breached, will turn the LED light on.
  5. LED light: One LED light will be used for basic visual feedback in our project.
  6. Resistors: 3 x 1kΩ resistors for ultrasonic sensor & 1 x 220Ω resistor for LED light.
  7. 5 jumper wires: A breadboard will hold the components, and we’ll connect everything using six jumper wires to ensure smooth signal flow.

Software Setup

  1. InfluxDB Cloud: Our sensor data will be stored in InfluxDB Cloud, a time-series database ideal for real-time data storage and access from anywhere. Cloud-based solutions offer scalability, remote access, and automated maintenance, allowing you to focus on your project without worrying about local setup complexities. You can sign up for free if you don’t have an account. Once you have an account, take note of the following details:
    1. Cloud URL: The URL is in your InfluxDB Cloud portal and will be the endpoint for sending and retrieving data.
    2. API token: This token will allow the Raspberry Pi to write data to your InfluxDB bucket. Make sure it has “read/write” permissions.
    3. Org name: Your organization name, found in the InfluxDB Cloud portal, is required for API requests.
    4. Bucket name: The bucket acts as your database in InfluxDB. You’ll need the bucket name to store sensor data.
  2. Node-RED: Node-RED is a visual programming tool built on Node.js, which is ideal for wiring together hardware devices, APIs, and online services. It can run on pretty much any hardware and OS as long as it supports Node.js runtime. Since it’s built on Node.js, you’ll need to have Node.js installed on your Raspberry Pi before installing Node-RED.

Update, Download, and Install Node.js and Node-RED

sudo apt update

sudo apt install nodejs npm

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Start Node RED

node-red-start

You can also start the Node-RED service on the Raspberry Pi OS Desktop by selecting the Menu -> Programming -> Node-RED menu option. It is also a good practice to secure access to Node-RED, which can be done by following their official guide.

Other helpful commands for Node-RED:

  • node-red-stop: this stops the Node-RED service
  • node-red-restart: this stops and restarts the Node-RED service
  • node-red-log: this displays the log output of the service

If you are using the browser on the Pi desktop, you can open the address http://localhost:1880 and login with your admin credentials.

When browsing from another machine, such as over ssh, you should use the hostname or IP address of the Raspberry Pi: http://<hostname>:1880, and you will be able to see the NodeRED editor on your machine.

You can find the IP address of Raspberry Pi by running the following command:

hostname -I

Setup Node-RED

We will connect Raspberry Pi with sensors using GPIO pins and have the sensor data stored in InfluxDB on the cloud, for which we need two node modules installed on Node-RED. You can easily do that from the “Manage Pallete” menu and installing the following:

  • node-red-node-pi-gpio (for Raspberry Pi GPIO)
  • node-red-contrib-influxdb (for InfluxDB connection)

Write Node-RED Programs/Flows

Let’s start programming using Node-RED. The programs in Node-RED are called “Flows” because they are based on the visual dataflow programming paradigm, where you connect “nodes” using cables and edit their logic.

Flow 1: Hello LED

Now that we have everything set up and node-RED started, let’s create our first program that will simply turn an LED on/off from Node-RED and save the data in the InfluxDB Cloud bucket. Here, we grab two “inject” nodes of type boolean, make one “true” and the other “false,” and connect them to a “rpi - gpio out” node. What we have now is a simple login. When we press on the true/false inject node, a signal is sent via Raspberry Pi’s GPIO to power the LED on/off.

Hardware Setup

  • Connect the LED with Raspberry Pi on the breadboard using GPIO pins:
    • Longer leg (anode) to GPIO 17 via a 1K resistor
    • Shorter leg (cathode) to GND pin

Node-RED Flow Setup

  • Open Node-RED in your browser (typically http://localhost:1880)
  • Add nodes:
    • Drag two inject nodes from the palette
    • Drag two rpi gpio out nodes
    • Drag one influxdb v3 node
    • Drag one debug node
  • Configure inject nodes:
    • Double-click the first inject node
      • Set payload type to boolean
      • Set payload to true
      • Name it “Turn On”
    • Double-click the second inject node
      • Set payload type to boolean
      • Set payload to false
      • Name it “Turn Off”
  • Configure GPIO nodes:
    • Double-click the first GPIO node
      • Select pin 17
      • Name it “Red LED”
  • Configure InfluxDB node:
    • Double-click the InfluxDB node
    • Click the pencil icon to add a new InfluxDB connection
      • URL: Your InfluxDB URL
      • Organization: Your org name
      • Token: Your API token
      • Query Mode: SQL Query
      • Click “Update”
  • Wire the nodes:
    • Connect both inject nodes to both GPIO node
    • Also, connect them to the InfluxDB node
  • Final touches:
    • Add a function node between inject and InfluxDB nodes
    • Configure the function node:
return {

    payload: `INSERT INTO led\_status (time, led\_red, led\_green) 

              VALUES (NOW(), ${msg.payload}, ${msg.payload});`

};

Flow 2: Hello SensorData

Building on the previous work in this flow, let’s connect the Ultrasonic distance sensor with three 1K Ohm resistors, as shown in the diagram below. The Ultrasonic sensor can detect distance from any object, such as a person in front of it. The ultrasonic sensor emits ultrasound waves and detects the wave reflected from the target. This information calculates distance using the classic mathematical formula: distance = speed of sound x (time/2) to calculate the distance of the object placed in front. You can imagine the many possibilities of such sensors in home and industrial IoT settings, from security to anti-collision detection in vehicles.

Hardware Setup

  • Connect your Ultrasonic sensor (like HC-SR04) to your Raspberry Pi using GPIO pins as follows:
    • Connect VCC to 5V pin
    • Connect GND to Ground pin
    • Connect TRIG to GPIO 23 (Pin 16) pin
    • Connect ECHO to GPIO 24 (Pin 18) pin
    • Connect 3 1K resistor in series
    • Connect ECHO to first resistor
    • Connect GPIO 24 to second resistor
    • Connect Ground to third resistor

Node-RED Flow Setup

  1. Drag an inject node (for timer)
  2. Drag two rpi gpio nodes (one in, one out)
  3. Drag two function nodes
    1. Drag an influxdb v3 node
    2. Drag a debug node
  4. Configure nodes:
    1. a. Timer (inject) node:
      • Set repeat interval to 2 seconds
      • Name it “Trigger Timer”

    b. GPIO nodes:

Configure output node:

  • Select pin 23
  • Name it “TRIG”

Configure input node:

  • Select pin 24
  • Set input type to “tri-state”
  • Name it “ECHO”

c. First function node (name it “Trigger Sequence”):

// Set trigger high
context.set('triggerState', 1);
node.send({payload: 1});

// Set trigger low after 10 microseconds
setTimeout(() => {
    context.set('triggerState', 0);
    node.send({payload: 0});
}, 0.01);

d. Second function node (name it “Calculate Distance”):

const duration = msg.payload;
const distance = duration \* 17150 / 1000000;

return {
    payload: `INSERT INTO distance\_measurements (time, distance) 

              VALUES (NOW(), ${distance.toFixed(2)});`
};

e. InfluxDB node:

  • Use the same configuration as Flow 1 with to wire the nodes:
    • Timer -> Trigger Sequence -> TRIG
    • ECHO -> Calculate Distance -> InfluxDB -> Debug``

Testing the Flow

  • Click “Deploy” in the top-right corner.
  • Watch the debug sidebar for distance measurements.
  • Open your InfluxData Cloud dashboard to verify data is being recorded.

Congratulations! If you have gotten this far and everything worked, you have successfully integrated two IoT projects with Raspberry Pi and sensors using Node-Red and are writing data to InfluxDB. This is just the beginning. I hope this inspires you to explore possibilities with IoT. Keep an eye out for future blogs where we will build a more complex setup with nice UI and data visualization for real-time monitoring purposes, all using the same technologies.

Happy building! We’d love to hear about your progress, so feel free to share your creations in our community or on social media.