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
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
// 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??
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...
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);
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
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:
// 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.
hey epam thanks a lot man :), 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 awesome
// 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
}
}
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.
More on the sort of thing you are trying to do at...
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!
hey kirti I have bought the 28j60 ethernet module i am connecting it with arduino uno with same settings and same code as you did, but my ethernet module is not responding , the power led on the module turns on but the led's on rj45 jack does not blink, theirs no response from the browser as well,
can you please tell me what corrections did you make to make it work..