"Data loss" with bluetooth.

Can anyone help with my problem. I'm sending some data with arduino to android via bluetooth and sometimes it just loses some parts of text and goes haywire for some time until it stabilizes.

Here is my loop;

void loop() {

float h = dht.readHumidity();
float t = dht.readTemperature();
unsigned long uptime = millis();

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;

}

if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(3, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(3, LOW);
}
inputString = "";
}
Serial.flush();

// Data update trough bluetooth timer
if (uptime - lastupdate >= delayonupdate) {
lastupdate = uptime;

Serial.print("Lämpötila: ");
Serial.print(t);
Serial.println(" *C ");
Serial.print(" \n");
Serial.print("Ilmankosteus: ");
Serial.print(h);
Serial.print(" %\n");
Serial.print("Lisäakku: 12.4V");
Serial.print(" \n");
Serial.print("Starttiakku: 13.1V");
Serial.print(" \n");

}

}

nollaw:
I'm sending some data with arduino to android via bluetooth

You appear to have two problems

  1. the code is incoherent junk
  2. it is not intended for your stated purpose

Publishing the whole code, with fewer blank lines, would be a a better start.

Nick_Pyner:
You appear to have two problems

  1. the code is incoherent junk
  2. it is not intended for your stated purpose

Publishing the whole code, with fewer blank lines, would be a a better start.

Sorry about that, it has been soo long ago when i last time did coding and it has always been horrible, and now mostly ctrl+c / ctrl+v. But here is the whole thing, with deleted extra blankies.

#include "DHT.h"

#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define led 3

String inputString="";
char junk;
int delayonupdate = 1500;
long lastupdate = 0;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
dht.begin();
}

void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
unsigned long uptime = millis();

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(3, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(3, LOW);
}
inputString = "";
}
Serial.flush();

// Data update trough bluetooth timer
if (uptime - lastupdate >= delayonupdate) {
lastupdate = uptime;

Serial.print("Lämpötila: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(" \n");
Serial.print("Ilmankosteus: ");
Serial.print(h);
Serial.print(" %\n");
Serial.print("Lisäakku: 12.4V");
Serial.print(" \n");
Serial.print("Starttiakku: 13.1V");
Serial.print(" \n");

}

}

Why does the code read from serial input?

jremington:
Why does the code read from serial input?

It reads received data from bluetooth and then acts if letter is correct.

I'm now answering to myself. I think i solved this. I just send one line of data to android like this;

Serial.print("s|"); // Start message
Serial.print(t); // Inside temperature
Serial.print("|");
Serial.print(h); // Inside humidity
Serial.print("|");
Serial.print("16.3"); // Outside temperature
Serial.print("|");
Serial.print("12.3"); // Gadget battery
Serial.print("|");
Serial.print("14.1"); // Starter battery
Serial.print("|");
Serial.println("e"); // End message

Then i parse that data on android software and print them out.

I have been testing it for some time now and no data loss at all, i think \n was the problem.