HC-06 stops receiving after ~2 minutes.

Hello,

I have managed to get an HC-06 module connected to my Arduino Uno and can operate communication in both directions to an Android app. If I am only sending data from the app to the Arduino (ie the Arduino code contains no write commands) the HC-06 happily sends data over the serial connection to the Arduino.

However, when I send collected data at regular one second intervals from the Arduino to the phone whilst also receiving data, the received data stops appearing at the Arduino after ~2 minutes. The Bluetooth connection is not lost, as data from the Arduino is still visible at the phone.

I am sure the baud rates are correct between the Arduino and the HC-06, and I am sending about 20 bytes each way max per interval.

The data generated by the Arduino is collected from 2 I2C breakout boards I have connected.

I have tried both the SoftwareSerial library and the AltSoftSerial library in case the issue was reading and writing occurring at the same time.

I am basically wondering if anyone has experienced similar and has any advice as to debugging/fixing the issue? I'm not confident enough to say the device is faulty, as I can obtain constant communication so long it is one way.

Thanks in advance, and hopefully I haven't missed a similar post in the past!

zaafiir:
I'm not confident enough to say the device is faulty

There is probably nothing to worry about there, but I can clearly see that your code is junk.

Hi Nick,

Thanks for your response, glad there’s nothing to worry about, an intermittent fault seemed unlikely.

I have attached my code, I know my print statement’s seem inefficient, that was just ease for me in all honesty, but I don’t think I am doing anything too suspicious but maybe you can identify the “junk” that is at fault.

Thanks again!

boolean debug = true;

#include <AltSoftSerial.h>
#include <Adafruit_TCS34725.h>
#include <Adafruit_VEML6075.h>


AltSoftSerial BTSerial;

// max length of command is 20 chrs
const byte numChars = 20;
char receivedChars[numChars];
boolean newData = false;

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
Adafruit_VEML6075 uv = Adafruit_VEML6075();

//for timing
unsigned long start;
unsigned long current;
const unsigned long period = 10000;


void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");

  BTSerial.begin(9600);
  bool check1 = false;
  bool check2 = false;
  while (!check1 || !check2)
  {
    if (tcs.begin())
    {
      Serial.println("RGB Sensor Detected");
      check1 = true;
    } else {
      Serial.println("RGB Sensor Not Detected");
    }
    if (uv.begin())
    {
      Serial.println("UV Sensor Detected");
      check2 = true;
    } else
    {
      Serial.println("UV Sensor Not Detected");
    }
    delay(5000);
  }
  start = millis();
}

void loop()
{
  if (BTSerial.available() > 0)     {
    recvWithStartEndMarkers();
  }
  if (newData) {
    parseData();
  }
  current = millis();
  if (current - start >= period)
  {
    sendRGB();
    sendUV();
    start = current;
  }
}

void sendRGB()
{
  uint16_t r, g, b, c, lux;
  tcs.getRawData(&r, &g, &b, &c);
  lux = tcs.calculateLux(r, g, b);
  BTSerial.print("<");
  BTSerial.print("R: "); BTSerial.print(r, DEC); BTSerial.print(" ");
  BTSerial.print("G: "); BTSerial.print(g, DEC); BTSerial.print(" ");
  BTSerial.print("B: "); BTSerial.print(b, DEC); BTSerial.print(" ");
  BTSerial.print("C: "); BTSerial.print(c, DEC); BTSerial.print(" ");
  BTSerial.print("Lux: "); BTSerial.print(lux, 4); BTSerial.print(" ");
  BTSerial.println("");
}

void sendUV()
{
  BTSerial.print("UV Index: "); BTSerial.print(uv.readUVI());
  BTSerial.print(">");
  Serial.print("UV Index: "); Serial.print(uv.readUVI());
}

void parseData()
{
  newData = false;
  if (debug) {
    Serial.println( receivedChars );
  }
  if (receivedChars[0] == 'O'  && receivedChars[1] == 'N' )  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  if (receivedChars[0] == 'O'  && receivedChars[1] == 'F' )  {
    digitalWrite(LED_BUILTIN, LOW);
  }
}


void recvWithStartEndMarkers()
{

  // function recvWithStartEndMarkers by Robin2 of the Arduino forums
  // See  http://forum.arduino.cc/index.php?topic=288234.0

  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  if (BTSerial.available() > 0)
  {
    rc = BTSerial.read();
    if (debug) {
      Serial.println(rc);
    }
    if (recvInProgress == true)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else
      {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

OK, what I was sort of expecting was Strings gumming things up, but no. Clearly nothing wrong with wiring or serial input either, the latter usually being the hard stuff.

While your loop timing will please the pedants, it is likely unnecessary and may be a problem. I bet you will never know the difference if you ditched it all and just put a delay(1000); in the loop. You might also send a flag to serial monitor as well, just so you really know that is working.

For all that, since your objective is to send live data to the phone, I see no point (ultimately) in sending data to the serial monitor. You might as well send it all to the phone via hardware serial, thereby dispensing with software serial of any type. This would allow you to debug everything on the monitor - including all serial traffic. This requires you to have Bluetooth disconnected when uploading.

I don't think your print statements are common practice but, if they work, they work, and are not the cause of the current problem.

Hi Nick,

Thanks for taking the time to reconsider your initial reaction, the loop timing was only done as such as I recall seeing similar code in a colleague's work when I was interfacing an FPGA via serial and I2C protocols to their microprocessors, I will try just using a delay.

I shall also try using the hardware serial pins (0 and 1 on the Uno) for the Bluetooth as you suggest, currently the serial monitor printing is merely for sanity checking, but I see your point that so long the PC does not send anything I can just listen in on the Bluetooth communications.

However, when I send collected data at regular one second intervals from the Arduino to the phone whilst also receiving data, the received data stops appearing at the Arduino after ~2 minutes.

This looks like sending at 10 second intervals

const unsigned long period = 10000;

I shall also try using the hardware serial pins (0 and 1 on the Uno) for the Bluetooth as you suggest

Yes, getting this on hardware serial may solve the issue, and if not, it will simplify what you have to troubleshoot.

zaafiir:
I can just listen in on the Bluetooth communications.

Correct. It's the sort of thing that does wonders for your self-confidence, not to mention your catalogue of computer resources, and other technical advantages. Note also that serial is serial, and, with Bluetooth disconnected, you can send dummy Bluetooth data as well as receive it, thereby being able to prove your entire operation before you even buy your Bluetooth (!).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.