I apologize in advance for my lack of experience with internet protocols and etc.
I'm trying to open an XML file that's continuously updating as time goes on. When I run my code, I can open the XML file one time, but I can't continuously stream from the data. It looks like after I read what the client has available, it becomes disconnected, but I can't seem to reconnect or reopen the page.
I have my code attached. It prints the XML file for the first read, but then doesn't print anything after that because I can't reconnect. I would like to continually print the XML file over and over again.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xA4, 0x2B, 0x8C, 0x7D, 0x71, 0xD6};
IPAddress server(129,1,15,35); // numeric IP for my device
IPAddress ip(129,1,15,6);
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Ethernet.begin(mac);
delay(1000);
Serial.println("connecting...");
client.connect(server,5000);
client.println("GET /current?path=//Path//DataItem[@id=\"p1_CurrentTool\"] HTTP/1.0");
client.println();
}
void loop()
{
while (client.available()) { //Read the data
char c = client.read();
Serial.print(c);
}
if (!client.connected()){ //If I'm not connected, which happens after I read everything
//Reconnect, but this part doesn't work!
client.connect(server,5000);
client.println("GET /current?path=//Path//DataItem[@id=\"p1_CurrentTool\"] HTTP/1.0");
client.println();
}
}
Why do you not check the return code from client.connect?
while (client.available()) { //Read the data
This should be in a while(client.connected()) block. While the client is connected, you need to wait until is has sent all the data instead of assuming that what has already arrived is all that is coming.
Event based client code. You probably could modify this type of code to make repeated client request to the server on a timed basis or similar.
//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "checkip.dyndns.com"; // 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 ip test 3/1/13"); // 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 / HTTP/1.1"); //download text
client.println("Host: checkip.dyndns.com");
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
}
Thanks a bunch! I modified the code and it worked like a charm! I would attach my modified code, but I really only modified the network parameters and made it that only SendGet() is in the loop().