Watch the video Tutorial part 1:

Ethernet is the leading wired standard for networking as it enables to connect a very large number of computers, microcontrollers and other computer-based equipment to one another.

In this project we are going to learn how to control any device like an LED, a relay, a light bulb, a motor and so on connected to a PIC Microcontroller from a remote location using a web browser. An operator can use a computer, tablet or even a cellphone to control a device at a remote location. With the help of a router connected to the internet, this device can be controlled anywhere from the world in real time.

This project can be used as a base for Final Year Project For Engineering Students

Connecting to a Microcontroller using a web browser is the easiest and reliable method of establishing connectivity and above all there is no need to develop a special Graphical User Interface software on the PC or cellphone for that matter as all PCs’ operating systems already have a built-in web browser or one can download one of the free other web browsers like Google chrome or Mozilla firefox for example. This method is based on HTTP (HTTP is short for HyperText Transfer Protocol). Not only it this method allows a simple way for a PC to communicate with a microcontroller, it simplifies as well device configuration. Each time a new product that’s need to be connected to ethernet, basic configurations like IP Address, Subnet mask, DNS, Gateway, Name and other technical settings can be configured with a web browser instead of using Telnet commands or DIP switches. Everyone already has a web browser, so it won’t gonna be an extra cost of development. Let us compare other alternatives, You could provide a RS-232 port for configuration, but then you either have to provide a custom GUI or explain to someone how to set up a Hyperterminal program with the right baud rate, start bits, stop bits, and parity. USB could be the other option but would generally mean a custom program to run on whatever platform (Windows, Linux…) the customer might have.

Controlling devices from the internet

Figure 1: Controlling devices from the internet

HTTP provides a standard for Web browsers and servers to communicate and to transfer data on the internet. The aim of HTTP protocol is to allow the transfer of HTML files between a browser in the client (in this case the client is a PC or a smart phone) and a Web  Server where the data is located in this case the server will be the Microcontroller. The client and the server can exchange request messages, let say a user wants to switch on an LED connected to the Microcontroller, the client will entering the url of the server then sends a request to the server to switch on the LED.

Assuming that the server URL is 192.168.0.5, then by entering the following command in the browser’s address bar of the PC, will establish a link with the microcontroller: http://192.168.0.5

Entering the server URL in the browser

Figure 2: Entering the server URL in the browser

The microcontroller (server) will respond for example by sending an HTML page as a response to the client. If this HTML has controls like buttons or check boxes for example, if a user clicks on a button, a command (for example GET) will be sent to the server which in turn will interpret it and know what to do, in our case for example to switch ON an LED. There are three main HTTP message types which are GET, POST and HEAD.

The circuit diagram is shown on figure 3. The interface between the PIC18F45K22 microcontroller and the ENC28J60 Ethernet controller chip is based on the SPI bus protocol, The SI, SO, and SCK pins of the Ethernet chip are connected to SPI pins (SDO, SDI and SCLK) of the microcontroller. The Ethernet controller chip operates at 3.3V, its output SO pin  cannot drive the microcontroller input pin without a voltage translator if the microcontroller is operated at 5V.ENC28J60 Ethernet Controller Connections

Figure 3: ENC28J60 Ethernet Controller Connections

To make the design of Ethernet applications easy, there are ready made boards that include the EC28J60 controller, voltage translation chip and an RJ45 connector. Figure 4 belows shows the the mikroElektronika Serial Ethernet Board. This is a small board that plugs in directly to PORTC of the EasyPI CV7 development board via a 10-way IDC plug simplifying the development of embedded  Ethernet  projects. This board is equipped with an EC28J60 Ethernet controller chip, a 74HCT245 voltage translation chip, three LEDs, a 5 to 3.3 voltage regulator and an RJ45 connector with an integrated transformer.

 Connecting the Serial Ethernet Board to EasyPIC7 V7 development board

Figure 4: Connecting the Serial Ethernet Board to EasyPIC7 V7 development board

Two Relays (Relay 1 and Relay 2) are connected to the microcontroller RD0 and RD1 output pins, by energizing or de-energizing  these relays, devices connected to them like a light bulb, a fan, a gate motor and so on, could be controlled remotely by standard  Web  Browser from a PC or a smart device.

To learn more on how to interface a relay with a PIC Microcontroller read:

Interfacing a Relay with PIC-Microcontroller 

If the PC and the Ethernet controller are on the same network and close to each other, then the two can be connected together using a crossover Ethernet cable, otherwise a hub or a switch may be required. In this case a straight cable can be used to connect the PC to the hub/switch and another straight cable to connect the Microcontroller to the hub/switch. If the PC and the microcontroller are located on different networks and are not close to each other, then routers may be required to establish connectivity between them.

Figure 5: Project Circuit diagram

