Monday, November 18, 2013

HelloWorld on TP-LINK TL-WR740N

TP-LINK TL-WR740N WiFi Router

Here's how to:
  1. Flash a TP-LINK TL-WR740N WiFi router with OpenWRT (Wikipedia)
  2. Install toolchain which will run on your x86 Ubuntu computer but compile for the MIPS processor (Atheros AR9330) present inside the TP-LINK TL-WR740N.
  3. Cross-compile a hello world C program on x86.
  4. Transfer the executable to the router and run it.

Flashing the router with OpenWRT

Figuring out the model number
  1. Look on the back side of the router and figure out your model number
  2. Mine was V4.23. I looked up the corresponding entry for that model on the OpenWRT's Wiki Page for TL-WR740N here under the "Specific Install Notes" section and read:
    "Hardware version 4.23 works perfectly with the unmodified Attitude Adjustment (12.09-beta) generic WR740N v4 image. The best installation method is flashing it through the stock TP-Link web interface."
  3. So I clicked on the image link in that line and downloaded the .bin file:
  4. Next I connected my WiFi router to my computer using an Ethernet cable and booted up the router. Make sure to disable all other network interfaces (WiFi or Ethernet) except for the wired one between the WiFi Router and your PC.
  5. Use a web browser to login to your routers web management interface and navigate to System Tools  > Firmware Upgrade.
  6. Browse and select the .bin file for the OpenWRT and click on the "Upgrade" button.
  7. Wait for the router to finish the firmware flashing process. You will know when its done when two particular LEDs turn back on: one shaped like the Sun or a cog. And the other one shaped like a computer monitor with a number inside it - the number being the Ethernet port number to which you connected your computer. Once that happens, login again to the web management GUI - this time it will be the OpenWRT one. The default IP address for a router newly flashed with OpenWRT is Login with the default password: "root".

    LEDs which turn back on when the flashing is done
    and the router had completed booting
  8. The router will ask you to set a new password for the "root" user, do so under System > and then reboot using the OpenWRT web interface itself. 
    Setting root password for OpenWRT Router
