Arduino Ethernet Shield Communication

Hello, I bought the Arduino Ethernet Shield. I've done some research on my problem but havent quite figured out a solution. I want to send Twitter messages with through Arduino. I used the twitter library and sample code. Basically, I was wondering if its feasible to connect to the internet via wireless connection on a laptop and then connect the ethernet shield to the computer with an ethernet cable to gain access to the internet. I was thinking the laptop could connect to a wireless signal, then act as a router, and give internet connection to the Arduino. Thanks.

If you use Windows, then yes. Internet connection sharing. http://support.microsoft.com/kb/306126 I'm not certain how to do it with Linux yet.

Ok, been trying to connect to the internet using the shield and the following code:


#if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDNS.h>
#include <Twitter.h>

// Ethernet Shield Settings
byte mac = { 0x90, 0xA2, 0xDA, 0x00, 0x87, 0x1E };

// substitute an address on your own network here
byte ip = {192, 168, 1, 127 };

byte gateway = { 192 , 168 , 1 , 1 };
byte subnet = { 255, 255, 255, 0 };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter(“417577148-pGqi3qTmEpV74zYn4g011LIlmHQMCkb7obcrR0”);

// Message to post
char msg = “Hello, World! I’m Arduino!”;

void setup()
{
delay(1000);
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);

Serial.println(“connecting …”);
if (twitter.post(msg)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println(“OK.”);
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println(“connection failed.”);
}
}

void loop()
{
}


However, I am still unable to connect. “connection failed” error. Would it be possible that a firewall is preventing connection? My laptop is connected to the arduino via ethernet cable and wirelessly to my LAN.

If you are using internet connection sharing, you did not pay close enough attention to the client setup.
This should put everything in the correct subnet.

byte ip[] = {192, 168, 0, 127 }; // anything 192.168.0.x where x = 2 to 254
byte gateway[ ] = { 192 , 168 , 0 , 1 }; // this must be this

However, the routers ip address is 192.168.1.1 Found this out when i went to the cmd window and typed in "ipconfig". The number was found under "default gateway". Not sure....

and Internet Connection Sharing "ICS" is also enabled.

kshopmate: However, the routers ip address is 192.168.1.1 Found this out when i went to the cmd window and typed in "ipconfig". The number was found under "default gateway". Not sure....

It does not matter what the ip of the wireless router is, as long as it isn't 192.168.0.1. But your ethernet port on your laptop is 192.168.0.1 when you start ICS. That is the gateway for the Arduino.

Edit: Usually you will need a CAT5 crossover cable to connect computer-to-Arduino without a switch or hub.

I have the CAT5 cable and used the code provided (192.168.0.1 and 192.168.0.127), however, I am still not connecting to the internet..Could it possibly be some sort of firewall or security system preventing connection?

Have you tried some of the web client examples to connect to google or something simple first. I don't know what the twitter deal is, but normally you need to open a connection to a server first.

Tried the sketch


/*
Web client

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.

Circuit:

  • Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis

*/

#include <SPI.h>
#include <Ethernet.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, 0x87, 0x1E };

// substitute an address on your own network here
byte ip = {192, 168, 0, 127 }; // anything 192.168.0.x where x = 2 to 254
byte gateway = { 192 , 168 , 0 , 1 }; // this must be this
byte subnet = { 255, 255, 255, 0 };
byte server = { 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 client(server, 80);

void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip, gateway, subnet);
// start the serial library:
Serial.begin(9600);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println(“connecting…”);

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

void loop()
{
// 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.stop();

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

still no connection…

I have the CAT5 cable...

Is it a cat5 crossover cable? Also use the # in the post tool bar for code boxes.

I am using one of the cables coming straight out of the router and plugged it into the ethernet shield. I am assuming this is the CAT5 crossover? or is it something else? oh and my bad, just recognized that function for the coding...thanks

Alright so forget connecting straight from the laptop…Just tried connecting the arduino ethernet shield straight to the wireless router. i have this code:

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 
 */

#include <SPI.h>
#include <Ethernet.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, 0x87, 0x1E };
byte ip[] = { 192, 168, 1, 127 };
byte server[] = { 173,194,33,104 }; // Google

byte gateway[ ] = { 192 , 168 , 1 , 1 }; // this must be this
byte subnet[ ] = { 255, 255, 255, 0 };

// 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 client(server, 80);

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip, gateway, subnet);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

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

void loop()
{
  // 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.stop();

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

and still no connection.

Some client test code. Note that the third number in the byte ip should to match your routers number (normally 0 or 1), and the fourth value should not be in use by any other device connected to the router. Send an e via the serial monitor and see what gets sent back.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 }; // arduino lan IP
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; // subnet mask
byte server[] = { 208, 104, 2, 86 }; // zoomkat web site
Client client(server, 80);

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

void setup()
{
  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println("Send an e via the serial monitor.");
  Serial.println();
}

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

void loop()
{
  if (Serial.available() > 0) 
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'e')
    {
      sendGET(); // call sendGET function
    }
  }
}
//////////////////////////
void sendGET() //client function to send/receie GET request data.
{
  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  } 
  else {
    Serial.println("connection failed");
    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();
    Serial.print(c);
  }

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

}

got the following results

starting simple arduino client test
Send an e via the serial monitor.

connected
HTTP/1.1 200 OK
Date: Sun, 27 Nov 2011 01:19:13 GMT
Server: Apache
Last-Modified: Sat, 13 Nov 2010 16:31:40 GMT
Accept-Ranges: bytes
Content-Length: 51
Connection: close
Content-Type: text/plain; charset=UTF-8

Woohoo! Your arduino ethernet client works!
zoomkat
disconnecting.
==================

therefore im guessing it worked there… hmm i wonder why the other codes i used didnt do anything…

Though, I just used the WebClient example and still could not get it to connect to google…

Though, I just used the WebClient example and still could not get it to connect to google.....

Using an IP address might not work for google.

http://arduino.cc/forum/index.php/topic,80362.msg607262.html#msg607262

Alright...ok i got the twitter sketch to work and send tweets when uploaded. however, this is when the ethernet shield is connected directly to the router. but, i need the arduino shield to be simply connected to my laptop and not a router because this arduino board will be on a moving robot. I wanna send tweets when my robot has finished moving. When i connect to my laptop, i can no longer gain access to the internet with the shield. any ideas?

When i connect to my laptop, i can no longer gain access to the internet with the shield. any ideas?

Crossover cable, ICS, etc.. Perhaps you should skip the laptop and make a routerbot setup. Some ideas below.

http://www.lynxmotion.net/viewtopic.php?f=20&t=6343

Because I have such a short time and money constraint, I am simply trying to use a laptop because it is being used for other processes of my robot. If I had a bit more time I would probably try this router type setup, seems pretty cool. However, might just need to stick with the laptop and figure that out (which I havent yet)...I've been looking into setting up my laptop as a wireless router, but no luck as of yet.

For fast, cheap, ~easy windows laptop, you can put a 100 ohm resistor on the arduino to prevent resetting, load apache on the laptop, and make a simple batch file in the cgi-bin folder to send the desired commands out the serial port to the attached arduino (ethernet shield not required). Control the bot like below from a web page. The below old pan/tilt cam setup uses a different servo control chip, but the arduino will work the same.

http://web.comporium.net/~shb/wc2000-PT-script.htm