2

enter image description here

I am recieving the following in my serial monitor. There is a mix of wierd characters then the spelling changes for words once it gets repeated. I don't know why its doing this. my esp01 chip uses 115200 baud rate and the program is 115200 baud rate as well. What is going on.

#include <SoftwareSerial.h>

#define TIMEOUT 5000 // mS
#define LED 5

SoftwareSerial mySerial(7, 6); // RX, TX

const int button = 11;
int button_state = 0;


void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(button, INPUT);

  Serial.begin(115200);
  mySerial.begin(115200);

  SendCommand("AT+RST", "Ready");

  delay(5000);

  SendCommand("AT+CWMODE=3", "OK");
  SendCommand("AT+CIFSR", "OK");
  SendCommand("AT+CIPMUX=1", "OK");
  SendCommand("AT+CIPSERVER=1,80", "OK");
}


void loop() {
  button_state = digitalRead(button);
  mySerial.println(button_state);

  if (button_state == HIGH) {
    SendCommand("AT+CIPSEND=0,23", "OK");

    delay(250);

    mySerial.println("<h1>Button was pressed!</h1>");

    delay(250);

    SendCommand("AT+CIPCLOSE=0", "OK");
  }

  String IncomingString = "";
  boolean StringReady = false;

  while (mySerial.available()) {
    IncomingString = mySerial.readString();
    StringReady = true;
  }


  if (StringReady) {
    Serial.println("Received String: " + IncomingString);

    if (IncomingString.indexOf("LED=ON") != -1) {
      digitalWrite(LED, HIGH);
    }

    if (IncomingString.indexOf("LED=OFF") != -1) {
      digitalWrite(LED, LOW);
    }
  }
}


boolean SendCommand(String cmd, String ack) {
  mySerial.println(cmd); // Send "AT+" command to module

  if (!echoFind(ack)) // timed out waiting for ack string
    return true; // ack blank or ack found
}


boolean echoFind(String keyword) {
  byte current_char = 0;
  byte keyword_length = keyword.length();
  long deadline = millis() + TIMEOUT;

  while (millis() < deadline) {
    if (mySerial.available()) {
      char ch = mySerial.read();
      Serial.write(ch);

      if (ch == keyword[current_char])
        if (++current_char == keyword_length) {
          Serial.println();
          return true;
        }
    }
  }

  return false; // Timed out
}
1
  • 4
    SoftwareSerial doesn't work reliably at 115200 baud. use 9600 baud. do not forget to change the baud rate at AT firmware side
    – Juraj
    Nov 14, 2020 at 6:11

2 Answers 2

1

Try adding:

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(button, INPUT);

  Serial.begin(115200);
  while(!Serial){}       // Loop until USB is ready

  mySerial.begin(115200);

It will unblock once the USB port is ready.

0

Your code for the most part looks ok. Your symptoms indicate a Baud mismatch or the receiver is not getting all of the bits. Try doing without the delay's, even reducing them may help. Note when in a delay the processor is committed and cannot do anything else. See if that helps. 115000 baud is quit hot for the Arduino and software serial. Maybe you can slow things down a little and see if that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.