Problem with Serial.println and esp8266.println

Hello,
I am a beginner in arduino and i am trying to understand the following problem.
I made a code for communication with ESP. If I remove all the comments (Serial.println) the code (communication with ESP) works, if I insert the comments the code (communication with ESP) does not work.
what could it be?

#include <SoftwareSerial.h>
#include "Arduino.h"

SoftwareSerial esp8266(8, 9);

const String vWifi = "xxx";
const String vPassWd = "xxx";
const String vServer = "xxx";
const String vServerPort = "8081";
const String vDisp="Dispositivo1";
const int vIdEmpresa=1;

double tempo=0.25;

int vTotal=0;

const int pinLedVerde = 7;
const int pinLedAmarelo = 6;
const int pinLedVermelho = 5;

const int pinDigital = 10;
const int pinAnalog = A0;

int vValorDigital = 0;
double vValorAnalogMedio = 0;
double vValorAnalogPico = 0;
int x=-1;
int n=-1;

int vRef=0;

bool vSendOk=false;

int vLed=0;


bool waitAndEcho(unsigned long t,String s){
	String buffer="";
	unsigned long start=millis();
	unsigned long last=0;
	unsigned int n=s.length();
	bool ret=false;
	delay(50);
	do{
		if(esp8266.available()){
			buffer+=(char)esp8266.read();
			last=millis();
			if(buffer.length()>=n){
				if(buffer.substring(buffer.length()-n).equals(s)){
					ret=true;
					break;
				}
			}
		}
	}while(millis()<start+t);

	buffer.replace("\r","\\r");
	buffer.replace("\n","\\n");
	(String(ret?"+":"-")+"(" + String(last - start) + "/" + String(t) + "):" + buffer);
	return ret;
}


bool espConfig() {


	Serial.println("Iniciando conexao");
	esp8266("AT+RST");
	if (waitAndEcho(1500, "ready\r\n")) {
		esp8266("AT+CWMODE=1");
		if (waitAndEcho(500, "OK\r\n")) {
			esp8266("AT+CWJAP=\"" + vWifi + "\",\"" + vPassWd + "\"");
			if (waitAndEcho(10000, "OK\r\n")) {
				Serial.println("Conexao realizada com sucesso");
				return true;
			}
		}
	}
	Serial.println("Falha na conexao!");
	return false;
}


void getReferencia(){

	Serial.println("Obtendo o valor de referencia");

	//leitura da faixa de referencia
	int vMedia=0;

	for(int i=0;i<60;i++){
		digitalWrite(pinLedVerde, LOW);
		digitalWrite(pinLedAmarelo, LOW);
		digitalWrite(pinLedVermelho, LOW);
		vMedia+=analogRead(A0);
		digitalWrite(pinLedVerde, HIGH);
		delay(100);
		digitalWrite(pinLedVerde, LOW);
		digitalWrite(pinLedAmarelo, HIGH);
		delay(100);
		digitalWrite(pinLedAmarelo, LOW);
		digitalWrite(pinLedVermelho, HIGH);
		delay(100);
	}

	vRef=vMedia/60;

	Serial.println("Valor de referencia: " + String(vRef));

	digitalWrite(pinLedVerde, HIGH);
	digitalWrite(pinLedAmarelo, LOW);
	digitalWrite(pinLedVermelho, LOW);

}

bool sendData(int vLed,double vValorAnalogMedio, double vValorAnalogPico) {

	Serial.println("Enviando as informacoes para o servidor");

	String values = "idd=[D]&idr=[R]&idl=[L]&ide=[E]&idam=[AM]&idap=[AP]";
	values.replace("[D]", String(vDisp));
	values.replace("[R]", String(vRef));
	values.replace("[L]", String(vLed));
	values.replace("[E]", String(vIdEmpresa));
	values.replace("[AM]", String(vValorAnalogMedio));
	values.replace("[AP]", String(vValorAnalogPico));

	String postString="POST /received.php HTTP/1.1\r\n"
			"Host: [S]\r\n"
			"Content-Type: application/x-www-form-urlencoded\r\n"
			"Content-Length: [LEN]\r\n"
			"Accept: */*\r\n"
			"User-Agent: Arduino/1.0\r\n"
			"Connection: close\r\n"
			"\r\n"
			"[VALUE]\r\n";

	postString.replace("[LEN]", String(values.length()));
	postString.replace("[VALUE]",values);
	postString.replace("[S]",String(vServer));

	Serial.println(postString);

	esp8266("AT+CIPMUX=0");
	if (waitAndEcho(500, "OK\r\n")) {
		esp8266("AT+CIPSTART=4,\"TCP\",\"" + String(vServer) + "\"," + String(vServerPort));
		if (waitAndEcho(5000, "OK\r\n")) {
			esp8266("AT+CIPSEND=" + String(postString.length()));
			if (waitAndEcho(3000, ">")) {
				esp8266(postString,"");
				if(waitAndEcho(15000,"OK\r\n")){
					vTotal++;
					vSendOk=true;
					Serial.println("Informacao enviada com sucesso");
					Serial.println("Registros enviados: " + String(vTotal));
						while (esp8266.available()) {
							String tmpResp = esp8266.readString();
							(tmpResp);
						}
					esp8266("AT+CIPCLOSE");
				}
				return true;
			}
		}
	}
	Serial.println("Falha no envio da informacao");
	return false;
}


