-->
Page 1 of 2

ESP to Arduino serial not displaying correct data

PostPosted: Thu Jun 16, 2016 11:43 am
by StewThom
Hi, I am currently working on a project that requires an EPS8266 to communicate with an Arduino over serial however i am having some issues with the serial passing the wrong characters. I come from a purely code background and am relatively new to the Arduino SDK so i may be making a stupid mistake.

Here is an image of my setup:
Image

When i press the button it triggers the ESP to send a char[] over serial to the Arduino which then passes it through it's serial connection to the serial monitor on my Mac.

The problem is that when the serial monitor receives the data it is occasionally printing the wrong characters.
For instance if i press the button 10 times with the string "Bla Bla Bla Bla" being passed each time the serial monitor prints:
Code: Select allBla Bla Bla Bla
Bla Bl` Bla Bla
Bla Bla Bla Bla
Bla Bl` Bla Bla
Bla Bla Bla Bla
Bl` Bla Bla Bl`
Bla Cla Bla Bla
Bla Bla Bla Bla
Bla Bla Bla Cla
Bla Cla Bla Bla

As you can see it prints correctly some of the time however it changes some characters the rest of the time.

I have done some checks and have determined that the string being received by the Arduino's serial is incorrect. so something is going wrong between the Serial.write() on the ESP and the ESPSerial.readString() on the Arduino.

I have tried different ESPs as well but it didn't change anything.

As i said i am relatively novice at this and i may be making a stupid mistake somewhere but i can't see it, any help would be appreciated.
Thanks.

ESP code:
Code: Select all#import <ESP8266WiFi.h>

int SwitchState;
int LastSwitchState = HIGH;
int DefaultState = HIGH;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  while(!Serial) {;}

  Serial.println("Setup");

  pinMode(2, INPUT_PULLUP);
 
}

void loop() {
  // put your main code here, to run repeatedly:

  int reading = digitalRead(2);

  if (reading != LastSwitchState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
 
  if ((millis() - lastDebounceTime) > debounceDelay) {

  if (reading != SwitchState) {
      SwitchState = reading;

      // only toggle the LED if the new button state is HIGH
      if (SwitchState == !DefaultState) {
        String str = "Bla Bla Bla Bla";
        int stringLength = str.length() + 1;
        char buf[stringLength];
        str.toCharArray(buf, stringLength);
        Serial.write(buf, stringLength);
      }
    }
  }

  LastSwitchState = reading;
}


Arduino code:
Code: Select all#include <SoftwareSerial.h>

SoftwareSerial ESPSerial(7,6);


void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  while(!Serial) {;}

  ESPSerial.begin(115200);
  while(!ESPSerial) {;}

  Serial.println("Arduino: Setup");
}

void loop() {
  // put your main code here, to run repeatedly:
  int bufferSize = 256;
  char data[bufferSize];

  String str;
  if(ESPSerial.available()) {
    delay(100);
    str = ESPSerial.readString();
    Serial.println(str);
  }
}

Re: ESP to Arduino serial not displaying correct data

PostPosted: Thu Jun 16, 2016 5:46 pm
by martinayotte
It is well known that the SoftwareSerial is having trouble at high speed such 115200.
Reduce it on both ESP and Arduino, something like 9600 or 19200, it should work better.

Re: ESP to Arduino serial not displaying correct data

PostPosted: Fri Jun 17, 2016 8:37 am
by StewThom
Thanks martin, reducing the baud rate to 9600 worked like a charm.

I do have one more question, The serial connection to the ESP is proving a bottleneck in my project, compared to a direct WiFi connection serial is pedestrian at best, it's not a major issue but it would be nice if i could speed up the data transfer, Is there any way of speeding it up or an alternative to serial that would be faster?

Thanks.

Re: ESP to Arduino serial not displaying correct data

PostPosted: Fri Jun 17, 2016 9:21 am
by martinayotte
If Serial rate is an issue, you should avoid using SoftwareSerial, and connect the ESP to real UNO Serial instead. It is possible but you won't be able to use the UNO USB Serial Monitor at the same time. You can add a jumper to disconnect the ESP temporarily while uploading new sketch to the UNO and placing it back to reconnect the ESP.

Another solution is to use another flavour of Arduino that have more than 1 hardware Serial, such the Mega, or even other kinds of boards, such as the MapleMini (which clones can be found on eBay for less than $4). MapleMinis are supported by stm32duino.com