Projects‎ > ‎

IP-to-IR bridge

One problem with using the serial port IR transmitter/receiver solution is that the PC must be powered on 24/7, another big problem is that most PC:s don't have a serial port and it is not possible to use a USB-to-serial adapter together with the serial IR circuit. 
The main idea with this project is to use a simple router, DIR-300, running OpenWRT, that is modified by adding an IR transmitter, thus creating a simple IP to IR bridge. The reason for wanting an IP to IR bridge is that I want to be able to control my TV from all sorts of products that support IP, for instance from my Windows 7 laptop or from my Android mobile.

Hardware

This guide will be using a DIR-300-B1. The router has 5 10/100 Ethernet interfaces and 32MB RAM / 4MB flash. The device is using RaLink RT288x/RT305x WiSoC@320 MHz. It's a quite cheep device which is good since we are going to modify the hardware.

Serial port

As usual when creating a custom build of an embedded system is it recommended to add a serial port. The easiest way is using an USB to serial converter(3,3V). The simplest way of getting such a converter is to modify a CA-42. This USB to serial converter is using a pl-2303 chip, which is supported by Linux.




Connections:
BLUE -> GND
GREEN -> RX
WHITE -> TX


Note: Ignore the black cables for now. The reason for adding them will be explained in the section Adding IR diod.
Connections: WHITE - (Not connected) - BLUE - GREEN 

Adding an IR diod

The black/blue(belongs to the serial converter) cable is connected to GND. The resistor is wired to the cathode of the WPS LED.
 

I'm using the WLAN antenna to make sure that the LED is at a good position.
The final product

Circuit

               +------+       
      +--------| 270R |---------------- GPIO
      |        +------+       
 IR  _|_
<--  \ /
<--   V 
     ---
      |
      +--------------------------------- GND

Component list

------------------------------------------------
Quantity                        1
Article number                  184365
Name                            LD271 = LD271L IR-S-DIODE EMITTER DIOD E
Price                           6:30 SEK
----------------------------------------------
Quantity                        1
Article number                  418188
Name                            METAL RESISTORS 0.6W 1% 270R BF
Price                           1:26 SEK
------------------------------------------------
Total = 7,98 SEK(about 1 USD)

The components can be found at Conrad

Software

Building OpenWRT

Since we are going to add new packages to OpenWRT must we build the source code ourselves: 
$ mkdir openwrt_trunk$ cd openwrt_trunk
$ svn co svn://svn.openwrt.org/openwrt/trunk
$ cd trunk
$ ./scripts/feeds update -a
$ ./scripts/feeds install -a
$ make menuconfig

Select the following settings:
  • Target System - Ralink RT288x/RT305x
  • Subtarget - RT305x based boards
The idea is that we should compile the image for the DIR-300 twice. The first time we make a clean/minimal build, to make sure that we can actually build before we add our own changes. This is usually a good idea, specially since we are using the bleeding edge of OpenWRT. The second build includes the compilation of the packages that we want to add to OpenWRT.

Start building:
$ make
The compilation OpenWRT(trunk: 29672) took 97 min on my computer

We are going to use Lirc to send IR codes but we first need to create a lirc driver that handles the generation of IR codes, by using the WPS led. We also need a configration file for the remote control we are going to use. See Serial port IR transmiter/receiver for more information about howto create a configuration file for Lirc. The package lirc.tgz includes a driver for our gpio based serial transmitter and a configuration file for my remote.

Add lirc: 
$ cd ~/openwrt_trunk/trunk/feeds/packages/utils
$ tar xvfz ~/lirc.tgz

Since the idea is that we want to be able to control the IR transmitter need we some sort of software and a very simple tcp based server that supports sending IR codes is included in package ip2ir.tgz
$ cd ~/openwrt_trunk/trunk/feeds/packages/utils
$ tar xvfz ~/ip2ir.tgz 

Command sequence:
  1. Connect to port 32001 on the DIR-300
  2. Send the command that will be executed, for instance MUTE, POWER, 1, SOURCE. The names of the command are the same as the name of a key on the remote. Note: Remember to remote "KEY_" when sending the command.
  3. The DIR-300 will send "OK" if the command could be executed, otherwise will "NOT OK" be sent back to the application.
