Wifi ESP8266 with arduino to transmit data to smartphone

Good Afternoon,
I have connect ESP8266 to arduino and I have created an app with "app inventor" tool to transmit data to arduino and it works. Now I want to receive data from arduino but it doesn't work.
First of all
I have connected ESP8266 to Arduino pin 6 and 7 and by using the library SoftwareSerial I have configured, with AT commands, the ESP8266 as a PC server, connected to my wifi, with static IP.
In app on smartphone I have used an external Client component that open connection with the specific IP of ESP8266 on port 333.
Then I use the Client component command to send a word and by using the "read" function of SoftwareSerial I can read the received data from ESP8266, and it works!!
Now I would send something to my app from Arduino and ESP8266 but When I use the AT+CIPSTATUS? to verify the connection the ESP8266 answer is "ERROR".
Anyway I try to send a char with the following commands

TCPSerial.println("AT+CIPSEND=2");
delay("1000");
TCPSerial.println("a");

But I don't receive anything on my app.
Moreover if in Arduino I send the command

TCPSerial.println("AT+CIPCLOSE");

I can send always data from my app so the connection is still up.

So my question is
How can I send data from arduino to my app, by using AT commands, if the connection is open by the client on my APP?

I send the pic of application and complete arduino code

Thanks a lot for any suggestion

#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial TCPSerial(6,7); // RX, TX
String messaggio;
int checkOK=0;
char psw[40];
int cerca=1;
char data[40];
char data2[80];
String compare;
volatile int count=0;
void TSC_Callback(){
  count=count+1;
  if (count>1200) {count=0;
  configurazione();
  }
  
  }
void configurazione() {

  checkOK=0;
  Serial.println("passo0"); 
  for(int i=0;i<40;i++){data[i]=" ";}
  while(checkOK==0){
  TCPSerial.println("AT+CWMODE_CUR=1");//configura la scheda come Computer

  delay(1000);
  if(TCPSerial.available()){  for(int i=0;i<40;i++){data[i]= TCPSerial.read();Serial.print(data[i]);}}
  compare=String(data);
  Serial.println(compare);
  if(compare.indexOf("OK")!=-1)checkOK=1;
  
  
  }
Serial.println("passo1");
  for(int i=0;i<80;i++){data2[i]=" ";}
  checkOK=0;
  while(checkOK==0){ 
     for(int i=0;i<80;i++){data2[i]=" ";} 
    TCPSerial.println("AT+CWJAP_DEF=\"air2bite_MV\",\"114121641\",\"e4:aa:55:55:55:55\"");
     delay(10000);
    
    if(TCPSerial.available()){  for(int i=0;i<80;i++){data2[i]= TCPSerial.read();}}
  compare=String(data2);
  if(compare.indexOf("178721747")!=-1)checkOK=1;
  Serial.println(compare);
    
    
  }
Serial.println("passo2");
  for(int i=0;i<40;i++){data[i]=" ";}
  checkOK=0;
  while(checkOK==0){  
    TCPSerial.println("AT+CIPMUX=1");
    delay(1000);
   if(TCPSerial.available()){  for(int i=0;i<40;i++){data[i]= TCPSerial.read();}}
  compare=String(data);
  if(compare.indexOf("OK")!=-1)checkOK=1;
    
    
  }
  Serial.println("passo3");
  checkOK=0;
  while(checkOK==0){  
    //configura la scheda come TCP server
    TCPSerial.println("AT+CIPSERVER=1");
    delay(1000);
   if(TCPSerial.available()){  for(int i=0;i<40;i++){data[i]= TCPSerial.read();}}
  compare=String(data);
  if(compare.indexOf("OK")!=-1)checkOK=1;
    
    
  }
Serial.println("passo4");
  for(int i=0;i<40;i++){data[i]=" ";}
  checkOK=0;
  while(checkOK==0){  
     for(int i=0;i<80;i++){data2[i]=" ";} 
    //configura l'indirizzo IP statico
    TCPSerial.println("AT+CIPSTA_CUR=\"192.168.159.199\",\"192.168.159.1\",\"255.255.255.0\"");
    delay(1000);
    if(TCPSerial.available()){  for(int i=0;i<80;i++){data2[i]= TCPSerial.read();}}
  compare=String(data2);
  if(compare.indexOf("192.168.159.199")!=-1)checkOK=1;
  Serial.println(compare);
    
    
   
  }
Serial.println("passo5");
  for(int i=0;i<40;i++){data[i]=" ";}
  checkOK=0;
  while(checkOK==0){  
    
    TCPSerial.println("AT+CIPSTO=30");
     delay(1000);
    if(TCPSerial.available()){  for(int i=0;i<40;i++){data[i]= TCPSerial.read();}}
  compare=String(data);
  if(compare.indexOf("OK")!=-1)checkOK=1;
   
    
  }

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

configurazione();


}

