0. Componentes y programación necesaria para el Módulo ENC28J60 Ethernet Arduino

Componentes requeridos:

{Product:2000}{Product:938}
  • Dos LED
  • Placa Board
  • • Cables dupont
  • Conexión Ethernet

I. Cómo utilizar el módulo ENC28J60 Ethernet con Arduino

El módulo Ethernet se comunica con la placa tipo Arduino mediante el protocolo SPI. Podemos usar el ENC28J60 junto a un procesador como Arduino para conectar nuestros proyectos de electrónica y robótica con Internet.

Este módulo soporta velocidades de 10Mbits/s y los modos Dúplex (Full-Duplex) y Semi-dúplex (Half-Duplex) con detección y corrección automática de la polaridad. El ENC28J60 cumple con las especificaciones IEEE 802.3 10BASE-T.

II. Procedimientos experimentales

Conexión

El módulo se alimenta directamente de Arduino, así que no necesitamos una fuente de alimentación externa. 

Los pines están conectados según la tabla:

 

Pin Módulo Ethernet

Pin de Arduino UNO

SI

D11 (MOSI)

CS

D10 (SS)

Vcc

3.3V

SO

D12 (MISO)

SCK

D13 (SCK)

RESET

D9 (RST)

GND

Gnd

Código

Al principio, debes agregar la librería Ethercard.h a Arduino IDE (Programa -> Incluir biblioteca -> Administrar Biliotecas ...), buscarla e instalar la última versión.
Con esta librería podemos ver los muchos ejemplos que nos ofrece, como mandar notificaciones a dispositivos Android.
Podemos también hacer que nuestro Arduino sea un Cliente o un Servidor Ethernet.

Ejemplo - Servidor Ethernet

En este ejemplo haremos que nuestro Arduino encienda y apague dos LED mediante web http (Arduino no es tan potente para soportar la carga de https).

Los LED los conectaremos a los pines de entradas A0 y A1, y a tierra.

Abra un sketch vacío y copie el siguiente código:

#include <EtherCard.h>

 

static byte mymac[] = { 0xDD, 0xDD, 0xDD, 0x00, 0x01, 0x05 };

static byte myip[] = { 192, 168, 1, 168 };

byte Ethernet::buffer[700];

 

const int pinLed1 = A0;

const int pinLed2 = A1;

char* statusLed1 = "OFF";

char* statusLed2 = "OFF";

 

void setup() {

 

   Serial.begin(9600);

 

   if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))

      Serial.println("No se ha podido acceder a la controlador Ethernet");

   else

      Serial.println("Controlador Ethernet inicializado");

 

   if (!ether.staticSetup(myip))

      Serial.println("No se pudo establecer la dirección IP");

   Serial.println();

 

   pinMode(pinLed1, OUTPUT);

   pinMode(pinLed2, OUTPUT);

   digitalWrite(pinLed1, LOW);

   digitalWrite(pinLed2, LOW);

}

 

static word mainPage()

{

   BufferFiller bfill = ether.tcpOffset();

   bfill.emit_p(PSTR("HTTP/1.0 200 OKrn"

      "Content-Type: text/htmlrnPragma: no-cachernRefresh: 5rnrn"

      "<html><head><title>Tutorial Solectro</title></head>"

      "<body>"

      "<div style='text-align:center;'>"

      "<h1>Salidas digitales</h1>"

      "<br /><br />Estado LED 1 = $S<br />"

      "<a href='./?data1=0'><input type='button' value='OFF'></a>"

      "<a href='./?data1=1'><input type='button' value='ON'></a>"

      "<br /><br />Estado LED 2 = $S<br />"

      "<a href='./?data2=0'><input type='button' value='OFF'></a>"

      "<a href='./?data2=1'><input type='button' value='ON'></a>"

      "<br /></div>\n</body></html>"), statusLed1, statusLed2);

 

   return bfill.position();

}

 

void loop() 

{

   word len = ether.packetReceive();

   word pos = ether.packetLoop(len);

 

   if (pos) 

   {

      if (strstr((char *)Ethernet::buffer + pos, "GET /?data1=0") != 0) {

         Serial.println("Led1 OFF");

         digitalWrite(pinLed1, LOW);

         statusLed1 = "OFF";

      }

 

      if (strstr((char *)Ethernet::buffer + pos, "GET /?data1=1") != 0) {

         Serial.println("Led1 ON");

         digitalWrite(pinLed1, HIGH);

         statusLed1 = "ON";

      }

 

      if (strstr((char *)Ethernet::buffer + pos, "GET /?data2=0") != 0) {

         Serial.println("Led2 OFF recieved");

         digitalWrite(pinLed2, LOW);

         statusLed2 = "OFF";

      }

 

      if (strstr((char *)Ethernet::buffer + pos, "GET /?data2=1") != 0) {

         Serial.println("Led2 ON");

         digitalWrite(pinLed2, HIGH);

         statusLed2 = "ON";

      }

 

 

      ether.httpServerReply(mainPage());

   }

}

Una vez comprobado placa y puerto, súbelo. Abre una pestaña en el navegador de tu ordenador e introduce la dirección: 192.168.1.168

Entonces te aparecerá una pequeña web con los botones de control para los LED. Al pulsar en cada botón se realiza una nueva solicitud a Arduino, con diferente URL a la original. Arduino captura la nueva solicitud, y emplea la URL recibida para realizar las acciones oportunas.

Con este ejemplo y los de la librería, ya estás preparado para darle conectividad de Internet a tu Arduino.