The client only works in Void loop? not in void setup?
There are NO voids in your code. There are FUNCTIONS.
Thanks for the great help, was amazing how it helped me!
I said that I had this code but was not mine because I am using it like everyone can because it is available on the internet. So I have it on my laptop, But it's not mine because it was not me who created it.
Savvy?
That's just PaulS making sure you know he's smarter than you. It's apparently his primary form of entertainment. He does it constantly.
Placido:
PaulS:
This is not my code
Then why did you say it was?
I have this code
Make up your mind.
The client only works in Void loop? not in void setup?
There are NO voids in your code. There are FUNCTIONS.
Thanks for the great help, was amazing how it helped me!
I said that I had this code but was not mine because I am using it like everyone can because it is available on the internet. So I have it on my laptop, But it's not mine because it was not me who created it.
Savvy?
The laptop must have an IP within that range. Look at the network settings for the laptop, and duplicate all but the IP in the Arduino settings. If you have questions about those settings, post them here and I will try to help you.
Below is some basic client test code. If it works, then your basic network is working ok and issues are probably in your code setup.
//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted
#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(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Better client test 9/22/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
}
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.1"); //download text
client.println("Host: web.comporium.net");
client.println("Connection: close"); //close 1.1 persistent connection
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
}
@zoomkat: Haven't you figured out this is not correct? If the client is still connected and there are no characters available in the socket rx buffer, it will print the funny y (ÿ) on the serial monitor. The client.read() function will return -1 (ÿ) if nothing in the rx buffer.
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, even if there aren't any and it is ÿ
Serial.print(c); //prints byte to serial monitor, even if ÿ
}
It should look like this:
while(client.connected()) {
while (client.available()) {
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
}
@zoomkat: Haven't you figured out this is not correct? If the client is still connected and there are no characters available in the socket rx buffer, it will print the funny y (ÿ) on the serial monitor. The client.read() function will return -1 (ÿ) if nothing in the rx buffer.
I tried the code before I posted it and it worked as expected with my setup. If you got funny characters, then you may have network stability issues that need to be investigated. I think the way my code is written prevents the loss of a socket when an orphan byte is left in the input buffer. YMMV
I tested your code on a busy network with a busy server. No instability issues, just some funny ys in the output when the network and the server are busy. On a localnet with no other server traffic, it seems to work fine as long as the server doesn't stall.
edit: If the connection breaks or the server stalls during the download, your code locks up until you restart the Arduino.
SurferTim:
I tested your code on a busy network with a busy server. No instability issues, just some funny ys in the output when the network and the server are busy. On a localnet with no other server traffic, it seems to work fine as long as the server doesn't stall.
edit: If the connection breaks or the server stalls during the download, your code locks up until you restart the Arduino.
For this simple test, the server seems fairly robust and I've not needed to add timeout code or other workarounds for bad connections and such. If the posted code does not work for the OP, then maybe he has network issues too.
zoomkat:
If the posted code does not work for the OP, then maybe he has network issues too.
The OP has network issues.
Subnet mask has not been assigned.
A default gateway has not been assigned.
I haven't checked what happens when a subnet mask is not assigned to the shield. If it defaults to 0.0.0.0, it could still respond to pings on the local subnet. The OP has indicated the mask needs to be no more than 19 bits, for the subnet to encompass the router and the IP specified for the Arduino shield.
If you don't set a gateway address, traffic cannot be routed off the local subnet, onto the wide area (Internet). This will definitely stop you accessing a google server.
Better client test 9/22/12
Send an e in serial monitor to test
connected
HTTP/1.1 200 OK
Date: Thu, 03 Jul 2014 11:55:11 GMT
Server: Apache/2.2.15 (CentOS)
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.
==================
zoomkat:
If the posted code does not work for the OP, then maybe he has network issues too.
The OP has network issues.
Subnet mask has not been assigned.
A default gateway has not been assigned.
I haven't checked what happens when a subnet mask is not assigned to the shield. If it defaults to 0.0.0.0, it could still respond to pings on the local subnet. The OP has indicated the mask needs to be no more than 19 bits, for the subnet to encompass the router and the IP specified for the Arduino shield.
If you don't set a gateway address, traffic cannot be routed off the local subnet, onto the wide area (Internet). This will definitely stop you accessing a google server.
If you do not assign a subnet mask, the library uses 255.255.255.0 by default.
If you do not assign a dns server or gateway, the library uses the assigned IP first three octets and sets the last octet to .1 by default.
edit: It is probably a network setting problem as I stated in replies #5 and #8.