{"id":1358,"date":"2016-01-21T13:29:33","date_gmt":"2016-01-21T21:29:33","guid":{"rendered":"http:\/\/internetofhomethings.com\/homethings\/?p=1358"},"modified":"2016-01-21T13:37:47","modified_gmt":"2016-01-21T21:37:47","slug":"mqtt-for-app-inventor-adding-configurable-settings","status":"publish","type":"post","link":"https:\/\/internetofhomethings.com\/homethings\/?p=1358","title":{"rendered":"MQTT for App Inventor &#8211; Adding Configurable Settings"},"content":{"rendered":"<h2 style=\"text-align: center;\"><strong>App Inventor to MQTT Communication<\/strong><\/h2>\n<p>In my <a href=\"http:\/\/wp.me\/p5NRQ8-lf\" target=\"_blank\">last post<\/a>, a basic MQTT client portal for App Inventor\u00a0was introduced. As result, there has been some interest expressed for TCP socket support. Note that the\u00a0implementation presented relies on WebSockets. Due to security vulnerabilities, pure TCP sockets are simply not possible with this design. If TCP sockets are needed for MQTT communication at your IoT device, a broker capable of both WebSockets and TCP sockets should be used &#8211; Websockets on the App Inventor side, and TCP sockets on the IoT device end.<\/p>\n<h2 style=\"text-align: center;\"><strong>Adding Configurable Setting<\/strong><\/h2>\n<p>My\u00a0next step was to refine the <a href=\"http:\/\/wp.me\/p5NRQ8-lf\" target=\"_blank\">App Inventor project<\/a> to support configurable MQTT setting.<\/p>\n<p><strong>NOTE: If you just want to review and use the updated App Inventor project without the walk-through\u00a0that follows, simply use the files provided in <a href=\"https:\/\/github.com\/internetofhomethings\/Configurable-Web-Server\/tree\/master\/AppInventor_CfgMQTT\" target=\"_blank\">this Github\u00a0repository<\/a>.<\/strong><\/p>\n<p>The first challenge while adding this capability was the inability to grant file system access to the JavaScript code. Native Android Apps can overcome this with a change to the App&#8217;s manifest file. But App Inventor does not expose this application setting.<\/p>\n<p>So the App Inventor file component had to be used to read and write to the file system. The configuration file settings are then passed on to the JavaScript to update the MQTT connection parameters.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/mqtt_cfg.jpg\" rel=\"attachment wp-att-1364\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1364\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/mqtt_cfg-1024x791.jpg\" alt=\"mqtt_cfg\" width=\"620\" height=\"479\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/mqtt_cfg-1024x791.jpg 1024w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/mqtt_cfg-300x232.jpg 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/mqtt_cfg-768x593.jpg 768w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/mqtt_cfg.jpg 1056w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<h2 style=\"text-align: center;\"><strong>App File Location<\/strong><\/h2>\n<p>This App now uses two\u00a0files stored on the Android device. These files have been moved to a common location which should be the same\u00a0on most Android devices:<\/p>\n<p>[wptg_comparison_table id=&#8221;15&#8243;]<\/p>\n<h2 style=\"text-align: center;\"><strong>Configuration file<\/strong><\/h2>\n<p>For this example, 3 parameters are configurable:<\/p>\n<ol>\n<li>Broker: This can be any on-line broker, on your own network or either publicly or privately provided.<\/li>\n<li>Request Topic:\u00a0MQTT topic used to send a client request<\/li>\n<li>Reply Topic: MQTT topic used to reply results to the client.<\/li>\n<\/ol>\n<p>Since this example uses the publicly available broker &#8216;test.mosquitto.org&#8217;, username\/password authentication is not provided. You could easily be add this to the example provided here, however, if you are using a broker requiring authentication.<\/p>\n<p>The file is formatted as a JSON string. This makes it very easy for the JavaScript to parse the values. The first time the App is run, these configurable parameters are set to the values shown in this JSON string:<\/p>\n<p>{<br \/>\n&#8220;mqtt_broker&#8221;:&#8221;ws:\/\/test.mosquitto.org:8080\/mqtt&#8221;,<br \/>\n&#8220;mqtt_txtopic&#8221;:&#8221;MyMqttSvrRqst&#8221;,<br \/>\n&#8220;mqtt_rxtopic&#8221;:&#8221;MyMqttSvrRply&#8221;<br \/>\n}<\/p>\n<h2 style=\"text-align: center;\"><strong>App Inventor File Read\/Write<\/strong><\/h2>\n<p>When the screen used to edit the configurable parameters is open, it first reads the cfg.txt file, extracts the parameter setting, and uses those values to populate the screen fields.<\/p>\n<p>But when using App Inventor, there are no (at least none that I am aware) libraries available for parsing JSON. So I created the procedure &#8220;GetParam&#8221;, which extracts the cfg.txt values with 3 calls, one for each parameter.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/readfile.jpg\" rel=\"attachment wp-att-1365\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1365\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/readfile.jpg\" alt=\"readfile\" width=\"374\" height=\"708\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/readfile.jpg 374w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/readfile-158x300.jpg 158w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/a><\/p>\n<p>Two values are determined initially\u00a0in order to extract the value. First is the position in the string that the value starts. The other value is the string length. These values are determined by using App Inventor&#8217;s string search (starts at) feature to find something unique (StartPiece and LenPiece) around the value wanted, and an offset from the parameter value&#8217;s exact position in the string.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/getparam.jpg\" rel=\"attachment wp-att-1366\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1366\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/getparam.jpg\" alt=\"getparam\" width=\"857\" height=\"577\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/getparam.jpg 857w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/getparam-300x202.jpg 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/getparam-768x517.jpg 768w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/a><\/p>\n<p>Once extracted, the &#8220;Edit Configuration&#8221; screen is populated with the values from this file.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_gui-1.jpg\" rel=\"attachment wp-att-1369\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1369\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_gui-1.jpg\" alt=\"cfg_gui\" width=\"337\" height=\"600\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_gui-1.jpg 337w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_gui-1-169x300.jpg 169w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/a><\/p>\n<p>Saving the values is just the opposite. The values from the screen are formatted into a JSON string and then saved back to the same file, cfg.txt.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/save.jpg\" rel=\"attachment wp-att-1370\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1370\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/save.jpg\" alt=\"save\" width=\"511\" height=\"386\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/save.jpg 511w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/save-300x227.jpg 300w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/a><\/p>\n<p>When this screen is exited, the new configuration values are sent to the JavaScript code. The JavaScript uses these new values and closes, then opens the MQTT connection with the new values. Here is how that has been implemented&#8230;<\/p>\n<h2 style=\"text-align: center;\"><strong>Updated Main Screen<\/strong><\/h2>\n<p>As I have gained knowledge coding with the App Inventor, it became obvious that meaningful names were needed for project components. Much to my surprise, this does not extend to the main screen. It is called &#8220;Screen1&#8221; by default, and cannot be changed.<\/p>\n<p>The screen GUI was changed somewhat for this update. Two new buttons were added. One to open the configuration screen and one to exit the application. The components were also move to the bottom of the screen (don&#8217;t want to cover the mosquito, do we?).<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/main.png\" rel=\"attachment wp-att-1372\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1372\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/main.png\" alt=\"main\" width=\"300\" height=\"533\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/main.png 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/main-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Every time the main screen is open, it&#8217;s 100 ms timer restarts. That is where the command\u00a0is sent to the JavaScript to update the configuration settings.<\/p>\n<p>Once.<\/p>\n<p>It is sent 1\/2 second (500 ms) after the screen is open. <strong>An important note:<\/strong> I found that the command did not execute if initiated concurrently with the opening of the screen. So the 500 ms delay (5 timer iterations) was introduced using a timer, which works every time.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer.jpg\" rel=\"attachment wp-att-1374\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1374\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer.jpg\" alt=\"timer\" width=\"506\" height=\"365\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer.jpg 506w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer-300x216.jpg 300w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/a><\/p>\n<p>At 500 ms, the cfg.txt file is read. &#8220;CFG:&#8221; is inserted at the beginning of the string before being sent to JavaScript via the WebViewString.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/sendcfg.jpg\" rel=\"attachment wp-att-1375\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1375\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/sendcfg.jpg\" alt=\"sendcfg\" width=\"570\" height=\"235\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/sendcfg.jpg 570w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/sendcfg-300x124.jpg 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n<p>A handler is also included for the first time this application is run. In that case, the cfg.txt file does not exists. As result, an exception is thrown when attempting to &#8220;ReadFrom&#8221;\u00a0the file. An error handler was added for this condition.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/error2.jpg\" rel=\"attachment wp-att-1387\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1387\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/error2.jpg\" alt=\"error2\" width=\"687\" height=\"398\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/error2.jpg 687w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/error2-300x174.jpg 300w\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" \/><\/a><\/p>\n<p>Error 2101 occurs when the file does not exist. In this case, a file is created with default\u00a0values for the configurable parameters.<\/p>\n<h2 style=\"text-align: center;\"><strong>JavaScript for Configurable MQTT<\/strong><\/h2>\n<p>The JavaScript also required revisions to support MQTT configurable parameters. The original version only handled IoT requests. These were identified with &#8216;GET:&#8217; as the first 4 characters. Similarly, the added configuration command is now recognized as WebViewStrings beginning\u00a0with &#8220;CFG:&#8221;<\/p>\n<pre class=\"easycode; title:;lang:;\">\/\/\u00a010\u00a0Hz\u00a0WebViewString\u00a0polling\u00a0timer\u00a0function\u00a0--------------------------------------&gt;\r\nfunction\u00a0AppInventorServer()\u00a0{\r\n    var\u00a0request\u00a0=\u00a0window.AppInventor.getWebViewString();\u00a0\/\/\u00a0Get\u00a0WebViewString\r\n\u00a0\u00a0\u00a0\u00a0if(request.substring(0,\u00a04)==\"GET:\")\u00a0{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Validate\u00a0request\r\n\u00a0\u00a0\u00a0\u00a0    window.AppInventor.setWebViewString(\"\");\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Reset\u00a0String\u00a0(process\u00a0once)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$(\"#request\").val(request.substring(4,\u00a0request.length));\u00a0\/\/set\u00a0request\u00a0html\u00a0textbox\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SendMqttRequest();\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\/\/\u00a0Send\u00a0Mqtt\u00a0Request\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0if(request.substring(0,\u00a04)==\"CFG:\")\u00a0{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Validate\u00a0request\r\n\u00a0\u00a0\u00a0\u00a0    window.AppInventor.setWebViewString(\"\");\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Reset\u00a0String\u00a0(process\u00a0once)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var\u00a0cfgpar\u00a0=\u00a0JSON.parse(request.substring(4,\u00a0request.length));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0txtopic\u00a0=\u00a0cfgpar.mqtt_txtopic;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0rxtopic\u00a0=\u00a0cfgpar.mqtt_rxtopic;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mqtturl\u00a0=\u00a0cfgpar.mqtt_broker;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0client.disconnect();\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0setTimeout(AppInventorServer,\u00a0100);\u00a0\u00a0\u00a0\/\/\u00a0run\u00a0AppInventorServer()\u00a0in\u00a0100\u00a0ms\r\n}<\/pre>\n<p>As you can see, parsing and extracting values from a JSON string is very simple and straight-forward with JavaScript. Once the new values are set, the MQTT broker connection is updated with a call to &#8216;client.disconnect()&#8217;.<\/p>\n<pre class=\"easycode; title:;lang:;\">\/\/\u00a0Callback\u00a0executed\u00a0upon\u00a0MQTT\u00a0broker\u00a0disconnection\u00a0--------------------------------&gt;\r\nclient.ondisconnect\u00a0=\u00a0function(rc){\r\n    client.connect(mqtturl);\r\n};\r\n\/\/ Callback executed upon MQTT broker connection -----------------------------------&gt;\r\nclient.onconnect = function(rc){\r\n    client.subscribe(rxtopic, 0);\r\n};\r\n\r\n<\/pre>\n<h2 style=\"text-align: center;\"><strong>Improving the Arduino Digital Commands<\/strong><\/h2>\n<p>The original project used hard-coded values for the Digital Get and Set commands. Ok for an example, but not so good if you need access to a channel outside the card-coded value. That was rectified with this update, which now supports digital channel selection.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/dig_new2.jpg\" rel=\"attachment wp-att-1377\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1377\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/dig_new2.jpg\" alt=\"dig_new2\" width=\"480\" height=\"427\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/dig_new2.jpg 480w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/dig_new2-300x267.jpg 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><\/p>\n<p>Select &#8220;Set Arduino Digital Channel&#8221; and a new screen is open. This screen allows you to select the channel to set.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/digital_sel-1.jpg\" rel=\"attachment wp-att-1394\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1394\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/digital_sel-1.jpg\" alt=\"digital_sel\" width=\"300\" height=\"533\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/digital_sel-1.jpg 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/digital_sel-1-169x300.jpg 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This screen is open with a &#8220;start value&#8221; received from the main screen that called it.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/select_digit.jpg\" rel=\"attachment wp-att-1380\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1380\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/select_digit.jpg\" alt=\"select_digit\" width=\"821\" height=\"398\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/select_digit.jpg 821w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/select_digit-300x145.jpg 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/select_digit-768x372.jpg 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/a><\/p>\n<p>The value returned when OK is clicked depends on the startValue. If it is a &#8220;Set&#8221; command, an Arduino SetDigital request string is returned. The selected channel and logic state from the screen&#8217;s selections are also returned with the request.<\/p>\n<p>Notice that a call to close the screen is made before the main screen (Screen1) is re-opened. That is necessary when returning a startValue. If the screen is not closed, it remains open for the duration of the App execution.<\/p>\n<p>Another call to this window will open another instance of the window. This is effectively a memory leak which will crash you Android device eventually. Takeaway: Close the screen before opening a new screen when passing a start value to avoid memory leaks.<\/p>\n<p>As you can see, a similar value is returned for &#8220;Get&#8221; requests.\u00a0But when &#8220;Get&#8221; requests are received, the channel state is not selected, it is returned from the request. When this screen is open with a &#8220;Get&#8221; request, the channel state selection GUI is suppressed.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/get_digit.jpg\" rel=\"attachment wp-att-1381\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1381\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/get_digit.jpg\" alt=\"get_digit\" width=\"458\" height=\"157\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/get_digit.jpg 458w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/get_digit-300x103.jpg 300w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/a><\/p>\n<h2 style=\"text-align: center;\"><strong>Sending the Arduino Digital\u00a0Request<\/strong><\/h2>\n<p>When the Digital channel selection screen is closed, the main screen is reopened. But how does it know to send the Arduino command to MQTT? After all, the screen is opened fresh, just like when the app is started.<\/p>\n<p>Not quite. Remember the &#8216;startValue&#8217; returned from the digital selection? That is checked to determine whether an arduino request is needed. It is done in the 100ms timer callback.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer.jpg\" rel=\"attachment wp-att-1374\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1374\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer.jpg\" alt=\"timer\" width=\"506\" height=\"365\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer.jpg 506w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/timer-300x216.jpg 300w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/a><\/p>\n<p>At 500 ms after the screen is open, the MQTT configuration is sent to the JavaScript. But then, at 1000 ms (10 100 ms intervals), the startval string is check for a non-null value. If a string is present, it is sent to the JavaScript via the WebViewString, by calling the SendRequest procedure.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/send_request.jpg\" rel=\"attachment wp-att-1383\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1383\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/send_request.jpg\" alt=\"send_request\" width=\"579\" height=\"100\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/send_request.jpg 579w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/send_request-300x52.jpg 300w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/a><\/p>\n<h2 style=\"text-align: center;\"><strong>Exiting the Application<\/strong><\/h2>\n<p>Just one more thing and we are done with this update. I thought it would be nice to add an &#8216;Exit&#8217; button to close the app. What I found was that it required several clicks before the app would actually close. What was happening was that every time a different screen returned to the main screen, the previous main screen remained open and a new main screen instance was created.<\/p>\n<p>Not exactly what I had in mind. Another memory leak!<\/p>\n<p>This was corrected by adding a &#8216;close screen&#8217; before each time one of the two added screens was open. This guarantees only one screen is open at a time while the app is running.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_mqttt.jpg\" rel=\"attachment wp-att-1384\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1384\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_mqttt.jpg\" alt=\"cfg_mqttt\" width=\"431\" height=\"94\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_mqttt.jpg 431w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2016\/01\/cfg_mqttt-300x65.jpg 300w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><\/a><\/p>\n<p>With this correction, the Exit button works as intended, with only one click required.<\/p>\n<h2 style=\"text-align: center;\"><strong>In Closing<\/strong><\/h2>\n<p>This example should provide a framework for anyone needing to use MQTT with an App Inventor project. The main caveat is that a broker that supports WebSockets is required. I leave it up to you as an exercise to add username and password credentials to the mqtt connection settings. The basic structure is here. And it should also be a simple task to add &#8220;GetAnalog&#8221; to the Arduino request options. Go ahead, you can do it! If you need it.<\/p>\n<p><a href=\"https:\/\/github.com\/internetofhomethings\/Configurable-Web-Server\/tree\/master\/AppInventor_CfgMQTT\">Again, here\u00a0are the project files.<\/a><\/p>\n<p>I hope you find this information useful&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>App Inventor to MQTT Communication In my last post, a basic MQTT client portal for App Inventor\u00a0was introduced. As result, there has been some interest expressed for TCP socket support. &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,64,10,19],"tags":[109,110],"class_list":["post-1358","post","type-post","status-publish","format-standard","hentry","category-alltheposts","category-arduino","category-esp8266","category-internet-of-things","tag-app-inventor-configurable-mqtt","tag-configuring-app-inventor"],"_links":{"self":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/1358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1358"}],"version-history":[{"count":18,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/1358\/revisions"}],"predecessor-version":[{"id":1397,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/1358\/revisions\/1397"}],"wp:attachment":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}