Saturday, March 17, 2012

Turn your PC into a Web Server and host your web-site for free


In this article, I am going to explain how to use your computer with Internet connection as a Web Server to host your own web-site.

Overview

Generally, if you want to host a web-site, you need to either buy a domain or look for some free web hosting services.You put your web-site's code there and then it can be accessed from anywhere through Internet. This way, whenever you type in the address of the web-site, it is redirected to that server's location (its IP address) and its corresponding port (which is mostly port 80). So, if we want to say, host our web-site at home, we need to make sure that whenever someone types your IP address, it should reach your PC's localhost server.

If you don't know anything about IP address, Port, DNS etc, please go through following links before reading this article. It will help you in understanding this article better.

Internet Protocol (IP)
Port Numbers
Domain Name System

Now, the things you are going to need are :

1). A PC with an Internet connection
2). Global IP address
3). Any web-server software

Set up your web-server 

To host a web-site at your home, first thing you need is of course your web-site running on your own PC. I use Apache-Tomcat for this as it is free. Other web servers are WAMP and Oracle etc. You can use any of them. All web servers run on a specified port no, which can be configured from the web-server settings (for Apache Tomcat, default port no is 8080). After starting your web server, start your web site on the server. You can check if your site is working or not by typing localhost/your-website-link. This depends on the configuration of your web-site and web server. So after this step, you are able to view your web-site on your own PC.

Next thing is to make your web-site accessible to the whole world so that when anyone type your IP address with appropriate port no, the request should reach your local server to corresponding port no. For this, you must have a Global and preferably a static IP address. So, you need to determine that whether your IP address is local or Global. Check your IP address by going to command prompt and typing IPconfig (in Mac OS, you can do this by going to Network Utility ). Also, go to google and type "what is my IP". If in both case your IP is same and your IP address is "NOT" in the following IP ranges, it means that you have a Global IP address.


10.0.0.0 through 10.255.255.255
169.254.0.0 through 169.254.255.255
172.16.0.0 through 172.31.255.255
192.168.0.0 through 192.168.255.255

If you have a global IP address, you just have to type in aaa.bbb.ccc.ddd:portno/your-website-link and you will able to access your web-site from anywhere as long as you keep the web-server software running on your PC and your IP doesn't change. If you have Global IP address, you can skip the next step and continue reading.

In this Video, I have explained how to start Apache Web Server and run your web-site on localhost. For testing purpose, I have created a simple page named "test.htm". After starting the Apache web-server, this page can be accessed from localhost

Port Forwarding

But, if the IP address you found from Google is different then the IP address that you found in the command prompt (for e.g., 192.168.xxx.yyy ), it may be because you have a routing device somewhere which creates a personal network.It assigns local IPs from above range to all the PCs connected to the router and then connects that network to Internet through the Global IP address. In this case, whenever someone types your IP address, it reaches the routing device instead of your PC. So, the web site that is hosted on your PC is not accessible from Internet. To overcome this problem, most routing devices have a facility called "Port Forwarding". Like its name suggests, it forwards requests on specific ports of router to your local IP address. This way, if your local IP address is 192.168.xxx.yyy, and the web server is running on port no 'dddd' , you have to 'tell' your routing device to transfer all the request on port 'dddd' to your PC's IP address (i.e. 192.168.xxx.yyy in this case). Most routing devices have this functionality inbuilt and it can be enabled from your router configuration ( to change router configuration, refer to your router's user manual ). After you have enabled port forwarding, rest of the process is same. You can access your web site by simply typing ppp.qqq.rrr.sss:portno/your-website-link, where, ppp.qqq.rrr.sss is your router's IP address and portno is the port no on which the web server is running.


In this Video, I have explained how to enable Port Forwarding in case you have a local IP (192.168.1.100 in this case) address. After enabling Port Forwarding, you will be able to access the localhost from the Internet using the Router's Global IP address (124.125.158.5 in this case).

Setting up DDNS

So till now, you are able to access your web-site from anywhere using global IP address and port no of the web-server running on your PC. This works well, but you have to type numeric IP address which is hard to remember. Also, problem with most of ISPs is that they provide a dynamic IP address. This means that your IP address keeps changing from time to time. So, every time your PC's IP address changes, link to your web site changes. For these problems, DDNS is the best solution. DDNS stands for Dynamic Domain Name System. Basically, DNS is the service that gives a "name" to IP address and DDNS is a service that can be used to provide "name" to dynamic IP address. This way, by using DDNS, you can access your web-site with yourname.something.net even if your PC's IP address keeps on changing. Both free and charged DDNS service providers are available. Here, I am going to explain free DDNS service NO-IP.com and how to use it.

