I am building an android application that opens a thread that receives data on a regular rate (every second) and can send commands back to the module.
I have programmed my application so far that I am able to send strings to the module and they arrive correctly, as ASCII strings.
However, sending data to my application through the HC-06 module and receiving it correctly in the application seems to be a problem. I write the data that I want to send to the module, and once it arrives in my application, the data seems to be corrupt.
I am wondering if the HC-06 maybe encodes the data, which I then have to decode in my application.
Any help would be appreciated,
Thanks
My Arduino code:
#include <ArduinoJson.h>
#include <Adafruit_BMP085.h>
#include <SoftwareSerial.h>
#include "TotalRGB.h"
#include <Servo.h>
Adafruit_BMP085 bmp;
CTotalRGB rgbControl(13, 12, 11);
//Servo servo;
SoftwareSerial mySerial(0, 1); // RX, TX
void setup() {
rgbControl.SetColor(PURPLE);
bmp.begin();
pinMode(10, OUTPUT);
// servo.attach(9);
}
void loop() {
CheckSensors();
if (mySerial.available()) {
rgbControl.SetColor(BLUE);
mySerial.read();
//TODO: If a certain character comes in, start the launch sequence
}
SerialFlush();
SendData("EngineTemperature", bmp.readTemperature(), 0);
mySerial.print("\r\n");
delay(1000);
}
void SerialFlush(){
while(mySerial.available() > 0) {
char t = mySerial.read();
}
}
void SendData(const String type, const float value, const long collectionTime){
StaticJsonBuffer<200> jsonBuffer;
JsonObject& data = jsonBuffer.createObject();
data["type"] = type;
data["value"] = value;
data["collectionTime"] = collectionTime;
data.printTo(mySerial);
}
void CheckSensors(){
while(!bmp.begin()){
rgbControl.Blink(BLACK, RED, 100);
digitalWrite(10, HIGH); // send high signal to buzzer
}
digitalWrite(10, LOW);
rgbControl.SetColor(GREEN);
}
Android thread snippet:
if(_dataInputStream != null){
int bytesAvailable = _dataInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
_dataInputStream.read(packetBytes);
for(int i = 0; i < packetBytes.length; i++)
{
byte byteData = packetBytes[i];
if(readBufferPosition + 1 > readBuffer.length){
break;
}
readBuffer[readBufferPosition++] = byteData;
}
_rocketDataCallback.onRocketDataReceived(new String(readBuffer, "ASCII"));
}
}