{"id":559,"date":"2024-04-27T00:41:28","date_gmt":"2024-04-26T19:11:28","guid":{"rendered":"https:\/\/hill9.org\/journal\/?p=559"},"modified":"2024-05-04T01:51:54","modified_gmt":"2024-05-03T20:21:54","slug":"home-lab-simple-remote-pdu-for-kernel-lab-machines","status":"publish","type":"post","link":"https:\/\/hill9.org\/journal\/2024\/04\/27\/home-lab-simple-remote-pdu-for-kernel-lab-machines\/","title":{"rendered":"Home lab : simple remote PDU for kernel lab machines"},"content":{"rendered":"\n<p>One of must-have features needed in a Linux kernel (or other system SW) development lab is the ability remotely power cycle lab machines. During bring-up of new kernels or while running tests, if the system hangs, test automation need to time-out the test and power cycle the system to programmatically to move to the next stage. For example, <a href=\"https:\/\/www.lavasoftware.org\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Lava (Linaro Automated Validation Architecture)<\/a> requires remote PDU capabilities in device under test (DUT). In this article, I describe my implementation of home brewed remote PDU in my home lab using Smart Life devices and publish a simple tool I wrote in python to control these PDUs from anywhere on the internet. The tool works reliably and the latency is often as good as 1 second.<\/p>\n\n\n\n<p>In a <a href=\"https:\/\/hill9.org\/journal\/2023\/07\/01\/home-lab-rack-accessories\/\" target=\"_blank\" rel=\"noopener\" title=\"\">previous article<\/a>, I had described the Wipro Smart Life power plugs I am using with various lab machines in my home lab rack. While the Smart Life mobile app allow power control of these devices, it was necessary to control these devices from a command line tool in order for lab and test automation to work. In order to remotely access all the Smart Life PDU devices in my home lab, I am using <a href=\"https:\/\/www.tuya.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Tuya cloud<\/a>. At a high level, these are the steps I took to facilitate access to the PDU devices using Tuya cloud and program them using a python tool I wrote:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a developer account in Tuya cloud<\/li>\n\n\n\n<li>Link the Smart Life devices in the home lab from the Smart Life mobile app<\/li>\n\n\n\n<li>Use the TinyTuya module to access the devices and control them from the python program (powerctl.py)<\/li>\n<\/ul>\n\n\n\n<p>To use the tool, look at my <em><a href=\"https:\/\/github.com\/dipsarma\/remote_pdu\" target=\"_blank\" rel=\"noopener\" title=\"\">remote_pdu<\/a><\/em> repository in github. It has the basic documentation on setup and running of the tool.<\/p>\n\n\n\n<p>Before downloading and using <em>powerctl.py<\/em>, the following steps need to be taken :<\/p>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-4260b7bfc100393379ebdc085068e2ab\">Install tinytuya <\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-vivid-green-cyan-color has-black-background-color has-text-color has-background has-link-color wp-elements-c43612d4dbfce8b2891dad1912e7b089\">python -m pip install tinytuya<\/pre>\n\n\n\n<p>See TUYA Account section of <a href=\"https:\/\/pypi.org\/project\/tinytuya\/\">https:\/\/pypi.org\/project\/tinytuya\/<\/a> for the next steps for setting up the Tuya developer account. The steps are :<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a Tuya account using these instructions &#8211; <a href=\"https:\/\/github.com\/jasonacox\/tinytuya\/files\/12836816\/Tuya.IoT.API.Setup.v2.pdf\">https:\/\/github.com\/jasonacox\/tinytuya\/files\/12836816\/Tuya.IoT.API.Setup.v2.pdf<\/a><\/li>\n\n\n\n<li>Create a Tuya Developer account on <a href=\"https:\/\/iot.tuya.com\" title=\"\">https:\/\/iot.tuya.com<\/a>. When it asks for the &#8220;Account Type&#8221;, select &#8220;Skip this step\u2026&#8221;<\/li>\n\n\n\n<li>Create a Tuya cloud project as described in the same section of the guide. You need to choose your region here.<\/li>\n\n\n\n<li>Click on &#8220;Cloud&#8221; icon -&gt; Select your project -&gt; <strong>Devices<\/strong> -&gt; <strong>Link Tuya App Account<\/strong> to link your Smart Life devices. <\/li>\n\n\n\n<li>Click on <strong>Add App Account<\/strong> and it will pop up a <strong>Link Tuya App Account <\/strong>dialog to link the Smart Life devices. Choose &#8220;Automatic&#8221; and &#8220;Read only status&#8221;, it will still allow commands. It then displays a QR code which needs to be scanned from the Smart Life mobile app (from the Me tab in the app). This can be repeated to add all the Smart Life devices in the Tuya App Account. <\/li>\n\n\n\n<li>Continue with the steps given in the guide to set up the Service API. IoT Core and Authorization must be added to make this work.<\/li>\n<\/ol>\n\n\n\n<p>At this point in time, you should be able to view the details of your devices in the Tuya development project you created :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"337\" src=\"https:\/\/hill9.org\/journal\/wp-content\/uploads\/2024\/04\/Tuya-devices-1024x337.png\" alt=\"\" class=\"wp-image-568\" srcset=\"https:\/\/hill9.org\/journal\/wp-content\/uploads\/2024\/04\/Tuya-devices-1024x337.png 1024w, https:\/\/hill9.org\/journal\/wp-content\/uploads\/2024\/04\/Tuya-devices-300x99.png 300w, https:\/\/hill9.org\/journal\/wp-content\/uploads\/2024\/04\/Tuya-devices-768x253.png 768w, https:\/\/hill9.org\/journal\/wp-content\/uploads\/2024\/04\/Tuya-devices-1536x506.png 1536w, https:\/\/hill9.org\/journal\/wp-content\/uploads\/2024\/04\/Tuya-devices-2048x675.png 2048w, https:\/\/hill9.org\/journal\/wp-content\/uploads\/2024\/04\/Tuya-devices-850x280.png 850w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The next step is to use the tinytuya wizard to scan the devices and set up a json database of the devices (devices.json) for ease of lookup and control :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-vivid-green-cyan-color has-black-background-color has-text-color has-background has-link-color wp-elements-e99793f4fad4c5bee763b6a1995c7938\">dipankar@conan:~$ python -m tinytuya wizard<br>TinyTuya Setup Wizard [1.13.2]<br><br><br>\tEnter API Key from tuya.com: XXXXXXXXXXXX<br>\tEnter API Secret from tuya.com: XXXXXXXXXXXX<br>\tEnter any Device ID currently registered in Tuya App (used to pull full list) or 'scan' to scan for one: XXXXXXXXXXXX<br><br>\t  Region List<br>\t\tcn\tChina Data Center<br>\t\tus\tUS - Western America Data Center<br>\t\tus-e\tUS - Eastern America Data Center<br>\t\teu\tCentral Europe Data Center<br>\t\teu-w\tWestern Europe Data Center<br>\t\tin\tIndia Data Center<br><br>\tEnter Your Region (Options: cn, us, us-e, eu, eu-w, or in): in<br><br>&gt;&gt; Configuration Data Saved to tinytuya.json<br>{<br>\t\"apiKey\": \"XXXXXXXXXXXX\",<br>\t\"apiSecret\": \"XXXXXXXXXXXX\",<br>\t\"apiRegion\": \"in\",<br>\t\"apiDeviceID\": \"XXXXXXXXXXXX\"<br>}<br><br>Download DP Name mappings? (Y\/n): Y<br><br><br>Device Listing<br><br>[<br>\t{<br>\t\t\"name\": \"HomeRack PS1\",<br>\t\t\"id\": \"XXXXXXXXXXXX\",<br>\t\t\"key\": \"XXXXXXXXXXXX\",<br>\t\t\"mac\": \"XXXXXXXXXXXX\",<br>\t\t\"uuid\": \"XXXXXXXXXXXX\",<br>\t\t\"sn\": \"XXXXXXXXXXXX\",<br>\t\t\"category\": \"cz\",<br>\t\t\"product_name\": \"10Amp Smart Plug\",<br>\t\t\"product_id\": \"jwv5jtbscfluha0g\",<br>\t\t\"biz_type\": 18,<br>\t\t\"model\": \"DSP1100\",<br>\t\t\"sub\": false,<br>\t\t\"icon\": \"https:\/\/images.tuyain.com\/smart\/icon\/ay1545304748499XogPc\/25a35a2b8a1668f67124311d5b50f141.png\",<br>\t\t\"mapping\": {<br>\t\t\t\"1\": {<br>\t\t\t\t\"code\": \"switch_1\",<br>\t\t\t\t\"type\": \"Boolean\",<br>\t\t\t\t\"values\": {}<br>\t\t\t},<br>\t\t\t\"9\": {<br>\t\t\t\t\"code\": \"countdown_1\",<br>\t\t\t\t\"type\": \"Integer\",<br>\t\t\t\t\"values\": {<br>\t\t\t\t\t\"unit\": \"s\",<br>\t\t\t\t\t\"min\": 0,<br>\t\t\t\t\t\"max\": 86400,<br>\t\t\t\t\t\"scale\": 0,<br>\t\t\t\t\t\"step\": 1<br>\t\t\t\t}<br>\t\t\t},<br>...........<br>...........<br>&gt;&gt; Saving list to devices.json<br>\t4 registered devices saved<br><br>&gt;&gt; Saving raw TuyaPlatform response to tuya-raw.json<br><br>Poll local devices? (Y\/n): n<br><br>Done.<\/pre>\n\n\n\n<p>The wizard prompts for Tuya cloud credentials. You can find the API Key (Access ID) and API Secret (Access Secret) from the Authorization tab in your Tuya developer project.<\/p>\n\n\n\n<p>Once the devices.json database is created, clone the <em>remote_pdu<\/em> repository and use the <em>powerctl.py<\/em> tool to control the devices.<\/p>\n\n\n\n<p><em>powerctl.py<\/em> expects the device data (output of tinytuya wizard) to be stored in a json and also expects the credentials of the cloud project to be stored in a file named <em>cred.json<\/em> in the following format :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-vivid-green-cyan-color has-black-background-color has-text-color has-background has-link-color wp-elements-6c3a55c4f47c6c2c076952b904490fca\">{<br>    \"apiKey\": \"XXXXXXXXXX\",<br>    \"apiSecret\": \"XXXXXXXXXXXX\",<br>    \"apiRegion\": \"in\",<br>    \"apiDeviceID\": \"XXXXXXXXXXXX\"<br>}<br><\/pre>\n\n\n\n<p>The syntax of the tool is as follows :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python powerctl.py [-h] -j JSON_FILE -d DEVICE_NAME -w SWITCH_NAME -c CDOWN_NAME [-t {on,off}] [-s]<\/pre>\n\n\n\n<p>DEVICE_NAME is the name of the Smart Life device typically given while setting it up using the mobile app. SWITCH_NAME is the name of a switch that need to be controlled or checked (e.g. switch_1, switch_2 etc). CDOWN_NAME is the countdown field corresponding to the switch to be controlled (e.g. countdown_1, countdown_2 etc.). -t indicates powering on or powering off. If -s is specified, it prints the current status of the switch (On | Off). -t and -s are mutually exclusive. See your <em>devices.json<\/em> file generated after running the tinytuya wizard.<\/p>\n\n\n\n<p>Some example use of the tool are :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-vivid-green-cyan-color has-black-background-color has-text-color has-background has-link-color wp-elements-16440901f06e000677ba7bee68ec1e19\">dipankar@conan:~\/src\/tuya$ python powerctl.py -j devices.json -d \"HomeRack SS1\" -w switch_2 -c countdown_2 -s<br>Off<br><br>dipankar@conan:~\/src\/tuya$ python powerctl.py -j devices.json -d \"HomeRack SS1\" -w switch_2 -c countdown_2 -t on<br>Sending command...<br>Results<br>: {'result': True, 'success': True, 't': 1714111812230, 'tid': 'a4321958039311ef91503a9eb4971f62'}<br>Switch switch_2 turned on successfully.<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of must-have features needed in a Linux kernel (or other system SW) development lab is the ability remotely power cycle lab machines. During bring-up of new kernels or while running tests, if the system hangs, test automation need to time-out the test and power cycle the system to programmatically to move to the next&#8230;<\/p>\n","protected":false},"author":2,"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,"footnotes":""},"categories":[15,6,8,3],"tags":[],"class_list":["post-559","post","type-post","status-publish","format-standard","hentry","category-electronics","category-homelab","category-kernel","category-linux"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/posts\/559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/comments?post=559"}],"version-history":[{"count":41,"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/posts\/559\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/posts\/559\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/media?parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/categories?post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hill9.org\/journal\/wp-json\/wp\/v2\/tags?post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}