Go to No-IP.com and sign-up there. After you have signed up, login with your credentials. After you login, click on "Add a Host" and fill the form. In Hostname, write any name that you want to use and choose any of the free available domains (various domains that NO-IP.com provides are : "zapto.org", "ddns.me" , "sytes.net" etc.). In the host type, select "Port 80 Redirect". In the IP address field, write your Global IP address and in port no, write your web-server's port no and click on "Create Host". After that, wait for five minutes for changes to take effect.

Now you can access your web-site just by typing "yourname.zapto.org" or "yourname.ddns.me". This address will work till your IP address does not change and web-server is running on your PC. There is one last step in this process to make this address valid even if your IP address change. To enable this, go to NO-IP.com and go to downloads page (https://www.no-ip.com/downloads.php) and download the version that is suitable to your OS. After downloading it, install the software.After finishing installation of the software, start the software. It will ask for credentials that you use to login to NO-IP.com. Enter the credentials and log-in to the software. On logging in, it will display the name of the site that you just created (i.e. yourname.zapto.org or yourname.ddns.me ). Select that name and click on Save and minimize the software. This software constantly monitors your IP address and whenever it changes, it informs such change to NO-IP.com, which changes the association of "yourname.zapto.org" to newer allocate IP address.


This video explains how to setup DDNS using NO-IP.com's free DDNS service. NO-IP.com provides free DDNS client to monitor your dynamically changing IP.


So, that's it. You have created your own web server which can be used to host your web sites and that too for absolutely FREE.

Download :

1). Apache Tomcat web server
2). NO-IP.com's free client software

If you liked / didn't understand / have questions about this article, post a comment here or write to me on dmehta17@gmail.com. You can also reach me through Facebook on www.facebook.com/damehta.

Sunday, June 5, 2011

IR Remote Controlled Switchboard (RC5 protocol decoder)

In an earlier article, I explained how a single switch can be controlled by using a simple IR transmitter and a simple receiver circuit. This article explains how to extenmd the same concept to control a number of switches.

Now, as more than one switches are to be controlled, we have to transmit a number of bits through IR transmission to indicate which is key is pressed on the remote and accordingly which switch of the switch-board to control. In this project, this is done by using a ready-made IR remote control which works on RC5 protocol.
RC5 protocol was developed by Phillips for controlling consumer electronics such as TV, VCD-DVD Player,etc. through IR Remote Controller. This type remote controller can be found in Phillips' 90 channel TVs. This is a very popular IR protocol and very easy to understand.
Basically, in a RC5 protocol, when a key on the remote controller is pressed, a total of 14 bits are serially transmitted. This includes 2 Start bits, 1 Toggle bit, 5 Address bit and 6 Data bit. These 14 bits are transmitted on a carrier of 38/36 khz carrier by using Manchester Coding. High and Low time are 889 us. For more on Manchester Coding and RC5 protocol, read this.

At the receiver, TSOP1738 receiver is used. It removes the carrier and provides serial data. This serial data is applied to AT89C51 microcontroller for decoding serially transmitted data and control the output accordingly. Relays are used for switching action and they are controlled by the microcontroller through the ULN2803 IC. Circuit diagram is shown below :

 
Source code of the project is given below. It is in assembly language.

;Program assuming following dataset from receiving side
;
;
; button    signal            hex
; 1        1 0 00000 011110 111    1E
; 2        1 1 00000 011101 111    1D
; 3        1 0 00000 011100 111   1C
; 4        1 1 00000 011011 111    1B
; 5        1 0 00000 011010 111   1A
; 6        1 1 00000 011001 111    19
; 7        1 0 00000 011000 111    18
; 8        1 1 00000 010111 111    17
; 9        1 0 00000 010110 111    16
; STDBY    1 1 00000 010011 111    13

CNT        equ r7              ;Temporary Variable
EXCNT    equ 10H                ;Count

ADDRS    equ 11H                ;Device address
CMD    equ 12H                    ;Command

ATEMP    equ    13H                ;Temperory store
TEMP     equ 14H                ;Temperory store
DLOCK    equ 15H                ;Setting lock permission

OTOG bit 00H                ;Flip bit
TOG    bit 01H                 ;Temp bit for flip

IR    equ P1.1                ;IR Receiver connected to this pin

SWPORT    equ P0                ;Port at which switches are connected
DSPPORT equ p2                ;Display port

CH1 equ p2.0                ;Signal Waiting  bit (determine whether controller is busy or not)
CH2 equ p2.1                ;Successfull command
CH3 equ p2.2                ;Lock status indicator
   
org 0000H                    ;Start of prog

mov SWport,#00H             ;switch all relays off!
mov DSPPORT,#00h            ;Setting display off
mov sp,#50H                    ;Stack pointer initialization
clr TOG                        ;Clear temp bit
mov DLOCK,#02H                ;Setting lock off
setb CH3                    ;Dispalying default status as an unlocked
   
