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.

With just a network switch, many different devices can easily communicate with one another with Ethernet, allowing different devices and equipment to be accessed remotely and this also provides a cost-effective and reliable means of remote control and monitoring. Most of computers nowadays have an Ethernet port implemented on them so it is with many electronic devices. Many microcontrollers have built-in Ethernet peripheral, like the PIC18F97J60, this PIC18 Microcontroller has an integrated 10Mbps Ethernet communications peripheral but many other microcontrollers don’t have a built-in Ethernet peripheral.

When a microcontroller which does not have an integrated Ethernet peripheral is used, Microchip offer a serial Ethernet chip that can easily be used by any microcontroller with an SPI interface to provide Ethernet capability to the application. The ENC28J60 is a popular 28-pin serial Ethernet chip, 10BASE-T stand alone Ethernet Controller with SPI interface, on board MAC & PHY, 8 Kbytes of Buffer RAM and an SPI serial interface. With a small foot print package size the ENC28J60 minimizes complexity, board space and cost.

ENC28J60 Ethernet Controller Features

  • IEEE 802.3™ Compatible Ethernet Controller
  • Fully Compatible with 10/100/1000Base-T Networks
  • Integrated MAC and 10Base-T PHY
  • Supports One 10Base-T Port with Automatic Polarity Detection and Correction
  • Supports Full and Half-Duplex modes
  • Programmable Automatic Retransmit on Collision
  • SPI Interface with Clock Speeds up to 20 MHz
  • Buffer: 8-Kbyte Transmit/Receive Packet Dual Port SRAM
  • MAC address: Supports Unicast, Multicast and Broadcast Packets
  • Operating Voltage of 3.1V to 3.6V (3.3V typical)
  • Temperature Range: -40°C to +85°C Industrial, 0°C to +70°C Commercial (SSOP only)
  • 28-Pin SPDIP, SSOP, SOIC, QFN Packages

More information can be obtained from the ENC28J60 datasheet. The figures below show ENC28J60 package types

ENC28J60 package types

Figure 2: ENC28J60 package types.

ENC28J60 Ethernet Controller Connections

The interface between the microcontroller and the Ethernet 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. Figure 3 below shows how the ENC28J60 Ethernet controller can be interfaced to a PIC Microcontroller.

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

 Embedded Ethernet applications

With internet in mind, one can think about many applications, below a few applications are listed:

  • Web-based monitoring: A person can monitor several vending machines located in different places which can be several kilometers apart. From the PC at the conform of your desk, you could monitor live every day the status and stock of each machine, without wasting time and money to travel to each machine. A simple online interface to the vending machines can allow you to monitor everything from the internet.
  • Access control: Monitor and control the access of an area from a remote location
  • Environmental monitoring: Environmental sensors can installed unattended in remote locations feeding data online to a control and monitoring center.
  • Industrial control: The status of machines could be remotely monitored, switch on or off,…
  • Home automation: Turning devices inside the house like lights ON and OFF from a remote location even from a hand held mobile device connected to internet like a smart phone.
  • Voice Over IP( VoIP)
  • Internet of Things (IoT) applications

MikroC Pro for PIC SPI Ethernet ENC28J60 Library Functions

Below is a quick descriptions of some ENC28J60 Library Functions, For more information, please visit online the mikroC pro for PIC SPI Ethernet ENC28J60 Library page.

This library is designed to simplify handling of the underlying hardware (ENC28J60). It works with any PIC with integrated SPI and more than 4 Kb ROM memory. 38 to 40 MHz clock is recommended to get from 8 to 10 Mhz SPI clock, otherwise PIC should be clocked by ENC28J60 clock output due to its silicon bug in SPI hardware. If you try lower PIC clock speed, there might be board hang or miss some requests.

