sending data from arduino to tablet using Bluetooth?

hello all, I am currently in the process of creating a project that will send data from an MPU6050 to my tablet using Bluetooth and an arduino uno.

I have working code that reads the RAW values from the mpu6050 and these values are displayed in the serial monitor, now I want to send the exact same data displayed in the serial monitor to my android tablet, but unfortunately I am having no luck with this.

Is there anyone who could possibly help me in achieving this?

I will attach my code here so you can read what I have so far.

Thanks for reading this and I hope that someone can help me on this problem.

Thanks.

/////////////CODE FOR READING MPU6050 DATA THAT I HAVE////////////////////////

#include “I2Cdev.h”

#include “MPU6050_6Axis_MotionApps20.h”

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include “Wire.h”
#endif

MPU6050 mpu;

#define OUTPUT_READABLE_YAWPITCHROLL

#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
bool blinkState = false;

// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorInt16 aa; // [x, y, z] accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
VectorFloat gravity; // [x, y, z] gravity vector
float euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector

// packet structure for InvenSense teapot demo
uint8_t teapotPacket[14] = { ‘$’, 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, ‘\r’, ‘\n’ };

//
int button = 3; // switch connected to pin 3
int buttonread = 0;
// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================

volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}

// ================================================================
// === INITIAL SETUP ===
// ================================================================

void setup() {
// join I2C bus (I2Cdev library doesn’t do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

Serial.begin(115200);
while (!Serial); // wait for Leonardo enumeration, others continue immediately

// initialize device
mpu.initialize();
// verify connection
Serial.println(mpu.testConnection() ? F(“MPU6050 connection successful”) : F(“MPU6050 connection failed”));

// load and configure the DMP
Serial.println(F(“Initializing DMP…”));
devStatus = mpu.dmpInitialize();

// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(0);
mpu.setYGyroOffset(0);
mpu.setZGyroOffset(0);
mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

// make sure it worked (returns 0 if so)
if (devStatus == 0) {
// turn on the DMP, now that it’s ready
Serial.println(F(“Enabling DMP…”));
mpu.setDMPEnabled(true);

// enable Arduino interrupt detection
Serial.println(F(“Enabling interrupt detection (Arduino external interrupt 0)…”));
attachInterrupt(0, dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();

// set our DMP Ready flag so the main loop() function knows it’s okay to use it
Serial.println(F(“DMP ready! Waiting for first interrupt…”));
dmpReady = true;

// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it’s going to break, usually the code will be 1)
Serial.print(F(“DMP Initialization failed (code “));
Serial.print(devStatus);
Serial.println(F(”)”));
}

pinMode(lefttilt, OUTPUT); // sets the digital pin as output
pinMode(righttilt, OUTPUT); // sets the digital pin as output
pinMode(backtilt, OUTPUT); // sets the digital pin as output
pinMode(button, INPUT); // sets the digital pin as input

}

// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================

void loop() {
// if programming failed, don’t try to do anything
if (!dmpReady) return;

// wait for MPU interrupt or extra packet(s) available
while (!mpuInterrupt && fifoCount < packetSize) {

}

// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();

// get current FIFO count
fifoCount = mpu.getFIFOCount();

// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();

// otherwise, check for DMP data ready interrupt (this should happen frequently)
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);

// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
Serial.print("\t");
Serial.print(ypr[0] * 180/M_PI);
Serial.print("\t");
Serial.print(ypr[1] * 180/M_PI);
Serial.print("\t");
Serial.println(ypr[2] * 180/M_PI);
#endif

else buttonread = digitalRead(button);

}

BTW I have added some code that will display when a certain value has been exceeded, according to the MPU6050 RAW vales.

I have now had a better look and would you think that if I added this code will it work?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 7); // RX, TX

////////////////////////////////////////////////////////////////

(in the void setup)

mySerial.begin(9600);

/////////////////////////////////////////////////////////////////

(in the void loop)

mySerial.print("\t");
mySerial.print(ypr[0] * 180/M_PI);
mySerial.print("\t");
mySerial.print(ypr[1] * 180/M_PI);
mySerial.print("\t");
mySerial.println(ypr[2] * 180/M_PI);

farmernathan:
I have now had a better look and would you think that if I added this code will it work?

#include <SoftwareSerial.h>

Probably not. I think the only significant line written is

I have working code that reads the RAW values from the mpu6050 and these values are displayed in the serial monitor

You can currently transmit the data with hardware serial, so this implies that the code is kosher and the problem is further down the line. Changing to software serial is thus not going to fix anything, and is something you will likely regret.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

EDIT
You also need to be specific about the type of bluetooth and type of tablet

hello, Thanks for the reply, I am using a Samsung tab 4 and a hc-05 Bluetooth module.

I am very new to this and I don't understand what you meant in the previous post.

I have read the information you gave me and that does not seem to work for me.

thanks

Your original code shows that you are using hardware serial (pins D0,D1), if you aren't you should be.

You also state that you are getting the proper result on the serial monitor.

This means there is nothing wrong with the code and the same code may be used, without any variation, to send that data over bluetooth. This is because both serial monitor and bluetooth are serial devices sharing the same port, and they both look the same to Arduino. Therefore, if you have a problem, it is down to improper use of the bluetooth, or bad wiring, not the code. In the light of that, changing the code to one using software serial is a pointless exercise, and likely to be something you later regret.

Your HC-05 and Samsung tablet should work together just fine. The material I linked to is certainly about as basic, and hopefully as bulletproof, as you can possibly get. I suggest you forget about all that hardware stuff for the moment, and go through the notes carefully to prove the bluetooth exclusively. You already know the other stuff works.

If this was my project I would use SoftwareSerial so that I could send the data using Bluetooth and ALSO send it to the Serial Monitor for debugging purposes.

Have you written a short program that just sends "Hello World" using Bluetooth?

...R

I note that you are communicating at 115200. It occurs to me that this may be the problem. I'm not aware of any HC-05 that runs at 115200 by default, hence this presupposes that you have configured it yourself, and I suspect you haven't, in which event bluetooth is probably running at 9600. In short, bad comms between arduino and bluetooth.

If you change

Serial.begin(115200);

to

Serial.begin(9600);

and also change the serial monitor while you are at it, you might then find that bluetooth works OK. If you really need to run at 115200, you can configure bluetooth later. The current objective is to prove the connections and procedures are kosher.

thank you all for the reply, I have now done it and it works like a dream, thanks for all the advice.

if you would like the full code for sending the mpu6050 raw values through bluetooth and to the serial monitor I will post it here.

Thanks

farmernathan: I have now done it and it works like a dream,

So what did you do?

Hi Nick.

I simply just used the serial library and set the communication up (e.g baud rate)as normal for any Bluetooth application and I then sent the data through bluetooth.print(RAW VALUES CALCULATIONS).

I also defined what pins I was using for the TX and RX.

farmernathan: (e.g baud rate)as normal for any Bluetooth application

What rate?