main:

    jb IR,main                ;Wait for first bit   
    clr CH1                    ;Reseting to indicate Busy
   
        mov CNT,#225        ;Reading the start bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#216
        djnz CNT,$
   
    mov c,IR
   
        mov CNT,#225        ;Reading the toggle bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    mov TOG,c
   
    mov EXCNT,#04h            ;Reading 4 address bits
    mov a,#00h
    adloop:
   
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a
    djnz EXCNT,adloop
   
        mov CNT,#225        ;Reading 5th address bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#180
        djnz CNT,$
   
    mov c,IR
    rlc a
   
    mov ADDRS,a                ;Storing ADDRESS
   
    mov a,#00h
   
        mov CNT,#225        ;Reading first data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
       
    clr c                    ; It is always 0 received
    rlc a

        mov CNT,#225        ;Reading second data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    setb c                    ;It is always 1 received
    rlc a

        mov CNT,#225        ;Reading third data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

        mov CNT,#225        ;Reading forth data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

        mov CNT,#225        ;Reading fifth data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

        mov CNT,#225        ; Reading sixth data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

    mov CMD,a                ;Storing command
   
    mov a,ADDRS                ;Validation 1
    cjne a,#00h,eop1

   
    mov a,#00h                ;Converting both toggles to byte
    mov c,OTOG
    rlc a
    mov TEMP,a
    mov c,TOG
    mov a,#00h
    rlc a
   
    cjne a,TEMP,cont1        ;Validation 2
    jmp eop1
    cont1:

    mov c,TOG
    mov OTOG,c
   
    mov TEMP,#00h
   
    mov a,CMD                ;Converting command to equivalent decimal count
    cjne a,#1Eh,skip1
    mov TEMP,#01h
    skip1:
    cjne a,#1Dh,skip2
    mov TEMP,#02h
    skip2:
    cjne a,#1Ch,skip3
    mov TEMP,#03h
    skip3:
    cjne a,#1Bh,skip4
    mov TEMP,#04h
    skip4:
    cjne a,#1Ah,skip5
    mov TEMP,#05h
    skip5:
    cjne a,#19h,skip6
    mov TEMP,#06h
    skip6:
    cjne a,#18h,skip7
    mov TEMP,#07h
    skip7:
    cjne a,#17h,skip8
    mov TEMP,#08h
    skip8:

    cjne a,#13h,cont2        ;Switching off SWPORT
        jmp offall
    cont2:
   
    cjne a,#1Fh,cont5        ;Checking for lock signal
        jmp checklock
    cont5:
   
    mov a,TEMP                ;Validation 3
    cjne a,#00h,cont3
       
    eop1:
        jmp eop
    cont3:
   
   
    mov a,DLOCK                ;Validation 4
    cjne a,#02h,eop
   
   
    clr c                    ;Inverting SWPORT bit as per count
    mov a,TEMP
    mov CNT,a
    mov a,SWPORT
    again1:
        rrc a
    djnz CNT,again1
    mov ATEMP,a
    mov a,#00h
    rlc a
    cpl a
    rrc a
    mov a,TEMP
    mov CNT,a
    mov a,ATEMP
    again2:
        rlc a
    djnz CNT,again2
    mov SWPORT,a
   
    setb CH2                ;SWPORT written indicator
   
    jmp cont4
    offall:                    ;SWPORT off sub module
   
        mov a,#00h
        mov SWPORT,a
    cont4:
   
    jmp cont6                ;Check lock sub module
    checklock:
       
        mov a,DLOCK
        cjne a,#02h,incmt
            mov DLOCK,#00h
            clr CH3
            jmp cont7
        incmt:
            inc DLOCK
            cjne a,#01,cont8
                setb CH3
            cont8:
        cont7:
    cont6:
   
    eop:
   
    mov EXCNT,#40
    loop1:
        mov CNT,#255
        djnz CNT,$
    djnz EXCNT,loop1

    setb CH1                ;Seting to indicate as Ready
    clr CH2                    ;Clearing as default state
   
   
    ljmp main
end

The code works by sampling data input pin at certain intervals and stores data in registers. After that, according to received data bits, switches are controlled. For example, if '1A' is received, then switch no 5 will be toggled. Similarly, if '17' is received, then switch no 8 will be toggled. Apart form this, if a '0' is pressed once, then it locks the device. If a device is locked, status of switches cannot be changed. For unlocking device, you have to press '0' button twice. After that, it will resume normal operation. Also, when 'STD BY' key of remote is pressed, then all the switches are turned off regardless of their previous state.

This is a very easy to make and fun to use project. You can use it in your house for controlling switches of the switchboard. If you have any problem regarding making this project or want to give Feedback, then post a comment here or mail me at dmehta17@gmail.com.