Rafał Praczyk
Rafał Praczyk

Reputation: 441

Sending continuous data from arduino to Android App using HC-05

I've been struggling with the problem of sending continuous data from arduino to Android. What I want to do is get analog read convert it to 0-5V information, and send that information to Android app. My arduino code is just simply:

//(...)defining pins and levels
SoftwareSerial BTSerial(rxPin, txPin);
void setup()
{
  pinMode(getData, INPUT);
  digitalWrite(keyPin, LOW);
  BTSerial.begin(9600);
}
void loop()
{
  contact = digitalRead(getData);
  if (contact == HIGH) {
    sensorValue = analogRead(sensorPin);
    double voltage = sensorValue * (5.0 / 1023.0);
    if (BTSerial.available()) {
      Serial.write(BTSerial.read());
    }
    BTSerial.println(voltage, 3);
    BTSerial.write("\r");
    if (Serial.available()) {
      BTSerial.write(Serial.read());
    }
  }
  delay(5);
}

I need to send data informing about measurment with ~200Hz frequency. After sending the data to application it seems that part of data is lost.

I tried higher bound rates but the problem still occurs. Is there a way to send continuous data from arduino using serial port without loosing some % of that data?

Upvotes: 1

Views: 3584

Answers (1)

Majkl
Majkl

Reputation: 763

I think the problem is in the design of the receiver. I Solved BTL communication in .net Xamarin, but the principle should be the same. In Android reading from InputStream must be quick and can not use sleep. You need to use an endless cycle and there quick read data into temp buffer. Immediately a dune bytes to an auxiliary large buffer (use read / write cursor) and then, for example, in timer treat the data (I suppose you are using some packet protocol)

        public override void Run()
    {
        WriteLogInfoToLog("ConnectedThread.Run() - before");
        while (true)
        {
            try
            {
                int readBytes = 0;
                lock (InternaldataReadLock)
                {
                    readBytes = clientSocketInStream.Read(InternaldataRead, 0, InternaldataRead.Length);
                    Array.Copy(InternaldataRead, TempdataRead, readBytes);
                }
                if (readBytes > 0)
                {
                    lock (dataReadLock)
                    {
                        dataRead = new byte[readBytes];
                        for (int i = 0; i < readBytes; i++)
                        {
                            dataRead[i] = TempdataRead[i];
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                btlManager.btlState = BTLService.BTLState.Nothing;//Spadlo spojeni, musi spustit cele od zacatku
                WriteLogInfoToLog("ConnectedThread.Run() - EXCEPTION " + e.Message + ", " + e.HResult + ", " + e.StackTrace + ", " + e.InnerException);
                if (e is Java.IO.IOException)
                {
                }
                else
                {
                }
                break;
            }
        }
        WriteLogInfoToLog("ConnectedThread.Run() - after");
    }

Upvotes: 2

Related Questions