How to Use Node-RED for IoT: Simple Guide and Example
Use
Node-RED to visually create IoT workflows by connecting device inputs, processing data, and sending outputs using drag-and-drop nodes. Install Node-RED, add IoT device nodes like MQTT, and build flows to collect and control IoT data without coding.Syntax
Node-RED flows are built by connecting nodes that represent inputs, outputs, and processing steps. Each node has configuration options and passes messages to the next node.
Input nodes: Receive data from IoT devices (e.g., MQTT, HTTP)Function nodes: Process or transform data using JavaScriptOutput nodes: Send data to devices, databases, or dashboards
Flows run continuously, handling messages as they arrive.
json
/* Example Node-RED flow JSON snippet */ [ { "id": "mqtt_in", "type": "mqtt in", "name": "Receive Sensor Data", "topic": "sensor/temperature", "broker": "mqtt_broker" }, { "id": "function_process", "type": "function", "name": "Convert to Celsius", "func": "msg.payload = (msg.payload - 32) * 5 / 9;\nreturn msg;" }, { "id": "debug_out", "type": "debug", "name": "Show Output" } ]
Example
This example shows a simple Node-RED flow that reads temperature data from an MQTT topic, converts it from Fahrenheit to Celsius, and outputs the result to the debug console.
json
[
{
"id": "mqtt_in",
"type": "mqtt in",
"z": "flow1",
"name": "Receive Sensor Data",
"topic": "sensor/temperature",
"qos": "2",
"datatype": "auto",
"broker": "mqtt_broker",
"x": 150,
"y": 100,
"wires": [["function_process"]]
},
{
"id": "function_process",
"type": "function",
"z": "flow1",
"name": "Convert to Celsius",
"func": "msg.payload = (msg.payload - 32) * 5 / 9;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 350,
"y": 100,
"wires": [["debug_out"]]
},
{
"id": "debug_out",
"type": "debug",
"z": "flow1",
"name": "Show Output",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 550,
"y": 100,
"wires": []
},
{
"id": "mqtt_broker",
"type": "mqtt-broker",
"name": "Local MQTT Broker",
"broker": "localhost",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
}
]Output
When a message with payload 77 (Fahrenheit) arrives on topic 'sensor/temperature', the debug output shows 25 (Celsius).
Common Pitfalls
- Not configuring MQTT broker correctly: Ensure the broker address and port match your MQTT server.
- Incorrect topic names: MQTT topics are case-sensitive and must match exactly.
- Function node errors: JavaScript errors in function nodes stop message flow; test code carefully.
- Not deploying flows: After changes, always click
Deployto activate the flow.
drone_programming
/* Wrong: MQTT broker set to wrong address */ { "broker": "wrong-address", "port": "1883" } /* Right: Correct local broker address */ { "broker": "localhost", "port": "1883" }
Quick Reference
Node Types:
mqtt in: Receive messages from MQTT topicsmqtt out: Send messages to MQTT topicsfunction: Run JavaScript to process messagesdebug: Output messages to debug sidebar
Basic Steps:
- Install Node-RED and start it
- Add MQTT broker configuration
- Create flows by connecting nodes
- Deploy and test your flows
Key Takeaways
Node-RED uses visual flows to connect IoT devices and process data without coding.
MQTT nodes are essential for IoT device communication in Node-RED.
Always configure your MQTT broker and topics correctly to avoid connection issues.
Use function nodes to transform or analyze IoT data with JavaScript.
Deploy your flows after changes to activate them.