int getValorRuido() {

	tempo=tempo*30; 

	Serial.println("Calculando o valor de som");
	int a=0;
	vValorDigital=0;
	int vValorAnalogOld=0;
	int vValorAnalogTotal=0;
	for(int i2=0;i2<tempo;i2++)
	{
		for(int i=0;i<100;i++)
		{
			vValorDigital+=digitalRead(pinDigital);
			vValorAnalogOld=analogRead(pinAnalog);

			if(vValorAnalogOld>vValorAnalogPico)
			{
				vValorAnalogPico=vValorAnalogOld; // Variável vValorAnalogPico encontra o valor analog de pico em 1s de medição
			}
			delay(10);
		}
		vValorAnalogTotal+=vValorAnalogPico;
		if(vValorDigital > 0)
		{
			digitalWrite(pinLedVerde, HIGH);
			delay(200);
			digitalWrite(pinLedVerde, LOW);
			delay(200);
			digitalWrite(pinLedVerde, HIGH);
			delay(200);
			digitalWrite(pinLedVerde, LOW);
			delay(200);
			digitalWrite(pinLedVerde, HIGH);
			delay(200);
			digitalWrite(pinLedVerde, LOW);
			a++;
		}
		else
		{
			digitalWrite(pinLedVerde, HIGH);
			digitalWrite(pinLedAmarelo, LOW);
			digitalWrite(pinLedVermelho, LOW);
			delay(1000);
		}

	}
	vValorAnalogMedio=vValorAnalogTotal/tempo;
	tempo=tempo/2;

	if(a>tempo)
	{
		Serial.println("Valor de som obtido: 1");
		return 1;
	}
	else
	{
		Serial.println("Valor de som obtido: -1");
		return -1;
	}
}

int getPositivo(int vNum){

	if(vNum>0){
		return vNum;
	}
	else{
		return vNum*-1;
	}

}

void setup() {

	Serial.begin(9600);
	while (!Serial); // wait for Arduino Serial Monitor to open

	esp8266.begin(9600);

	pinMode(pinLedVerde, OUTPUT);
	pinMode(pinLedAmarelo, OUTPUT);
	pinMode(pinLedVermelho, OUTPUT);

	pinMode(pinDigital, INPUT);
	pinMode(pinAnalog, INPUT);

	digitalWrite(pinLedVerde, HIGH);
	digitalWrite(pinLedAmarelo, LOW);
	digitalWrite(pinLedVermelho, LOW);

}


void loop() {

	waitAndEcho(1000,".");
	Serial.println("------Inicio da medicao-------");

	n = getValorRuido();  //Chamada de função que faz a leitura do ruído

	if (n == -1 && x == -1) {
		digitalWrite(pinLedVerde, HIGH);
		digitalWrite(pinLedAmarelo, LOW);
		digitalWrite(pinLedVermelho, LOW);
		vLed=1;
	}
	if(n==-1 && x==0) {
		digitalWrite(pinLedVerde, HIGH);
		digitalWrite(pinLedAmarelo, LOW);
		digitalWrite(pinLedVermelho, LOW);
		x=-1;
		vLed=1;
	}
	if(n==1 && x==0){
		digitalWrite(pinLedVerde, LOW);
		digitalWrite(pinLedAmarelo, LOW);
		digitalWrite(pinLedVermelho, HIGH);
		x=1;
		vLed=3;
	}
	if(n==1 && x==1){
		digitalWrite(pinLedVerde, LOW);
		digitalWrite(pinLedAmarelo, LOW);
		digitalWrite(pinLedVermelho, HIGH);
		x=1;
		vLed=3;
	}
	if(n==1 && x==-1) {
		digitalWrite(pinLedVerde, LOW);
		digitalWrite(pinLedAmarelo, HIGH);
		digitalWrite(pinLedVermelho, LOW);
		x=0;
		vLed=2;
	}
	if(n==-1 && x==1) {
		digitalWrite(pinLedVerde, LOW);
		digitalWrite(pinLedAmarelo, HIGH);
		digitalWrite(pinLedVermelho, LOW);
		x=0;
		vLed=2;
	}

	vSendOk=false;

	//Envia a notificacao
	while(vSendOk==false){
		if (espConfig()) {
			sendData(vLed,vValorAnalogMedio,vValorAnalogPico);
		}
		else{
			delay(3000);
		}
	}

	Serial.println("------Fim da medicao-------");
	Serial.println("Referencia " + String(vRef));
	Serial.println("Medio " + String(vValorAnalogMedio));
	Serial.println("Medio-ref " + String(vValorAnalogMedio-vRef));
	Serial.println("Pico " + String(vValorAnalogPico));

	vValorAnalogMedio = 0;
	vValorAnalogPico = 0;

}

tks

Did you get the special Arduino with 2 terabytes of memory? You are pissing memory away like crazy, and, I'm nearly certain, running out.

Get rid of EVERY String instance. Wrap string literals in the F() macro. Then, maybe, you will have a chance.