0

I am using the ESP8266 WiFi module to receive a string and send it to a Arduino over serial. I am using the SoftwareSerial library to do this. My problem is that the string is sometimes garbled after it is sent to the Arduino.

After searching other similar questions most people suggest to use a baud rate of 9600, however my sketch is already using this.

Below is a code snippet:

void setup() 
{
  Serial.begin(BAUD_RATE);
  shieldcomm.beginSerial(new SoftwareSerial(RX_SERIAL, TX_SERIAL), BAUD_RATE);
  relay1.init();
  relay2.init();
  relay3.init();
  relay4.init();
  relay5.init();
  orderhandler.init(&relay1, &relay2, &relay3, &relay4, &relay5);
  dispensing.init();
}

void loop() 
{
  if(shieldcomm.incoming())
  {
    order = shieldcomm.receiveOrder();
    if(order.slot != "" && order.soda_amount != 0)
    {
      Serial.println(order.slot); // Garbled output
      dispensing.setIndicator(true);
      orderhandler.executeOrder(order); 
      dispensing.setIndicator(false);
    }
  }
}

ShieldCommunicator is a custom class. This is the source:

struct SodaRequest ShieldCommunicator::receiveOrder()
{
  struct SodaRequest request; 
  while (_shieldcomm->available() > 0)
  {
    request.slot = _shieldcomm->readStringUntil(':');
    request.soda_amount = _shieldcomm->readStringUntil('\n').toInt();
  }
  return request;
}
  • Are you totally sure that order.slot contains only printable chars? – frarugi87 Apr 19 '18 at 11:55
  • Yes, when order.slot is correct it prints for example "slot_1". But sometimes it can print something like "?lot_1". I also tried lowering the baud rate to 1200. This results in more correct output. – Thomas Apr 19 '18 at 12:55
  • 1
    What's the generator/source of the data being received? What is shieldcomm? Post library links. – TisteAndii Apr 19 '18 at 18:33
  • do you have a long wire run for the serial cables? or maybe they run parallel to something electrically noisy, like a coil? – dandavis Apr 20 '18 at 2:41
  • Can you post a Minimal, Complete, and Verifiable example? Your code in ShieldCommunicator::receiveOrder looks like it might terminate prematurely. Also the struct request does not look like it is initialized. – Nick Gammon Apr 23 '18 at 8:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.