Pages: [1]   Go Down
Author Topic: ethernet module help (enc28j60)  (Read 4959 times)
0 Members and 1 Guest are viewing this topic.
Hyderabad, India.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys, i got a new breakout board enc28j60
my connections are
enc28j60-arduino pin
CS-10
MOSI-11
MIS0-12
SCK-13
and 3.3-3.3, gnd-gnd
i got the following code and library from the website

http://trollmaker.com/article11/arduino-1-0-with-enc28j60-ethernet-shield-v1-1/

the library i opened enc28j60.c and found
#define ENC28J60_CONTROL_CS     10
#define SPI_MOSI            11
#define SPI_MISO            12
#define SPI_SCK            13


Code:
// A simple web server that turn an LED on or off"

#include "etherShield.h"
#include "ETHER_28J60.h"

int outputPin = 6;

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};   // this just needs to be unique for your network,
                                                           
static uint8_t ip[4] = {192, 168, 1, 15}; // IP address for the webserver

static uint16_t port = 80; // Use port 80 - the standard for HTTP

ETHER_28J60 e;

void setup()
{
  e.setup(mac, ip, port);
  pinMode(outputPin, OUTPUT);
}

void loop()
{
  char* params;
  if (params = e.serviceRequest())
  {
    e.print("<h1><a href='/?led=off'>Arduino Web Remote</a></h1>");
    if (strcmp(params, "?led=on") == 0)
    {
      digitalWrite(outputPin, HIGH);
      e.print("<a href='?led=off'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>LED IS ON</button></a>");
    }
    else if (strcmp(params, "?led=off") == 0)
    {
      digitalWrite(outputPin, LOW);
      e.print("<a href='?led=on'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>LED IS OFF</button></a>");
    }
    e.respond();
  }
}

i connected my enc28j60 to belkin router using lan cable

i upload the code and then hit 192.168.1.15 on browser and it doesnot respond, i ping 192.168.1.15 and request timed out, and i checked the LAN>DHCP client list in belkin router and only my pc was visible, enc28j60 was not.

also i tried connecting MOSI-12
MIS0-11 but it dint work

what am i doing wrong here?? is it network issue or my connections??
Logged

0
Offline Offline
Shannon Member
****
Karma: 159
Posts: 10409
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check the ENC28J60 datasheet - perhaps the reset line needs driving?  Find a schematic for the shield that code is designed for and compare with your set-up...
Logged

[ I won't respond to messages, use the forum please ]

Hyderabad, India.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Thanks, could you help me out please....

here is the schematic of ethernet shield
http://www.nuelectronics.com/download/projects/Ethernet_v1_0.pdf
Logged

Europe
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Electronics hobbist
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

May I ask which Arduino board have you been using?

ENC28J60 - Arduino
SISPI_MOSI
SOSPI_MISO
SCKSPI_SCK
CSENC28J60_CONTROL_CS

Arduino Uno
#define ENC28J60_CONTROL_CS 10
#define SPI_MOSI 11
#define SPI_MISO 12
#define SPI_SCK 13

Arduino Mega
#define ENC28J60_CONTROL_CS 53
#define SPI_MOSI 51
#define SPI_MISO 50
#define SPI_SCK 52

If an SD memory port exists on your ethernet board, you should have it disabled before using network, usually:
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);

You could also have a look at this library:
https://github.com/jcw/ethercard
Logged

Hyderabad, India.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey epam i am using UNO, and its a enc28j60 breakout board so no sd card slot on it....

the etherCard library works with my breakout board but i dont have a code in which we can toggle LED's in etherCard library.... etherShield library has perfect code to toggle led's but its not working smiley-sad
Logged

Europe
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Electronics hobbist
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I have also been using an ENC28J60 breakout board with Andy's EtherShield Library on Arduino Mega, for about a year now. However, Andy has stopped developing his library so I am migrating to Ethercard.

Here is a very basic example I have written for switching a LED:
Code:
// Ethercard LED example
#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,222 };

#define BUFFER_SIZE 500
byte Ethernet::buffer[BUFFER_SIZE];
BufferFiller bfill;

#define LED 13  // define LED pin
bool ledStatus = false;

const char http_OK[] PROGMEM =
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n\r\n";

const char http_Found[] PROGMEM =
    "HTTP/1.0 302 Found\r\n"
    "Location: /\r\n\r\n";