To be able to use the new packages must we select the new/updated packages before building again. 
$ make menuconfig
Select:
  • Utilities -> ip2ir
  • Utilities -> lirc 
Start a new build:
$ make
This build took 5 min on my computer

To be able to flash the image on the DIR-300 must it first be converted to a different format with v2image
$ wget ftp://gpl.dlink.com/DIR-300_revB/dir300b.v2.02.source.tar.gz
$ tar xvfz dir300b.v2.02.source.tar.gz
$ ./dir300b_v2.02/tools/buildimg/v2image -v -i openwrt_trunk/trunk/bin/ramips/openwrt-ramips-rt305x-dir-300-b1-squashfs-sysupgrade.bin -o openwrt_trunk/trunk/bin/ramips/openwrt-dir300b1-squashfs-webflash.bin -d /dev/mtdblock/2 -s wrgn23_dlwbr_dir300b


Installing OpenWRT

Using the serial port

Serial port settings: Speed: 57600 8N1 HardwareFlowControl = No

$ minicom -D /dev/ttyUSB0 -b 57600 -8

Flashing OpenWRT image 

The u-boot version that is used in DIR-300 has a nice feature and the feature is that the boot loader has a built in web-server that can be used to flash an image. This is especially useful if you for some reason manage to create an archive that for instance is missing some of the tools that are needed to install a new image.

Connect a cable to Ethernet port 3. Press reset and keep it pressed while you remove the power cable to the switch. After a few seconds should you add the power back again. Release the button after you see the line "Running ...". 
Since we are connected with a serial port can we see the output from the boot loader(u-boot):
U-Boot 1.1.3 (Jan 13 2009 - 17:40:14) (Alpha) - SVN revision : 105

Model: WRGN23

Board: RT2880 DRAM:  32 MB
twe0 set to <NULL>
toe0 set to <NULL>
============================================ 
Ralink UBoot Version: 3.1 Alpha
-------------------------------------------- 
ASIC 3052_MP2 (MAC to GigaMAC Mode)
DRAM COMPONENT: 256Mbits 
DRAM BUS: 16BIT 
Total memory: 32Mbytes
Date:Jan 13 2009  Time:17:40:14
============================================ 
icache: sets:128, ways:4, linesz:32 ,total:16384
dcache: sets:128, ways:4, linesz:32 ,total:16384 
Eth0 (10/100-M) :  ethaddr = (34:08:04:0a:c6:1a) 34 08 04 0A C6 1A 
Entering HTTP server.
Mac to giga Mac mode
Using Eth0 (10/100-M) device                                                    
Start HTTP server on port 80, 192.168.0.1                                       
Running ...      
          
The boot-loader(u-boot) has now started a small web-server that allows us to upload a new image even if the last image we flashed was broken. The DIR-300's ip is 192.168.0.1 so we should connect a PC and set the the ip/netmask to 192.168.0.2/255.255.255.0
Upload the flash image that was created above, i.e.  openwrt-dir300b1-squashfs-webflash.bin


Flashing logs + startup of DIR-300 with new image
Start HTTP server on port 80, 192.168.0.1                                                     
Running ...                                                                                   
mpfd_decode: UPLOADED, upsize = 2490464                                                       
IMGHDR========================================                                                
 signature      : wrgn23_dlwbr_dir300b                                                        
 magic          : 20040220                                                                    
==============================================                                                
IMGBLOCK======================================                                                
 maigc          : 20040220                                                                    
 size           : 2490368                                                                     
 offset         : 0                                                                           
 devname        : /dev/mtdblock/2                                                             
 digest         : D60B8C46616ECD0BB11CBCE74D972A76                                            
==============================================                                                
Erase linux block (0xbf050000 ~ 0xbf2affff)                                                   
                                                                                              
