Arduino to ESP8266 serial communication

Hi guys,

I'm currently working on a project that involves getting an Arduino Uno to chat to an ESP8266 (cheap WiFi module) over serial. The idea being that the Arduino can send data to the ESP8266 which will then forward the data onto a server.

However I'm having a little bit of trouble with my serial connection between the two. So far I've setup SoftwareSerial on pins 8 and 9 on the Arduino Uno which connect to the RX and TX on the ESP8266 and set the ESP and Uno to have a baud rate of 115200. I can send data using Serial.println(somedata) from the Uno to the ESP, which works fine every time. However when I try to send data from the ESP to the Uno, the data arrives but with some incorrect characters. Below is what the uno received when I got the to ESP send the string "Testing 123 Testing" to the Uno every second with the Uno looking for serial information every 0.5 seconds using Serial.read:

Tesring 123 Tesring
Tdsting 123 Testing
Testing 023 Testing
Testiog 123 Testiog
Testing 123 Testing
Testing 123 Testing
Testing 023 Testing

As you can see, it seems to be muddling up characters somewhere down the line. Below is the code I'm using on the Uno to receive the Serial string from the ESP which is being called every 0.5 seconds:

void receiveCommand()
{
  int x = 0;
  str="";
  do{  
    if(Serial1.available()){
      in = Serial1.read();
      str = str + in;
    }
    x++;
  }while(in != '\n' && x<50);

  str.trim();
  Serial1.flush();
}

I've been banging my head against my desk for a while now, so any help would be much appreicated!

Many thanks,
Chris

have you tried lowering the baud rate?

Why is in a global variable?

Do you KNOW what flush() does? If so, explain why you need to call it. If not, explain why you ARE calling it.

Lowering the baud rate helped a bit (by that I mean I'm not getting incorrect characters) however there is still a problem. Currently I'm getting a lot of this from the Uno serial monitor window when receiving serial data from the ESP:

WiFi con'�����

Which is meant to say Wifi connected.

PaulS in response to your question I believe flush lets the Serial.print finish before moving on (correct me if I'm wrong, as you can probably tell I'm a bit of a noobie when it comes to serial communication on Arduino). And what were you wondering about a global variable?

How are you powering the ESP8266?

The Arduino 3.3v pin cannot provide enough current. I used an LM317 to get 3.3v from the Arduino 5v pin.

...R

I'm using a separate PSU that generates 3.3V for this

PaulS in response to your question I believe flush lets the Serial.print finish before moving on (correct me if I'm wrong, as you can probably tell I'm a bit of a noobie when it comes to serial communication on Arduino).

It does exactly that. But, why do you need to do that? Serial output is buffered. It will be sent when it can be sent. There is no reason, usually, to assure that it has all been sent before moving on.

Ah ok my bad, I'll remove that. Do you reckon that is the source of my issues?

Have you tried directly communicating with the esp8266 via the serial monitor to see if you have the same issue?

zoomkat:
Have you tried directly communicating with the esp8266 via the serial monitor to see if you have the same issue?

Yeah I have, the output reads fine every time

I have only had success at 9600 BAUD between this type of module and Arduinos. and where ever possible, I try to use Hardware serial rather than software, but if you do need to send info, you could use a checksum - always a good idea anyway for any data transfer.