1

I'm trying to make a simple GET request to a server (which has a static IP but does not have a name). I'm using EtherCard library and a cheap ENC28J60 Ethernet Module. The basic configurations are OK (I can get DHCP assignment and DNS lookup), but when I try to make a simple GET request with the browseUrl method, nothing happens. I've tried to call the page from the browser and everything works fine, so it's not a server issue. The code is the following:

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x54, 0x55, 0x58, 0x10, 0x00, 0x24 };

const char website[] PROGMEM = "http://10.10.10.10"; // my server
const char dnsLook[] PROGMEM = "google.com";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

// called when the client request is complete
static void requestCallback (byte status, word off, word len) {
  Serial.print(">>>");
  Serial.print(status);
  Serial.print("-");
  Serial.print(status);
  Serial.print("-");
  Serial.print(status);
  Serial.println("<<<");
}

void setup () {

  Serial.begin(9600);
  delay (10000);
  int res = ether.begin(sizeof Ethernet::buffer, mymac, 8);

  if (res == 0) 
    Serial.println( "Cannot access ethernet interface");

  const static uint8_t ip[] = {192,168,1,117};
  const static uint8_t gw[] = {192,168,1,1};
  const static uint8_t dns[] = {8,8,8,8};

  if (!ether.staticSetup(ip, gw, dns))
  {
      Serial.println("Cannot set static parameters");
  }

  // manually set netmask
  ether.netmask[0] = 255;
  ether.netmask[1] = 255;
  ether.netmask[2] = 255;
  ether.netmask[3] = 0;

  // dns lookup
  if (!ether.dnsLookup(dnsLook))
    Serial.println("DNS failed");
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  if (millis() > timer) {
    timer = millis() + 10000;
    ether.browseUrl(PSTR("/page.php?"), "var=value", website, requestCallback);
  }
}
3
  • run example code first – jsotola Jun 8 at 16:51
  • 2
    I recommend you to use my EthernetENC library – Juraj Jun 8 at 17:09
  • @Juraj I'll give a try – user2959923 Jun 9 at 6:39
3

I made it by using EthernetENC library, as suggested by @Juraj. To install the library, simply use the library manager and search for EthernetENC. For those in need, below is a working code

#include <SPI.h>
#include <EthernetENC.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // ethernet module MAC address
IPAddress server(10,10,10,10);                        // server IP address, without DNS
//char server[] = "www.site.com";                     // if your server has a DNS name, use this variable for connection

// static configuration variables
IPAddress staticIp(192, 168, 1, 217);
IPAddress staticDnsServer(8, 8, 8, 8);

EthernetClient client;                                // ethernet client object

void setup() {
  // configure CS pin
  Ethernet.init(8);

  // begin serial connection
  Serial.begin(9600);

  // wait some seconds to let the module start
  delay(5000);

  // initialize ethernet module with static configuration
  Ethernet.begin(mac, staticIp, staticDnsServer);

  // wait to let the module properly initialize
  delay(1000);

  // connection to the server
  Serial.print("Connecting to: ");
  Serial.print(server);
  Serial.println("...");

  // notify successful connection
  if (client.connect(server, 80)) {

    Serial.print("Connected to ");
    Serial.println(client.remoteIP());

    // simple GET request
    client.println("GET /page.php?var=test HTTP/1.1");
    client.println("Host: 10.10.10.10");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.println();

  } else {
    // connection failed notifiation
    Serial.println("Connection failed");
  }
}

void loop() {

  // if I got a response from the server, read and print to serial
  int len = client.available();
  if (len > 0) {
    byte buffer[80];
    if (len > 80) len = 80;
    client.read(buffer, len);
    Serial.write(buffer, len);
  }

  // disconnection
  if (!client.connected()) {

    Serial.println();
    Serial.println("Disconnected");
    client.stop();

    // stop execution
    while (true) {
      delay(1);
    }
  }
}

at this link, you can see how to properly connect this module to Arduino UNO.

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.