Problem using esp8266 on arduino´s UART

Hi everybody,

I have been the last 2 days trying to understand what is going on with my esp8266.
I have an android app that connects via wifi with the esp8266 to a server that esp8266 creates.
I have been using the Software serial to connect the esp8266 in order to have the Hardware serial free to comunicate with the computer (It works great):

#include <SoftwareSerial.h>
SoftwareSerial esp8266(6,4);

So what is the problem? The problem is that I want to upload sketches via wifi and to do so, I need to have the esp8266 connected to the HARDWARE serial, not to the SOFTWARE serial.
So I said, it is fine, y connect there and I change the code to use Serial instead of “Software Serial”:

// Using software serial
esp8266.begin(9200);
esp8266.println
esp8266.write

//Using hardware serial
Serial.begin(9200)
Serial.println
Serial.write

After doing this everything seemed to work fine, I could change the SSID of the esp8266, I could create a server and I could receive data from my app. But I don´t know why when sending data with Serial.write , NOW my app doesn´t read ANYTHING, but apparently the esp8266 is sending data because I can see the blue led blinking.
I have tried everything , I tried connecting from my pc with a python client and It does not read anything.

I would like to know what could be the problem, I think that there souldn´t be differences between the way the Software and Hardware serials send data, both of them should send the data serialized. Shouldn´t they?

It think I am missing something.

Thank you for your help :wink:

I can only say that the speed is non-standard, although I don't know if it is relevant to your problem:

Serial.begin(9200)

Yes, sorry, I made a mistake, I confused 9600 and 19200. My actual speed is 19200

I continue with this issue, I am not able to solve it...

Is that code snippet running on an ESP8266 ? The architecture of your set up is not very clear in the description but pin 6 would be an unusual choice of pin on an ESP8266, even if it is broken out because it appears to be used to talk to the flash memory. See: http://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations

Posting your entire code might be a good idea.