const char http_Unauthorized[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";

void homePage()
{
    bfill.emit_p(PSTR("$F"
        "<meta http-equiv='refresh' content='5'/>"
        "<title>Ethercard LED</title>"
        "ledStatus: <a href=\"?led=$F\">$F</a>"),
        http_OK,
        ledStatus?PSTR("off"):PSTR("on"),
        ledStatus?PSTR("ON"):PSTR("OFF"));
}

void setup()
{
    Serial.begin(9600);
   
    pinMode(LED, OUTPUT);
   
    if (ether.begin(BUFFER_SIZE, mymac) == 0)
        Serial.println("Cannot initialise ethernet.");
    else
        Serial.println("Ethernet initialised.");

    ether.staticSetup(myip);
}

void loop()
{
    digitalWrite(LED, ledStatus);   // write to LED digital output
   
    delay(1);   // necessary for my system
    word len = ether.packetReceive();   // check for ethernet packet
    word pos = ether.packetLoop(len);   // check for tcp packet

    if (pos) {
        bfill = ether.tcpOffset();
        char *data = (char *) Ethernet::buffer + pos;
       
        if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
            bfill.emit_p(http_Unauthorized);
        }
        else {
            data += 5;
           
            if (data[0] == ' ') {
                // Return home page
                homePage();
            }
            else if (strncmp("?led=on ", data, 8) == 0) {
                // Set ledStatus true and redirect to home page
                ledStatus = true;
                bfill.emit_p(http_Found);
            }
            else if (strncmp("?led=off ", data, 9) == 0) {
                // Set ledStatus false and redirect to home page
                ledStatus = false;
                bfill.emit_p(http_Found);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
       
        ether.httpServerReply(bfill.position());    // send http response
    }
}

Probably not the simplest one but it does the job well.
Good luck.
Logged

Hyderabad, India.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey epam thanks a lot man smiley, i uploaded the code you provided and it says ethernet initialized but nothing after that, when i open the webpage it does not load anything....

the backsoon example loads perfectly

EDIT

i made some modification and its working smiley-grin awesome smiley-grin

Code:
// Present a "Will be back soon web page", as stand-in webserver.
// 2011-01-30 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
 
#include <EtherCard.h>

#define STATIC 0  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,1,200 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define LED 5  // define LED pin
bool ledStatus = false;

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
BufferFiller bfill;

void setup(){
  Serial.begin(57600);
  Serial.println("\n[backSoon]");
 
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
#if STATIC
  ether.staticSetup(myip, gwip);
#else
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip); 
  ether.printIp("DNS: ", ether.dnsip); 
}
const char http_OK[] PROGMEM =
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n\r\n";

const char http_Found[] PROGMEM =
    "HTTP/1.0 302 Found\r\n"
    "Location: /\r\n\r\n";

const char http_Unauthorized[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";

void homePage()
{
    bfill.emit_p(PSTR("$F"
        "<meta http-equiv='refresh' content='5'/>"
        "<title>Ethercard LED</title>"
        "ledStatus: <a href=\"?led=$F\">$F</a>"),
        http_OK,
        ledStatus?PSTR("off"):PSTR("on"),
        ledStatus?PSTR("ON"):PSTR("OFF"));
}

void loop(){
  // DHCP expiration is a bit brutal, because all other ethernet activity and
  // incoming packets will be ignored until a new lease has been acquired
  if (!STATIC && ether.dhcpExpired()) {
    Serial.println("Acquiring DHCP lease again");
    ether.dhcpSetup();
  }
    digitalWrite(LED, ledStatus);
    Serial.println(ledStatus);
  // wait for an incoming TCP packet, but ignore its contents
   word len = ether.packetReceive();
   word pos = ether.packetLoop(len);
  if (pos) {
      // write to LED digital output
   
    delay(1);   // necessary for my system
        bfill = ether.tcpOffset();
        char *data = (char *) Ethernet::buffer + pos;
    if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
            bfill.emit_p(http_Unauthorized);
        }
        else {
            data += 5;
           
            if (data[0] == ' ') {
                // Return home page
                homePage();
            }
            else if (strncmp("?led=on ", data, 8) == 0) {
                // Set ledStatus true and redirect to home page
                ledStatus = true;
                bfill.emit_p(http_Found);
            }
            else if (strncmp("?led=off ", data, 9) == 0) {
                // Set ledStatus false and redirect to home page
                ledStatus = false;
                bfill.emit_p(http_Found);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
       
        ether.httpServerReply(bfill.position());    // send http response
    }

  }
« Last Edit: July 05, 2012, 06:58:36 am by kiriti » Logged

Hyderabad, India.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can any one help me modify the code so that it can control more than one device?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

More than one arduino?  These need to be unique:

Code:
static byte myip[] = { 192,168,1,200 };
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

Your trying to DHCP so the first one might not need to be unique though it will not hurt the second must be unique inside any layer 2 network so it could be the same if they were scattered around the country.  For ease of use I put a number in the last eeprom position as use it for the last byte of each so my code is always the same.
Logged

Hyderabad, India.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i meant more than one leds, not more than one arduino smiley
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i meant more than one leds, not more than one arduino smiley

Just expand out the if else like so.

Code:
if (strcmp(params, "?led0=on") == 0)
    {
      digitalWrite(outputPin0, HIGH);
      e.print("<a href='?led0=off'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>LED IS ON</button></a>");
    }
    else if (strcmp(params, "?led0=off") == 0)
    {
      digitalWrite(outputPin0, LOW);
      e.print("<a href='?led0=on'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>LED IS OFF</button></a>");
    }
    else if (strcmp(params, "?led1=on") == 0)
    {
      digitalWrite(outputPin1, HIGH);
      e.print("<a href='?led1=off'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>LED IS ON</button></a>");
    }
    else if (strcmp(params, "?led1=off") == 0)
    {
      digitalWrite(outputPin1, LOW);
      e.print("<a href='?led1=on'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>LED IS OFF</button></a>");
    }

You will need to define the new outputPins and otherwise clean it up.
Logged

Hyderabad, India.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it says params undefined, i guess its because params is only for etherShield and not etherCard
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

More on the sort of thing you are trying to do at...

ArduServer.com

Beware: When working with Arduinos as web servers, at least with the shields and libraries I've used, failure tends to be messy. Often, not a simple, "nice", failure to run program with an error message. Things Just Go Weird.

TWO sorts of "too big" seem to come into play....

If EITHER....

your overall program is too big (has to be pretty big, unless you do silly things declaring variables.... unless you are using an old Arduino clone with limited memory)

the web-page you are trying to have the Arduino serve, i.e. the HTML it is sending, is too big... and this doesn't have to be very big at all.

... then things can just freeze, or you may get inconsistent gibberish served by the Arduino.

If any two eyed kings out there know how one eyed kings like me get into the sort of trouble described, comments would be welcome!
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am having trouble with my module as well...i have it hooked up for my mega as it said earlier in the post and it won't go past the ether.begin
Logged

Pages: [1]   Go Up
Jump to: