[{"data":1,"prerenderedAt":351},["ShallowReactive",2],{"node-red-\u002Fnode-red\u002Fprotocol\u002Fmqtt":3},{"id":4,"title":5,"body":6,"description":343,"extension":344,"meta":345,"navigation":346,"path":347,"seo":348,"stem":349,"__hash__":350},"nodeRed\u002Fnode-red\u002Fprotocol\u002Fmqtt.md","Using MQTT with Node-RED",{"type":7,"value":8,"toc":334},"minimark",[9,17,21,26,36,39,47,51,59,63,66,104,107,118,122,125,175,183,189,192,201,205,208,273,279,287,291,294,331],[10,11,13],"h1",{"id":12},"",[14,15],"binding",{"value":16},"meta.title",[18,19,20],"p",{},"Getting devices to talk to each other in industrial environments isn't trivial. You're dealing with spotty networks, power constraints, and devices that need to share data without constant back-and-forth polling. MQTT solves these problems by keeping communication lightweight and flexible. This guide walks you through what MQTT is, why it works well for IIoT, and how to get it running with Node-RED.",[22,23,25],"h2",{"id":24},"understanding-mqtt","Understanding MQTT",[18,27,28,35],{},[29,30,34],"a",{"href":31,"rel":32},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FMQTT",[33],"nofollow","MQTT"," is a messaging protocol that's been around since 1999. It uses a publish-subscribe model, which is different from the request-response pattern you see with HTTP.",[18,37,38],{},"Here's the basic setup: devices\u002Fsystems (clients) connect to a central server called a broker. When a device has data to share, it publishes a message to a specific topic. Other devices\u002Fsystems subscribe to topics they care about and automatically receive messages when they're published. The broker handles all the routing.",[18,40,41,42,46],{},"This architecture means devices\u002Fsystems don't need to know about each other or maintain direct connections. A temperature sensor can publish readings to ",[43,44,45],"code",{},"enterprise\u002Fsite\u002Farea\u002Fline\u002Fcell\u002Ftemperature"," without caring who's listening.",[22,48,50],{"id":49},"setting-up-mqtt-in-node-red","Setting Up MQTT in Node-RED",[18,52,53,54,58],{},"Node-RED comes with MQTT nodes built in, so you don't need to install anything extra. You'll need access to an MQTT broker - you can use a cloud service, run your own with Mosquitto, use a public test broker for experimenting, or if you're using FlowFuse, it provides a ",[29,55,57],{"href":56},"\u002Fblog\u002F2024\u002F10\u002Fannouncement-mqtt-broker\u002F","built-in MQTT broker service",".",[22,60,62],{"id":61},"configuring-the-mqtt-broker-connection","Configuring the MQTT Broker Connection",[18,64,65],{},"Before you can publish or subscribe to messages, you need to configure the broker connection. You only need to do this once - the same broker configuration can be reused across multiple MQTT nodes.",[67,68,69,82,85,88,95,98,101],"ol",{},[70,71,72,73,77,78,81],"li",{},"Add either an ",[74,75,76],"strong",{},"MQTT out"," or ",[74,79,80],{},"MQTT in"," node to your canvas",[70,83,84],{},"Double-click the node to open its configuration",[70,86,87],{},"Click the pencil icon next to \"Server\" to add a new broker connection",[70,89,90,91,94],{},"Enter your broker's address (e.g., ",[43,92,93],{},"broker.flowfuse.cloud",")",[70,96,97],{},"Add the port (usually 1883 for unencrypted, 8883 for TLS)",[70,99,100],{},"If your broker requires authentication, switch to the Security tab in the same configuration dialog by clicking on it \"Security\", then enter your username and password",[70,102,103],{},"Give the connection a name and click Add",[18,105,106],{},"Once configured, this broker connection will appear in the Server dropdown for all MQTT nodes in your flow.",[18,108,109,114],{},[110,111],"img",{"alt":112,"src":113},"Configuring an MQTT broker in Node-RED","\u002Fnode-red-media\u002Fprotocol\u002Fimages\u002Fmqtt-broker-config.png",[115,116,117],"em",{},"Setting up the broker connection",[22,119,121],{"id":120},"publishing-messages-to-a-broker","Publishing Messages to a Broker",[18,123,124],{},"Let's start by sending data to an MQTT broker.",[67,126,127,134,139,144,151,158,169],{},[70,128,129,130,133],{},"Add an ",[74,131,132],{},"MQTT Out"," node.",[70,135,136,137,133],{},"Connect your data source node’s output (use an Inject node to simulate data if you don’t have one) to the ",[74,138,132],{},[70,140,141,142,133],{},"Double-click the ",[74,143,132],{},[70,145,146,147,150],{},"Select your configured broker from the ",[74,148,149],{},"Server"," dropdown (or create a new one by following the steps above).",[70,152,153,154,157],{},"Enter a topic such as ",[43,155,156],{},"enterprise\u002Fsite\u002Farea\u002Fline1\u002Fcell\u002Ftemperature"," (topics use forward slashes as separators, similar to file paths).",[70,159,160,161,164,165,168],{},"Set the ",[74,162,163],{},"QoS"," level if needed for message reliability, and enable ",[74,166,167],{},"Retain"," if you want the broker to store the last published message for new subscribers.",[70,170,171,172,58],{},"Click ",[74,173,174],{},"Done",[18,176,177,181],{},[110,178],{"alt":179,"src":180},"MQTT Out node — publishing data to a topic","\u002Fnode-red-media\u002Fprotocol\u002Fimages\u002Fmqtt-out.png",[115,182,179],{},[67,184,186],{"start":185},9,[70,187,188],{},"Click Deploy",[18,190,191],{},"The MQTT out node should show \"connected\" with a green dot.",[18,193,194,198],{},[110,195],{"alt":196,"src":197},"Connected MQTT node showing green status","\u002Fnode-red-media\u002Fprotocol\u002Fimages\u002Fconnected-mqtt-node.png",[115,199,200],{},"Green status means you're connected",[22,202,204],{"id":203},"subscribing-to-messages-from-a-broker","Subscribing to Messages from a Broker",[18,206,207],{},"Now let's receive messages from the MQTT broker.",[67,209,210,216,222,230,234,239,248,259,264],{},[70,211,129,212,215],{},[74,213,214],{},"MQTT In"," node to the canvas.",[70,217,218,219,133],{},"Add a ",[74,220,221],{},"Debug",[70,223,224,225,227,228,133],{},"Connect the ",[74,226,214],{}," node to the ",[74,229,221],{},[70,231,141,232,133],{},[74,233,214],{},[70,235,146,236,238],{},[74,237,149],{}," dropdown (or create a new one if needed).",[70,240,160,241,244,245,58],{},[74,242,243],{},"Action"," to ",[74,246,247],{},"Subscribe to a single topic",[70,249,250,251,254,255,258],{},"Enter the topic you want to subscribe to, for example: ",[43,252,253],{},"enterprise\u002Fsite\u002Farea\u002F+\u002Fcell\u002Ftemperature"," (the ",[43,256,257],{},"+"," symbol acts as a wildcard for one level).",[70,260,160,261,263],{},[74,262,163],{}," level based on your reliability requirements.",[70,265,171,266,268,269,272],{},[74,267,174],{},", then ",[74,270,271],{},"Deploy"," your flow.",[18,274,275,276,278],{},"Once deployed, you should see the messages appear in the ",[74,277,221],{}," sidebar.",[18,280,281,285],{},[110,282],{"alt":283,"src":284},"MQTT In node — subscribing to a topic","\u002Fnode-red-media\u002Fprotocol\u002Fimages\u002Fmqtt-in-config.png",[115,286,283],{},[22,288,290],{"id":289},"using-wildcards-in-topics","Using Wildcards in Topics",[18,292,293],{},"MQTT supports wildcards for subscribing to multiple topics at once:",[295,296,297,316],"ul",{},[70,298,299,302,303,305,306,308,309,312,313],{},[74,300,301],{},"Single-level wildcard (+)",": Matches one level. ",[43,304,253],{}," matches ",[43,307,156],{}," and ",[43,310,311],{},"enterprise\u002Fsite\u002Farea\u002Fline2\u002Fcell\u002Ftemperature"," but not ",[43,314,315],{},"enterprise\u002Fsite\u002Farea\u002Fline1\u002Fcell\u002Fstation1\u002Ftemperature",[70,317,318,321,322,325,326,308,328],{},[74,319,320],{},"Multi-level wildcard (#)",": Matches multiple levels. ",[43,323,324],{},"enterprise\u002Fsite\u002Farea\u002F#"," matches everything under that area, including ",[43,327,156],{},[43,329,330],{},"enterprise\u002Fsite\u002Farea\u002Fline1\u002Fcell\u002Fstation1\u002Fpressure",[18,332,333],{},"When deployed you should again see the status bubble turn green, and have a\ntimestamp appear in the sidebar every second!",{"title":12,"searchDepth":335,"depth":335,"links":336},2,[337,338,339,340,341,342],{"id":24,"depth":335,"text":25},{"id":49,"depth":335,"text":50},{"id":61,"depth":335,"text":62},{"id":120,"depth":335,"text":121},{"id":203,"depth":335,"text":204},{"id":289,"depth":335,"text":290},"Learn how to use MQTT with Node-RED.","md",{},true,"\u002Fnode-red\u002Fprotocol\u002Fmqtt",{"title":5,"description":343},"node-red\u002Fprotocol\u002Fmqtt","PFQo-rg0Ftjeu8y8RBcryX79SKbBiXwJMyichKdasZo",1780070557895]