Here is an image of my setup:
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:
Bla 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:
#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:
#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);
}
}