**********Erased 38 sectors                                                                   
Copy linux image [2490368 bytes] to Flash [0xBF050000] ...                                    
Copy 2490368 byte to Flash ...done                                                            
## Booting image at bf050000 ...                                                              
   Image Name:   MIPS OpenWrt Linux-2.6.39.4                                                  
   Created:      2012-01-06  20:06:42 UTC                                                     
                                                                                              
 System Control Status = 0x03400000                                                           
   Image Type:   MIPS Linux Kernel Image (lzma compressed)                                    
   Data Size:    806882 Bytes = 788 kB                                                        
   Load Address: 80000000                                                                     
   Entry Point:  80000000                                                                     
   Verifying Checksum ... OK                                                                  
   Uncompressing Kernel Image ... OK                                                          
                                                                                              
Starting kernel ...                                                                           
                                                                                              
[    0.000000] Linux version 2.6.39.4 (robert@robert-desktop) (gcc version 4.5.4 20110808 (pr2
[    0.000000] bootconsole [early0] enabled                                                   
[    0.000000] CPU revision is: 0001964c (MIPS 24Kc)                                          
[    0.000000] Ralink RT3350   id:1 rev:2 running at 320.00 MHz                               
[    0.000000] Determined physical RAM map:                                                   
[    0.000000]  memory: 02000000 @ 00000000 (usable)                                          
[    0.000000] Initrd not found or empty - disabling initrd                                   
[    0.000000] Zone PFN ranges:                                                               
[    0.000000]   Normal   0x00000000 -> 0x00002000                                            
[    0.000000] Movable zone start PFN for each node                                           
[    0.000000] early_node_map[1] active PFN ranges                                            
[    0.000000]     0: 0x00000000 -> 0x00002000                                                
[    0.000000] Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 8128      
[    0.000000] Kernel command line:  board=DIR-300-B1 mtdparts=physmap-flash.0:192k(u-boot)ro2
[    0.000000] PID hash table entries: 128 (order: -3, 512 bytes)                             
[    0.000000] Dentry cache hash table entries: 4096 (order: 2, 16384 bytes)                  
[    0.000000] Inode-cache hash table entries: 2048 (order: 1, 8192 bytes)                    
[    0.000000] Primary instruction cache 16kB, VIPT, 4-way, linesize 32 bytes.                
[    0.000000] Primary data cache 16kB, 4-way, VIPT, no aliases, linesize 32 bytes            
[    0.000000] Writing ErrCtl register=0005e293                                               
[    0.000000] Readback ErrCtl register=0005e293                                              
[    0.000000] Memory: 30020k/32768k available (1851k kernel code, 2748k reserved, 315k data,)
[    0.000000] SLUB: Genslabs=9, HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1         
[    0.000000] NR_IRQS:48                                                                     
[    0.000000] console [ttyS1] enabled, bootconsole disabled                                  
[    0.000000] console [ttyS1] enabled, bootconsole disabled                                  
[    0.010000] Calibrating delay loop... 212.58 BogoMIPS (lpj=1062912)                        
[    0.090000] pid_max: default: 32768 minimum: 301                                           
[    0.090000] Mount-cache hash table entries: 512                                            
[    0.100000] NET: Registered protocol family 16                                             
[    0.110000] MIPS: machine is D-Link DIR-300 B1                                             
[    0.130000] bio: create slab <bio-0> at 0                                                  
[    0.140000] Switching to clocksource MIPS                                                  
[    0.150000] NET: Registered protocol family 2                                              
[    0.160000] IP route cache hash table entries: 1024 (order: 0, 4096 bytes)                 
[    0.180000] TCP established hash table entries: 1024 (order: 1, 8192 bytes)                
[    0.190000] TCP bind hash table entries: 1024 (order: 0, 4096 bytes)                       
[    0.210000] TCP: Hash tables configured (established 1024 bind 1024)                       
[    0.220000] TCP reno registered                                                            
[    0.220000] UDP hash table entries: 256 (order: 0, 4096 bytes)                             
[    0.240000] UDP-Lite hash table entries: 256 (order: 0, 4096 bytes)                        
[    0.250000] NET: Registered protocol family 1                                              
[    0.310000] squashfs: version 4.0 (2009/01/31) Phillip Lougher                             
[    0.320000] JFFS2 version 2.2 (NAND) (SUMMARY) (LZMA) (RTIME) (CMODE_PRIORITY) (c) 2001-20.
[    0.340000] msgmni has been set to 58                                                      
[    0.350000] io scheduler noop registered                                                   
[    0.360000] io scheduler deadline registered (default)                                     
[    0.370000] Serial: 8250/16550 driver, 2 ports, IRQ sharing disabled                       
[    0.380000] serial8250: ttyS0 at MMIO 0x10000500 (irq = 13) is a 16550A                    
[    0.400000] serial8250: ttyS1 at MMIO 0x10000c00 (irq = 20) is a 16550A                    
[    0.420000] physmap platform flash device: 00800000 at bf000000                            
[    0.430000] physmap-flash.0: Found 1 x16 devices at 0x0 in 16-bit bank. Manufacturer ID 0x0
[    0.450000] Amd/Fujitsu Extended Query Table at 0x0040                                     
[    0.460000]   Amd/Fujitsu Extended Query version 1.3.                                      
[    0.470000] number of CFI chips: 1                                                         
[    0.480000] 6 cmdlinepart partitions found on MTD device physmap-flash.0                   
[    0.490000] Creating 6 MTD partitions on "physmap-flash.0":                                
[    0.500000] 0x000000000000-0x000000030000 : "u-boot"                                       
[    0.520000] 0x000000030000-0x000000040000 : "devdata"                                      
[    0.530000] 0x000000040000-0x000000050000 : "devconf"                                      
[    0.550000] 0x000000050000-0x000000120000 : "kernel"                                       
[    0.560000] 0x000000120000-0x000000400000 : "rootfs"                                       
[    0.580000] mtd: partition "rootfs" set to be root filesystem                              
[    0.590000] mtd: partition "rootfs_data" created automatically, ofs=280000, len=180000     
[    0.600000] 0x000000280000-0x000000400000 : "rootfs_data"                                  
[    0.620000] 0x000000050000-0x000000400000 : "firmware"                                     
[    0.650000] TCP westwood registered                                                        
[    0.650000] NET: Registered protocol family 17                                             
[    0.660000] 802.1Q VLAN Support v1.8 Ben Greear <greearb@candelatech.com>                  
[    0.680000] All bugs added by David S. Miller <davem@redhat.com>                           
[    0.700000] VFS: Mounted root (squashfs filesystem) readonly on device 31:4.               
[    0.720000] Freeing unused kernel memory: 156k freed                                       
[    3.000000] input: gpio-buttons as /devices/platform/gpio-buttons/input/input0             
[    3.190000] Button Hotplug driver version 0.4.1                                            
- preinit -                                                                                   
Press the [f] key and hit [enter] to enter failsafe mode                                      
- regular preinit -                                                                           
jffs2 not ready yet; using ramdisk                                                            
- init -                                                                                      
                                                                                              
Please press Enter to activate this console. [    8.050000] Compat-wireless backport release:9
[    8.060000] Backport based on wireless-testing.git master-2011-12-01                       
[    8.240000] cfg80211: Calling CRDA to update world regulatory domain                       
[    8.690000] cfg80211: World regulatory domain updated:                                     
[    8.700000] cfg80211:     (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
[    8.720000] cfg80211:     (2402000 KHz - 2472000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)     
[    8.740000] cfg80211:     (2457000 KHz - 2482000 KHz @ 20000 KHz), (300 mBi, 2000 mBm)     
[    8.750000] cfg80211:     (2474000 KHz - 2494000 KHz @ 20000 KHz), (300 mBi, 2000 mBm)     
[    8.770000] cfg80211:     (5170000 KHz - 5250000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)     
[    8.780000] cfg80211:     (5735000 KHz - 5835000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)     
[    9.480000] usbcore: registered new interface driver usbfs                                 
[    9.490000] usbcore: registered new interface driver hub                                   
[    9.510000] usbcore: registered new device driver usb                                      
272+0 records in                                                                              
272+0 records out                                                                             
[   10.710000] PPP generic driver version 2.4.2                                               
[   11.010000] ip_tables: (C) 2000-2006 Netfilter Core Team                                   
[   11.420000] NET: Registered protocol family 24                                             
[   11.610000] nf_conntrack version 0.5.0 (471 buckets, 1884 max)                             
[   12.350000] dwc_otg: version 2.72a 24-JUN-2008                                             
[   15.750000] device eth0.1 entered promiscuous mode                                         
[   15.760000] device eth0 entered promiscuous mode                                           
[   15.820000] br-lan: port 1(eth0.1) entering forwarding state                               
[   15.830000] br-lan: port 1(eth0.1) entering forwarding state                               
                                                                                              
                                                                                              
                                                                                              
BusyBox v1.19.3 (2012-01-06 19:52:41 CET) built-in shell (ash)                                
Enter 'help' for a list of built-in commands.                                                 
                                                                                              
  _______                     ________        __                                              
 |       |.-----.-----.-----.|  |  |  |.----.|  |_                                            
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|                                           
 |_______||   __|_____|__|__||________||__|  |____|                                           
          |__| W I R E L E S S   F R E E D O M                                                
 ATTITUDE ADJUSTMENT (bleeding edge, r29672) ----------                                       
  * 1/4 oz Vodka      Pour all ingredients into mixing                                        
  * 1/4 oz Gin        tin with ice, strain into glass.                                        
  * 1/4 oz Amaretto                                                                           
  * 1/4 oz Triple sec                                                                         
  * 1/4 oz Peach schnapps                                                                     
  * 1/4 oz Sour mix                                                                           
  * 1 splash Cranberry juice                                                                  
 -----------------------------------------------------                                        
root@OpenWrt:/# 

I'm using a 192.168.0.X network at home so I need to update the DIR-300's ip by editing /etc/config/network and changing the ip address for interface lan. Restart the router after changing the IP. It should now be possible to connect to the router.

It is possible to control the leds from user space by using sysfs. Note: The WPS led is used by the lirc driver.
# remove led driver
rmmod leds_gpio
$ echo 9 > /sys/class/gpio/export
$ echo out > /sys/class/gpio/gpio9/direction
# disable led
$ echo 1 > /sys/class/gpio/gpio9/value
# enable led
$ echo 0 > /sys/class/gpio/gpio9/value 
$ echo 9 > /sys/class/gpio/unexport 

GPIO pins:
DIR_300B_GPIO_LED_STATUS_AMBER  8
DIR_300B_GPIO_LED_STATUS_GREEN  9
DIR_300B_GPIO_LED_WPS           13


Login on the DIR-300 and execute some commands to make sure that the IR transmitter is working OK:
root@OpenWrt:/# irsend SEND_ONCE Philips_42PFL3606H_12 KEY_MUTE
root@OpenWrt:/# irsend SEND_ONCE Philips_42PFL3606H_12 KEY_1
root@OpenWrt:/# irsend SEND_ONCE Philips_42PFL3606H_12 KEY_4

Sending IR command from PC to IP-to-IR bridge
The easiest way of sending command from a PC to the DIR-300 is to use netcat: 
$ echo "MUTE " | netcat 192.168.0.50 32001
$ echo "VOLUMEUP " | netcat 192.168.0.50 32001
$ echo "VOLUMEUP " | netcat 192.168.0.50 32001
$ echo "VOLUMEUP " | netcat 192.168.0.50 32001
$ echo "VOLUMEUP " | netcat 192.168.0.50 32001
$ echo "VOLUMEDOWN " | netcat 192.168.0.50 32001
$ echo "VOLUMEDOWN " | netcat 192.168.0.50 32001
$ echo "6 " | netcat 192.168.0.50 32001

So we are now able to control the IP2IR bridge from a PC. 
The next step is to be control the bridge from a mobile by using my Android TV application
ċ
ip2ir.tgz
(1k)
Robert Eklund,
Jan 6, 2012, 2:52 PM
ċ
lirc.tgz
(14k)
Robert Eklund,
Jan 6, 2012, 2:52 PM
ċ
openwrt-dir300b1-squashfs-webflash.bin
(2432k)
Robert Eklund,
Jan 6, 2012, 2:52 PM
Comments