2

I am making a circuit which turns a LED on and off through internet. I am connecting an Arduino to a LED and to an Ethernet controller(ENC28J60), but when I make a GET request to the device, it takes too long to respond - around 2 minutes - but my internet connection is fast.Before i get ip "ether.begin(sizeof Ethernet::buffer, mymac,8)" command fails it returns zero still i am able to control it.

Code:

#include <EtherCard.h>

#define STATIC 0

// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 172,26,40,250 };
// gateway ip address
static byte gwip[] = { 172,26,40,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"
"<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(':');
  }

  if (ether.begin(sizeof Ethernet::buffer, mymac,8) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  else
    Serial.println("\nEthernet Controller Initialized");


#if STATIC
  Serial.println( "Getting static IP.");
  if (!ether.staticSetup(myip, gwip)){
    Serial.println( "could not get a static IP");

  }
#else

  Serial.println("Setting up DHCP");
  if (!ether.dhcpSetup()){
    Serial.println( "DHCP failed");
  }
#endif

  ether.printIp("My IP: ", ether.myip);
  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);
}

circuit

migrated from electronics.stackexchange.com Nov 9 '16 at 15:53

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.