arduino ethernet speed problem

I want to post the analog values to my website every second to let them show up in real time. With php-code I do some stuff with it to store data in a database.

I think the arduino is not fast enough to perform a http-post every second. Somebody knows a way to get around that?

For the moment I only reach a frequence of data-post every 4 seconds.

Yes. Persistent connection. Open the connection and leave open. Not as easy as open-send-receive-close.

Do not use a domain name for the server. IP is faster.

keep the number of posted data minimal,

Can you post you POST code? maybe we see other optimizations

Thanks!
This code is working but too slow for me. I must keep the interval between two intervals at 4000ms, to keep it going.
It is only for 2 temperatures, but the project will be expanded.

#include <DallasTemperature.h>
#include <OneWire.h>
#include <SPI.h>
#include <Ethernet.h>
// Data wire temp sensor is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
DeviceAddress temp1 = { 0x28, 0xD2, 0x56, 0xF3, 0x03, 0x00, 0x00, 0xA0 };
DeviceAddress temp2 = { 0x28, 0xE1, 0x76, 0xF3, 0x03, 0x00, 0x00, 0xEC };
// assign a MAC address for the ethernet controller.
 // fill in your address here:
 byte mac[] = { 0xEF, 0x12, 0xDA, 0xAB, 0x17, 0x24};
 // fill in an available IP address on your network here,
 // for manual configuration:

 IPAddress server(78,22,214,56); //IP of mysite
 
// initialize the library instance:
 EthernetClient client;
 
unsigned long lastConnectionTime = 10;          // last time you connected to the server, in milliseconds
 boolean lastConnected = false;                 // state of the connection last time through the main loop
 const unsigned long postingInterval = 40*1000;  // delay between updates, in milliseconds
 
void setup() {
   // start serial port:
   Serial.begin(9600);
   //instellen temperatuursensors
 sensors.begin();
 sensors.setResolution(temp1, 10);
 sensors.setResolution(temp2, 10);
   // give the ethernet module time to boot up:
   delay(1000);
   // start the Ethernet connection:
   if (Ethernet.begin(mac) == 0) {
     Serial.println("Failed to configure Ethernet using DHCP");
   // no point in carrying on, so do nothing forevermore:
     for(;;)
       ;
   }
   // print the Ethernet board/shield's IP address:
   Serial.print("My IP address: ");
   Serial.println(Ethernet.localIP());
 }
 
void loop() {
  
   // if there's incoming data from the net connection.
   // send it out the serial port.  This is for debugging
   // purposes only:
   if (client.available()) {
     char c = client.read();
     Serial.print(c);
   }
   // if there's no net connection, but there was one last time
   // through the loop, then stop the client:
   if (!client.connected() && lastConnected) {
     Serial.println();
     Serial.println("disconnecting.");
     client.stop();     
   }
 
  // if you're not connected, and ten seconds have passed since
   // your last connection, then connect again and send data:
   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
     httpRequest();     
   }
   // store the state of the connection for next time through
   // the loop:
   lastConnected = client.connected();   
 }
 
// this method makes a HTTP connection to the server:
 void httpRequest() {
   // temperaturen lezen

 sensors.requestTemperatures();
 float temp11 = sensors.getTempC(temp1);
 float temp12 = sensors.getTempC(temp2);

 delay(1000);
   // if there's a successful connection:
   if (client.connect(server, 80)) {
     Serial.println("connecting...");
     client.print("GET http://www.mysite.be/fillDB.php?t1="); 
     //fillDB.php = code to put the values in database
     client.print(temp11);
     client.print("&t2=");
     client.print(temp12);
     client.println(" HTTP/1.0");
     client.println();
 Serial.print(temp11);
 Serial.print(" ");
 Serial.println(temp12);
    // note the time that the connection was made:
     lastConnectionTime = millis();
     
   } 
  else {
     // if you couldn't make a connection:
     Serial.println("connection failed");
     Serial.println("disconnecting.");
     client.stop();
   }
 }
 const unsigned long postingInterval = 40*1000;  // delay between updates, in milliseconds
// then later in your code, you are waiting that long. ??
 if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
     httpRequest();     
   }

Try this code. http://www.arduino.cc/playground/Code/WebClient It sends a request every 30 seconds, but you can change that.

Hmmmm..

Opening closing sockets like that every second is asking for trouble esp if your connection is lagged and can't keep up.

Personally, i'd open a socket on the server and keep it alive, and just send the data every second to a constantly open socket rather than close it, to do this i'd probably opt for a server side script eg php or if it's your own server, write a small server to deal with the arduino.

 void httpRequest() {
   // temperaturen lezen

 sensors.requestTemperatures();
 float temp11 = sensors.getTempC(temp1);
 float temp12 = sensors.getTempC(temp2);

 delay(1000);

Fail. Using the delay() function to halt activity for one second, and expecting the rest of the code to complete in zero time is completely unrealistic.

I tried this code from SurferTim.
I cannot use IPAddress server(46,30,211,56); because this is the IP of my provider (one.com).
So I have no private IP address.
I think I have to use my domain name (www.mysite.com).
What do I have to change in the code to perform that change?

/*
   Web client sketch for IDE v1.0.1 and w5100/w5200
   Posted October 2012 by SurferTim
*/

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

byte mac[] = {  0x92, 0xA2, 0xEF, 0x0D, 0x24, 0xAD };
IPAddress ip(192,168,1,153);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

//IPAddress server(74,125,227,16); // Google
IPAddress server(46,30,211,56); // my provider

EthernetClient client;
int totalCount = 0;
int loopCount = 0;
char pageAdd[32];

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println("Ready");
}

void loop()
{
  if(loopCount < 16)
  {
    // if loopCount is less than 30, just delay a second
    delay(1000);
  }
  else
  {
    // every thirty seconds this runs
    loopCount = 0;

    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/",totalCount);
    
    
    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
}

byte getPage(IPAddress ipBuf,char *page)

{

  int inChar;
  char outBuf[128];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  } 
  else
  {
    Serial.println("failed");
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println("disconnecting.");
  // close client end
  client.stop();

  return 1;
}

I think you are trying to say your server uses virtual hosting. You need to include your domain name in the "Host" parameter.

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    // change this to a single "\r\n"
    sprintf(outBuf,"GET %s HTTP/1.0\r\n",page);
    client.write(outBuf);
    // add the Host with "\r\n\r\n" (blank line)
    client.write("Host: www.mysite.com\r\n\r\n");

  }

edit: The virtual server may slow things up a bit also. When it gets busy, the connection will slow up. It is probably handling more domains than just yours.

BTW, the Arduino/shield is plenty fast enough. I use a persistent connection, and I transfer joystick data to my Arduino 15 times per second.

Thank you SurferTim. I tried to change the sprintf function, but it doesn't work (current is 0 and not 128). What should be the right syntax, and can I use variables in place of values (128 in example hereunder)? Current should be a float-variable (I expect also values like 32.12).

sprintf(pageAdd,"/vul.php?current=%f",128);

float is not supported in the sprintf function in the Arduino library. :( This should work.

sprintf(pageAdd,"/vul.php?current=%u",128);

How are you getting the value (128) you are sending?

I will store the value in a variable, by example "curr1" and then replace 128 with curr1 in sprintf.

OK. If it doesn't matter where that value comes from, then

unsigned int curr1 = 128;
sprintf(pageAdd,"/vul.php?current=%u",curr1);

to be complete: the value comes from a sensor at an analog port. I will read it out and put the value in curr1.

More sensors are following (temperatures and currents).

to be complete: the value comes from a sensor at an analog port.

That is an integer, not a float.

yes, you're right. I forgot to tell that I perform some calculation before posting. I need a number of samples because it is AC, those measures have to be calculated to the right value.

OK. What conversion needs to be done to that analog integer value?

This is de calculation I used in previous project. numberOfSamples was 3000.
Now I will take less samples to avoid too many slow down the program.

 for (int n=0; n<numberOfSamples; n++)
{
   //Used for offset removal
   lastSampleI=sampleI; 
   //Read in voltage and current samples.    
   sampleI = analogRead(inPinI);
   //Used for offset removal
   lastFilteredI = filteredI;
   //Digital high pass filters to remove 2.5V DC offset.
   filteredI = 0.996*(lastFilteredI+sampleI-lastSampleI);
   //Root-mean-square method current
   //1) square current values
   sqI = filteredI * filteredI;
   //2) sum 
   sumI += sqI;  
}

//Calculation of the root of the mean of the voltage and current squared (rms)
//Calibration coeficients applied. 
Irms = ICAL*sqrt(sumI / numberOfSamples)-Ioffset;
if (Irms < 0.08) Irms = Irms/2.65; // correction at low current

I want to give credit where credit is due. This is not my code. You must convert the float to a character array. http://arduino.cc/forum/index.php/topic,72682.msg545115.html#msg545115

or I can multiply the value with 100 (curr1 = int(curr1*100)) and post it then serverside divide by 100 again (in php). This is maybe the easiest way.