Pages: [1]   Go Down
Author Topic: Arduino can't connect to the Internet  (Read 1781 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

i just build a device to measure my pool temperatures and send these temperatures to my local(!!) apache webserver.
The Arduino works as Server and Client. This worked fine.

Now i want my website to be on an external webserver, so my computer doesn't have to run all day long.

But long story short, thats my problem:
My Arduino CAN'T connect (as client) to any server outside of my local network.
Even though,
- I can access to my arduino (acting as server) from the internet.
- My Arduino can connect to my local webserver (acting as client) and send temperature data.

I just don't understand why the arduino can receive commands from the internet (server mode), but can't send commands by itself (client mode).

My sketch is veery long, so here are the important parts:
Code:
   #include <SPI.h>
    #include <Ethernet.h>
      byte ip[] = { 192, 168, 178, 16 };   //ip address to assign the arduino
      byte gateway[] = { 192, 168, 178, 1 };    // the router's gateway address
      byte subnet[] = { 255, 255, 255, 0 };
      byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xEA, 0xC9 };

      byte bridge_server[] = {192, 168, 178, 21}; // LOCAL SERVER, WORKS!

//     char bridge_server[] = "pool.xxxxxxx.de"; // WEBSERVER'S DOMAIN, DOESN'T WORK
//     byte bridge_server[] = {88, 84, xxx, xxx}; // WEBSERVER'S IP, DOESN'T WORK
//     byte bridge_server[] = {74, 125, 224, 72}; //GOOGLE'S IP, DOESN'T WORK

    EthernetServer server = EthernetServer(80); // LISTEN ON PORT 80,
   // I SET UP THE PORTFORWARDING IN MY ROUTER, I CAN ACCESS TO THE ARDUINO FROM OUTSIDE OF
   // MY NETWORK (tested with iphone and wlan off)
void setup()
{
      Ethernet.begin(mac, ip, gateway, subnet); // initialize the ethernet device
     server.begin(); // start listening for clients
}

void loop()
{
    EthernetClient client;
    if (client.connect(bridge_server, 80)) // HERE IT STUCKS, client.connect returns false (exception: local webserver 192.168.178.21)
    {
      Serial.println("CONNECTION OK!");
      ... do stuff ...
    }
    else
    {
      Serial.println("CANT CONNECT!");
    }

      EthernetClient client = server.available();
      if (client)
      {
          while (client.connected())
          {
            if (client.available())
            {
              .... do something and answer to the connected client, works fine.
            }
          }
       }
}

Does anyone have a solution for my problem??
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some client test code:

Code:
//zoomkat 2-13-12
//DNS and DHCP-based web client test code
//for use with IDE 1.0
//open serial monitor and send an e to test
//and to see test result
//for use with W5100 based ethernet shields
//browser equivelant URL:
// http://web.comporium.net/~shb/arduino.txt
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){
  Serial.begin(9600);
  Serial.println("DNS and DHCP-based web client test 2/13/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  // 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:
    while(true);
  }
  // print your local IP address:
  Serial.print("Arduino 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();
  Serial.println();
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Sr. Member
****
Karma: 2
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

i figured out what went wrong.

The problem must have been related to my router (dhcp-server), when i only use mac and ip in Ethernet.begin() it works.
Logged

New Zealand
Offline Offline
Newbie
*
Karma: 1
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From IDE version 1.0 there was an update to the Ethernet library which meant that the parameters for the .begin() method are different.

The updated documentation: http://arduino.cc/en/Reference/EthernetBegin

You now need to specify the IP address of the DNS server (most times this would be the same as your router/gateway):
Code:
Ethernet.begin(mac, ip, dns, gateway, subnet);

By using the old parameter order with the new library, you would have been setting the gateway IP address to the subnet.

Hope that helps!
Logged

Arduino Internet / Web Control with Teleduino
https://www.teleduino.org

Pages: [1]   Go Up
Jump to: