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:
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!
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?
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.
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.