{"id":90,"date":"2018-05-22T19:46:24","date_gmt":"2018-05-22T18:46:24","guid":{"rendered":"http:\/\/www.hoagieshouse.com\/blog\/?p=90"},"modified":"2018-05-22T19:46:24","modified_gmt":"2018-05-22T18:46:24","slug":"using-a-remote-control-for-computer-password-entry","status":"publish","type":"post","link":"http:\/\/www.hoagieshouse.com\/blog\/stuff\/using-a-remote-control-for-computer-password-entry\/","title":{"rendered":"Using a remote control for computer password entry"},"content":{"rendered":"<p>This article describes how to make a small device that allows you to use a small keyfob remote control to enter your computer password with one button press.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_Keyfob.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-91\" src=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_Keyfob-300x300.jpg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_Keyfob-300x300.jpg 300w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_Keyfob-150x150.jpg 150w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_Keyfob-768x768.jpg 768w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_Keyfob-100x100.jpg 100w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_Keyfob.jpg 1000w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p>Notice I didn&#8217;t say Windows. That&#8217;s because this should work on just about anything that uses a USB keyboard. I know I can use it to enter the Bitlocker password before windows has started up and I&#8217;ve tried it successfully on a Chromebook. I don&#8217;t have my Mac anymore but If anything is going to fail it will be an Apple device.<\/p>\n<p>Here&#8217;s a picture of the keyfob, the receiver and a Pound Coin for scale.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard_with_remote_and_pound_for_scale.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-102\" src=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard_with_remote_and_pound_for_scale-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard_with_remote_and_pound_for_scale-300x225.jpg 300w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard_with_remote_and_pound_for_scale.jpg 400w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Hardware<\/h2>\n<p>I already had the keyfob and have no idea where I got it, or for that matter why, but its ideal for this project. I know you can get these from eBay as I &#8220;borrowed&#8221; the picture above from there.<\/p>\n<p>The device attached to the computer is built around an Arduino Pro Micro clone. Here&#8217;s the picture from the eBay listing.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/Arduino.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-92\" src=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/Arduino.jpg\" alt=\"\" width=\"275\" height=\"275\" srcset=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/Arduino.jpg 275w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/Arduino-150x150.jpg 150w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/Arduino-100x100.jpg 100w\" sizes=\"(max-width: 275px) 100vw, 275px\" \/><\/a><\/p>\n<p>It&#8217;s based on the\u00a0ATmega32U4 IC which has built in USB functionality and can imitate a HID (Human Interface Device) keyboard. I used the 16MHz 5V version as the radio receiver works on 5V. The USB port is USB mini, rather than the newer micro, but that&#8217;s not a problem as I had plenty of those leads lying around. I suspect anyone thinking of following this article does too. These modules cost a little over three pounds, fresh from china as usual.<\/p>\n<p>To receive the commands from the remote control I used another eBay purchase. It&#8217;s described as &#8220;2PCS 433Mhz RF transmitter and receiver link&#8221; and you get two transmitters and two receivers for 94p, including delivery. How is beyond me, surely the delivery alone is worth more than that. Here&#8217;s the eBay picture to see what you&#8217;re looking for.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-93\" src=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC.jpg\" alt=\"\" width=\"275\" height=\"275\" srcset=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC.jpg 275w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC-150x150.jpg 150w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC-100x100.jpg 100w\" sizes=\"(max-width: 275px) 100vw, 275px\" \/><\/a><\/p>\n<p>The hardware fits snugly into a box, from eBay as usual, like this.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/box.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-95\" src=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/box.jpg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/box.jpg 300w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/box-150x150.jpg 150w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/box-100x100.jpg 100w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This actually came from a UK supplier so cost over 4 pounds, but I didn&#8217;t want to wait weeks for the Chinese postal service.<\/p>\n<p>My original antenna was 19cm of wire roughly coiled up and shoved into the box. It gave me a range of about 6 inches, so I got back onto eBay and bought myself a couple of proper coiled 433MHz antennas. I used a UK supplier again for speed, but got these in 2 days.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/433MHzAntenna.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-99\" src=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/433MHzAntenna.jpg\" alt=\"\" width=\"200\" height=\"158\" \/><\/a><\/p>\n<p>Other than that I just needed three connecting wires from the Arduino board to the receiver. These connect the Vcc on the Arduino to Vcc on receiver. Likewise the Gnd. The data pin on the receiver (For some odd reason there are two that do exactly the same thing) is connected to the Arduino pin marked 15. Which is one of the Arduino General Purpose Input Output (GPIO) pins. Three wires turned out to be stiff enough to hold the receiver in place too. The whole thing stacked up like this<\/p>\n<p style=\"text-align: center;\">.<a href=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-100\" src=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard-300x236.jpg\" alt=\"\" width=\"300\" height=\"236\" srcset=\"http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard-300x236.jpg 300w, http:\/\/www.hoagieshouse.com\/blog\/wp-content\/uploads\/2018\/05\/RC_keyboard.jpg 400w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The blue PCB is the Arduino board, the green one is the 433MHz receiver with the coil antenna attached.<\/p>\n<p>The whole thing is powered from the USB, and the computer believes it has another keyboard plugged in. It also sees a USB serial port, but you can ignore that.<\/p>\n<h2>Software<\/h2>\n<p><a href=\"http:\/\/www.hoagieshouse.com\/RaspberryPi\/RCSockets\/RCPlug.html\">This old page of mine<\/a> describes how to get the codes from a 433 MHz transmitter. I now have an oscilloscope so this was a bit easier for me, but the audacity method works fine. Closer examination and a bit more reading on my part showed the codes to be 24 bits of data made up of a 20 bit ID and 4 bits of data. Once transcribed to binary they can easily be converted to Hex for use in the code.<\/p>\n<p>The code was written using the Arduino IDE <a href=\"https:\/\/www.arduino.cc\/en\/Main\/Software\">downloaded from here.<\/a>\u00a0To work with the Arduino Pro Mini clone, set the board to Arduino Leonardo in the IDE menu.<\/p>\n<p>The code runs once every approximately 100 microseconds thanks to the delay at the end of the loop function. It uses a state machine to read the data from the remote control receiver on Pin 15. When 24 bits have been read, it calls a routine to decide whether they are a known code. If so, it calls another routine to transmit the key presses to the computer.<\/p>\n<p>Here&#8217;s the listing with my codes and passwords changed.<\/p>\n<pre class=\"a-b-r-La\">\/\/ Bring in the keyboard library.\r\n#include &lt;Keyboard.h&gt;\r\n\r\n\/\/ Definitions of input pin and state machine values\r\n#define RCPin 15\r\nenum state\r\n{\r\n  preambleHi,\r\n  preambleLo,\r\n  subHi,\r\n  subLo\r\n};\r\n\r\n\/\/ Data buffer and pointers in it.\r\nbyte data[3];\r\nbyte bitmask;\r\nbyte byteIndex;\r\n\r\n\/\/ Count of how many cycles we've been in a state.\r\nbyte count;\r\n\r\n\/\/ Storage for the current state.\r\nstate RCState;\r\n\r\n\/\/ Initialize everything.\r\nvoid setup()\r\n{\r\n  setState (preambleLo); \/\/ Convenient way to initialize data buffer and state machine.\r\n  pinMode(RCPin, INPUT); \/\/ Input from the 433MHz receiver.\r\n\r\n  Keyboard.begin(); \/\/ Initialize the keyboard library.\r\n}\r\n\r\nvoid loop() \r\n{\r\n  RC(); \/\/ Run the state machine.\r\n\r\n  if (byteIndex == 3) \/\/ This indicates we're about to read the 25th Bit so must already have 24.\r\n  {\r\n    \/\/ we have our data packet, process it.\r\n    processData();\r\n  }\r\n  \r\n  delayMicroseconds(100); \/\/ We want this to run every 100 uSec so wait here.\r\n}\r\n\r\n\/\/ Called when a transition of the input or timeout of some kind is detected.\r\n\/\/ Resets data buffer if starting a new packet read.\r\n\/\/ Resets the counter so we know how long we've been in current state.\r\nvoid setState (state s)\r\n{\r\n  count = 0;\r\n  if (s == preambleLo)\r\n  {\r\n    bitmask = 0x80;\r\n    byteIndex = 0;\r\n    data[0] = 0;\r\n    data[1] = 0;\r\n    data[2] = 0;\r\n  }\r\n\r\n  RCState = s;\r\n}\r\n\r\n\/\/ Main RC state machine.\r\n\/\/ Calls subroutine for the current state.\r\n\/\/ Increments count so we know how long we've been in the current state.\r\nvoid RC()\r\n{\r\n  if (count &lt; 250) \/\/ Max count is 250 to avoid wrapping issues.\r\n  {\r\n    count++;\r\n  }\r\n\r\n  switch (RCState)\r\n  {\r\n    case preambleHi:\r\n      preambleHiHandler();\r\n    break;\r\n    case preambleLo:\r\n      preambleLoHandler();\r\n    break;\r\n    case subHi:\r\n      subHiHandler();\r\n    break;\r\n    case subLo:\r\n      subLoHandler();\r\n    break;\r\n  }\r\n}\r\n\r\n\/\/ Nothing to do except wait for the low preamble signal.\r\nvoid preambleHiHandler()\r\n{\r\n  if (!digitalRead(RCPin))\r\n  {\r\n    setState(preambleLo);\r\n  }\r\n}\r\n\r\n\/\/ Do nothing if RC pin is low\r\n\/\/ If Hi, check how long the preamble low was and either start reading data or wait for preamble again.\r\nvoid preambleLoHandler()\r\n{\r\n  if (digitalRead(RCPin))\r\n  {\r\n    if (count &lt; 12)\r\n      setState(preambleHi);\r\n    else\r\n      setState(subHi);\r\n  }\r\n}\r\n\r\n\/\/ If high for too long, abort this packet.\r\n\/\/ If pin goes low sotre the bit value depending on how long it was high.\r\nvoid subHiHandler()\r\n{\r\n  if (count &gt; 12)\r\n  {\r\n    setState(preambleHi);\r\n  }\r\n\r\n  if (!digitalRead(RCPin))\r\n  {\r\n    setBit(count &gt; 6);\r\n    setState(subLo);\r\n  }\r\n}\r\n\r\n\/\/ If low for too long, abort.\r\n\/\/ Otherwise just wait for the next high.\r\nvoid subLoHandler()\r\n{\r\n  if (count &gt; 12)\r\n  {\r\n    setState(preambleLo);\r\n  }\r\n\r\n  if (digitalRead(RCPin))\r\n  {\r\n    setState(subHi);\r\n  }\r\n}\r\n\r\n\/\/ Store the bit in the buffer and move index \/ pointer to next bit\r\nvoid setBit(bool bit)\r\n{\r\n  if (bit)\r\n  {\r\n    data[byteIndex] = data[byteIndex] | bitmask;\r\n  }\r\n  \r\n  bitmask = bitmask &gt;&gt; 1;\r\n  if (!bitmask)\r\n  {\r\n    byteIndex ++;\r\n    bitmask = 0x80;\r\n  }\r\n}\r\n\r\n\/\/ Check the data against known values and call appropriate function.\r\nvoid processData()\r\n{\r\n  if (data[0] == 0xff &amp;&amp; data[1] == 0x5b)\r\n  {\r\n    \/\/ lightning button\r\n    if (data[2] == 0x07)\r\n    {\r\n      lightning();\r\n      delay(1000);\r\n    }\r\n\r\n    \/\/ lock button\r\n    if (data[2] == 0x03)\r\n    {\r\n      lock();\r\n      delay(1000);\r\n    }\r\n    \r\n    \/\/ unlock button\r\n    if (data[2] == 0x05)\r\n    {\r\n      unlock();\r\n      delay(1000);\r\n    }\r\n\r\n    \/\/ bell button\r\n    if (data[2] == 0x06)\r\n    {\r\n      bell();\r\n      delay(1000);\r\n    }\r\n  }\r\n\r\n  \/\/ Get ready to read next packet.\r\n  setState (preambleLo);\r\n}\r\n\r\n\/\/ Types Password1!\r\nvoid lightning()\r\n{\r\n  Keyboard.press(0x81);\r\n\r\n  Keyboard.press('p');\r\n  Keyboard.release('p');\r\n\r\n  Keyboard.release(0x81);\r\n\r\n  Keyboard.press('a');\r\n  Keyboard.release('a');\r\n\r\n  Keyboard.press('s');\r\n  Keyboard.release('s');\r\n\r\n  Keyboard.press('s');\r\n  Keyboard.release('s');\r\n\r\n  Keyboard.press('w');\r\n  Keyboard.release('w');\r\n\r\n  Keyboard.press('o');\r\n  Keyboard.release('o');\r\n\r\n  Keyboard.press('r');\r\n  Keyboard.release('r');\r\n\r\n  Keyboard.press('d');\r\n  Keyboard.release('d');\r\n\r\n  Keyboard.press('1');\r\n  Keyboard.release('1');\r\n\r\n  Keyboard.press(0x81);\r\n\r\n  Keyboard.press('1');\r\n  Keyboard.release('1');\r\n\r\n  Keyboard.release(0x81);\r\n}\r\n\r\n\/\/ Windows key L to lock PC\r\nvoid lock()\r\n{\r\n  Keyboard.press(0x83);  \/\/ The windows key code.\r\n\r\n  Keyboard.press('l');  \r\n  Keyboard.release('l');\r\n  \r\n  Keyboard.release(0x83);\r\n}\r\n\r\n\/\/ Types Password2!\r\nvoid unlock()\r\n{\r\n  Keyboard.press(0x81); \/\/ The shift key code.\r\n\r\n  Keyboard.press('p');\r\n  Keyboard.release('p');\r\n\r\n  Keyboard.release(0x81);\r\n\r\n  Keyboard.press('a');\r\n  Keyboard.release('a');\r\n\r\n  Keyboard.press('s');\r\n  Keyboard.release('s');\r\n\r\n  Keyboard.press('s');\r\n  Keyboard.release('s');\r\n\r\n  Keyboard.press('w');\r\n  Keyboard.release('w');\r\n\r\n  Keyboard.press('o');\r\n  Keyboard.release('o');\r\n\r\n  Keyboard.press('r');\r\n  Keyboard.release('r');\r\n\r\n  Keyboard.press('d');\r\n  Keyboard.release('d');\r\n\r\n  Keyboard.press('2');\r\n  Keyboard.release('2');\r\n\r\n  Keyboard.press(0x81);\r\n\r\n  Keyboard.press('1');\r\n  Keyboard.release('1');\r\n\r\n  Keyboard.release(0x81);\r\n}\r\n\r\n\/\/ Types Password3!\r\nvoid bell()\r\n{\r\n  Keyboard.press(0x81);\r\n\r\n  Keyboard.press('p');\r\n  Keyboard.release('p');\r\n\r\n  Keyboard.release(0x81);\r\n\r\n  Keyboard.press('a');\r\n  Keyboard.release('a');\r\n\r\n  Keyboard.press('s');\r\n  Keyboard.release('s');\r\n\r\n  Keyboard.press('s');\r\n  Keyboard.release('s');\r\n\r\n  Keyboard.press('w');\r\n  Keyboard.release('w');\r\n\r\n  Keyboard.press('o');\r\n  Keyboard.release('o');\r\n\r\n  Keyboard.press('r');\r\n  Keyboard.release('r');\r\n\r\n  Keyboard.press('d');\r\n  Keyboard.release('d');\r\n\r\n  Keyboard.press('3');\r\n  Keyboard.release('3');\r\n\r\n  Keyboard.press(0x81);\r\n\r\n  Keyboard.press('1');\r\n  Keyboard.release('1');\r\n\r\n  Keyboard.release(0x81);\r\n}<\/pre>\n<p>The next time I have to change my password, I&#8217;ll have to edit my code and re-program my little receiver box. That&#8217;s a small price to pay for the convenience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article describes how to make a small device that allows you to use a small keyfob remote control to enter your computer password with one button press.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/posts\/90"}],"collection":[{"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":6,"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":103,"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/posts\/90\/revisions\/103"}],"wp:attachment":[{"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hoagieshouse.com\/blog\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}