Adding strings together

I am trying to create a String yourdata which should hold "F=1&D0=3&D1=5.....D180=9" .If i just try to show 33 data than there is no problem i can see ;"F=1&D0=1&D2...D34=5" in your data variable.If i increase the data to 180 as shown below then yourdata just hold "D175=5&D176=8...D180=5".How can prevent this?I need long string data to send.

#include <SPI.h>
#include <WiFi.h>
#include <TextFinder.h>
#include <Servo.h> 
#include <MemoryFree.h>
#define echoPin A1 // Echo Pin = Analog Pin 0#include <Servo.h> 

#define trigPin A2 // Trigger Pin = Analog Pin 1

boolean rotation =0;
int i=0;
Servo myservo;
int pos = 0;
int status = WL_IDLE_STATUS;

 String yourdata;
 long distance=0; // Calculated Distance

long duration; // Duration used to calculate distance
void setup(){

Serial.begin(9600); 
 pinMode(trigPin, OUTPUT);
 myservo.attach(6);
 pinMode(echoPin, INPUT);

 analogReference(INTERNAL);
 while (status != WL_CONNECTED) {

    

   Serial.print(F("Attempting to connect to SSID: "));

    Serial.println(ssid);


    status = WiFi.begin(ssid,pass);



    // wait 10 seconds for connection:

    delay(10000);

  }
  
 

  
}
void loop{
for(int i=0;i<181;i++){
 myservo.write(pos);
 if(pos>179)
 {
   rotation = 1;
 }
 if (pos<1)
 {
   rotation =0;
 }
 
 if(rotation == 0)
 {pos=pos+1;}

 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
  
  




String d=String(i);

 if(i==0){

 yourdata="F=1&D"+d+"="+String(distance);



 }

 else{
 yourdata=yourdata+"&"+"D"+d+"="+String(distance);





 } 



if(i==181){
 

Serial.println(yourdata);


  
 }



 
 delay(500);
}
}

I need long string data to send.

Do you have to build the whole String before you send it or could you send it section by section ?

Do you need to use a String at all. Could you use a zero terminated array of chars aka a C style string ?

Make sure you are following the examples in the below tutorial.

zoomkat:
Make sure you are following the examples in the below tutorial.

http://www.arduino.cc/en/Tutorial/StringAppendOperator

Yes this could have been the answer.
yourdata.concat("&D"+d+"="+distance);
But it now become the length problem.After 106 data it eats a lot of memory and stops adding.Free memory method shows freeMemory()=164 .

UKHeliBob:
Do you have to build the whole String before you send it or could you send it section by section ?

Do you need to use a String at all. Could you use a zero terminated array of chars aka a C style string ?

I am going to send this data to server as post.Let me share full code.Of course i don't have to but i don't know the proper way.

#include <SPI.h>
#include <WiFi.h>
#include <TextFinder.h>
#include <Servo.h> 
#include <MemoryFree.h>
#define echoPin A1 // Echo Pin = Analog Pin 0#include <Servo.h> 

#define trigPin A2 // Trigger Pin = Analog Pin 1
char ssid[] = "taylan"; //  your network SSID (name)AIRTIES_RT-205
char myChar;
char pass[] = "";    // your network password (use for WPA, or use as key for WEP)
boolean rotation =0;
int i=0;
Servo myservo;
int pos = 0;
int status = WL_IDLE_STATUS;
char server[] = "arduinocontrol.somee.com";    
WiFiClient client;

 String yourdata;

 long distance=0; // Calculated Distance
boolean startultrasonic=false;
long duration; // Duration used to calculate distance
void setup(){

Serial.begin(9600); 
 pinMode(trigPin, OUTPUT);
 myservo.attach(6);
 pinMode(echoPin, INPUT);

 analogReference(INTERNAL);
 while (status != WL_CONNECTED) {

    

   Serial.print(F("Attempting to connect to SSID: "));

    Serial.println(ssid);


    status = WiFi.begin(ssid,pass);



    // wait 10 seconds for connection:

    delay(10000);

  }
  
 

  
}

