Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Ethernet UDP-Based Control and Monitoring with PIC Microcontroller – MikroC

Ethernet UDP-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 or a motor connected to a PIC Microcontroller from a remote location using a customized computer Graphical User Interface (GUI) software designed with C#.

A number of client computers from different locations can be used to control the field devices from anywhere. Communication between the client computer and the microcontroller is via UDP protocol. With the help of a router connected to the internet, these devices can be controlled or monitored anywhere in the world in real time.

This project can be used as a base for Final Year Project For electronic and Computer Science Engineering Students.

Controlling devices from the internet

Figure 1: Controlling and Monitoring devices from a PC GUI Software

In this project a PC GUI Software is designed with C# to control a motor and a light bulb connected to a PIC microcontroller using relays. We’re gonna also read periodically the temperature and display it graphically on the screen and plot a live chart of temperature variations.

With a GUI Software unlike a simple web browser as we learned from Web-Based Control and Monitoring with PIC Microcontroller project, we will have more flexibility how we can design the GUI software. We could easily create some sort of SCADA software by directly interacting with devices such as sensors, valves, pumps, motors and more through Human-Machine Interface (HMI) software.

It’s easy to also record events into a log file, send notification messages via email or SMS or raise some audible alarms.

TCP vs. UDP

A number of client computers from different locations can be used to control the field devices from anywhere. In the Web-Based Control and Monitoring with PIC Microcontroller project, we demonstrated how to use TCP protocol with mikroC to control devices connected to a PIC microcontroller, in this example we’re gonna learn how to use the UDP protocol. Here are some few differences between these two protocols:

  • TCP protocol provides reliable ordered delivery of packets. It uses error detection, re-transmissions and acknowledgements. Typical TCP applications include email and web browsing.
  • UDP protocol doesn’t care if every packet is received. This enables faster transmissions. Typical UDP applications include VoIP and music streaming.
  • TCP is strictly used for point to point or unicast transmissions
  • UDP can be used for unicast (one to one), multicast (one to many) and broadcast (one to all) transmissions.

Computer Graphical User Interface (GUI)  software

The figure 2 below shows the PC GUI Software.

Computer Graphical User Interface (GUI)  softwareFigure 2: Computer Graphical User Interface (GUI)  software

We are using two buttons to control the devices connected to PIC microcontroller. By clicking on the BULB button, it will change the color to a lit BULB and Switch ON the BULB connected to the PIC. Clicking it again with turn the BULB OFF.

The FAN button controls the FAN connected to the PIC, clicking it will turn it ON and clicking it again will turn it OFF.

A Temperature Gauge displays the temperature graphically every 2 seconds and a live chart plots the temperature variations continuously.

To enhance the display, we also created an analog clock. A File menu has buttons to Connect/Disconnect, Settings and Exit.

Figure 3: GUI Settings

In the Settings dialog box, the skin of the application can be set from Blue, Light Blue, Dark, Gray, Black, White, Dark, Coffee, Sky Blue, Caramel, Silver, Green and Pink.

You can also set the IP address of the PIC microcontroller and the Port number we’re gonna communicate with.

Port numbers are used to identify processes running in the applications on a host. For example in one PC, more than one applications that require TCP/IP communications can run at the same time like a web browser and an email client and both will send and receive packets with the same IP address. The Transport layer will differentiate a web browser packet from an email packet using port numbers.
Some well-know port numbers have been reserved for specific applications, typically for server application.

Here are some of them:

  • Port 21: FTP
  • Port 23: Telnet
  • Port 25: SMTP (email outgoing)
  • Port 53: DNS
  • Port 67: DHCP
  • Port 80: HTTP (Web)
  • Port 110: POP3 (email, incoming)
  • Port 143: IMAP (email, incoming)

Client side port numbers are generated and assigned by the Transport layer. They could be any number from 1024 to 65535. These port numbers are typically allocated for short term use, our application uses port 10001

Creating Simple UDP Server And Client to Transfer Data Using C#

By using simple network applications, we can send files or messages over the internet. A socket is a low-level connection point to the IP stack. This socket can be opened or closed or one of a set amount of intermediate states. A socket can deliver and receive data down this connection. Data is generally sent in blocks of a few kilobytes at a time and are called packets.

All packets that travel on the web must use the Internet communications protocol. This means that the source IP address, destination address must be provided in the packet. Some data packets also contain a port number like TCP/IP and UDP.

This simple code below shows the steps to send UDP data from client to server. In this example we are sending a ‘2’ to the PIC microcontroller.

The first task is to create a UDP Client object (public UdpClient ClientSocket = new UdpClient();). This is a socket that can send UDP packets. ClientSocket.Connect() takes two arguments, the server IP address and the port number, these values are saved in application settings. A port number is selected arbitrarily. Here, the port number 10001 will be read from the application settings, remember this number it is not in the first 1024 port numbers reserved for specific use by IANA.

To detect incoming data, the application will become a server, the code below shows the steps to receive a temperature data from the PIC.

A key feature of servers is multi-threading, they can handle hundreds of simultaneous requests. In this case, we must have at least two threads: one deals with incoming UDP data, and the other one the main thread to execute the rest of the program like buttons click events or sending data to PIC. This will make sure the user interface does not appear hung.

Below is the complete code of the application. You can also download the full Visual studio project and the end of this article.

Circuit Diagram

Figure 4 below shows the circuit diagram. 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 4: 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 5 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 5: Connecting the Serial Ethernet Board to EasyPIC7 V7 development board

Relay 1 and Relay 2 are connected to the microcontroller RD0 and RD1 output pins. A control signal to energize or de-energize  these relays, will also control the devices connected to them like a light bulb, a fan or a gate motor.

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.

Project Circuit diagramFigure 6: Project Circuit diagram

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.

They are now running a PCB contest until 12 December to provide a platform for hardware hackers, makers, Electronic Engineers, Hobbyists and artists to compete to build a better future with open hardware. You could also win cash prize money of $1000. So hurry-up now, post your project or vote for your favorite project before the deadline.

To learn more, please read this article:

Join PCB Design Contest and Stand a Chance to Win $1000 in Cash

or click on the image below to visit the PCB Design Contest Page to get all the information.

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

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

Step 1:

Set the MAC Address and IP Address. To access your PIC outside your loacal network or from internet, the DNS, Gateway address and Subnet may also be defined here. Our local network parameters are as follows:

Step 2:

  • In the main program, set PORTD pins as Digital input/output pins and pin RA0 as analog input pin for the LM35 temperature sensor.
  • Initialize Timer0 to generate a timer interrupt every 2 second.
  • Initialize the SPI module of the microcontroller and Initialize the Serial Ethernet module chip ENC28J60

Step 3:

Write the code within the Spi_Ethernet_userUDP function that will interpret the commands from the UDP client then energize/de-energize the relays on PORTD by checking any data recieved on port 10001

Step 4:

Read received data in an endless loop using the function “SPI_Ethernet_doPacket()”. When a timer interrupt occurs every 2 seconds, read the temperature value and send it to remote PC using the function “SPI_Ethernet_sendUDP()”.

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

Interfacing ENC28J60 Ethernet Controller with PIC MicroController

Step 5:

Generate 2 Second timer interrupt using the Timer Calculator.

Timer CalculatorFigure 7: Timer Calculator

Full Project Source Code

You can download the full project files below. 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: UDP_Ethernet_Proteus_Project

MikroC Source Code: UDP_Ethernet-MikroC-Project

PC GUI Software ProjectUDP_Control_Monitoring_PC_GUI_Software_Project

Share this post


Select your currency
EUR Euro
USD United States (US) dollar