Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:16:03+00:00 2026-05-26T03:16:03+00:00

I have this web client example that gets an IP address via DHCP. It

  • 0

I have this web client example that gets an IP address via DHCP. It connects to my router at IP address 192.168.0.1 successfully, but then it fails to send a GET HTTP to google.com. Essentially, I cannot allow the traffic from Arduino to go out into the Internet.

I have a Linksys/Cisco E2000 router connected to the Arduino.

My code is below.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetDHCP.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x78, 0x0B};
byte ip[] = {192, 168, 0, 125};
byte gateway[] = {192, 168, 0, 1};
byte subnet[] = {255,255,255,0};
byte serverLocal[] = { 192,168,0,1 }; // Google
byte serverExternal[] = { 173,194,33,104 }; // 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):
Client clientLocal(serverLocal, 80);
Client clientExternal(serverExternal, 80);

const char* ip_to_str(const uint8_t*);

void setup() {
    Serial.begin(9600);

    Serial.println("Attempting to obtain a DHCP lease...");

    // Initiate a DHCP session. The argument is the MAC (hardware) address that
    // you want your Ethernet shield to use. This call will block until a DHCP
    // lease has been obtained. The request will be periodically resent until
    // a lease is granted, but if there is no DHCP server on the network or if
    // the server fails to respond, this call will block forever.
    // Thus, you can alternatively use polling mode to check whether a DHCP
    // lease has been obtained, so that you can react if the server does not
    // respond (see the PollingDHCP example).
    EthernetDHCP.begin(mac);

    // Since we're here, it means that we now have a DHCP lease, so we print
    // out some information.
    const byte* ipAddr = EthernetDHCP.ipAddress();
    const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
    const byte* dnsAddr = EthernetDHCP.dnsIpAddress();

    Serial.println("A DHCP lease has been obtained.");

    Serial.print("My IP address is ");
    Serial.println(ip_to_str(ipAddr));

    Serial.print("Gateway IP address is ");
    Serial.println(ip_to_str(gatewayAddr));

    Serial.print("DNS IP address is ");
    Serial.println(ip_to_str(dnsAddr));

    // if you get a connection, report back via serial:
    if (clientLocal.connect()) {
        Serial.println("connected internally");
        // Make a HTTP request:
        clientLocal.println("GET /index.html HTTP/1.0");
        clientLocal.println();
    }
    else {
        // kf you didn't get a connection to the server:
        Serial.println("connection failed internally");
    }

    // if you get a connection, report back via serial:
    if (clientExternal.connect()) {
        Serial.println("connected externally");
        // Make a HTTP request:
        clientExternal.println("GET /search?q=arduino HTTP/1.0");
        clientExternal.println();
    }
    else {
        // kf you didn't get a connection to the server:
        Serial.println("connection failed externally");
    }
}

void loop()
{
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (clientLocal.available()) {
        char c = clientLocal.read();
        Serial.print(c);
    }

    // if the server's disconnected, stop the client:
    if (!clientLocal.connected()) {
        Serial.println();
        Serial.println("disconnecting.");
        clientLocal.stop();
    }

    // if there are incoming bytes available
    // from the server, read them and print them:
    if (clientLocal.available()) {
        char c = clientLocal.read();
        Serial.print(c);
    }

    // if the server's disconnected, stop the client:
    if (!clientExternal.connected()) {
        Serial.println();
        Serial.println("disconnecting.");
        clientExternal.stop();

        // do nothing forevermore:
        for(;;)
            ;
    }
}

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:16:03+00:00Added an answer on May 26, 2026 at 3:16 am

    Here’s an example of working code (maybe you used this as a source?). The only differences I could see are the lines:

    Serial.begin(9600);
    delay(1000);
    

    just before the line:

    if (clientLocal.connect()) {
    

    I doubt the additional Serial.begin() has any effect but the delay() may be required (although it’s a long shot…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little problem. My client wanted two distinct swf on a web
I have this web application that has grown to an unmanageable mess. I want
I have this .NET web app that draws a table in Page_Load. Then after
I have this c# web app that relies heavily on two dll files that
I have seen some links that tells how to use web client but one
I have seen some links that tells how to use web client but one
I have website that uses ASP.net Membership Provider. This web site will be served
I have this web application using Spring Web Flow framework. In my main page
I have this web service : using System.Web.Services; namespace Contracts { [WebService(Namespace = http://tempuri.org/)]
So we have this web app where we support UTF8 data. Hooray UTF8. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.