Notes:

  • The PIC16 library does NOT have ARP, DNS, UDP and DHCP client support implemented due to PIC16 RAM/Flash limitations
  • Global library variable SPI_Ethernet_userTimerSec is used to keep track of time for all client implementations (ARP, DNS, UDP and DHCP). It is user responsibility to increment this variable each second in it’s code if any of the clients is used.
  • As the ENC28J60 Ethernet Controller uses SPI for communication with the microcontroller, the appropriate hardware SPI module must be initialized first before using any of the SPI Ethernet ENC28J60 library routines. Refer to the PIC Microcontroller Communication with SPI Bus article for more information.
  • For Microcontrollers with two SPI modules it is possible to initialize both of them and then switch by using the SPI_Set_Active() routine.
  • The following variables must be defined in all projects using SPI Ethernet ENC28J60 Library:
  1. ENC28J60 chip select pin: extern sfr sbit SPI_Ethernet_CS;     //Example: sbit SPI_Ethernet_CS at RC1_bit;
  2. ENC28J60 reset pin: extern sfr sbit SPI_Ethernet_RST;     //Example: sbit SPI_Ethernet_Rst at RC0_bit;
  3. Direction of the ENC28J60 chip select pin: extern sfr sbit SPI_Ethernet_CS_Direction; //Example:sbit SPI_Ethernet_CS_Direction at TRISC1_bit;
  4. Direction of the ENC28J60 reset pin: extern sfr sbit SPI_Ethernet_RST_Direction; //Example: sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
  • The following routines must be defined in all project using SPI Ethernet ENC28J60 Library:
    1. TCP request handler: unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags);
    2. UDP request handler: unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags);

SPI_Ethernet_Init

This routine initializes ENC28J60 controller. The parameters that need to be specified in this routines are: MAC address, IP address and duplex mode (Valid values: 0 (half duplex mode) and 1 (full duplex mode)) the rest of the parameters could be left to their default values. Note that the SPI module has to be initialized before using this routine.

Example: 

SPI_Ethernet_Enable

This routine enables appropriate network traffic on the ENC28J60 module by the means of it’s receive filters (unicast, multicast, broadcast, crc). More than one type of network traffic can be enabled at the same time by ORing appropriate input value or the use of a predefined library const (_SPI_Ethernet_UNICAST, _SPI_Ethernet_BROADCAST,_SPI_Ethernet_MULTICAST,_SPI_Ethernet_CRC). Before using this routine, the Ethernet module has to be initialized first.
Example:

SPI_Ethernet_Disable

This routine disables appropriate network traffic on the ENC28J60 module by the means of it’s receive filters (unicast, multicast, broadcast, crc).

Example: 

SPI_Ethernet_doPacket

This routine processes the received packet if such exists. Packets are processed in the following manner:

  • ARP & ICMP requests are replied automatically.
  • upon TCP request the SPI_Ethernet_UserTCP function is called for further processing.
  • upon UDP request the SPI_Ethernet_UserUDP function is called for further processing.

Before using this routine, the Ethernet module has to be initialized first and this routine must be called as often as possible in user’s code, the best way is to put it in a while(1) loop.

Example: 

SPI_Ethernet_putByte, SPI_Ethernet_putBytes and SPI_Ethernet_putConstBytes

These routines store one byte, stores requested number of bytes and stores requested number of const bytes into ENC28J60 RAM respectively.

Example:

SPI_Ethernet_putString

This routine stores whole string (excluding null termination) into ENC28J60 RAM.

Example:

SPI_Ethernet_getByte and SPI_Ethernet_getBytes

The SPI_Ethernet_getByte routine fetch a byte while the SPI_Ethernet_getBytes fetches requested number of bytes from ENC28J60 RAM starting from given address. If value of 0xFFFF is passed as the address parameter, the reading will start from current ENC28J60 read pointer (ERDPT) location.

Example: 

SPI_Ethernet_UserTCP

This routine is the TCP handling code and it is internally called by the library. The function source code is provided with appropriate example projects. The code should be adjusted by the user to achieve desired reply.

Prototype: unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags);

SPI_Ethernet_UserUDP

This routine is the UDP handling code and it is internally called by the library. The function source code is provided with appropriate example projects. The code should be adjusted by the user to achieve desired reply.

Prototype: unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags);

SPI_Ethernet_confNetwork

This routine configures the network parameters (IP subnet mask, gateway IP address, DNS IP address) when DHCP is not used and is not available for PIC16 family of microcontrollers.

Example: 

SPI_Ethernet_sendUDP

This routine sends an UDP packet on the network. The required parameters are: remote host IP address, local UDP source port number, destination UDP port number, packet to transmit and length in bytes of packet to transmit

Example: