HC-05 stops sending data

Hi everyone!

So I am building a data logger out of an Arduino nano and a HC-05 bluetooth module which are combined onto one board. Basically I am measuring a voltage on one of the analog pins on the arduino, split the 10 Bit value into two bytes and send them with a checksum to the HC-05 module. To measure in precise intervals I am using a timer interrupt. Also, I want to turn one of the DigitalOut pins (the one of the LED) HIGH or LOW to toggle a transistor, wich then toggles a load in another circuit.
I also wrote a program for my PC to recieve all the data in Visual Basic and using Windows Forms (probably not the best solution, but the only one I at least have some experience with).
Basically my program works, I am able to recieve the data and thus use, print and save it. Switching the Load on and off also works fine. But after a some time I am not able to recieve any data anymore. I have tested it in various different ways:

Using the code given below and my VB-programm results in no new data after ~5-15 minutes usually, sometimes more
Using the code without the timer but with an delay of 50ms and the VB-program: 1-1.5 hours
Using the code below and my VB-program, but connecting via USB: Runs perfectly for 4+ hours
Using the code below and showing the data with the arduino serial monitor: ~45 minutes (hadn’t checked whether the values made sense, because I used serial.write; might also just have been lucky)

Increasing the delay/timer doesn’t benefit it as well.

Everytime the transmission “stops” the connection to my Computer is still there. I still can switch the load/LED on and off. Also the Arduino is still sending data to the HC-05 module, which is indicated by a LED, wich flashes, when something is sent from the Arduino to the HC-05.

Right now I am completely clueless on what I’m doing wrong, since both programs seem to work perfectly fine on their own and also together, but only for a couple of minutes…

Thanks for every advice!

This is my Code for the Arduino:

String Text="";
int sensorValue = 0;
byte highbyte, lowbyte, crc, signal;

#define rxPin 11     //virtual RX-Pin for communication with the HC-05 module
#define txPin 12     //virtual TX-Pin for communication with the HC-05 module
#define ledpin 13   //output for LED and load
#define sensorPin 0 //AnalogInput

#include <SoftwareSerial.h>
SoftwareSerial HC05(txPin, rxPin);

void setup() {
  HC05.begin(38400);

Text = set("AT+RESET");
  if (!Text.startsWith("OK")) {
    while(1);     
  }
  delay(1000);

  Text = set("AT+UART=38400,0,0");
  if (!Text.startsWith("OK")) {
    while(1);     
  }
  
  Text = set("AT+INIT");
  Text = set("AT+DISC");

  analogReference(DEFAULT);
  
  noInterrupts();  
  //reset timer
  TCCR1A = 0;  //timer1 control register A
  TCCR1B = 0;  //timer1 control register B 
  TCNT1  = 0;  //timer1 counter

  OCR1A = 3124; //set compare match register of timer1 to 3124
  //set prescaler to 256 
  TCCR1B |= (1 << CS12);
  //activate timer
  TCCR1B |= (1 << WGM12);  //enable timer1 CTC mode (Clear Timer on Compare)
  TIMSK1 |= (1 << OCIE1A); //enable timer1 compare input
  interrupts(); 
}

ISR(TIMER1_COMPA_vect){  
  sensorValue = analogRead(sensorPin);
  highbyte=sensorValue/256;
  lowbyte=sensorValue%256;
  crc=170^highbyte^lowbyte;
  HC05.write(highbyte);
  HC05.write(lowbyte);  
  HC05.write(crc);
}

void loop() {  
 //recieve data from HC-05 module
 while(Serial.available() > 0){   
    signal = HC05.read();
    if (signal == 1) {                  
      digitalWrite(ledpin, HIGH);
    }
    if (signal == 0){
      digitalWrite(ledpin, LOW);
    }     
 }
}

String set(String command){
  char ch;                            
  String result="";                   /
  HC05.println(command);                    
  delay(500);
  while(HC05.available() > 0){
    ch = HC05.read();
    result.concat(ch);                 
  }  
  return result;
}

This is the important part of my code in the VB-application:
Recieving Data:

Private Sub SerialPort1_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim cnt As Byte                  'counter for incoming bytes
        Dim HighByte As Byte         'part one of the measured value
        Dim LowByte As Byte          'part two
        Dim crc As Byte                  'sent checksum
        Dim crc_ok As Byte             'calculated checksum
        Dim value As Integer           'measuerd value of the arduino (0-1023)
        Dim input_data(9) As Byte   'Array for incoming bytes
        Dim in_bytes As Integer       'number of recieved bytes
        Dim voltage As Double


        Try
            If SerialPort1.IsOpen Then

                Control.CheckForIllegalCrossThreadCalls = False

                in_bytes = SerialPort1.BytesToRead              'read Bytes from Buffer

                Do While in_bytes Mod 3 <> 0 Or in_bytes = 0  'waits for mulitples of 3 bytes
                    in_bytes = SerialPort1.BytesToRead
                Loop

                'reads bytes
                For cnt = 0 To in_bytes - 1
                    input_data(cnt) = SerialPort1.ReadByte
                Next

                For i As Integer = 0 To (in_bytes / 3) - 1 
                    HighByte = input_data(i * 3)                     'save bytes
                    LowByte = input_data(i * 3 + 1)
                    crc = input_data(i * 3 + 2)
                    crc_ok = 170 Xor HighByte Xor LowByte    'calculates checksum

                    'inserting a timestamp here

                    TextBox1.AppendText("Bytes: " & in_bytes & vbTab)
                    
                    If crc = crc_ok Then
                        value = HighByte * 256 + LowByte 'calculate voltage
                        voltage = value * (5.0 / 1024.0)
                        TextBox1.AppendText("V: " & Format(voltage, "0.000") & Environment.NewLine)
                    Else TextBox1.AppendText(Environment.NewLine)
                        VoltageList.Add(0.0)
                    End If
                Next

            End If
        Catch ex As Exception

        End Try
    End Sub

Toggling load:

Private Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles BtnLoad.Click
        Dim b1() As Byte = {1}
        Dim b0() As Byte = {0}
        Try
            If SerialPort1.IsOpen Then
                If BtnLoad.Text = "Load Off" Then
                    SerialPort1.Write(b1, 0, 1)
                    BtnLoad.Text = "Load On"
                Else
                    SerialPort1.Write(b0, 0, 1)
                    BtnLoad.Text = "Load Off"
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub

Here is a sketch of my board with the HC-05 module:

I am speaking as a complete novice here but I also had issues with the HC-05 dropping out, serial communication in my case. I found that the COM ports disconnected on sleep or screen saver, I also found my bluetooth mouse interfered with teh HC-05 but thats probably not related

Dan

Thank you for your answer!

I had the same problem with the computer going to sleep disturbing the connection. I don't think they disconnected though.

Actually I (sort of) found a solution to my problem. It seems that the array I was reading the serial data into was just too small and my VB-application too slow to keep up with the datastream. So if there were more than 9 bytes in the bluetooth buffer, which equals 3 measurements, the code to calculate and print all the measurements would not run. This always happened after a certain amount of time and sometimes randomly. By now I increased the array size and made some improvements to the runtime. Since then it works perfectly fine for me.