I am currently trying to get the Ethernet Shield working on my Mega. I was trying to run the Webserver example but the program seems to stuck at one point, so I tried to start from scratch.
This is my test code:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0F, 0xF6, 0x3D
};
byte subnet[] = { 255,0,0,0 };
byte gateway[] = { 2,0,0,1 };
IPAddress ip(2, 0, 0, 1);
EthernetServer server(80);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
Serial.println("Ethernet started");
server.begin();
Serial.println("Server started");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Loop");
}
The output I get from the serial console is:
Etrted
Ethernet started
So I think the program gets stuck inside the EthernetServer::begin() function.
I am aware that there are earlier versions of ethernet shields which are not compatible to the mega, but the vendor of my shield says it is.
Also I don't understand, why it outputs the first line.
Thanks for your hints!
Arduino.cc and Arduino.org are not the same... Arduino.org, who is selling the ethernet shield 2, has their own IDE with the correct library! You can download it at http://www.arduino.org/downloads and the source can be found at https://github.com/arduino-org/Arduino/tree/1.7.4/libraries
Try this code form (http://www.arduino.cc/en/Tutorial/DhcpAddressPrinter):
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}
And post what the serial monitor output is.
Try this:
NB. You can reuse your code written for Arduino Ethernet Shield, simply replacing
#include <Ethernet.h> --> #include <Ethernet2.h>
#include <EthernetUdp.h> --> #include <EthernetUdp2.h>
See this: http://labs.arduino.org/Arduino+Ethernet+Shield+2
Related
Good day, I have this problem with my simple project. I have a database with all RFID number of employee. What I want to do is to use an rfID to check if it is registered in the database. If it is, it would return a value of 1 else would be a value of 0.
I tried with arduino HanRun Ethernet shield. I was able to get a response from the php web server to the serial monitor. Tried the seeedrfid it successfully get the rfcard number. However, when i tried to combine the two, it would hang on Ethernet.begin(mac, ip) initialization. My code is below.
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include <SeeedRFID.h>
#define RFID_RX_PIN 10
#define RFID_TX_PIN 11
SeeedRFID RFID(RFID_RX_PIN, RFID_TX_PIN);
char state = '0';
char c;
byte mac[] = {0x60, 0xF8, 0x1D, 0xBA, 0x1D, 0x52};
IPAddress ip(192, 168, 20, 228);
EthernetClient client;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Serial set up");
Ethernet.begin(mac, ip);
Serial.print("My IP Address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
long rfID;
// put your main code here, to run repeatedly:
if (RFID.isAvailable()) {
rfID = RFID.cardNumber();
Serial.println(rfID);
}
}
Serial.println(rfID) is not executed unless I remove the Ethernet.begin(mac, ip). Can anybody point me to the right direction?
Thank you everyone for helping out. My supervisor who gave me the shield just remembered that there might be a pin changes(or anything you call it). That I might need to try other pin in order for the rf reader and ethernet shield to work together. now its working fine..
I've purchased two of the Arduino Ethernet Shields made by Arduino. I am using the Arduino Uno for my project. I cannot seem to assign an IP address to the shield. When I run the following code I receive 0.253.253.253 as the IP for one of the shields, and for the other one I receive 0.15.15.15 with the exact same code. I have no Idea what's going on here. Any help would be appreciated. Thanks
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x30, 0x71 };
IPAddress ip(192,168,1,22);
void setup() {
Serial.begin(9600);
// disable SD card if one in the slot
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
delay(5000);
Serial.println("Starting w5100");
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());
}
void loop() {
}
If both the boards are connected on the same network then they can't have the same IP address, change the address of one of the boards.
edit :
Check if it is indeed starting up properly
if(!Ethernet.begin(mac, ip))
Serial.println("failed");
else
Serial.println(Ethernet.localIp());
Also try it without the ip, to check if your router can assign the ip address.
I am trying to use Ethernet module ENC28J60 (http://www.amazon.com/ENC28J60-Ethernet-Network-Module-STM32/dp/B008B4QIV2) and SD Card module in single project. I have to read data from the network and write them into the micro sd card.
How can I achieve this? I am getting following output from by sketch below
initialization done.
DONE Ethernet as well with IP: 255.255.255.255
SD card is initialized correctly, but Ethernet is not.
I have used pin 10,4 for CS of Ethernet, SD card respectively.
Program is as below
#include "SD.h"
#include "SPI.h"
#include "Ethernet.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,200);
IPAddress gateway(192,168,1,99);
IPAddress mask(255,255,255,0);
//File myFile;
EthernetServer server(80);
void setup()
{
Serial.begin(9600);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}else {
Serial.println("initialization done.");
}
Ethernet.begin(mac, ip,gateway,gateway,mask);
server.begin();
delay(5000);
Serial.print("DONE Ethernet as well with IP: ");
Serial.println(Ethernet.localIP());
}
void loop(){
//todo
}
Just declare both CS pins as output. It will work.
I use the combination of Arduino mega r3 and ethernet shield. When using the example of DhcpAddressPrinter, I can not get any result. I did not change any code.
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}
Afterwards, I added some "println" in the code as follows:
#include
#include
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
Serial.println("1");
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("2");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("3");
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}
I can get the result 1 and 2 from Serial Monitor, but cannot receive 3;
So I doubt that the function Ethernet.begin(mac) is keeping running all the time and don't know why.
I have change the mac address to others, but get the same result.
Are you using a microSD in the same time ? Sometimes it can create problems with DHCP on the Ethernet Shield
I have a brand new Ethernet shield on Arduino Uno and have worked through many (non-Ethernet) examples without any issues, until I tried to use the Ethernet shield.
Using the provided EthernetClient example, I get a connection failed. The return code is -5 (and I could only find answers for -4 through 1).
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
Created 18 Dec 2009
Modified 9 Apr 2012
by David A. Mellis
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0x71 };;
char server[] = "google.com"; // Google
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// Give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
Serial.println("Obtaining local IP address");
IPAddress myIPAddress = Ethernet.localIP();
Serial.println(myIPAddress);
// if you get a connection, report back via serial:
int ret = client.connect(server, 80);
if (ret == 1) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("Connection failed");
Serial.println(ret);
Serial.println(client.status());
}
}
void loop()
{
// If there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// If the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting.");
client.stop();
// Do nothing forevermore:
for(;;)
;
}
}
The results are always:
Connecting...
Obtaining local IP address
192.168.0.7
Connection failed
-5
0
disconnecting.
Not sure why this helped, but adding a delay after the Serial is intialized, before beginning Ethernet, and also increasing the delay before using Ethernet seemed to work.
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
modified 9 Apr 2012
by David A. Mellis
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0x71 };;
char server[] = "google.com"; // Google
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
delay(5000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
delay(5000);
Serial.println("connecting...");
Serial.println("Obtaining local IP");
IPAddress myIPAddress = Ethernet.localIP();
Serial.println(myIPAddress);
// if you get a connection, report back via serial:
int ret = client.connect(server, 80);
if (ret == 1) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println(ret);
Serial.println(client.status());
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for(;;)
;
}
}
That Google IP address (173.194.33.104) is not valid now. Try to use 74.125.226.242 instead:
IPAddress server(74,125,226,242); // Google
And before your try it on Arduino, ensure your can open this IP address in your browser:
http://74.125.226.242
Try defining the server IP address as it is shown on the Arduino Reference Page:
byte server[] = { 64, 233, 187, 99 }; // Google
Try a couple different example programs. There have been some revisions with the move to IDE 1.0 that could affect compatibility.