void loop(){


  
 for(int i=0;i<181;i++){
 myservo.write(pos);
 if(pos>179)
 {
   rotation = 1;
 }
 if (pos<1)
 {
   rotation =0;
 }
 
 if(rotation == 0)
 {pos=pos+1;}

 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
  
  




String d=String(i);

 if(i==0){
   

 yourdata="F=1&D"+d+"="+String(distance);


//i=i+1;
 }

 else{
 
 //yourdata ="&"+"D"+d+"="+distance;

yourdata.concat("&D"+d+"="+distance);

 

 //i=i+1;

 } 

 //Serial.println("Data"+d+"="+String(distance));

if(i==180){
 
 Serial.print("freeMemory()=");
    Serial.println(freeMemory());
Serial.println(yourdata);
httpsendRequest();

  
 }




                


 
 delay(500);
}
 
}
void httpsendRequest(){
  
 client.stop();
Serial.print(F("Connecting.."));


if (client.connect(server, 80)) {

 client.println("POST /Graph.aspx HTTP/1.1");
 client.print("Host: ");
 client.println(server);
  client.println("Connection: close");
 client.println("Content-Type: application/x-www-form-urlencoded");
 client.print("Content-Length: ");
 client.println(yourdata.length());
 client.println();
 //client.println(data);
int x = yourdata.length(), i=0,j;

const char * charptr=yourdata.c_str();  // get access to the internal c_str of data.

while (i<x){
   j=50;
   if((i+j)>x) j = x - i;
   client.write(&charptr[i],j);  // starting at charptr[i] send then next j bytes.
   i = i + j;
}


 Serial.print(F("Send"));
}
else{
 Serial.print(F("error"));

}
  }

You might develop your String addition code just using the serial monitor for testing purposes first.

It sounds like you're up against the limits of the memory on the '328p, if you need to send it all at once. The '328p only has 2048 bytes of ram, and your large string will take almost 1000, right off the bat, not counting anything else you might be doing.

Either find a way to break up the transmission into parts (what if you build it in here as you send it?) or switch to a chip with more ram (the 1284p is a good choice, as it has a huge amount of memory - 16k, the most of any 8-bit avr - and a lot of people here love it)

while (i<x){
j=50;
if((i+j)>x) j = x - i;
client.write(&charptr_,j); // starting at charptr send then next j bytes._
* i = i + j;*
}
Finally, does F() work with client.print()? I don't know that library off the top of my head. If it does, you can save some more sram that way.

Why do you need to send it all at once?

And just drop the stupid String class. Just use a C++ string (char myString[10] = "Some text"). Also see Nick Gammons Trap #23.

DrAzzy:
It sounds like you're up against the limits of the memory on the '328p, if you need to send it all at once. The '328p only has 2048 bytes of ram, and your large string will take almost 1000, right off the bat, not counting anything else you might be doing.

Either find a way to break up the transmission into parts (what if you build it in here as you send it?) or switch to a chip with more ram (the 1284p is a good choice, as it has a huge amount of memory - 16k, the most of any 8-bit avr - and a lot of people here love it)

Finally, does F() work with client.print()? I don't know that library off the top of my head. If it does, you can save some more sram that way.

I tried a lot of things that take much time.And somehow my code is working its not too different.
Good advices but i want to use the ram as efficient as possible.Even if i have larger Ram recently i realised that my memory always decrease which means i can't do same posting again.With larger sram i will be able to this easier but in the end i will run out of memory.This is my new problem i can't gain my old memory back.However i just want to get rid of this your data after post and free its used memory.
F() not working with client this was the first thing i searched when i learned F().
I will add my code here;

#include <SPI.h>

#include <WiFi.h>
#include <String.h>
#include <TextFinder.h>
#include <Servo.h> 

#define echoPin A1 // Echo Pin = Analog Pin 0#include <Servo.h> 

#define trigPin A2 // Trigger Pin = Analog Pin 1
boolean rotation =0;
int i=0;
int pos = 0;
boolean getdist=false;
Servo myservo;
long distance=0; // Calculated Distance
boolean startultrasonic=false;
long duration; // Duration used to calculate distance
boolean sendrequest=false;

char ssid[] = "taylan"; //  your network SSID (name)AIRTIES_RT-205

char pass[] = "19911991";    // your network password (use for WPA, or use as key for WEP)

String yourdata ;

int status = WL_IDLE_STATUS;

char server[] = "arduinocontrol.somee.com";   

WiFiClient client;

TextFinder finder(client);

void setup() {
 Serial.begin(9600);
// yourdata.reserve(1400);
   pinMode(trigPin, OUTPUT);
 myservo.attach(6);
 pinMode(echoPin, INPUT);
yourdata="";
 analogReference(INTERNAL);

  //Initialize serial and wait for port to open:

 





Serial.println(WiFi.localIP());

  // check for the presence of the shield:

  if (WiFi.status() == WL_NO_SHIELD) {

    Serial.println(F("WiFi shield not present"));

    // don't continue:

    while (true);

  }



  String fv = WiFi.firmwareVersion();

  if ( fv != "1.1.0" )

    Serial.println(F("Please upgrade the firmware"));



  // attempt to connect to Wifi network:

  while (status != WL_CONNECTED) {

    

   Serial.print(F("Attempting to connect to SSID: "));

    Serial.println(ssid);

  
    status = WiFi.begin(ssid,pass);



    // wait 10 seconds for connection:

    delay(10000);

  }



Serial.println("\nStarting connection to server...");

      httpRequest();


}

void loop() {
   


if(startultrasonic){
getDistance();
}

if(sendrequest && !startultrasonic){

delay(3000);

httpRequest();



}
if(!startultrasonic){
  if(finder.find("?")){

   Serial.println(F("Dont Start"));

    
getdist=false;
httpRequest();
  }
else{
  
Serial.println(F("Start"));

delay(300);

startultrasonic=true;

}

}
}



void printWifiStatus() {

  // print the SSID of the network you're attached to:

    Serial.println(F("Connected to network"));



    Serial.print(WiFi.SSID());


 if(WiFi.RSSI()>=-35){

     Serial.println(F("Very good connection"));

   }

   if(WiFi.RSSI()>=-65 && WiFi.RSSI()<-35){

     Serial.println(F("%50 good connected"));

   }

   if(WiFi.RSSI()<-65){

     Serial.println(F("Worst connection"));

   }

 
  }


void httpRequest() {

 

  // close any connection before send a new request.

  // This will free the socket on the WiFi shield

  client.stop();



  // if there's a successful connection:

  if (client.connect(server, 80)) {

    Serial.println(F("connecting for data receive..."));

 

    // send the HTTP PUT request:

   client.println("GET /command.txt HTTP/1.1");

    client.println("Host: arduinocontrol.somee.com");

    client.println("Connection: close");

    client.println();

    Serial.println("DATA RECEIEVED");

  sendrequest=false;

    // note the time that the connection was made:

 delay(6000);


  }

  else {



    // if you couldn't make a connection:

    Serial.println(F("connection failed"));

  }

}

  void httpsendRequest() {

 
  client.stop();

  // if there's a successful connection:
 Serial.println(F("connecting for data send..."));
  if (client.connect(server, 80)) {

   Serial.println(yourdata);
 

  client.println("POST /Graph.aspx HTTP/1.1");

  client.println("Host:  arduinocontrol.somee.com");

  client.println("User-Agent: Arduino/1.0");

  client.println("Connection: close");

  client.println("Content-Type: application/x-www-form-urlencoded;");

 client.print("Content-Length: ");

  client.println(yourdata.length());

  client.println();

  int x = yourdata.length(), i=0,j;


const char * charptr=yourdata.c_str();  // get access to the internal c_str of data.

while (i<x){
   j=50;
   if((i+j)>x) j = x - i;
   client.write(&charptr[i],j);  // starting at charptr[i] send then next j bytes.
   i = i + j;
} 

yourdata=" ";
Serial.println(yourdata);
    Serial.println(F("DATA SEND"));

    sendrequest=true;

 delay(6000);


  }

  else {

    Serial.println(F("connection failed"));

  }

  

}

void getDistance(){ 
for(int i=0;i<181;i++){


 myservo.write(pos);
 if(pos>179)
 {
   rotation = 1;
 }
 if (pos<1)
 {
   rotation =0;
 }
 
 if(rotation == 0)
 {pos=pos+1;}

 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
  Serial.print(String(distance));
String d=String(i);

 if(i==0){

 yourdata="F=1&D"+d+"="+String(distance);

 }

 else{

yourdata.concat("&D"+d+"="+distance);

 } 



if(d=="180"){
 

startultrasonic=false;
 httpsendRequest();

  
 }

 
 delay(500);
}
 
}

zoomkat:
You might develop your String addition code just using the serial monitor for testing purposes first.

To see if i am successful adding strings together? Yes i made it and working.But after post i can't do same thing again just one time post.Memory just decreasing i assume again this is because of yourdata.And when i am done with your data i just want gain my old memory which i believe not easy as i think with the way i am holding yourdata i don't know if its possible.