The code is running on the arduino nano, and the TX and RX are connected to the pin 6,4 of the arduino nano. This WORKS great, even if I connect for example to the pin 3,4 continues working.
The thing is that with the SOFTWARE serial everything works fine, but when I connect to the arduino nano´s TX and RX (HARDWARE SERIAL) the the arduino can receive input data from the esp8266 (Send with an android app) but when sending data from the arduino´s TX using the esp8266, de movil phone or the client does not read anything (but the esp8266 seems to send well because the led blinks.

So my question is why using the SERIAL software the data is sent well, but when using the HARDWARE serial the data is sent in another way that I can´t read from a client.

Thank you for your answer, I hope now is more clear to understand.

Are you performing serial operations on the Nano (Serial.print etc.) which also use the hardware serial interface and may interfere with whatever it is you are doing with the ESP ? Why not post your code ?

6v6gt:
Are you performing serial operations on the Nano (Serial.print etc.) which also use the hardware serial interface and may interfere with whatever it is you are doing with the ESP ?
Why not post your code ?

Yes, but the Serial operations, if the esp8266 is connected to the hardware serial, shouldn´t work over the wifi and send the data correctly?

Here are these 2 codes, the first one using the SOFTWARE serial, it WORKS well:

#include <SoftwareSerial.h>
SoftwareSerial esp8266(6, 4); 

String espconfig[] =
{ "AT+UART_DEF=19200,8,1,0,0",
  "AT+RST",
  "AT+CWMODE=2",
  "AT+CWSAP=\"ssidname\",\"12345678\",5,3",
  // "AT+CIFSR" ,
  "AT+CIPMUX=1",
  "AT+CIPSERVER=1,80",
};
String content;
float ECHO=0;
int connectionId=0;

void setup() {
  esp8266.begin(19200);
  for (int i = 0; i < 6; i++)
  { delay(500);
    esp8266.println(espconfig[i]);
  }

}

void loop() {
  wifireceive();

}
void wifireceive(){
 if (esp8266.available()){
   if (esp8266.find("+IPD,")){
     delay(5);
     connectionId = esp8266.read() - 48;
     if (esp8266.find("?")){
       float Number = esp8266.parseInt();
       if (Number >= 9000) { //Number must be >9000
        ECHO = Number;
       }
     }

   }

 }
 content=ECHO;
 wifisend();
 while ( esp8266.available() > 0 && esp8266.read() != -1 );
}
void wifisend(){
  String httpResponse;
  httpResponse += "\r\n";
  httpResponse = content + httpResponse;// + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
  
  String cipSend = "AT+CIPSEND=";
  cipSend += connectionId;
  cipSend += ",";
  cipSend += httpResponse.length();
  cipSend += "\r\n";
  esp8266.println(cipSend);
  
  delay(5);
  int dataSize = httpResponse.length();
  char data1[dataSize];
  httpResponse.toCharArray(data1, dataSize);
  esp8266.write(data1, dataSize);
  delay(5);
  while ( esp8266.available() > 0 && esp8266.read() != -1 );
  content = "";

  
}

And here is the same code but with the esp8266 attached to the HARDWARE serial (DOESN´T WORK):

String espconfig[] =
{ "AT+UART_DEF=19200,8,1,0,0",
   "AT+RST",
  "AT+CWMODE=2",
  "AT+CWSAP=\"ssidname\",\"12345678\",5,3",
  // "AT+CIFSR" ,
  "AT+CIPMUX=1",
  "AT+CIPSERVER=1,80",
};
String content;
float ECHO=0;
int connectionId=0;

void setup() {
  Serial.begin(19200);
  for (int i = 0; i < 6; i++)
  { delay(500);
    Serial.println(espconfig[i]);
  }

}

void loop() {
  wifireceive();

}
void wifireceive(){
 if (Serial.available()){
   if (Serial.find("+IPD,")){
     delay(5);
     connectionId = Serial.read() - 48;
     if (Serial.find("?")){
       float Number = Serial.parseInt();
       if (Number >= 9000) { //Number must be >9000
        ECHO = Number;
       }
     }

   }

 }
 content=ECHO;
 wifisend();
 while ( Serial.available() > 0 && Serial.read() != -1 );
}
void wifisend(){
  String httpResponse;
  httpResponse += "\r\n";
  httpResponse = content + httpResponse;// + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
  
  String cipSend = "AT+CIPSEND=";
  cipSend += connectionId;
  cipSend += ",";
  cipSend += httpResponse.length();
  cipSend += "\r\n";
  Serial.println(cipSend);
  
  delay(5);
  int dataSize = httpResponse.length();
  char data1[dataSize];
  httpResponse.toCharArray(data1, dataSize);
  Serial.write(data1, dataSize);
  delay(5);
  while ( Serial.available() > 0 && Serial.read() != -1 );
  content = "";

  
}

With the SOFTWARE serial I can read and send data by wifi to mi client correctly, everything works fine.

With the HARDWARE serial I can read the inputs of the wifi, but when sending data with the wifi my client can´t read anything, I don´t know what the wifi is sending…

Thank you :slight_smile:

Are you using the USB cable to connect the Nano to your PC at the same time as using the hardware serial pins 0 and 1 on the Nano to connect to your ESP device ? Are you using any form of level shifting between the 5volt Nano and the 3.3 volt ESP device ?

6v6gt: Are you using the USB cable to connect the Nano to your PC at the same time as using the hardware serial pins 0 and 1 on the Nano to connect to your ESP device ? Are you using any form of level shifting between the 5volt Nano and the 3.3 volt ESP device ?

I tried both of them. First I started trying with the usb connected to the pc but later I thought that maybe that was the problem so I used an external power suply connected to the Vin but still didn´t work.

Yes, I use a tension divider in TX (I have tried withput the tension divider too), but I power the esp8266 with 4,7V. I know it is recommended to power it with just 3,3V but I have been using it for the last 6 months and it works perfect.

I have done the same I do when using the software serial, the same configuration, but it just doesn´t work with the hardware serial.

Anyone?