2
All you need is a Atmega 32 microcontroller and an ENC28J60 serial Ethernet chip. This chip is a great solution for other microcontrol- ler families as well such as PIC, dsPIC etc. The CviLux CJCBA8HF1Y0 RJ-45 connector is used for connection to the Ethernet network. An LED connected to the microcontroller PORTB. pin0 simulates a home appliance we want to control. The mikroC PRO for AVR compiler con- tains the SPI_Ethernet library that will considerably simplify the process of writ- ing a program for the microcontroller. By using a few routines from this library, it is possible to create the program that will enable electrical appliances in your home to be controlled via a web browser. It is necessary to perform the following op- erations within the program: Step 1. Create an html page to run the microcontroller through. Import it in the code as a string. Step 2. Set IP, DNS, Gateway addresses and Subnet mask obtained from your internet provider. Advertising article of MikroElektronika www.mikroe.com mikroC ® is registered trademark by MikroElektronika. All rights reserved. Figure 1. MikroElektronika’s Serial Ethernet module with ENC28J60 chip ADVERTISEMENT OK. OK. After the web browser “GET” request is re- ceived, sent from your computer to the con- trol system IP address, the microcontroller will respond with a web page stored in its memory. This page will then be automati- cally displayed on the computer screen by the browser. When the ON command is received, the LED connected to the PORTA.0 will be turned on. Likewise, when the OFF command is re- ceived the LED will be turned off. If you have a relay instead of LED, it is possible to control any appliance such as lighting, se- curity system, heating system etc. The control of any home appliance consists of entering control system IP address in the web browser and specifying the desired commands. Of course, it is possible to control more than Home automation, home control, smart or digital home are just different names for comfort, convenience, security and power saving. Power saving systems are of increas- ing importance nowadays. Even though such systems are very expensive, rest assured that they can also be very cheap if you make them yourself. There are many ways to control a smart home system. One of them is over Ethernet. ETHERNET ETHERNET Now you need ... Now you need ... By Srdjan Tomic MikroElektronika - Software Department Serial Ethernet module connected to EasyAVR5A Development System For example, our local network parameters are as follows: IP : 192.168.20.60 (Control System address) DNS : 192.168.20.1 (Domain Name System address) GATEWAY : 192.168.20.6 (Gateway address) SUBNET : 255.255.255.0 (Subnet mask) Step 3. Disable PORTA analogue inputs. The microcontroller pin should be cleared and configured as an out- put. Step 4. Initialize the SPI module of the Atmega 32 microcontroller. Step 5. Initialize the Serial Ethernet mod- ule chip ENC28J60. Step 6. Write the code within the Spi_Ethernet_userTCP function that will, after receiving command via web browser, turn on/off the LED connected to the PORTA.0. Step 7. Read received data in an endless loop. The most important part of the program is the Spi_Ethernet_userTCP function, processing all received commands

En Article c Avr 02 09

Embed Size (px)

Citation preview

Page 1: En Article c Avr 02 09

All you need is a Atmega 32 microcontroller and an ENC28J60 serial Ethernet chip. This chip is a great solution for other microcontrol-ler families as well such as PIC, dsPIC etc. The CviLux CJCBA8HF1Y0 RJ-45 connector is used for connection to the Ethernet network. An LED connected to the microcontroller PORTB.pin0 simulates a home appliance we want to control.

The mikroC PRO for AVR compiler con-tains the SPI_Ethernet library that will considerably simplify the process of writ-ing a program for the microcontroller. By using a few routines from this library, it is possible to create the program that will enable electrical appliances in your home to be controlled via a web browser.

It is necessary to perform the following op-erations within the program: Step 1. Create an html page to run the microcontroller through. Import it in the code as a string.

Step 2. Set IP, DNS, Gateway addresses and Subnet mask obtained from your internet provider.

Advertising article of MikroElektronika www.mikroe.commikroC® is registered trademark by MikroElektronika. All rights reserved.

Figure 1. MikroElektronika’s Serial Ethernet module with ENC28J60 chip

ADVERTISEMENT

OK.OK.

After the web browser “GET” request is re-ceived, sent from your computer to the con-trol system IP address, the microcontroller will respond with a web page stored in its memory. This page will then be automati-cally displayed on the computer screen by the browser.When the ON command is received, the LED connected to the PORTA.0 will be turned on. Likewise, when the OFF command is re-ceived the LED will be turned off . If you have a relay instead of LED, it is possible to control any appliance such as lighting, se-curity system, heating system etc.

The control of any home appliance consists of entering control system IP address in the web browser and specifying the desired commands. Of course, it is possible to control more than

Home automation, home control, smart or digital home are just diff erent

names for comfort, convenience, security and power saving. Power saving systems are of increas-

ing importance nowadays. Even though such systems are very expensive, rest assured that they can also be very cheap if

you make them yourself. There are many ways to control a smart home system. One of them is over Ethernet.

ETHERNETETHERNETNow you need ...Now you need ...

By Srdjan TomicMikroElektronika - Software Department

Serial Ethernet module connected to EasyAVR5A Development System

For example, our local network parameters are as follows:

IP : 192.168.20.60 (Control System address)DNS : 192.168.20.1 (Domain Name System address)GATEWAY : 192.168.20.6 (Gateway address)SUBNET : 255.255.255.0 (Subnet mask)

Step 3. Disable PORTA analogue inputs. The microcontroller pin should be cleared and confi gured as an out- put.

Step 4. Initialize the SPI module of the Atmega 32 microcontroller. Step 5. Initialize the Serial Ethernet mod- ule chip ENC28J60.

Step 6. Write the code within the Spi_Ethernet_userTCP function that will, after receiving com mand via web browser, turn on/off the LED connected to the PORTA.0.

Step 7. Read received data in an endless loop.

The most important part of the program is the Spi_Ethernet_userTCP function, processing all received commands

Page 2: En Article c Avr 02 09

// duplex confi g fl ags#defi ne Spi_Ethernet_HALFDUPLEX 0x00 // half duplex#defi ne Spi_Ethernet_FULLDUPLEX 0x01 // full duplex

// mE ehternet NIC pinoutsfr sbit SPI_Ethernet_Rst at PORTB.B0; // reset pinsfr sbit SPI_Ethernet_CS at PORTB.B1; // chip select pinsfr sbit SPI_Ethernet_Rst_Direction at DDRB.B0; // reset pin directionsfr sbit SPI_Ethernet_CS_Direction at DDRB.B1; // chip select pin direction// end ethernet NIC defi nitions

const char httpHeader[] = “HTTP/1.1 200 OK\nContent-type: “; // HTTP headerconst char httpMimeTypeHTML[] = “text/html\n\n”; // HTML MIME typeconst char httpMimeTypeScript[] = “text/plain\n\n”; // TEXT MIME type

// default html pagechar indexPage[] =“<html><head><title>mikroElektronika</title></head><body>\<h3 align=center>MikroElektronika Home Automatization System</h3>\<form name=\”input\” action=\”/\” method=\”get\”>\<table align=center width=200 bgcolor=#4974E2 border=2><tr>\<td align=center colspan=2><font size=4 color=white><b>Heat Control</b></font>\</td></tr><tr><td align=center bgcolor=#4974E2><input name=\”tst1\” width=60 \type=\”submit\” value=\”ON\”></td><td align=center bgcolor=#FFFF00>\<input name=\”tst2\” type=\”submit\” value=\”OFF\”></td></tr></table>\</form></body></html>”;

// network parameterschar myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f }; // my MAC addresschar myIpAddr[4] = {192, 168, 20, 60}; // my IP addresschar gwIpAddr[4] = {192, 168, 20, 6}; // gateway IP addresschar dnsIpAddr[4] = {192, 168, 20, 1}; // dns IP addresschar ipMask[4] = {255, 255, 255, 0}; // subnet mask// end network parameters

unsigned char getRequest[20]; // HTTP request buff er

unsigned int SPI_Ethernet_UserTCP( char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength){ unsigned int len; // my reply length if(localPort != 80) return(0); // I listen only to web request on port 80

// get 10 fi rst bytes only of the request, the rest does not matter here for(len = 0 ; len < 15 ; len++) getRequest[len] = SPI_Ethernet_getByte(); getRequest[len] = 0;

if(memcmp(getRequest, “GET /”, 5)) return(0); // only GET method

if(!memcmp(getRequest+11, “ON”, 2)) // do we have ON command PORTA.F0 = 1; // set PORTA bit0 else if(!memcmp(getRequest+11, “OFF”, 3)) // do we have OFF command PORTA.F0 = 0; // clear PORTA bit0

if (PORTA.F0) { memcpy(indexPage+340, “#FFFF00”, 6); // highlight (yellow) ON memcpy(indexPage+431, “#4974E2”, 6); // clear OFF } else { memcpy(indexPage+340, “#4974E2”, 6); // clear ON memcpy(indexPage+431, “#FFFF00”, 6); // highlight (yellow) OFF }

len = SPI_Ethernet_putConstString(httpHeader); // HTTP header len += SPI_Ethernet_putConstString(httpMimeTypeHTML); // with HTML MIME type len += SPI_Ethernet_putString(indexPage); // HTML page fi rst part return len; // return to the library with the number of bytes to transmit}unsigned int SPI_Ethernet_UserUDP( char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength){ return 0; // back to the library with the length of the UDP reply}void main(){ // set PORTA as output PORTA0_bit = 0; // clear PORTA.B0 DDRA.F0 = 1; // set PORTA.B0 as output (rele control pin)

// starts ENC28J60 with: reset bit on PORTB.F0, CS bit on PORTB.F1, // my MAC & IP address, full duplex SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV4, _SPI_CLK_LO_LEADING); Spi_Rd_Ptr = SPI1_Read; // pass SPI Read function of used SPI module // full duplex, CRC + MAC Unicast + MAC Broadcast fi ltering SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX);

// dhcp will not be used here, so use preconfi gured addresses SPI_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr);

while(1) { // do forever SPI_Ethernet_doPacket(); // process incoming Ethernet packets }}

Example 1: Program to demonstrate control over Ethernet

... making it simple

Code for this example written for AVR® microcontrollers in C, Basic and Pascal as well as the programs written for dsPIC® and PIC® microcontrollers can be found on our web site: www.mikroe.com/en/article/N

OTE

:

one microcontroller pin, which enables you to govern a large number of appli-ances or complete home automation system as well.

The screendump illustrates the web page displayed by the web browser after en-tering the control system IP address. In our example, ON and OFF button clicks cause the LED to be turned on and off, thus simulating the heating control system.

mikroC PRO

for AVR

Written in compiler

SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD www.mikroe.com

Atmel®, logo and combinations thereof, AVR® and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.

Below is a list of ready to use functions contained in the SPI Ethernet Library.This library is integrated in mikroC PRO for AVR compiler.

* SPI Ethernet Library functions used in program

Spi_Ethernet_Init()* Init ENC28J60 controllerSpi_Ethernet_Enable() Enable network traffi c Spi_Ethernet_Disable() Disable network traffi c Spi_Ethernet_doPacket()* Process received packet Spi_Ethernet_putByte() Store a byte Spi_Ethernet_putBytes() Store bytes Spi_Ethernet_putConstBytes() Store const bytes Spi_Ethernet_putString()* Store stringSpi_Ethernet_putConstString()* Store const string Spi_Ethernet_getByte()* Fetch a byteSpi_Ethernet_getBytes() Fetch bytes Spi_Ethernet_UserTCP()* TCP handling codeSpi_Ethernet_UserUDP() UDP handling code Spi_Ethernet_getIpAddress() Get IP address Spi_Ethernet_getGwIpAddress() Get Gateway address Spi_Ethernet_getDnsIpAddress() Get DNS addressSpi_Ethernet_getIpMask() Get IP maskSpi_Ethernet_confNetwork()* Set network parameters Spi_Ethernet_arpResolve() Send an ARP requestSpi_Ethernet_sendUDP() Send an UDP packet Spi_Ethernet_dnsResolve() Send an DNS request Spi_Ethernet_initDHCP() Send an DHCP requestSpi_Ethernet_doDHCPLeaseTime() Process lease timeSpi_Ethernet_renewDHCP() DHCP renewal request

Spi_Init() Initialize microcontroller SPI module memcpy() Copy microcontroller RAM memory locationsmemcmp() Compare microcontroller RAM memory locations

Other mikroC PRO for AVR functions used in program:

Schematic 1. Connecting the Serial Ethernet module to a Atmega 32

WEB

Router