Arduino Leonardo ENC28J60 Ethernet Shield

Pada tutorial kali ini akan dibahas mengenai koneksi ethernet ke arduino menggunakan modul ethernet ENC28J60. Perlu dicatat bahwa tutorial ini menggunakan arduino leonardo sehingga koneksi pin antara arduino dan modul ethernet akan berbeda jika menggunakan arduino uno atau yang lainnya.

  • Hubungkan pin pada arduino leonardo dengan shield ENC28J60 seperti pada tabel berikut ini:
Arduino ENC28J60 Shield
3.3v VCC
GND GND
10 CS
MOSI (ICSP) SI
MISO (ICSP) SO
SCK (ICSP) SCK
  • Clone library Ethercard dari github:
# cd /usr/share/arduino/libraries
# git clone https://github.com/jcw/ethercard.git Ethercard
  • Selanjutnya, Anda perlu melakukan patching library ethercard jika Anda menggunakan avr-libc versi 1.8.0. Anda dapat menggunakan sed atau menggunakan file diff seperti ini:
# wget https://gist.github.com/john-117/bfa55e15d2b291a1da21/raw/b8b821aa3cdeea922d9fbce37767d924cd9870fa/ethercard.diff
# patch -p0 < ethercard.diff
  • Untuk mencoba apakah library ethercard, Anda dapat menggunakan salah satu contoh yang disertakan pada library ethercard misalnya RBBB web server. Berikut ini adalah source codenya:
// This is a demo of the RBBB running as webserver with the Ether Card
// 2010-05-28 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#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[] = { 10,10,10,200 };

byte Ethernet::buffer[500];
BufferFiller bfill;

void setup () {
  if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
    Serial.println( "Failed to access Ethernet controller");
  ether.staticSetup(myip);
}

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();
}

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

  if (pos)  // check if valid tcp data is received
    ether.httpServerReply(homePage()); // send web page data
}
  • Pada contoh di atas, IP yang digunakan adalah 10.10.10.200. Hubungkan komputer ke ethernet shield ENC28J60 dan atur alamat IP komputer Anda, misalnya menggunakan IP 10.10.10.1.
  • Untuk menguji koneksi, Anda dapat melakukan ping seperti ini:
% ping -c 3 10.10.10.200
PING 10.10.10.200 (10.10.10.200) 56(84) bytes of data.
64 bytes from 10.10.10.200: icmp_seq=1 ttl=64 time=1.32 ms
64 bytes from 10.10.10.200: icmp_seq=2 ttl=64 time=1.30 ms
64 bytes from 10.10.10.200: icmp_seq=3 ttl=64 time=1.39 ms

--- 10.10.10.200 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 1.306/1.339/1.390/0.055 ms
  • Anda juga dapat membuka browser dan memasukkan alamat IP sesuai dengan yang telah diatur pada source code yaitu 10.10.10.200. Tampilannya kurang lebih seperti ini.
  • Sekian tutorial kali ini, semoga bermanfaat.

Leave a comment