To learn how to simulate the ENC28J60 wit Proteus, please watch the video below for all the steps:

Watch the video Tutorial part 2:

This project is fairly easy to understand, these are basically the few steps to control a device from a web browser:

Step 1:

Create an html page that will be displayed in the web browser to control the microcontroller. Figure 5 below shows the html code that is sent to the browser. This html code will be saved into the microcontroller’s RAM in our char variable array StrtWebPage[].
You may have noticed that the html code is slightly different to the one inside the code, every time a double quotation mark (“) is needed a backslash () is inserted before it and at the end of the line another one is inserted. This is just for the mikroC compiler and are omitted after a successful compilation. Without these backslashes, the compiler will generate errors. In mikroC a backslash () is used as a continuation character to extend a string constant across line boundaries. A backslash character () is also used to introduce an escape sequence, which allows a visual representation of certain non-graphic characters. One of the most common escape constants is the newline character (n). For more information See mikroC Pro For PIC help file for String Constants and Character Constants

//
<html>
<body> 
  <form name="input" method="get">
  <table align=center width=500 bgcolor= LightGray border=2>
    <tr>
    <td align=center colspan=2><font size=5 color=RoyalBlue  face="verdana"><b>Web-Based Control and Monitoring with PIC Microcontroller</br></font></td>
    </tr> 
    <tr>
    <td height="200" align=center bgcolor=LightGray><input name="TA" type="submit"value="TOGGLE RELAY A"></td>
    <td height="200" align=center bgcolor=LightGray><input name="TB" type="submit"value="TOGGLE RELAY B"></td>
    </tr>
   <tr>
    <td align=center colspan=2><font size=2 color=RoyalBlue  face="verdana"> <a href= "http://www.studentcompanion.co.za"target="_blank">© 2014 Student Companion</a> </br></font></td>
   </tr>
  </table>
  </form>
</body>
</html>

Figure 6: Web browser html code

To learn more how to creat HTML Web Pages read:

Creating Web Pages using html – Introduction .

Form displayed by the web browser

Figure 6: Form displayed by the web browser

Step 2:

Set the MAC Address and IP Address. If connectivity beyond your local network is required, the DNS, Gateway address and Subnet may also be defined here. Our local network parameters are as follows:

//
unsigned char MACAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f}; //MAC Address
unsigned char IPAddr[4] =  {192,168,0,5};  //IP Address

Step 3:

In the main program, disable PORTD analogue inputs, in our case me made all inputs to become digital. Initialize the SPI module of the PIC18F4520  microcontroller and Initialize the Serial Ethernet module chip ENC28J60

Step 4:

Write the code within the Spi_Ethernet_userTCP function that will interpret the commands from the web browser then energize/de-energize the relays on PORTD. When web browser sends an HTTP request by entering the following url: http://192.168.0.5

Upon receipt of this request, the microcontroller in turn will send the HTML code to be displayed to the browser (figure 5) together with the HTTP header. The Content-Type field is used by the browser to tell which format the document it receives is in. HTML is identified with “text/html”, and ordinary text is identified with “text/plain”.

The Web Browser in the PC or mobile smart device will then display the form shown in figure 6. By clicking on the buttons, the user can toggle either relay A or relay B. Assuming that button TOGGLE RELAY A is clicked, the Web Browser sends the following command to the microcontroller:

GET /?TA=TOGGLE+RELAY+A The microcontroller will check the received command (inside function TCP) and toggles Relay A in this case.

Step 5:

Read received data in an endless loop using the function “SPI_Ethernet_doPacket()”.

To learn more how to use the ENC28J60 Ethernet Controller library read:

Interfacing ENC28J60 Ethernet Controller with PIC MicroController

You can easily create a PCB for this project as we have learned in our PCB start to finish tutorials, all you need is to get a good PCB prototyping company that will offer your good quality PCBs at a good price. We recommend PCBWay.com, a China Shenzhen-based manufacturer specializing in PCB prototyping, small-volume production and PCB Assembly service all under one.

There are many reasons we recommend PCBWay.com, here are 13 reasons you may consider them:

  1. Easy to place an order: Free online quote system to get your price instantly and immediately.
  2. Best Value: 10 standard 100 mm x 100 mm 1 or 2 layers PCBs of great quality from as little as $5.
  3. PCB & PCBA Under One Roof: PCBWay.com also offers Assembly service for the PCB customers.
  4. Quality Assurance: Boards go through the most stringent tests other than the basic visual check.
  5. Capabilities: Whatever your PCB requirements are, they might be able to assist you, from up to 14 layers PCB size of 500×1100 mm or even bigger, PCB, PCB assembly, SMD stencils and even Flexible PCBs etc.
  6. Low Minimums: You can order as little as 5 PCBs
  7. Free DFM: Their engineers will review your Gerber files for manufacturability before you even pay.
  8. Fast Turnarounds: Normal build time of 2-3 days, but you can select Express that can be as fast as 24 hours.
  9. On-time Shipping: On-time delivery rate of 99%.
  10. Return and Refund: In case the PCB or assembled boards are not usable due to their fault, you can ask for compensation. They can refund to your account directly or credit your PCBWay account.
  11. 24 hours Customer Service: Live customer service person to respond to your emails or messages.
  12. Free PCB for students: They sponsor engineering student at university or at High School with free PCBs.
  13. Referral Program: You will gain $20 discount coupon when you refer a person to PCBWay.

For more information, please read the article: Low Cost PCB Prototype and assembly with PCBWay or visit their website by clicking on the image below:

PCBWay Home Page

Full Project Source Code

//
/*
 * Project name:  Web-Based Control and Monitoring
 * Copyright:
 *   (c) www.studentcompanion.co.za, 2014.
 * Test configuration:
 *    MCU:             PIC18F4520
 *   Oscillator:      8.000 MHz
 * Compiler mikroC Pro for PIC v6.5.0
 */
 
const char HTTPheader[] = "HTTP/1.1 200 OKnContent-type:";
const char HTTPMimeTypeHTML[] = "text/htmlnn";
const char HTTPMimeTypeScript[] = "text/plainnn";
//
// Define the HTML page to be sent to the PC
//
char StrtWebPage[] =
"<html><body>
<form name="input" method="get">
 <table align=center width=500 bgcolor= LightGray border=2>
 <tr><td align=center colspan=2><font size=5 color=RoyalBlue face="verdana">
 <b>Web-Based Control and Monitoring with PIC Microcontroller</br></font></td>
 </tr><tr> <td height="200" align=center bgcolor=LightGray><input name="TA"
 type="submit"value="TOGGLE RELAY A"></td>
 <td height="200" align=center bgcolor=LightGray><input name="TB"
 type="submit"value="TOGGLE RELAY B"></td></tr><tr>
 <td align=center colspan=2><font size=2 color=RoyalBlue  face="verdana">
 <a href= "http://www.studentcompanion.co.za"target="_blank">© 2014 Student Companion</a>
 </br></font></td> </tr>
 </table></form></body></html>" ;
// Ethernet NIC interface definitions
//
sfr sbit SPI_Ethernet_Rst at RC0_bit;
sfr sbit SPI_Ethernet_CS  at RC1_bit;
sfr sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
sfr sbit SPI_Ethernet_CS_Direction  at TRISC1_bit;
//
// Define Serial Ethernet Board MAC Address, and IP address to be used for the communication
//
unsigned char MACAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ;
unsigned char IPAddr[4] = {192,168,0,5};
unsigned char getRequest[10];
 
typedef struct
{
  unsigned canCloseTCP:1;
  unsigned isBroadcast:1;
}TethPktFlags;
 
//
// TCP routine. This is where the user request to toggle Realy 1 and Relaye 2 are processed
//
unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost,
                                  unsigned int remotePort, unsigned int localPort,
                                  unsigned int reqLength, TEthPktFlags *flags)
{
  unsigned int Len;
  for(len=0; len<10; len++)getRequest[len]=SPI_Ethernet_getByte();
  getRequest[len]=0;
  if(memcmp(getRequest,"GET /",5))return(0);
 
  if(!memcmp(getRequest+6,"TA",2))RD0_bit = ~ RD0_bit;
  else if(!memcmp(getRequest+6,"TB",2))RD1_bit = ~ RD1_bit;
 
  if(localPort != 80)return(0);
  Len = SPI_Ethernet_putConstString(HTTPheader);
  Len += SPI_Ethernet_putConstString(HTTPMimeTypeHTML);
  Len += SPI_Ethernet_putString(StrtWebPage);
  return Len;
}
 
//
// UDP routine. Must be declared even though it is not used
//
unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost,
                                  unsigned int remotePort, unsigned int destPort,
                                  unsigned int reqLength, TEthPktFlags *flags)
{
   return(0);
}
 
//
// Start of MAIN program
//
void main()
{
     ADCON1 = 0x0F;         // All AN pins as digital
     CMCON  = 0x07;         // Turn off comparators
     TRISD = 0;             // Configure PORTD as output
     PORTD = 0;
     SPI1_Init();           // Initialize SPI module
     SPI_Ethernet_Init(MACAddr, IPAddr, 0x01); // Initialize Ethernet module
 
     while(1)                          // Do forever
     {
        SPI_Ethernet_doPacket();      // Process next received packet
     }
}

You can download the full project files (MikroC source code) below here.

All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).

Proteus Schematic Project: Web_Based_Control_Proteus_Project

MikroC Source Code: Web_Control_and_Monitoring_MikroC