I've recently bought an ethernet enc28j60 to try it with my arduino mega 2560 and an example code, but when i try to run it, it seems that the program doesn't work. When I put in the cmd ping plus the ip i gave to the arduino it appears that host is inaccessible.
I'm connecting my arduino to my laptop and there's a server between arduino and the router and i don't know how to configure the code.
I need help.
I can help you
please tell me how
if you show what you have
Now i read the instructions so i know what i have to post.
First the following is my code. I followed an example from a page.
There are parts in spanish because that's my native language, but i think i manage myself well with my reading and writing skills in english.
/*Ejemplo 02: Ethernet Hanrun HR911105A*/
#include <EtherCard.h>
// Enviar y recibir buffer (TCP/IP)
byte Ethernet::buffer[500];
// Si colocamos 1 deshabilitamos el DHCP
// Si colocamos 0 colocamos una IP estática
#define ESTATICA 1
#if ESTATICA
// Colocamos la dirección IP al dispositivo
static byte myip[] = { 192, 168, 1, 220 };
// Colocamos la dirección IP de la puerta de enlace de nuestro red
static byte gwip[] = { 192, 168, 1, 12 };
#endif
// Colocamos la dirección MAC, que sera único en nuestra red
static byte mymac[] = { 0xD8, 0xB2, 0xC1, 0x24, 0x18, 0xD3 };
const char page[] PROGMEM =
"HTTP/1.0 503 Ethernet Hanrun HR911105A - \r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
"<head><title>"
"Ejemplo #2: Ethernet Hanrun HR911105A"
"</title></head>"
"<body>"
"<h3>Hola Mundo</h3>"
"<p><em>"
"Esto es una prueba.<br />"
":)."
"</em></p>"
"</body>"
"</html>"
;
void setup() {
Serial.begin(19200);
Serial.println("\n[Vuelva pronto]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Error al acceder al controlador Ethernet");
#if ESTATICA
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
Serial.println("DHCP falló");
#endif
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
}
void loop() {
//Se espera un paquete TCP entrante, luego se ignora el contenido
if (ether.packetLoop(ether.packetReceive())) {
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page - 1);
}
}
I change the IP and mac adress from the example for mine.
The folllowing picture is how i wired it
The arduino is connect to my laptop.
The problem is that it says that the host is innacesible.
I've already watch a lot of guides but i don't know how to proceed.
you could try my EthernetENC library with the WebClient example of the Ethernet library available in the IDE Examples menu. only change the include to EthernetENC.h
The WebClient example has some diagnostics.
I change and use the code as you say
/*
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
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <EthernetENC.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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 231);
IPAddress myDns(192, 168, 1, 1);
// 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;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
beginMicros = micros();
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
byteCount = byteCount + len;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
delay(1);
}
But now when i run the code it just print "Initialize Ethernet with DHCP:" and that's all.
try to wire the reset pin to reset pin of the Mega
try
//Serial.println("Initialize Ethernet with DHCP:");
// if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
// } else {
// Serial.print(" DHCP assigned IP ");
// Serial.println(Ethernet.localIP());
// }
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.