Installing the toolchain and compiling a hello world C program
  1. We need to know which toolchain to download, so before we do that we can SSH (using PuTTY) into the router and confirm some system information.
  2. Using PuTTY connect to your router. Enter the IP address as (default one) or any new one that you might have set.
  3. When the connection succeeds, login as "root" and then enter whatever password you had set for the router in the previous step. You will be presented with a welcome screen:
    OpenWRT Welcome screen 
  4. Note from the welcome screen that my OpenWRT distribution version is "ATTITUDE ADJUSTMENT 12.09-beta"
  5. Now you can issue a the following commands to gain some more information regarding the information, these
       cat /proc/cpuinfo
       uname -a
       ls /lib
    These commands will give you the processor architecture and the libc version number incase you decide to use a different GCC toolchain other than the one provided by OpenWRT. My processor has a MIPS Big Endian core (and NOT an ARM core). I know its a MIPS big endian and not MIPS little endian because the cpu model says "MIPS" and not "mipsel". The router has uClibc and not the full-fledged libc on it. The uclibc version is The Linux kernel version on my router is 3.3.8.
    Gathering system information
  6. Now I have enough info to visit and navigate to the SDK corresponding to this version. In my case this was:
    Its 584 MB in size.
  7. I downloaded it to my x86 Ubuntu PC and extracted it to a folder on my hard drive using the command:
    tar xvjf OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc- -C ~/openwrt

    Make sure to cd to the folder where you have placed the tar.bz2 file and that the target firectory (~/openwrt in my case) exists before you extract. The files will take up around 2.1 GB of space on your hard drive.
  8. Next I figured out the bin folder were the gcc and ld executables are placed, in my case this was:
  9. Two system variables (PATH and STAGING_DIR) must be set before we can use the tools present at this path. Issue the following commands:

    export PATH=~/openwrt/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-$PATH

    export STAGING_DIR=~/openwrt/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-
  10. Now you are ready to compile using this new toolchain. Issue the command "gedit hello.c" and type out a hello world program and save it:

    main() {
        printf("Hello World\n");
  11. Compile the program by issuing the command:
    mips-openwrt-linux-gcc hello.c -o hello
Copying the executable to the router
Connect using WinSCP to transfer the executable to the router
  1. To copy the "hello" executable onto the router, I moved the file to my Windows PC and used WinSCP to copy it to the home directory on the router. If you want to copy the file to the router from your Linux PC, use any of the methods, described here. Make sure to set the protocol to "SCP". The login name would be "root" and the password the one you set just after flashing the router with OpenWRT.
  2. Once the file is copied to the router, use PuTTY to SSH into the router.
  3. Change the file permissions of "hello" to make it executable by issuing the following command via the PutTTY terminal: chmod +x hello
  4. Now execute the program by issuing the command: ./hello
    Great Success!!

  1. Haros's post here:

Saturday, November 9, 2013

Measuring the hot resistance and cold resistance of Incandescent Bulbs

The Bulbs

So I took 3 bulbs of 40 Watts, 60 Watts and 100 Watts respectively and measured the hot and cold resistances

I used the Philips softtone frosted lamp series.

40 Watts : Rs 20
60 Watts : Rs 20
100 Watts  : Rs 21

Blub packs - Front

Blub packs - Back

Cold Resistances

The cold resistance readings were as follows:

40 Watts : around 97.6 Ohms
60 Watts : around 71.4 Ohms
100 Watts : around 39.5 Ohms

Measuring Cold resistance of 40W Bulb

Measuring Cold resistance of 60W Bulb

Measuring Cold resistance of 100W Bulb

Hot Resistances (Calculated)

Calculated values of hot resistance can be obtained using the formula: Resistance = (Voltage^2) / Power
In India the AC voltage RMS value is 230 Volts.

So we get the hot resistances as:

40W Bulb : (230*230) / 40 = 1322.5 Ohms
60W Bulb : (230*230) / 60 = 881.67 Ohms
100W Bulb : (230*230) / 100 = 529 Ohms

Hot Resistances (Measured)

To get the actual values of the hot resistance, you have to measure the current flowing through the bulb. In case of AC supply, this can be done using a clamp meter. It measures the current flowing through a wire by sensing the amount of magnetic flux induced around it. This allows one to measure the current flowing through the wire without making physical contact with the wire conductors. The current should be measured only by clamping it around one of the two wires - live or neutral. Each of these wires carry current in opposite directions so if you clamp the meter around both the wires together, the current will read as zero because the magnetic fields induced in the clamp jaw will cancel each other out.

We can assume the AC mains voltage to be constant at 230Vrms but it does vary from place to palace, so I connected a multimeter in parallel with the bulb contacts to measure the voltage too.

The measured values are:

40W Bulb : 242 / 0.15 = 1613.33 Ohms
60W Bulb : 241 / 0.23 = 1052.17 Ohms
100W Bulb : 241 / 0.39 = 617.95 Ohms

Measuring the hot resistance of a 40W Bulb

Measuring the hot resistance of a 60W Bulb

Measuring the hot resistance of a 100W Bulb

Measuring the power directly using a digital energy meter

Now days small digital energy meters are available which can measure power consumed by a single device during a period of time. In India, you can get one from Visha Electronics, Mumbai ( I just wanted to check by how many watts did the value of the actual power consumed vary from the one printed on the box.

40W Bulb : 42.9 Watts
60W Bulb : 64.1 Watts
100W Bulb : 104 Watts

Measuring the power consumed by a 40W Bulb using a digital energy meter

Measuring the power consumed by a 60W Bulb using a digital energy meter

Measuring the power consumed by a 100W Bulb using a digital energy meter

Insides of a digital energy meter

Irfanview : Auto adjust colors

I refuse to use any computer without first installing a copy of IrfanView on it. This light weight multi-format image viewer and converter has become ubiquitous amongst my circle of tech geeks - we can no longer imagine life without it. One of the specific features that I love is "Auto adjust colors".

Consider the following images. The images in the left windows are the originals and the images in the right windows are obtained by applying the "Auto adjust colors".

As you can see, "Auto adjust colors" tweaks the various parameters of the image and brings out the details which remained unseen earlier. One of the major operation that "Auto adjust colors" does is Histogram stretching. Look at the screenshot below to get a better idea of what that is all about:

Histogram Stretching
Here are the original images for reference.

All in all, using this nifty feature of IrfanView makes me feel like I am performing some macho image forensics - like they show on crime series on TV.

PS: I noticed that while uploading images to this blog, blogger/Picasa Web also perform some color enhancements of their own to the images. Due to this the difference between the before and after images embedded above are not as much as what I can see when I open the image files directly on my computer.

Thursday, November 7, 2013

Integrating FlashMagic with Keil uVision4 for P89V51RD2

If you are using P89V51RD2 microcontroller with Keil uVision4 for your project, then you might find it convenient to add FlashMagic as a Tools Menu Option within Keil.
What this will do is allow you to download the program onto P89V51RD2 using a single click from within Keil.
Calling Flash Magic from within Keil to download the program into P89V51RD2 conveniently.
Note the console messages printed in the pane below by FlashMagic's Command Line utility (FM.exe)


  1. Keil uVision4 (with the hex output option enabled for your current project)
  2. FlashMagic from
  3. A target board wired properly to allow DTR to control RESET. If you are using FT232 (USB <> UART) then refer the connection diagram below on how to design your circuit board appropriately.
Enable the "Create HEX File" option for your project in Keil uVision4

Schematic for wiring FT232 (USB <> UART) so as to allow the
computer to use DTR to control the RESET pin of P89V51RD2

Adding FlashMagic to Tools menu:

  1. Click on "Customize Tools" option from the Tools menu.
  2. Add a new tool by clicking on the New button. Type the tool name as "Flash Magic".
  3. Type the following lines in the "Command" and "Arguments" textboxes and click ok.
    C:\Program Files (x86)\Flash Magic\fm.exe

  4. Now, after building your project, you can click on the "Flash Magic" option from the tools menu to download the hex file into the microcontroller

Explanation of FM.exe arguments:

  1. DEVICE(89V51RD2, 11.0592, 0)
    We are using P89V51RD2 with a 11.0592 MHz Crystal. The last zero refers to the flash block we want to program. In case of P89V51RD2, it is "0".
    We want to erase the whole flash before programming it with the new hex file
    Here we specify the hex file. #H is like a wildcard character which tells Keil to replace it with the actual path of the current project's hex file output.
  4. COM(11, 9600)
    We want to use COM11 at 9600 baud rate. You will have to change the COM port number according to the one on your system
  5. HARDWARE(BOOTEXEC, 50, 100)
    We want tell FM.exe to explicitly use the DTR line to control the RESET of P89V51RD2 and to release it after programming the flash. The values of T1 and T2 are 50 and 100 milliseconds respectively. Refer to the HARDWARE section under the Command Line Interface chapter of FlashMagic User Manual (C:\Program Files (x86)\Flash Magic\Manual.pdf)

Adding to Flash menu instead:

Instead of adding a FlashMagic to the tools menu, you can configure Keil to invoke Flash Magic (FM.exe) whenever the the user selects the Download option from the Flash menu. This is a per project setting which you will have to perform individually for each project. Just enter the same settings as above in the "Configure Flash Tools" dialog box:

Configure Flash Tools

Select "Use External Tool for Flash Programming"

"Download" option in the Flash menu can now be used to invoke FlashMagic


  1. Integrating Flash Magic command line with Keil as ISP
  2. Interfacing Flashmagic with KEIL
  3. 89V51RD2 and FM command line
  4. Flash Magic Manual installed at C:\Program Files (x86)\Flash Magic\Manual.pdf