void loop() {
 
cerca=1;
while(cerca){

for(int i=0;i<40;i++){ data[i]=" ";}
if(TCPSerial.available()){
      data[0]= TCPSerial.read();
     
      }
 messaggio=String(data);
 
if((messaggio.indexOf("c")!=-1)or(messaggio.indexOf("p")!=-1))

 { 

    for(int i=1;i<15;i++){
                          
                          data[i]= TCPSerial.read();
                         
                          }
  
  compare=String(data);
  if((compare.indexOf("cancello")!=-1)or(compare.indexOf("porta")!=-1)) 
  {
    cerca=0; 
    //Serial.print(compare);
  }

  }
}

if(compare.indexOf("cancello")!=-1)
{
  digitalWrite(8,HIGH);
  delay(300);
   digitalWrite(8,LOW);
   Serial.println("cancello");
   TCPSerial.println("AT+CIPMODE?");
   delay("500");
   for(int i=0;i<40;i++){ data[i]=" ";}
   for(int i=1;i<40;i++){ data[i]= TCPSerial.read(); Serial.print(data[i]);  }
   
   TCPSerial.println("AT+CIPSTATUS?");
   delay("500");
   for(int i=0;i<40;i++){ data[i]=" ";}
   for(int i=1;i<40;i++){ data[i]= TCPSerial.read(); Serial.print(data[i]);  }
   TCPSerial.println("AT+CIPSEND=2");
   delay("1000");
   TCPSerial.println("a");
  }
  
if(compare.indexOf("porta")!=-1)
  {
    digitalWrite(9,HIGH);
    delay(300);
     digitalWrite(9,LOW);
     Serial.println("porta");
   TCPSerial.println("AT+CIPSTATUS?");
   delay("500");
   for(int i=0;i<40;i++){ data[i]=" ";}
   for(int i=1;i<40;i++){ data[i]= TCPSerial.read(); Serial.print(data[i]);  }
   TCPSerial.println("AT+CIPSEND=2");
   delay("1000");
   TCPSerial.println("a");
   TCPSerial.println("AT+CIPCLOSE");
    }

}


sorry I found the problem.....in the bottom when I checked the IPSTATUS I put question mark at the end and that was an error. Moreover I used the wrong command to send data. The correct one is

 TCPSerial.println("AT+CIPSTATUS");
   delay("500");
   for(int i=0;i<80;i++){ data2[i]=" ";}
   for(int i=1;i<80;i++){ data2[i]= TCPSerial.read(); Serial.print(data2[i]);  }
   TCPSerial.println("AT+CIPSENDEX=0,2");
   delay("1000");
     for(int i=0;i<80;i++){ data2[i]=" ";}
   for(int i=1;i<40;i++){ data2[i]= TCPSerial.read(); Serial.print(data2[i]);  }
   TCPSerial.println("a");
   delay("1000");
     for(int i=0;i<80;i++){ data2[i]=" ";}
   for(int i=1;i<40;i++){ data2[i]= TCPSerial.read(); Serial.print(data2[i]);  }

Thanks a lot and I hope this code can help other people
Bye!!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.