An idea I have in my mind is to create a mood light with arduino. It also should have an internet connection to be able to pull some information from the web. For example it could poll the data from yahoo weather and would change its color according to the forecast for the day…
The other day I bought a ENC28J60 ethernet breakout board on eBay for around $4.50. Lots cheaper than an ethernet shield of course.
To test it out I connected the ENC28J60 to my Arduino UNO according to this post. Which means I made the following connections:
VCC – 3.3V
GND – GND
SCK – Pin 13
SO – Pin 12
SI – Pin 11
CS – Pin 8
After that I downloaded the ethercard library from github. It also comes with a few helpful examples.
My sketch below is a mixture of examples: depending on the settings it gets an IP via DHCP or it connects statically to 192.168.0.200. It then waits for GET requests to turn the LED on pin 10 on or off.
#include <EtherCard.h> #define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below) // mac address static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; // ethernet interface ip address static byte myip[] = { 192,168,0,200 }; // gateway ip address static byte gwip[] = { 192,168,0,1 }; // LED to control output int ledPin10 = 10; byte Ethernet::buffer[700]; char page[] PROGMEM = "HTTP/1.0 503 Service Unavailable\r\n" "Content-Type: text/html\r\n" "Retry-After: 600\r\n" "\r\n" "<html>" "<head><title>" "Service Temporarily Unavailable" "</title></head>" "<body>" "<h3>This service is currently unavailable</h3>" "<p><em>" "The main server is currently off-line.<br />" "Please try again later." "</em></p>" "</body>" "</html>" ; void setup () { pinMode(ledPin10, OUTPUT); Serial.begin(9600); Serial.println("Trying to get an IP..."); Serial.print("MAC: "); for (byte i = 0; i < 6; ++i) { Serial.print(mymac[i], HEX); if (i < 5) Serial.print(':'); } Serial.println(); if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) Serial.println( "Failed to access Ethernet controller"); #if STATIC Serial.println( "Getting static IP."); if (!ether.staticSetup(myip, gwip)){ Serial.println( "could not get a static IP"); blinkLed(); // blink forever to indicate a problem } #else Serial.println("Setting up DHCP"); if (!ether.dhcpSetup()){ Serial.println( "DHCP failed"); blinkLed(); // blink forever to indicate a problem } #endif ether.printIp("My IP: ", ether.myip); ether.printIp("Netmask: ", ether.mymask); ether.printIp("GW IP: ", ether.gwip); ether.printIp("DNS IP: ", ether.dnsip); } void loop () { word len = ether.packetReceive(); word pos = ether.packetLoop(len); // IF LED10=ON turn it ON if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=ON") != 0) { Serial.println("Received ON command"); digitalWrite(ledPin10, HIGH); } // IF LED10=OFF turn it OFF if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=OFF") != 0) { Serial.println("Received OFF command"); digitalWrite(ledPin10, LOW); } // show some data to the user memcpy_P(ether.tcpOffset(), page, sizeof page); ether.httpServerReply(sizeof page - 1); } void blinkLed(){ while (true){ digitalWrite(ledPin10, HIGH); delay(500); digitalWrite(ledPin10, LOW); delay(500); } }
The output on the serial monitor looks as follows:
Trying to get an IP...
MAC: 74:69:69:2D:30:31
Setting up DHCP
My IP: 192.168.0.24
Netmask: 255.255.255.0
GW IP: 192.168.0.1
DNS IP: 62.2.24.162
Firing up Firefox and pasting
http://192.168.0.24/?LED10=ON
in the URL bar turns the LED on,
http://192.168.0.24/?LED10=OFF
turns it off…
Hi there, I’ve got the same card and I’m trying to do the same, I followed all the steps you mentioned before but on the serial output I get a 0.0.0.0 Subnet , so I’m not even able to ping it. I’m not using DHCP so I’ve also added manually a
“static byte mymask[] = { 255,255,255,0 };” but the card doesn’t seem to read it. Don’t know what is wrong whit it.
BTW, great blog, many ideas are just awesome! Keep up the great work man!
Thanks Alessio!
about your problem: check this line
static byte gwip[] = { 192,168,0,1 };
is your router at 192.168.0.1? to find out open up a command line and type
ipconfig
search for the line “Default Gateway” and check the ip given and change it if you need to.
This of course only works if your on a windows comp.
comment the line : ether.printIp(“Netmask: “, ether.mymask);
My gateway is on another IP address and I had yet modified the gwip coherently.
Still wondering if it’s a board issue or just some library lines to modify.
Thanks anyway for the reply.
Hey
first … thnx 4 this useful blog ^_^
but i have problems 🙁 I get those lines (Trying to get an IP…
MAC: 74:69:69:2D:30:31
Setting up DHCP
)
but without anything else 🙁 and when I’m trying to reset my arduino many times I get (Trying to get an IP…
MAC: 74:69:69:2D:30:31
Failed to access Ethernet controller
)
or
it gives me IP address but when I’m trying to put it into my browser it gives me error … and i tried to ping it but it seems it isn’t connected
waiting for ur reply 🙂
maybe you try
#define STATIC 1
and make sure you update the gateway according to your settings, ie is it
192.168.0.1
?
Hi everting seems to be working fine but as soon as i turn the led on i cant turn if of and the bord keeps time out if i ping it
Failed to get DHCP everytime. My LAN port also doesn’t have its lights on. I connected everything as told. I don’t know what is wrong
Notice that the specification for the 3.3v regulator is only 50mA
The ENC28J60 can draw up to 300mA
If you feel the regulator running hot, you have an instable connection.
Try adding a larger regulator like LD1086v33 to your project
Thnx a lot for your sketch. I have found a mistake to use
#Define Static 1
then you must use :
#if Static
….
#endif
All works fine then.
i had connected enc28j60 ethernet controller and my pc to the same Router. Then it was connected over internet successfully .But when i connected both to My LAN port instead of Router then it was not connected over ethernet successfully.
In my program i have only assigned IP and MAC address to the ethernet controller.
Can anybody tell me what is the issue causing it ?
Your Ethernet port does not have automatic MDI/MDI-X capability. ENC does not, a lot of modern computers do.
Everything Works ! Serial output is perfect.
The ip adress is set and no errors.
but the led at the pin is not ON or OFF
Have a problem,
I use Uno with 28J60, but the compiler is exit with status 1
variable ‘page’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
The problematic line:
char page[] PROGMEM =
Whai can I do with this?
Thomas
hello,
i solved the problems
“char page[] PROGMEM” = >> “char const page[] PROGMEM =”
AND
“static byte mymask[] = { 255,255,255,0 };” TO “static byte netmask[] = { 255,255,255,0 };”
Hello, nice blog!
I have a little problem I setup static, and he will not give me full ip
static byte myip[] = { 10,50,33,227 };
// gateway ip address
static byte gwip[] = { 10,50,33,225 };
static byte netmask[] = { 255,255,255,0 }; … and serial print is
My ip: 10.50.3
can anyone help Me? thy
Arduino Mega 2650 port with the ENC28J60 below:
//For those of you wanting a 2650 mega install with the ENC28J60, use the following pins..
//cs = 53
//s1 = 51
//s0 = 50
//sck = 52
//reset = reset
//vcc = 3.3v
//gnd = gnd
//Works perfectly, semi modified the script below to output a page with links to turn on/off the led on pins 10 and gnd respectivly on any ip.
//As you can see below, im using dhcp.
//Sped up the baud because I had problems with the page and links after a few clicks. Everything is ok now. Great example!
#include
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,3,200 };
// gateway ip address
static byte gwip[] = { 192,168,3,1 };
// LED to control output
int ledPin10 = 10;
byte Ethernet::buffer[700];
const char page[] PROGMEM =
“HTTP/1.0 503 Service Unavailable\r\n”
“Content-Type: text/html\r\n”
“Retry-After: 600\r\n”
“\r\n”
“”
“”
“Public Service Temporarily Unavailable”
“”
“”
“This service is currently beta testing”
“”
“ON”
“OFF”
“”
“”
“”
;
void setup () {
pinMode(ledPin10, OUTPUT);
Serial.begin(57600);
Serial.println(“Trying to get an IP…”);
Serial.print(“MAC: “);
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
Serial.println( "Failed to access Ethernet controller");
#if STATIC
Serial.println( "Getting static IP.");
if (!ether.staticSetup(myip, gwip)){
Serial.println( "could not get a static IP");
blinkLed(); // blink forever to indicate a problem
}
#else
Serial.println("Setting up DHCP");
if (!ether.dhcpSetup()){
Serial.println( "DHCP failed");
blinkLed(); // blink forever to indicate a problem
}
#endif
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
}
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
// IF LED10=ON turn it ON
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=ON") != 0) {
Serial.println("Received ON command");
digitalWrite(ledPin10, HIGH);
}
// IF LED10=OFF turn it OFF
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=OFF") != 0) {
Serial.println("Received OFF command");
digitalWrite(ledPin10, LOW);
}
// show some data to the user
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page – 1);
}
void blinkLed(){
while (true){
digitalWrite(ledPin10, HIGH);
delay(500);
digitalWrite(ledPin10, LOW);
delay(500);
}
}
Hello!, I’ve tried the same thing and when I entered the static ip address in the address bar, it says the site cannot be reached, it refused to connect. Can anyone please help me solve this problem?
did you check the first comment? http://blog.roman-mueller.ch/index.php/2012/10/03/connecting-the-enc28j60-ethernet-breakout-board/#comment-234
nice project,
I’m trying to do the same with LAN only, but I don’t know where to start. I appreciate any help rendered.
Hi & Thanks
I have problem to compile this in my arduino compiler
I am amateour
Please help me to add ethercard to my library
Hi Reza, I did exactly how I’ve been told here:
https://github.com/PaulStoffregen/EtherCard/blob/master/README.md
hope that helps!