Web-Based Control and Monitoring with PIC Microcontroller – MikroC

Web-Based Control and Monitoring with PIC Microcontroller – MikroC

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, then by entering the following command in the browser’s address bar of the PC, will establish a link with the microcontroller:

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 below 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 EasyPIC 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

Figure 6: Web browser html code

To learn more how to create 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:

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:

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 you 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 x 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

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

Share this post

Select your currency