{"id":12798,"date":"2022-07-05T05:29:11","date_gmt":"2022-07-05T11:29:11","guid":{"rendered":"http:\/\/fallows.ca\/wp\/?p=12798"},"modified":"2022-06-11T13:06:48","modified_gmt":"2022-06-11T19:06:48","slug":"iot-controller-command-protocol","status":"publish","type":"post","link":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/","title":{"rendered":"IoT Controller Command Protocol"},"content":{"rendered":"<p><a href=\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12799\" src=\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg\" alt=\"IoT controller command protocol\" width=\"304\" height=\"268\" srcset=\"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg 304w, https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol-300x264.jpg 300w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/a><\/p>\n<p>Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof. <!--more--><\/p>\n<p>Remember the old days when you needed to type commands to you computer? Commands were specific text strings. (I guess Linux still works this way.) Anyway, even if you use a GUI, a mouse click or screen swipe still results in a text command under the hood. You just don&#8217;t see it.<\/p>\n<p>Simply put, a protocol is a formal way to control behaviors. You can design an IoT controller command protocol by defining a restricted set of text strings. These, and only these, tell your Arduino or NodeMCU what to do. Here is command protocol for my relay switch.<\/p>\n<pre class=\"brush: cpp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/* Command Variables *\/\r\nString CommandStrings&#x5B;] = { &quot;C&quot;, &quot;X&quot;, &quot;?&quot;, &quot;R&quot;, &quot;F&quot;, &quot;M1&quot;, &quot;M2&quot; };\r\nenum CommandIDs\r\n{\r\nsetControlOn, \/\/ C Set controller ON\r\nsetControlOff, \/\/ X Set controller OFF\r\nsetReportStatus,\/\/ ? or ?=0..4 request status report\r\nsetReceiver, \/\/ R=0..4 select one of four radios\r\nsetFilter, \/\/ F=0..1 insert Medium Wave filter\r\nsetLoopMode_1, \/\/ M1=1..4 to switch AAA-1C modes\r\nsetLoopMode_2 \/\/ M2=1..4\r\n};\r\n<\/pre>\n<p>Each of my commands is a one or two character text followed by an optional parameter, separated by an &#8220;=&#8221; sign. Each of my commands is then ended or delimited with a Line Feed, &#8220;\\n&#8221; or ASCII 10. Any other text arriving at the microcontroller outside of this restricted set is ignored.<\/p>\n<p>My IoT controller command protocol is supported by some important definitions.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: cpp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n#define COMMAND_ERROR_NONE 0\r\n#define COMMAND_ERROR_INVALID_COMMAND 1\r\n#define COMMAND_ERROR_BAD_PARAMETER 2\r\n#define COMMAND_ERROR_NOT_CONNECTED 3\r\n#define COMMAND_ERROR_BUSY 4\r\n#define PARAM_NULL 99999 \/\/ Magic number for null parameter\r\n#define COMMAND_COUNT 7 \/\/ Number of different commands\r\n<\/pre>\n<p>Every time a command is received, the controller sends back an error code. Obviously, you desire a &#8220;no error&#8221; response. A wide variety of technologies can be used to end and receive simple ASCII text for control.<\/p>\n<h2>IoT Controller Command Protocol Actions<\/h2>\n<p>Inside your code, you need a routine to parse commands and take appropriate actions. Here&#8217;s my approach.<\/p>\n<pre class=\"brush: cpp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nint ProcessCommands() {\r\nString Command;\r\nString Parameter;\r\nlong ParamValue = 0;\r\nint i;\r\nint CommandIndex = -1;\r\n\r\n\/* Pre process Buffer - Parse *\/\r\ni = (Buffer.indexOf(&quot;=&quot;));\r\nif (i == -1){\r\n  Command = Buffer;\r\n  Parameter = &quot;&quot;;}\r\nelse {\r\n  Command = Buffer.substring(0, i);\r\n  Parameter = Buffer.substring(i + 1);\r\n  }\r\n\r\n\/* Identify command index *\/\r\nfor (int k = 0; k &lt; COMMAND_COUNT; k++) {\r\n  if (Command.equals(CommandStrings&#x5B;k])) {\r\n    CommandIndex = k;\r\n    break;\r\n    }\r\n  }\r\n\r\nif (CommandIndex == -1) return COMMAND_ERROR_INVALID_COMMAND; \/\/E2\r\n\r\nif (Parameter == &quot;&quot;) {\r\n  ParamValue = PARAM_NULL;}\r\nelse {\r\n  ParamValue = Parameter.toInt();\r\n  }\r\n\r\nif ((!Status.Connected) &amp;&amp; (CommandIndex != 0)) {\r\n  return COMMAND_ERROR_NOT_CONNECTED; \/\/E1\r\n  }\r\n\r\nswitch (CommandIDs(CommandIndex))\r\n{\r\n  case setControlOn:\r\n    Respond(&quot;+&quot;);\r\n    delay(50);\r\n    Status.Connected = true;\r\n  break;\r\n  case setControlOff:\r\n    Status.Connected = false;\r\n    Initialize();\r\n    delay(50);\r\n    Respond(&quot;-&quot;);\r\n  break;\r\n  case setReportStatus:\r\n    if (ParamValue == PARAM_NULL) {\r\n      Respond(Report(0));}\r\n    else {\r\n      Respond(Report(ParamValue));\r\n    }\r\n  break;\r\n  case setReceiver:\r\n    if ((ParamValue &lt; 0) || (ParamValue &gt; 4)) {\r\n      return COMMAND_ERROR_BAD_PARAMETER;\r\n    }\r\n    SetReceiver(ParamValue);\r\n  break;\r\n  case setFilter:\r\n    if ((ParamValue &lt; 0) || (ParamValue &gt; 1)) {\r\n      return COMMAND_ERROR_BAD_PARAMETER;\r\n    }\r\n    SetFilter(ParamValue);\r\n  break;\r\n  case setLoopMode_1:\r\n    if ((ParamValue &lt; 0) || (ParamValue &gt; 3)) {\r\n      return COMMAND_ERROR_BAD_PARAMETER;\r\n    }\r\n    SetLoopMode(1, ParamValue);\r\n  break;\r\n  case setLoopMode_2:\r\n    if ((ParamValue &lt; 0) || (ParamValue &gt; 3)) {\r\n      return COMMAND_ERROR_BAD_PARAMETER;\r\n    }\r\n    SetLoopMode(2, ParamValue);\r\n  break;\r\n  }\r\nreturn COMMAND_ERROR_NONE\r\n}\r\n\u00a0<\/pre>\n<p>As you can see, the first part of this code is all about parsing and formatting, making sure the commands are proper. Then, the second part takes action based on what the command wants to do, and makes sure your parameters are within range. Actions are taken by a group of routines later in the program to turn relays on and off.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"IoT Controller Command Protocol #IoT #NodeMCU","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[6],"tags":[32,21],"series":[338],"class_list":["post-12798","post","type-post","status-publish","format-standard","hentry","category-software-projects","tag-iot","tag-nodemcu","series-anatomy-of-an-iot-controllers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>IoT Controller Command Protocol - Living On The Horizon<\/title>\n<meta name=\"description\" content=\"Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"IoT Controller Command Protocol - Living On The Horizon\" \/>\n<meta property=\"og:description\" content=\"Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/\" \/>\n<meta property=\"og:site_name\" content=\"Living On The Horizon\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-05T11:29:11+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg\" \/>\n<meta name=\"author\" content=\"John VE6EY\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@VE6EY\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John VE6EY\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/\",\"url\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/\",\"name\":\"IoT Controller Command Protocol - Living On The Horizon\",\"isPartOf\":{\"@id\":\"https:\/\/fallows.ca\/wp\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg\",\"datePublished\":\"2022-07-05T11:29:11+00:00\",\"author\":{\"@id\":\"https:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945\"},\"description\":\"Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof.\",\"breadcrumb\":{\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#primaryimage\",\"url\":\"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg\",\"contentUrl\":\"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg\",\"width\":304,\"height\":268,\"caption\":\"IoT controller command protocol\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fallows.ca\/wp\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Christmas\",\"item\":\"https:\/\/fallows.ca\/wp\/tag\/christmas\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"IoT Controller Command Protocol\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fallows.ca\/wp\/#website\",\"url\":\"https:\/\/fallows.ca\/wp\/\",\"name\":\"Living On The Horizon\",\"description\":\"Blogs and Stuff\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fallows.ca\/wp\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945\",\"name\":\"John VE6EY\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fallows.ca\/wp\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g\",\"caption\":\"John VE6EY\"},\"sameAs\":[\"https:\/\/x.com\/VE6EY\"],\"url\":\"https:\/\/fallows.ca\/wp\/author\/play\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"IoT Controller Command Protocol - Living On The Horizon","description":"Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/","og_locale":"en_US","og_type":"article","og_title":"IoT Controller Command Protocol - Living On The Horizon","og_description":"Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof.","og_url":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/","og_site_name":"Living On The Horizon","article_published_time":"2022-07-05T11:29:11+00:00","og_image":[{"url":"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg","type":"","width":"","height":""}],"author":"John VE6EY","twitter_card":"summary_large_image","twitter_creator":"@VE6EY","twitter_misc":{"Written by":"John VE6EY","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/","url":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/","name":"IoT Controller Command Protocol - Living On The Horizon","isPartOf":{"@id":"https:\/\/fallows.ca\/wp\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#primaryimage"},"image":{"@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#primaryimage"},"thumbnailUrl":"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg","datePublished":"2022-07-05T11:29:11+00:00","author":{"@id":"https:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945"},"description":"Designing and using an IoT controller command protocol is as simple as typing some text. Ideally, the system should be foolproof.","breadcrumb":{"@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#primaryimage","url":"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg","contentUrl":"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/command-protocol.jpg","width":304,"height":268,"caption":"IoT controller command protocol"},{"@type":"BreadcrumbList","@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/iot-controller-command-protocol\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fallows.ca\/wp\/"},{"@type":"ListItem","position":2,"name":"Christmas","item":"https:\/\/fallows.ca\/wp\/tag\/christmas\/"},{"@type":"ListItem","position":3,"name":"IoT Controller Command Protocol"}]},{"@type":"WebSite","@id":"https:\/\/fallows.ca\/wp\/#website","url":"https:\/\/fallows.ca\/wp\/","name":"Living On The Horizon","description":"Blogs and Stuff","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fallows.ca\/wp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945","name":"John VE6EY","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fallows.ca\/wp\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g","caption":"John VE6EY"},"sameAs":["https:\/\/x.com\/VE6EY"],"url":"https:\/\/fallows.ca\/wp\/author\/play\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6wKKr-3kq","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts\/12798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/comments?post=12798"}],"version-history":[{"count":3,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts\/12798\/revisions"}],"predecessor-version":[{"id":12826,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts\/12798\/revisions\/12826"}],"wp:attachment":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/media?parent=12798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/categories?post=12798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/tags?post=12798"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/series?post=12798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}