ClientConnect  Issues

I am trying to get this sketch to connect. http://arduino.cc/en/Reference/ClientConnect.

I seem to be unable to figure out the correct IP address to feed the sketch. I am using 192.168.1.1 from a laptop on wifi. I have the Arduino connected to the ethernet board on the laptop. The program compiles and uploads. I then get the following in the serial monitor window:

connecting... connection failed

disconnecting.

I checked the URL for the server and indeed it is Google. Please tell me what I should be expecting and what I am doing wrong. This is driving me nuts.

Hank

Hi Hank. In order to get the Arduino on the net, you'll need to have it plugged into the router and you'll need to change the IP back to something like we discussed in the other thread. 192.168.1.10 or something similar. The 192.168.1.1 is the address of your router, so you don't want to use that.

Keep going, you'll get there!

:) :) :) :) :)Got there. You the man. 9600 baud does not cut it. Have it jacked up to 19200 to get some gibberish, repeatable gibberish as well.

I was thinking I had to put in the gateway and subnet and all of stuff but wouldn't you know it was simple. Thanks Much.

Hank

Glad to hear it ;)

Thanks again. I am wondering if the gibberish is just gibberish. Why won’t it come out readable? I remember in the old days of modems one would play with the baud rate. If I set the baud rate to 9600 I get back plain text telling me I can not connect. If I set the baud rate higher I get garbage. So my initial feeling is now questionable. I have the Google example which is very simple and a more complex example connecting with the Avaition Weather, both of which act the same. The difference is that the second example has a lot of code to parse the info. I am going to try the WX example URL on the Google and see if it is the same.

Hank

This is what I get @ 9600 baud and then at 14400 baud?

9600 Baud

connecting…
connection failed

disconnecting.

14400 baud

ònßânâ;nä&Ïò?ÄbO??ò.Ïânâ;Nìn&ÏÄ?ܱ?nâAn£ò# Æ .|ònßâ'â;nänÏòÜ?$?

What do you think? Did I connect?

Hank

Are you changing your baud rate in the monitor to match what you set in the sketch? It should not be total gibberish. Maybe post the code that is not working.

You're just getting the same "can't connect" message at the wrong baud. Leave it at 9600, something else is the problem.

OK. I though so but was not sure. Here is the code. I am connecting straight to the router. Appreciate your help.

Hank

===================================
#include <Ethernet.h>

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192, 168,1, 13 };
byte server = { 64, 233, 187, 99 }; // Google

Client client(server, 80);

void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);

Serial.println(“connecting…”);

if (client.connect()) {
Serial.println(“connected”);
client.println(“GET /search?q=arduino HTTP/1.0”);
client.println();
} else {
Serial.println(“connection failed”);
}
}

void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting.”);
client.stop();
for(;:wink:
;
}
}

You may need the updated ethernet library.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1227978344

Make sure you delete the .o files so that it recreates them after making the change.

OK. I replaced the lines in both and deleted the .o file and reran at 9600 baud. It would not connect. But I guess the .o file was supposedly going to be regenerated somehow but it was not. I guess that is the part of the puzzle that I still need input about please.

sorry for your trouble and I appreciate the help.

Hank

Just thought I would update the file saying that the "o" file did regenerate after a few tries. Unfortunately, I am still not able to connect.

Hank :-X

Arduino on your LAN has to know your router's IP address and the subnet mask to connect to the machines outside your LAN. Try the following modification.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 13 };
byte gateway[] = { 192, 168, 1, 1 };   //your router's IP address
byte subnet[] = { 255, 255, 255, 0 };    //subnet mask of the network

and

Ethernet.begin(mac, ip, gateway, subnet);

Hank, did you compile and upload to the board after making the changes to the ethernet files?

tasasaki, I agree it can't hurt, but I have never needed to specify the gateway or subnet in my sketches and they talk to the outside world.

Ho I did not. I suspect that was going to be necessary. I just did it with the IDE. I have a "0" file. I made the changes we talked about above but I do not have a "client O". I have and ethernet.o but no client.o. I did verify/compile within the IDE???

Also, going more insane I am going right to the wire coming into the house and bypassing the router all together. The internet server provided software at 192.168.1.1 recognizes the Arduino as 192.168.1.13.

To be clear, I replaced only the lines on Client.h and left the rest of the lines in client.h alone. I replaced the entire file in client.cpp. I could not figure out what was changed there so thought this was the safest bet.

So we are not connecting. I have no Client.o (I really should download it again and look if it exists) and we are not connecting.

Ok Obe Wan the old grasshopper is hanging in there. If it works for somebody it should work for me as well with patience.

Hank

It needs to be plugged into the router or that IP address will not be valid and it will not work.

The entire contents of client.cpp were not in that post, so now you are missing some code. Here is the whole thing:

extern "C" {
  #include "types.h"
  #include "w5100.h"
  #include "socket.h"
}

#include "Ethernet.h"
#include "Client.h"
#include "Server.h"

Client::Client(uint8_t sock) {
  _sock = sock;
}

Client::Client(uint8_t *ip, uint16_t port) {
  _ip = ip;
  _port = port;
  _srcport = 0;
}

uint8_t Client::connect() {
  _sock = 255;
  
  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    if (getSn_SR(i) == SOCK_CLOSED) {
      _sock = i;
    }
  }
  
  if (_sock == 255)
    return 0;

  _srcport++;
    
  socket(_sock, Sn_MR_TCP, (_srcport + 1024), 0);
  
  if (!::connect(_sock, _ip, _port))
    return 0;
    
  while (status() != SOCK_ESTABLISHED) {
    if (status() == SOCK_CLOSED)
      return 0;
  }
  
  return 1;
}

void Client::write(uint8_t b) {
  send(_sock, &b, 1);
}

int Client::available() {
  return getSn_RX_RSR(_sock);
}

int Client::read() {
  uint8_t b;
  if (!available())
    return -1;
  recv(_sock, &b, 1);
  return b;
}

void Client::flush() {
  while (available())
    read();
}

void Client::stop() {
  close(_sock);
  disconnect(_sock);
  EthernetClass::_server_port[_sock] = 0;
}

uint8_t Client::connected() {
  uint8_t s = status();
  return !(s == SOCK_LISTEN || s == SOCK_CLOSED || (s == SOCK_CLOSE_WAIT && !available()));
}

uint8_t Client::status() {
  return getSn_SR(_sock);
}

// the next three functions are a hack so we can compare the client returned
// by Server::available() to null, or use it as the condition in an
// if-statement.  this lets us stay compatible with the Processing network
// library.

uint8_t Client::operator==(int p) {
  return _sock == 255;
}

uint8_t Client::operator!=(int p) {
  return _sock != 255;
}

Client::operator bool() {
  return _sock != 255;
}

Just to be clear, you will need to actually upload to the board again to get the modified code into the arduino. The o files are created at compile time.

Thanks a bunch. We are now flying.

Hank

I've been having this exact issue as well. Were you ever able to resolve it?