{"id":157,"date":"2018-07-25T20:34:24","date_gmt":"2018-07-25T20:34:24","guid":{"rendered":"http:\/\/techministry.blog\/?p=157"},"modified":"2018-07-25T20:34:24","modified_gmt":"2018-07-25T20:34:24","slug":"controlling-a-wifi-plug-over-the-network-using-tcp-messages","status":"publish","type":"post","link":"https:\/\/techministry.blog\/?p=157","title":{"rendered":"Controlling a Wifi Plug over the network using TCP Messages"},"content":{"rendered":"<p>We recently purchased some TP-Link HS100 Kasa Smart Wi-Fi Plugs to allow us to remotely turn on speaker amps to support our outdoor speaker systems. Turning the amps on each time by going to the physical amp location in the racks located all across the campus was very inconvenient, so this was a great upgrade.<\/p>\n<figure id=\"attachment_158\" aria-describedby=\"caption-attachment-158\" style=\"width: 298px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"  wp-image-158 aligncenter\" src=\"https:\/\/techministry.blog\/wp-content\/uploads\/2018\/07\/hs100us2-0-package_1508143441338j.jpg\" alt=\"HS100(US)2.0-package_1508143441338j.jpg\" width=\"298\" height=\"298\" \/><figcaption id=\"caption-attachment-158\" class=\"wp-caption-text\">This is what the plug looks like.<\/figcaption><\/figure>\n<p>It pairs with an app called Kasa to allow you to turn the outlets on and off pretty easily, however in our testing, we couldn&#8217;t get it to work with more than one smartphone. Plus, we didn&#8217;t want volunteers to have to install this app on their phone, or provide a phone just to run this app.<\/p>\n<p>The TP-Link protocol has been reverse engineered pretty well, so I had hopes that this would be a quick and easy project, and it was! The core protocol uses JSON to communicate, however it is obfuscated heavily to the user and you have to send a hexadecimal payload to it in the end in order to trigger an action.<\/p>\n<p>Once we set up the devices as recommended through the app to use our internal network WiFi, I had our IT department create IP address reservations based on the MAC addresses of the outlets, so that they would always have the same IP address. If you use these plugs, I recommend doing that in your setup as well.<\/p>\n<p>Then, I researched the protocol and figured out what the hexadecimal payloads needed to be to power it on and off. I didn&#8217;t really care about any of the other status settings. Just a simple on and off.<\/p>\n<p>Here is the function needed to do that, written in ogScript for Ross Dashboard:<\/p>\n<pre>function tellOutlet(address, port, protocol, command)\n{\n    var enabled = ogscript.getObject('outletEnabled');\n\n    if (enabled)\n    {\n        switch(protocol)\n        {\n            case \"TPLINK-HS100\":\n                var payload_on_hex = \"0000002AD0F281F88BFF9AF7D5EF94B6C5A0D48BF99CF091E8B7C4B0D1A5C0E2D8A381F286E793F6D4EEDFA2DFA2\";\n                var payload_off_hex = \"0000002AD0F281F88BFF9AF7D5EF94B6C5A0D48BF99CF091E8B7C4B0D1A5C0E2D8A381F286E793F6D4EEDEA3DEA3\";\n                  \n                var sendCommand = \"\";\n                  \n                if (command == \"on\")\n                {\n                    sendCommand = payload_on_hex;\n                }\n                else if (command == \"off\")\n                {\n                    sendCommand = payload_off_hex;\n                }\n                                 \n                rosstalk.sendAsBytes(address, port, sendCommand, callback);\n                ogscript.debug(\"Outlet turned \" + command + \".\");\n                break;\n            default:\n                break;\n        }\n    }\n}<\/pre>\n<p>The key function here is the RossTalk SendAsBytes command which sends the TCP message as hexadecimal data.<\/p>\n<p>Once I did a test and was satisfied it was working properly, I built out custom panels that use our master production control system, so now volunteers can easily turn the amps on or off by just clicking a button!<\/p>\n<p>I also built a stand-alone version in case anyone else could benefit from it.<\/p>\n<figure id=\"attachment_159\" aria-describedby=\"caption-attachment-159\" style=\"width: 402px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-159 aligncenter\" src=\"https:\/\/techministry.blog\/wp-content\/uploads\/2018\/07\/screen-shot-2018-07-25-at-4-06-54-pm.png\" alt=\"Screen Shot 2018-07-25 at 4.06.54 PM.png\" width=\"402\" height=\"294\" \/><figcaption id=\"caption-attachment-159\" class=\"wp-caption-text\">The buttons are first disabled when you open the panel until you&#8217;ve configured the outlet in the setup tab.<\/figcaption><\/figure>\n<figure id=\"attachment_160\" aria-describedby=\"caption-attachment-160\" style=\"width: 391px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-160 aligncenter\" src=\"https:\/\/techministry.blog\/wp-content\/uploads\/2018\/07\/screen-shot-2018-07-25-at-4-07-00-pm.png\" alt=\"Screen Shot 2018-07-25 at 4.07.00 PM.png\" width=\"391\" height=\"277\" \/><figcaption id=\"caption-attachment-160\" class=\"wp-caption-text\">In the setup tab, you can configure the outlet IP address, the port (the default is 9999), and choose the protocol. Currently, the panel only supports the TP-Link HS100\/101 models.<\/figcaption><\/figure>\n<figure id=\"attachment_161\" aria-describedby=\"caption-attachment-161\" style=\"width: 394px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-161 aligncenter\" src=\"https:\/\/techministry.blog\/wp-content\/uploads\/2018\/07\/screen-shot-2018-07-25-at-4-06-42-pm.png\" alt=\"Screen Shot 2018-07-25 at 4.06.42 PM.png\" width=\"394\" height=\"280\" \/><figcaption id=\"caption-attachment-161\" class=\"wp-caption-text\">Just click On or Off to toggle the outlet!<\/figcaption><\/figure>\n<p>The panel is up on my <a href=\"https:\/\/github.com\/josephdadams\/RossDashboardPanels\" target=\"_blank\" rel=\"noopener\">Github repository<\/a>, if you&#8217;d like to download it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We recently purchased some TP-Link HS100 Kasa Smart Wi-Fi Plugs to allow us to remotely turn on speaker amps to support our outdoor speaker systems. Turning the amps on each time by going to the physical amp location in the racks located all across the campus was very inconvenient, so this was a great upgrade. &hellip; <a href=\"https:\/\/techministry.blog\/?p=157\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Controlling a Wifi Plug over the network using TCP Messages&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[22,29,50,51],"class_list":["post-157","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-dashboard","tag-javascript","tag-production","tag-programming"],"_links":{"self":[{"href":"https:\/\/techministry.blog\/index.php?rest_route=\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techministry.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techministry.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techministry.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techministry.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":0,"href":"https:\/\/techministry.blog\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"wp:attachment":[{"href":"https:\/\/techministry.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techministry.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techministry.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}