Pages: [1]   Go Down
Author Topic: Unable to connect ethernet shield to internet  (Read 1381 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i am trying to connect arduino to twitter using ethernet sheild. But i am unable to connect to the website. I think the arduino is getting an IP assigned , but i think the problem is at client.connect() function. pls help me. here is my code
Code:
#include <SPI.h>
#include <Ethernet.h>

byte mac[]      = {  0x90, 0xA2, 0xDF, 0xC4, 0x70, 0x69 };  // MAC Address of Arduino Ethernet Shield
        byte ip[]       = { 192,168,1,177 };                       // IP assigned to arduino
        byte gateway[]  = { 192,168,1,1 };                          // Gateway address
        byte subnet[]   = { 255,255,255,0 };                        // Subnet Mask address
byte server[]   = {199, 59,  148, 10 };                     // IP address of Twitter

        EthernetClient client;

void setup()
{
   
                Serial.begin(9600);
                Serial.println("Program Initailising...");
                delay(2000);
     
Ethernet.begin(mac, ip, subnet, gateway);
delay(1000); 
Serial.println("connecting...");
                 
                 
}

void loop()
{                if(client.connect(server,80))
                {
                        client.println("GET http://www.twitter.com/statuses/user_timeline/HAutomation.rss HTTP/1.0");  // Senting an GET request to Twitter
                        client.println();
                }
                else
                {
                        Serial.println("Unable to connect to Twitter");
                }
                  // if there are incoming bytes available
  // from the server, read them and print them:
                if (client.available()) {
                char c = client.read();
                Serial.print(c);
                }

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

}

 
  }
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8068
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using Arduino 1.0 then the arguments to Ethernet.begin() have changed.  Your best bet is to pass only the MAC address and let DHCP assign everything else.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i tried that, when ethernet.begin() has only mac as its argument, it took a little more time to disconnect, otherwise it disconnects fast
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do not send the URL in the GET, just the file.
Code:
         if(client.connect(server,80))
         {
              client.println("GET /statuses/user_timeline/HAutomation.rss HTTP/1.0");
              client.println();
          }
          else
          {
               Serial.println("Unable to connect to Twitter");
           }
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8068
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps File->Examples->Ethernet->TwitterClient would be a good starting point.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thaks guys. But the problem is with the client.connect(server,80). Its always returning false no matter what
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you ping the shield ip from a localnet computer?
ping 192.168.1.177

edit: The correct format for the begin function is:
Ethernet.begin(mac, ip, dns, gateway, subnet);
or if you do not use dns, you can use the gateway for the dns server ip
Ethernet.begin(mac, ip, gateway, gateway, subnet);
« Last Edit: May 16, 2012, 12:36:15 pm by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ya, i can ping the arduino.
But the exection of of client.connect() is not working correctly. Its always returning a FALSE no matter what
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the ip address (edit: and subnet/gateway) of the localnet computer you used to ping the Arduino? Does it have internet access?

Do you have a localnet web server you can test with? Maybe that would help get the bugs out.

« Last Edit: May 25, 2012, 12:35:01 pm by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i havent set up a local server in my computer. How can i do it and how can i filter the bugs with the server??
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you use Windows, then IIS is the web server software. I use WinXP, and install it using
Control panel - Add or Remove Programs - Add/Remove Windows Components - Internet Information Services

If you use Linux, then Apache is the web server. Usually, your distro will have a pre-compiled version in the repository for your flavor of Linux.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, after that i have to change the web address to localhost and then check whether i got the connection or not??
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, after that i have to change the web address to localhost and then check whether i got the connection or not??
You need to change the server address on the Arduino to the localnet ip of your new server.

You did not answer the question about the localnet computer you are using (your new server now?). What ip/subnet/gateway does it have?
« Last Edit: May 26, 2012, 07:36:16 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am using ubuntu, so am planned to install an apache server,

is there anyway i can communicate with the arduino through the ethernet port of my system, using a C, C++ or any other languages.
I am trying to read and respond to the arduino from the computer now
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I modified Linux telnet server code written in C that I found on the internet. It wasn't easy. The Arduino end was the easy part.
Logged

Pages: [1]   Go Up
Jump to: