Controlling devices from the internet

Figure 1: Controlling devices from the internet

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.

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.

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 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.

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 to a PIC Microcontroller: 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. 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 5: Web browser html code

>>> To learn more on creating Web Pages with HTML: 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 on ENC28J60 Ethernet Controller library: Interfacing ENC28J60 Ethernet Controller with PIC MicroController

MikroC 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).

MikroC Source Code: Web Control and Monitoring