0% found this document useful (0 votes)
114 views

10 Interface

The document describes setting up an Ethernet connection on an Arduino using the ENC28j60 chip and the EtherCard library. It discusses initializing the Ethernet connection, obtaining an IP address via DHCP, sending UDP packets, and creating a web server to display an updating countdown of the time since startup.

Uploaded by

Gogo P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

10 Interface

The document describes setting up an Ethernet connection on an Arduino using the ENC28j60 chip and the EtherCard library. It discusses initializing the Ethernet connection, obtaining an IP address via DHCP, sending UDP packets, and creating a web server to display an updating countdown of the time since startup.

Uploaded by

Gogo P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Mrea

Ethernet

Ethernet
Dva ipa: ENC i Wiznet
Wiznet integriraniji i manji,
ENC vei manje integrirani
Mi emo koristiti
ENC28j60
ip integrira sve funkcije
etherneta osim filtera koji
je u konektoru
Spoj ipa je sa SPI
sabirnicom

Spoj

- ENC SO -> Arduino pin 12 (MISO)


- ENC SI -> Arduino pin 11 (MOSI)
- ENC SCK -> Arduino pin 13 (SCK)
- ENC CS -> Arduino pin 8 (Data pin)
- ENC VCC -> Arduino 5V
- ENC GND -> Arduino Gnd pin

EtherCard library

Inicijalizacija
uint8_t Ethernet::buffer[700]; // configure buffer size to 700 octets
static uint8_t mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; // define (MAC) address
uint8_type nFirmwareVersion ether.begin(sizeof Ethernet::buffer, mymac);
if(0 == nFirmwareVersion) // handle failure to initiate network interface
Adresa preko DHCP
if(!ether.dhcpSetup()) // handle failure to obtain IP address via DHCP
ether.printIp("IP: ", ether.myip); // output IP address to Serial
ether.printIp("GW: ", ether.gwip); // output gateway address to Serial
ether.printIp("Mask: ", ether.mymask); // output netmask to Serial
ether.printIp("DHCP server: ", ether.dhcpip); // output IP address of the DHCP server
Statika IP adresa
const static uint8_t ip[] = {192,168,0,100}; const static uint8_t gw[] = {192,168,0,254};
const static uint8_t dns[] = {192,168,0,1};
if(!ether.staticSetup(ip, gw, dns) // handle failure to configure static IP address
DNS lookup
if(!ether.dnsLookup("google.com")) // handle failure of DNS lookup
ether.printIp("Server: ", ether.hispip);

EtherCard library

Slanje UDP paketa


char payload[] = "My UDP message";
uint8_t nSourcePort = 1234;
uint8_t nDestinationPort = 5678;
uint8_t ipDestinationAddress[4];
ether.parseIp(ipDestinationAddress, "192.168.0.200");
ether.sendUdp(payload, sizeof(payload), nSourcePort, ipDestinationAddress, nDestinationPort);

DHCP

Povucite adresu
sa DHCP
posluitelja.
Ispiite je na
serijskom portu

#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500];
BufferFiller bfill;
void setup () {
Serial.begin(9600);
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) Serial.println( "Fail");
if(!ether.dhcpSetup()) Serial.println("DHCP failed");
ether.printIp("IP:",ether.myip);
ether.printIp("NETMASK:",ether.mymask);
ether.printIp("GW:",ether.gwip);
ether.printIp("DNS:",ether.dnsip);
}
void loop () {
}

Zadatak

#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,10,205 };
byte Ethernet::buffer[500];
BufferFiller bfill;

Poaljite UDP
void setup () {
paket na adresu
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) Serial.println( "Failed to ac
ether.staticSetup(myip);
ureaja, ispiite
ether.hisport = 7777;
paket na serijskom ether.udpServerListenOnPort(&udpSerialPrint, 7777);
Serial.begin(9600);
Serial.println("finish setup");
portu
}
void loop () {
ether.packetLoop(ether.packetReceive());
}
//callback that prints received packets to the serial port
void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
Serial.println(data);
}

Slanje paketa

Program packet sender

http://packetsender.com/

UDP server

Zadatak

Poaljite UDP paket na


adresu ureaja, ako ste ga
primili vratite poruku OK

#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,10,205 };
static byte remoteip[] = { 192,168,10,99 };
byte Ethernet::buffer[500];
BufferFiller bfill;
char string1[3]="OK";
void setup () {
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) Serial.println( "Fail ");
ether.staticSetup(myip);
ether.hisport = 7777;
ether.udpServerListenOnPort(&udpSerialPrint, 7777);
Serial.begin(9600);
}
void loop () {
ether.packetLoop(ether.packetReceive());
}
//callback that prints received packets to the serial port
void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
Serial.println(data);
ether.sendUdp ((char*) string1, 3, 7776, remoteip, 7776);
}

Zadatak
Osigurajte web stranicu na statikoj adresi koja odbrojava
Koliko je vremena proteklo od paljenja ureaja.
Vrijeme mora biti prikazano na razini sekunde, stranica se
Treba osvjeavati na razini sekunde.
#include <EtherCard.h>
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,203 };
byte Ethernet::buffer[500];
BufferFiller bfill;
void setup () {
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
ether.staticSetup(myip);
}

void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);

if (pos) // check if valid tcp data is received


ether.httpServerReply(homePage()); // send
}
static word homePage() {
long t = millis() / 1000;
word h = t / 3600;
byte m = (t / 60) % 60;
byte s = t % 60;
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n"
"<meta http-equiv='refresh' content='1'/>"
"<title>RBBB server</title>"
"<h1>$D$D:$D$D:$D$D</h1>"),
h/10, h%10, m/10, m%10, s/10, s%10);
return bfill.position();
}

You might also like