Pages: [1]   Go Down
Author Topic: getting Ethernet WebClient to Constantly Pulling Data  (Read 802 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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



Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Updated code

Code:
#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:
t:
t:
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 936
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Trying to keep my house under control http://www.desert-home.com/

0
Offline Offline
Tesla Member
***
Karma: 148
Posts: 9753
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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.
  }
}
 
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 936
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem with that solution is that you still don't know how to do it.
Logged

Trying to keep my house under control http://www.desert-home.com/

0
Offline Offline
Tesla Member
***
Karma: 148
Posts: 9753
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The problem with that solution is that you still don't know how to do it.

Some what of a lame assumption. One can easily learn from code that works as opposed to trying to learn from broken code.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem with that solution is that you still don't know how to do it.

While this is true,  I learned PHP by my self looking at other peoples code.       I understand basic parts of C however the way he did it, using arrays It would of taken me a while to figure that out.   


I think this overall project that we are working on, with 20+ Connected Arduino's will be pretty cool.



Logged

Pages: [1]   Go Up
Jump to: