{"id":44,"date":"2021-01-08T23:28:26","date_gmt":"2021-01-09T04:28:26","guid":{"rendered":"https:\/\/pressbooks.bccampus.ca\/vahidnia\/?post_type=chapter&#038;p=44"},"modified":"2023-05-24T12:44:28","modified_gmt":"2023-05-24T16:44:28","slug":"chapter-5-chapter-5-mqtt-function","status":"publish","type":"chapter","link":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/chapter\/chapter-5-chapter-5-mqtt-function\/","title":{"raw":"Chapter 5: MQTT Function","rendered":"Chapter 5: MQTT Function"},"content":{"raw":"<h1>5.1 Introduction<\/h1>\r\nIn this chapter, we would like a BG96 board to act as a MQTT client and publish its data with a specific topic to a broker. To build a broker, we use an architecture similar to the one used in Chapter 3. The broker is built on a computer by using Node-RED[footnote]https:\/\/nodered.org\/[\/footnote] programming tool to wire together hardware devices, and online services to establish a communication with the BG96 module through MQTT protocol.\r\n\r\nThe data published on the broker will be sent to all the MQTT clients that have subscribed to the topic. To have another MQTT client, we use a MQTT client software on the same computer. The configuration used in this lab is shown in Figure 5.1. The MQTT client on the computer also can publish a message to MQTT broker that can be forwarded to BG96 board.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n[caption id=\"attachment_91\" align=\"aligncenter\" width=\"875\"]<img class=\"wp-image-91 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module.jpg\" alt=\"\" width=\"875\" height=\"225\" \/> Figure5.1. MQTT broker and clients using the BG96 module[\/caption]\r\n<h1>5.2 MQTT Broker<\/h1>\r\nIn Project A of this lab, we try to build the MQTT-broker and a MQTT client on a computer. In project B, we set up the BG96 board to become a MQTT client and publish the data. Since the computer is behind a NAT router and uses internal IP address, the router needs to have been setup properly and we should do port forwarding for Port 1880 and 1883.\r\n<h2>A.\u00a0 Install and run Node-RED<\/h2>\r\n<ol>\r\n \t<li>Download and install the recommended version of the JavaScript runtime Node.js[footnote]https:\/\/nodejs.org\/en\/#home-downloadhead[\/footnote]<\/li>\r\n \t<li>Open Command Prompt and type in the following code to install Node-RED as a global module including its all dependencies:<\/li>\r\n<\/ol>\r\n<p style=\"text-align: center\"><strong><em>npm install -g --unsafe-perm node-red<\/em><\/strong><\/p>\r\n\r\n<ol start=\"3\">\r\n \t<li>Once installed, in the Command Prompt type in <strong><em>Node-red<\/em><\/strong> to start Node-RED. If you want to stop Node-RED you can press Ctrl-C or close the terminal window.<\/li>\r\n \t<li>Access the Node-RED editor by entering <a href=\"http:\/\/localhost:1880\">http:\/\/localhost:1880<\/a> at your browser\u2019s address bar. To get familiar with Node-Red, you can watch Intro to Node-RED: Part 1 fundamentals[footnote]https:\/\/www.youtube.com\/watch?v=3AR432bguOY[\/footnote].<\/li>\r\n \t<li>Go to Settings -&gt; Palette-&gt;Install and search for \u201cnode-red-contrib-mqtt-broker\u201d to install Mosca MQTT broker (server).<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_92\" align=\"aligncenter\" width=\"672\"]<img class=\"wp-image-92 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/35..gif\" alt=\"\" width=\"672\" height=\"255\" \/> Figure5.2. Installing Mosca MQTT broker[\/caption]\r\n<ol start=\"6\">\r\n \t<li>From the left side pallet drag and drop the following nodes to form the MQTT communication and set the settings according to Table5.1.<\/li>\r\n<\/ol>\r\n<p style=\"text-align: center\">Table5.1. Node description and settings:1<\/p>\r\n<img class=\"wp-image-134 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61.jpg\" alt=\"\" width=\"534\" height=\"235\" \/>\r\n<ol start=\"7\">\r\n \t<li>Connect the nodes as shown below. Before deploying the flow, using the same port forwarding process as you did in the previous lab, make sure that you have access to Port 1880 and 1883. To make sure that everything is set up correctly, you should download and install MQTT.fx[footnote]https:\/\/mqttfx.jensd.de\/[\/footnote] to use as a MQTT client.<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_93\" align=\"aligncenter\" width=\"538\"]<img class=\"wp-image-93 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/36.-MQTT-clients-and-broker-nodes-interconnection.jpg\" alt=\"\" width=\"538\" height=\"243\" \/> Figure5.3. MQTT clients and broker nodes interconnection[\/caption]\r\n<h2>B.\u00a0 MQTT function of Quectel BG96 module through AT commands<\/h2>\r\nIn Lab 2, you learned how to obtain the public IP address of your computer (e.g., 172.218.32.19). Also, as it was shown in Table5.1, the MQTT port was set as 1883 (TCP\/IP port 1883 is reserved with\u00a0\u00a0Internet Assigned Numbers Authority (IANA) for MQTT) and the topic was chosen as \u201csensor\u201d, i.e., any device that subscribes to this topic will receive the messages published with this topic. If you refer to BG96 MQTT Application Note[footnote]https:\/\/www.quectel.com\/UploadImage\/Downlad\/Quectel_BG96_MQTT_Application_Note_V1.1.pdf[\/footnote], you can find the AT Commands to publish\/subscribe to a specific topic.\r\n<ol start=\"8\">\r\n \t<li>Replace your own public IP address and enter the following AT Commands. You should be able to see the message in the debug slide bar of Node-RED (press the button to access).<\/li>\r\n<\/ol>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>AT+QMTOPEN=0,\" 172.218.32.19 \",1883\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Open a network for MQTT client. Use your own IP<\/em>\r\n\r\nOK\r\n\r\n&nbsp;\r\n\r\nQMTOPEN: 0,0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/Opened the MQTT client network successfully<\/em>\r\n\r\n<strong>AT+QMTCONN=0,\u201cMyClientID\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Connect a client to MQTT server. Use any client ID<\/em>\r\n\r\nOK\r\n\r\n&nbsp;\r\n\r\nQMTCONN: 0,0,0\r\n\r\n<strong>AT+QMTPUB=0,0,0,0,\u201csensor\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Publish a message to topic \u201csensor\u201d<\/em>\r\n\r\n<strong>&gt;Hello MQTT. This is my message to publish.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Press CTR Z to send<\/em>\r\n\r\nOK\r\n\r\n&nbsp;\r\n\r\nQMTPUB: 0,0,0<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<ol start=\"9\">\r\n \t<li>Enter the following AT Command to subscribe the BG96 module to the \u201csensor\u201d topic. Then, in Node-RED, inject timestamp or any string. You should see the received message on the BG96 module. You may use <strong>AT QMTDISC=0<\/strong> AT Command to disconnect the client from the MQTT server.<\/li>\r\n<\/ol>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>AT+QMTSUB=0,1,\u201csensor\u201d,2<\/strong>\r\n\r\nOK\r\n\r\n&nbsp;\r\n\r\nQMTSUB: 0,1,0,1\r\n\r\nQMTRECV: 0,2,\"sensor\",\"1583885887680\"\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/ This shows the received timestamp<\/em>\r\n\r\n<em>\u00a0<\/em>\r\n\r\n<strong>AT+QMTDISC=0<\/strong>\r\n\r\n&nbsp;\r\n\r\nQMTDISC: 0,0\r\n\r\n&nbsp;<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<ol start=\"10\">\r\n \t<li>From the left side palette and under the \u201cstorage\u201d nodes, drag and drop the \u201cfile\u201d node. This is where you can store the data received from module. You can add a \u201cfunction\u201d node to format the message and add a timestamp before storing the message in your file. Also, install the \u201cEmail\u201d node, then drag and drop it. You will use this node to send a notification if a specific message is received (e.g., if the temperature is above a certain number or if the string is \u201cHello\u201d). Table5.2 summarizes the description and required settings for these three nodes. If you are using Gmail, go to your \u201caccount preferences\u201d make sure that \u201cAllow less secure apps\u201d is enabled.<\/li>\r\n<\/ol>\r\n<p style=\"text-align: center\">Table5.2. Node description and settings:1<\/p>\r\n<img class=\"wp-image-113 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7.jpg\" alt=\"\" width=\"569\" height=\"185\" \/>\r\n\r\nThe flow looks like the one depicted in Figure5.4.\r\n\r\n[caption id=\"attachment_94\" align=\"aligncenter\" width=\"352\"]<img class=\"wp-image-94 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/37.-The-flow-with-all-necessary-nodes.jpg\" alt=\"\" width=\"352\" height=\"291\" \/> Figure5.4. The flow with all necessary nodes[\/caption]\r\n<ol start=\"11\">\r\n \t<li>Go to Hamburger Menu -&gt; Export to generate the \u201c*.json\u201d file for future use.<\/li>\r\n<\/ol>","rendered":"<h1>5.1 Introduction<\/h1>\n<p>In this chapter, we would like a BG96 board to act as a MQTT client and publish its data with a specific topic to a broker. To build a broker, we use an architecture similar to the one used in Chapter 3. The broker is built on a computer by using Node-RED<a class=\"footnote\" title=\"https:\/\/nodered.org\/\" id=\"return-footnote-44-1\" href=\"#footnote-44-1\" aria-label=\"Footnote 1\"><sup class=\"footnote\">[1]<\/sup><\/a> programming tool to wire together hardware devices, and online services to establish a communication with the BG96 module through MQTT protocol.<\/p>\n<p>The data published on the broker will be sent to all the MQTT clients that have subscribed to the topic. To have another MQTT client, we use a MQTT client software on the same computer. The configuration used in this lab is shown in Figure 5.1. The MQTT client on the computer also can publish a message to MQTT broker that can be forwarded to BG96 board.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_91\" aria-describedby=\"caption-attachment-91\" style=\"width: 875px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-91\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module.jpg\" alt=\"\" width=\"875\" height=\"225\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module.jpg 2284w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-300x77.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-1024x263.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-768x197.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-1536x395.jpg 1536w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-2048x526.jpg 2048w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-65x17.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-225x58.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/34.-MQTT-broker-and-clients-using-the-BG96-module-350x90.jpg 350w\" sizes=\"auto, (max-width: 875px) 100vw, 875px\" \/><figcaption id=\"caption-attachment-91\" class=\"wp-caption-text\">Figure5.1. MQTT broker and clients using the BG96 module<\/figcaption><\/figure>\n<h1>5.2 MQTT Broker<\/h1>\n<p>In Project A of this lab, we try to build the MQTT-broker and a MQTT client on a computer. In project B, we set up the BG96 board to become a MQTT client and publish the data. Since the computer is behind a NAT router and uses internal IP address, the router needs to have been setup properly and we should do port forwarding for Port 1880 and 1883.<\/p>\n<h2>A.\u00a0 Install and run Node-RED<\/h2>\n<ol>\n<li>Download and install the recommended version of the JavaScript runtime Node.js<a class=\"footnote\" title=\"https:\/\/nodejs.org\/en\/#home-downloadhead\" id=\"return-footnote-44-2\" href=\"#footnote-44-2\" aria-label=\"Footnote 2\"><sup class=\"footnote\">[2]<\/sup><\/a><\/li>\n<li>Open Command Prompt and type in the following code to install Node-RED as a global module including its all dependencies:<\/li>\n<\/ol>\n<p style=\"text-align: center\"><strong><em>npm install -g &#8211;unsafe-perm node-red<\/em><\/strong><\/p>\n<ol start=\"3\">\n<li>Once installed, in the Command Prompt type in <strong><em>Node-red<\/em><\/strong> to start Node-RED. If you want to stop Node-RED you can press Ctrl-C or close the terminal window.<\/li>\n<li>Access the Node-RED editor by entering <a href=\"http:\/\/localhost:1880\">http:\/\/localhost:1880<\/a> at your browser\u2019s address bar. To get familiar with Node-Red, you can watch Intro to Node-RED: Part 1 fundamentals<a class=\"footnote\" title=\"https:\/\/www.youtube.com\/watch?v=3AR432bguOY\" id=\"return-footnote-44-3\" href=\"#footnote-44-3\" aria-label=\"Footnote 3\"><sup class=\"footnote\">[3]<\/sup><\/a>.<\/li>\n<li>Go to Settings -&gt; Palette-&gt;Install and search for \u201cnode-red-contrib-mqtt-broker\u201d to install Mosca MQTT broker (server).<\/li>\n<\/ol>\n<figure id=\"attachment_92\" aria-describedby=\"caption-attachment-92\" style=\"width: 672px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-92\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/35..gif\" alt=\"\" width=\"672\" height=\"255\" \/><figcaption id=\"caption-attachment-92\" class=\"wp-caption-text\">Figure5.2. Installing Mosca MQTT broker<\/figcaption><\/figure>\n<ol start=\"6\">\n<li>From the left side pallet drag and drop the following nodes to form the MQTT communication and set the settings according to Table5.1.<\/li>\n<\/ol>\n<p style=\"text-align: center\">Table5.1. Node description and settings:1<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-134\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61.jpg\" alt=\"\" width=\"534\" height=\"235\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61.jpg 1952w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61-300x132.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61-1024x450.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61-768x338.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61-1536x675.jpg 1536w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61-65x29.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61-225x99.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T61-350x154.jpg 350w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/p>\n<ol start=\"7\">\n<li>Connect the nodes as shown below. Before deploying the flow, using the same port forwarding process as you did in the previous lab, make sure that you have access to Port 1880 and 1883. To make sure that everything is set up correctly, you should download and install MQTT.fx<a class=\"footnote\" title=\"https:\/\/mqttfx.jensd.de\/\" id=\"return-footnote-44-4\" href=\"#footnote-44-4\" aria-label=\"Footnote 4\"><sup class=\"footnote\">[4]<\/sup><\/a> to use as a MQTT client.<\/li>\n<\/ol>\n<figure id=\"attachment_93\" aria-describedby=\"caption-attachment-93\" style=\"width: 538px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-93\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/36.-MQTT-clients-and-broker-nodes-interconnection.jpg\" alt=\"\" width=\"538\" height=\"243\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/36.-MQTT-clients-and-broker-nodes-interconnection.jpg 506w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/36.-MQTT-clients-and-broker-nodes-interconnection-300x136.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/36.-MQTT-clients-and-broker-nodes-interconnection-65x29.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/36.-MQTT-clients-and-broker-nodes-interconnection-225x102.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/36.-MQTT-clients-and-broker-nodes-interconnection-350x158.jpg 350w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><figcaption id=\"caption-attachment-93\" class=\"wp-caption-text\">Figure5.3. MQTT clients and broker nodes interconnection<\/figcaption><\/figure>\n<h2>B.\u00a0 MQTT function of Quectel BG96 module through AT commands<\/h2>\n<p>In Lab 2, you learned how to obtain the public IP address of your computer (e.g., 172.218.32.19). Also, as it was shown in Table5.1, the MQTT port was set as 1883 (TCP\/IP port 1883 is reserved with\u00a0\u00a0Internet Assigned Numbers Authority (IANA) for MQTT) and the topic was chosen as \u201csensor\u201d, i.e., any device that subscribes to this topic will receive the messages published with this topic. If you refer to BG96 MQTT Application Note<a class=\"footnote\" title=\"https:\/\/www.quectel.com\/UploadImage\/Downlad\/Quectel_BG96_MQTT_Application_Note_V1.1.pdf\" id=\"return-footnote-44-5\" href=\"#footnote-44-5\" aria-label=\"Footnote 5\"><sup class=\"footnote\">[5]<\/sup><\/a>, you can find the AT Commands to publish\/subscribe to a specific topic.<\/p>\n<ol start=\"8\">\n<li>Replace your own public IP address and enter the following AT Commands. You should be able to see the message in the debug slide bar of Node-RED (press the button to access).<\/li>\n<\/ol>\n<table>\n<tbody>\n<tr>\n<td><strong>AT+QMTOPEN=0,&#8221; 172.218.32.19 &#8220;,1883\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Open a network for MQTT client. Use your own IP<\/em><\/p>\n<p>OK<\/p>\n<p>&nbsp;<\/p>\n<p>QMTOPEN: 0,0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/Opened the MQTT client network successfully<\/em><\/p>\n<p><strong>AT+QMTCONN=0,\u201cMyClientID\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Connect a client to MQTT server. Use any client ID<\/em><\/p>\n<p>OK<\/p>\n<p>&nbsp;<\/p>\n<p>QMTCONN: 0,0,0<\/p>\n<p><strong>AT+QMTPUB=0,0,0,0,\u201csensor\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Publish a message to topic \u201csensor\u201d<\/em><\/p>\n<p><strong>&gt;Hello MQTT. This is my message to publish.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Press CTR Z to send<\/em><\/p>\n<p>OK<\/p>\n<p>&nbsp;<\/p>\n<p>QMTPUB: 0,0,0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol start=\"9\">\n<li>Enter the following AT Command to subscribe the BG96 module to the \u201csensor\u201d topic. Then, in Node-RED, inject timestamp or any string. You should see the received message on the BG96 module. You may use <strong>AT QMTDISC=0<\/strong> AT Command to disconnect the client from the MQTT server.<\/li>\n<\/ol>\n<table>\n<tbody>\n<tr>\n<td><strong>AT+QMTSUB=0,1,\u201csensor\u201d,2<\/strong><\/p>\n<p>OK<\/p>\n<p>&nbsp;<\/p>\n<p>QMTSUB: 0,1,0,1<\/p>\n<p>QMTRECV: 0,2,&#8221;sensor&#8221;,&#8221;1583885887680&#8243;\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/ This shows the received timestamp<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><strong>AT+QMTDISC=0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>QMTDISC: 0,0<\/p>\n<p>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol start=\"10\">\n<li>From the left side palette and under the \u201cstorage\u201d nodes, drag and drop the \u201cfile\u201d node. This is where you can store the data received from module. You can add a \u201cfunction\u201d node to format the message and add a timestamp before storing the message in your file. Also, install the \u201cEmail\u201d node, then drag and drop it. You will use this node to send a notification if a specific message is received (e.g., if the temperature is above a certain number or if the string is \u201cHello\u201d). Table5.2 summarizes the description and required settings for these three nodes. If you are using Gmail, go to your \u201caccount preferences\u201d make sure that \u201cAllow less secure apps\u201d is enabled.<\/li>\n<\/ol>\n<p style=\"text-align: center\">Table5.2. Node description and settings:1<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-113\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7.jpg\" alt=\"\" width=\"569\" height=\"185\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7.jpg 999w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7-300x98.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7-768x250.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7-65x21.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7-225x73.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T7-350x114.jpg 350w\" sizes=\"auto, (max-width: 569px) 100vw, 569px\" \/><\/p>\n<p>The flow looks like the one depicted in Figure5.4.<\/p>\n<figure id=\"attachment_94\" aria-describedby=\"caption-attachment-94\" style=\"width: 352px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-94\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/37.-The-flow-with-all-necessary-nodes.jpg\" alt=\"\" width=\"352\" height=\"291\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/37.-The-flow-with-all-necessary-nodes.jpg 579w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/37.-The-flow-with-all-necessary-nodes-300x248.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/37.-The-flow-with-all-necessary-nodes-65x54.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/37.-The-flow-with-all-necessary-nodes-225x186.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/37.-The-flow-with-all-necessary-nodes-350x290.jpg 350w\" sizes=\"auto, (max-width: 352px) 100vw, 352px\" \/><figcaption id=\"caption-attachment-94\" class=\"wp-caption-text\">Figure5.4. The flow with all necessary nodes<\/figcaption><\/figure>\n<ol start=\"11\">\n<li>Go to Hamburger Menu -&gt; Export to generate the \u201c*.json\u201d file for future use.<\/li>\n<\/ol>\n<hr class=\"before-footnotes clear\" \/><div class=\"footnotes\"><ol><li id=\"footnote-44-1\">https:\/\/nodered.org\/ <a href=\"#return-footnote-44-1\" class=\"return-footnote\" aria-label=\"Return to footnote 1\">&crarr;<\/a><\/li><li id=\"footnote-44-2\">https:\/\/nodejs.org\/en\/#home-downloadhead <a href=\"#return-footnote-44-2\" class=\"return-footnote\" aria-label=\"Return to footnote 2\">&crarr;<\/a><\/li><li id=\"footnote-44-3\">https:\/\/www.youtube.com\/watch?v=3AR432bguOY <a href=\"#return-footnote-44-3\" class=\"return-footnote\" aria-label=\"Return to footnote 3\">&crarr;<\/a><\/li><li id=\"footnote-44-4\">https:\/\/mqttfx.jensd.de\/ <a href=\"#return-footnote-44-4\" class=\"return-footnote\" aria-label=\"Return to footnote 4\">&crarr;<\/a><\/li><li id=\"footnote-44-5\">https:\/\/www.quectel.com\/UploadImage\/Downlad\/Quectel_BG96_MQTT_Application_Note_V1.1.pdf <a href=\"#return-footnote-44-5\" class=\"return-footnote\" aria-label=\"Return to footnote 5\">&crarr;<\/a><\/li><\/ol><\/div>","protected":false},"author":197,"menu_order":5,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-44","chapter","type-chapter","status-publish","hentry"],"part":3,"_links":{"self":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/users\/197"}],"version-history":[{"count":18,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/44\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/44\/revisions\/309"}],"part":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/44\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapter-type?post=44"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/contributor?post=44"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/license?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}