{"id":34,"date":"2014-01-08T09:27:45","date_gmt":"2014-01-08T08:27:45","guid":{"rendered":"http:\/\/npham.dk\/?p=34"},"modified":"2020-09-21T19:45:54","modified_gmt":"2020-09-21T18:45:54","slug":"raspberry-pi-rf-remote","status":"publish","type":"post","link":"https:\/\/npham.dk\/?p=34","title":{"rendered":"Raspberry Pi RF remote"},"content":{"rendered":"<p>So I have one of these cheap remote controlled outlets, and I just finished my IR remote control project with the Raspberry Pi, so why not try to control these with the Raspberry Pi also?<br \/>\n<a href=\"\/wp-content\/uploads\/2013\/12\/RemoteControlOutlet.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-92\" src=\"\/wp-content\/uploads\/2013\/12\/RemoteControlOutlet-300x223.jpg\" alt=\"Remote Control Outlet\" width=\"300\" height=\"223\" srcset=\"https:\/\/npham.dk\/wp-content\/uploads\/2013\/12\/RemoteControlOutlet-300x223.jpg 300w, https:\/\/npham.dk\/wp-content\/uploads\/2013\/12\/RemoteControlOutlet.jpg 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>How does it work?<\/h3>\n<p>The way these outlets work, is you set some dip-switches on the remote, and on the outlets so they match.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2013\/12\/IMG_0871.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-93\" src=\"\/wp-content\/uploads\/2013\/12\/IMG_0871-300x225.jpg\" alt=\"RF Remote\" width=\"300\" height=\"225\" srcset=\"https:\/\/npham.dk\/wp-content\/uploads\/2013\/12\/IMG_0871-300x225.jpg 300w, https:\/\/npham.dk\/wp-content\/uploads\/2013\/12\/IMG_0871.jpg 640w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><a href=\"\/wp-content\/uploads\/2013\/12\/IMG_0872.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-94\" src=\"\/wp-content\/uploads\/2013\/12\/IMG_0872-300x225.jpg\" alt=\"RF outlet\" width=\"300\" height=\"225\" srcset=\"https:\/\/npham.dk\/wp-content\/uploads\/2013\/12\/IMG_0872-300x225.jpg 300w, https:\/\/npham.dk\/wp-content\/uploads\/2013\/12\/IMG_0872.jpg 640w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>Creating RF transmitter \/ receiver<\/h3>\n<p>Source:&nbsp;<a href=\"http:\/\/shop.ninjablocks.com\/blogs\/how-to\/7506204-adding-433-to-your-raspberry-pi\">http:\/\/shop.ninjablocks.com\/blogs\/how-to\/7506204-adding-433-to-your-raspberry-pi<\/a><\/p>\n<p>I bought my RF transmitter and receiver from <a href=\"http:\/\/www.ebay.com\/sch\/i.html?_nkw=433mhz+transmitter\">eBay<\/a> a about 1$.<br \/>\nConnecting RF transmitter and receiver to the Raspberry Pi is pretty simple.<br \/>\nBoth the reciver and transmitter has a VCC, GND and DATA pins.<br \/>\nAnd all you do is connect it to the Raspberry Pi GPIO pins.<br \/>\n<img decoding=\"async\" src=\"\/wp-content\/uploads\/2014\/01\/RF_GPIO.jpg\" alt=\"RF_GPIO\"><\/p>\n<h3>Install&nbsp;RPi_utils<\/h3>\n<p>Source: <a href=\"https:\/\/github.com\/ninjablocks\/433Utils\/tree\/master\/RPi_utils\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/ninjablocks\/433Utils\/tree\/master\/RPi_utils<\/a><\/p>\n<p>Start by installing RPI_utils<\/p>\n<pre>sudo apt-get update\n\n#Installs GIT\nsudo apt-get install git\nsudo apt-get install git-core\n\n#Installs 433util\ngit clone git:\/\/github.com\/ninjablocks\/433Utils.git\ncd 433Utils\/RPi_utils\/\n\n#Installs wiringPI\ngit clone git:\/\/git.drogon.net\/wiringPi\ncd wiringPi\ngit pull origin\n.\/build\n\n#Builds RPI_util\ncd ..\nmake all<\/pre>\n<h3>Testing and decoding RF signal<\/h3>\n<p>To capture and decode the RF signal from the remote, I ran the program&nbsp;RFSniffer.<\/p>\n<pre>sudo .\/RFSniffer<\/pre>\n<p>When I push the A (on) button on the remote, I get the following code on the screen.<\/p>\n<pre>Received 5260625\nReceived 5260625\nReceived 5260625\nReceived 5260625\nReceived 5260625<\/pre>\n<p>To test if the transmitter works you can send the received value with this command.<\/p>\n<pre>sudo .\/codesend 5260625<\/pre>\n<p>If you convert 5260625 to binary, you get 10100000100010101010001.<br \/>\nAnd looking at the binary value of all the buttons, you start to see a pattern.<\/p>\n<table  class=\" table table-hover\" >\n<tbody>\n<tr>\n<td>Button<\/td>\n<td>Decimal value<\/td>\n<td>Binary value<\/td>\n<\/tr>\n<tr>\n<td>A (On)<\/td>\n<td>5260625<\/td>\n<td>10100000100010101010001<\/td>\n<\/tr>\n<tr>\n<td>A (Off)<\/td>\n<td>5260628<\/td>\n<td>10100000100010101010100<\/td>\n<\/tr>\n<tr>\n<td>B (On)<\/td>\n<td>5263697<\/td>\n<td>10100000101000101010001<\/td>\n<\/tr>\n<tr>\n<td>B (Off)<\/td>\n<td>5263700<\/td>\n<td>10100000101000101010100<\/td>\n<\/tr>\n<tr>\n<td>C (On)<\/td>\n<td>5264465<\/td>\n<td>10100000101010001010001<\/td>\n<\/tr>\n<tr>\n<td>C (Off)<\/td>\n<td>5264468<\/td>\n<td>10100000101010001010100<\/td>\n<\/tr>\n<tr>\n<td>D (On)<\/td>\n<td>5264657<\/td>\n<td>10100000101010100010001<\/td>\n<\/tr>\n<tr>\n<td>D (Off)<\/td>\n<td>5264660<\/td>\n<td>10100000101010100010100<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>They all starts with 1010000010, and all the ON buttons ends with 0001 and all the OFF buttons ends with 0100.<br \/>\nThe 1010000010 matches with the dip-switches; 10 = down and 01 = up.<br \/>\nThe middle part, is a value that identifies A,B,C and D.<br \/>\nYou can see the pattern here:<\/p>\n<table  class=\" table table-hover\" >\n<tbody>\n<tr>\n<td colspan=\"2\"><\/td>\n<td colspan=\"2\">Dip switch<\/td>\n<td colspan=\"2\">Button Id<\/td>\n<td>On \/ Off<\/td>\n<\/tr>\n<tr>\n<td>A<\/td>\n<td style=\"padding: 0;\">=<\/td>\n<td>10 10 00 00 10<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td><b>00<\/b> 10 10 10 1<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td>0001 \/ 0100<\/td>\n<\/tr>\n<tr>\n<td>B<\/td>\n<td style=\"padding: 0;\">=<\/td>\n<td>10 10 00 00 10<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td>10 <b>00<\/b> 10 10 1<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td>0001 \/ 0100<\/td>\n<\/tr>\n<tr>\n<td>C<\/td>\n<td style=\"padding: 0;\">=<\/td>\n<td>10 10 00 00 10<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td>10 10 <b>00<\/b> 10 1<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td>0001 \/ 0100<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td style=\"padding: 0;\">=<\/td>\n<td>10 10 00 00 10<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td>10 10 10 <b>00<\/b> 1<\/td>\n<td style=\"padding: 0;\">+<\/td>\n<td>0001 \/ 0100<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Script<\/h3>\n<p>I then created the following bash script.<\/p>\n<pre>#!\/bin\/bash\nBTN=$1\nSTATUS=$2\n\nBTN=$(echo $BTN | tr '[:lower:]' '[:upper:]')\nSTATUS=$(echo $STATUS | tr '[:lower:]' '[:upper:]')\n\nDIP_SWITCH=\"DDUUD\"  #Change dip switches to match the remote\n\n#hardcoded values\nBTN_A=\"001010101\"\nBTN_B=\"100010101\"\nBTN_C=\"101000101\"\nBTN_D=\"101010001\"\n\nDIP_SWITCH=$(echo $DIP_SWITCH | sed 's\/D\/10\/g' | sed 's\/U\/00\/g')\n\ncase $BTN in\n    A )\n        BTN=$BTN_A ;;\n    B )\n        BTN=$BTN_B ;;\n    C )\n        BTN=$BTN_C ;;\n    D )\n        BTN=$BTN_D ;;\n    * )\n        echo \"Please define the button [A-D]\";exit;\nesac\ncase $STATUS in\n    ON )\n        STATUS=\"0001\" ;;\n    OFF )\n        STATUS=\"0100\" ;;\n    * )\n        echo \"Please define the button state [ON\/OFF]\";exit;\nesac\n\nBIN=$(echo $DIP_SWITCH$BTN$STATUS)\nDEC=$((2#$BIN))\n\n#echo $DEC\nsudo \/home\/pi\/codesend $DEC<\/pre>\n<p>And to emulate a click, I just call<\/p>\n<pre>.\/RFISend.sh A ON<\/pre>\n<h3>Control via web<\/h3>\n<p>Because the codesend uses wiringpi, it is required to call with sudo.<br \/>\nBut if you need to call it from a webserver, it creates some problem, because sudo requires a password.<\/p>\n<p>So we need to edit visudo, so it doesn&#8217;t require a password.<br \/>\n(it may not be a secure thing to do, but in my case the webserver is not exposed to the internet, and is only for &#8220;at home&#8221; use)<\/p>\n<pre>sudo visudo<\/pre>\n<p>Add the following line to the file.<br \/>\n#NOTE: use TAB and not space, except for the last one &#8211; after NOPASSWD:<\/p>\n<pre>www-data        ALL=(root) NOPASSWD: \/home\/pi\/433Utils\/RPi_utils\/codesend<\/pre>\n<p>Save the file and reboot the Raspberry Pi, and you&#8217;re good to go.<\/p>\n<p>Now the local PHP webserver can call the script with:<\/p>\n<pre>&lt;?php\n  shell_exec(\"\/home\/pi\/433Utils\/RPi_utils\/RFISend.sh A ON\");\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So I have one of these cheap remote controlled outlets, and I just finished my IR remote control project with the Raspberry Pi, so why not try to control these with the Raspberry Pi also? How does it work? The way these outlets work, is you set some dip-switches on the remote, and on the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[7,9,8],"_links":{"self":[{"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts\/34"}],"collection":[{"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34"}],"version-history":[{"count":31,"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":304,"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions\/304"}],"wp:attachment":[{"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}