Hello Arduino guys and gals!
I have come to a dead end trying to make an Arduino client connect to an Arduino server in a LAN.
Let me start with the hardware:
An ADSL router, a 5port switch, a pc and two Mega2560 boards, each with it's Ethernet Shield (W5100). IDE v1.0.1.
I have already set up a server that successfully communicates with the PC - typing "http://192.168.1.192/vi0" at Chrome connects to the board and it replies to the browser as it should.
I have successfully set up a client that receives info from the internet (option 1, see code below) (tweaking some very good bildr code - thanks guys!). If I use the pc's IP as server (option 3), the Arduino client also gets a (LAN) response (nothing special, but it makes a LAN connection all right).
However, when I try to connect the Arduino client to the Arduino server (option 2), nothing happens. server.available() returns nothing, client nothing. It is as if the request never happened.
Yet, the server ethernet connection sometimes halts for a while and it is not accessible from the pc either (Oops! Google Chrome could not connect to 192.168.1.192), at least for a little while and then it works again (some times) without my intervention. At the same time though, the server responds fine to serial input and replies there are no connections open. :~
Any ideas? Am I forgetting anything fundamental? Have you witnessed anything similar yourselves??? Is it a bug?
This is the client code
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte (*server)[4];
byte server1[] = { 174,123,231,247 }; //ip Address of the server you will connect to
byte server2[] = { 192,168,1,192 }; //ip Address of the server you will connect to
byte server3[] = { 192,168,1,14 }; //ip Address of the server you will connect to
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location;
String location1 = "GET /~bildr/examples/ethernet/ HTTP/1.0";
String location2 = "GET /vi0 HTTP/1.0";
String location3 = "GET / HTTP/1.0";
// Do not forget to change MAC address, devices on the same address will not communicate
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
////////////////////////////////////////////////////////////////////////
//EthernetClient client;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
Serial.println(Ethernet.localIP());
}
void loop()
{
if (Serial.available())
{
char c = Serial.read();
delay(100); //wait for serial
while (Serial.available())
Serial.read(); //flush serial
switch (c)
{
case '1':
server = &server1;
location = location1;
break;
case '2':
server = &server2;
location = location2;
break;
case '3':
server = &server3;
location = location3;
break;
default:
return;
}
Serial.println("Connecting to: " + String((*server)[0]) + '.' + String((*server)[1]) + '.' + String((*server)[2]) + '.' + String((*server)[3]) + ' ' + location);
String pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
Serial.println();
}
}
String connectAndRead()
{
//connect to the server
Serial.println("Connecting");
EthernetClient client;
//port 80 is typical of a www page
if (client.connect(*server, 80))
{
client.println(location);
client.println();
// client.println("Host: 192.168.1.192");
// client.println();
// client.println("Connection: keep-alive");
// client.println();
Serial.println("Connected");
// client.flush();
}
else
{
return "Connection failed";
}
delay(2000); //wait for response
if (client.connected())
Serial.println("Still connected");
else
{
client.stop();
return "Client unavailable";
}
if (!client.available())
{
client.stop();
return "No response";
}
while(client.available())
{
char c = client.read();
Serial.print(c);
}
Serial.println("disconnecting.");
client.stop();
return "OK";
}
this is the Serial output:
Connecting to: 192.168.1.192 GET /vi0 HTTP/1.0
Connecting
Connected
Still connected
No response
and this is the server code, the part checking for client presence. It returns "false"...
boolean CDKWebAccess::IsClient()
{
boolean newClient = false;
if (!isEthernet) return false;
if (!client)
{
client = server.available();
newClient = true;
}
if (client)
{
if (newClient)
{
Serial.println("Client detected");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// client.print("This is");
// PrintLine(" a message");
// PrintLine();
}
return true;
}
else
{
// if (server.available()) return true;
return false;
}
}
