Unreliable Serial Communication between Arduino serial and Android via HC-05 Bluetooth SPP

androidarduinobluetoothserial

I have an Andriod APP which connects to Arduino UNO over bluetooth( HC-05), and receives sensor readings using Serial.print() command in Arduino sketch. The App is made using APP inventor and uses Bluetooth blocks in that program.

Here's a snapAPPINVENTOR Block

It works fine until I use a Dust sensor, which needs Android to sample on pin D8.

int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 1000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
unsigned long timeElapsed=2000;
unsigned long sTime;

void setup() {
Serial.begin(9600);
pinMode(8,INPUT);
starttime = millis();
}

void loop() {

duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;

if ((millis()-starttime) > sampletime_ms)//if the sample time == 1s
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet    curve
 Serial.print(analogRead(analogInPin)/20);
 Serial.print("s");
 Serial.print(analogRead(A5));
 Serial.print("s");
 Serial.print(analogRead(A3));
 Serial.print("s");
 Serial.print(concentration);
 Serial.print("s");
lowpulseoccupancy=0;
starttime=millies();
 } 

 }

Adding the variable "concentration" above to the Serial.print() leads to the error in which the APP receives no information block on one clock timer, then receives two consecutive in the next clock timer. The APP is designed to check bluetooth every 1000 ms, and I am sending the sensor data after 1000ms. When it receives no data, it shows error="Cannot acces item/list is empty. What to do? It works fine if I leave "concentration". Serial rate is 9600bps.

Best Answer

In the arduino a long is 32 bits, I think a float for concentration is not enought, so, the aruino go in overflow maybe resetting it self before writing enything to the serial.