Go Down

Topic: getting Ethernet WebClient to Constantly Pulling Data (Read 821 times) previous topic - next topic

wwrgsww

Hi,

So I am building a larger network of Arduino Uno's / Network Shields.  About 20 of them will constantly query a server I have (about every 1.5 seconds).


The server simply will output a small amount of data.  either "0:" or "1:m:s"  (where m = Minutes | s = seconds) and it will parse that data for minutes and seconds.   


I can get the arduino to connect to the server and pull the data once during the loop with this code.


Code: [Select]


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


byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,177 };
byte server[] = { 192,168,0,98};


Client client(server, 80);

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





void loop()
{
client.println("GET /client.php");
    client.println();
    char c = client.read();
    Serial.print(c); 
 
}



So they are not pulling a lot of data, but the issue is it only runs once,  I need it to loop.  keep querying that server over and over




wwrgsww

Updated code

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>


byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,177 };
byte server[] = { 192,168,0,98};

int mytime = 0;

Client client(server, 80);

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





void loop()
{
  while (mytime == 0){
   
    //Ethernet.begin(mac, ip);
    client.println("GET /client.php");
    client.println();
    char c = client.read();
    Serial.print(c); 
    delay(2000);
  }//While
 
}



That gets me this


Code: [Select]
t:
t:
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

draythomp

I see a couple of problems initially.  You need to do some checking to see if you got the connection, wait for the connection, wait for the response, etc.  Take a look at the various tutorials out there and start from there.  The code at http://arduino.cc/en/Reference/ClientConnected should be enough to get you started.  The idea is to get a single working connection, then start looping on it.
Trying to keep my house under control http://www.desert-home.com/

zoomkat

You may have several problems. Sounds like you may have overlapping request to the server.The w5100 based ethernet shield is only setup for four connections, and your code apparently has nothing to close the current connection when the data is received. Below is some simple client test code for reference.

Code: [Select]

//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 }; // Arduino IP address
byte server[] = { 208, 104, 2, 86 }; // zoomkat's web site

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt 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.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;); //stops loop for a single get request.
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

wwrgsww

My Code was so horrible.


I had one of my co-workers who is amazing at C look at it.   he took care of everything,  once this project is done, Ill post plenty of pictures / code.

Go Up