Checking if the bluetooth module on my Arduino board works - bluetooth

I'm trying to have my Arduino UNO board to work with a BlueSmirf Gold (http://www.sparkfun.com/products/10268).
I wired it as explained on various tutorial (for example here: http://www.instructables.com/id/how-to-Control-arduino-by-bluetooth-from-PC-pock/)
I've set the baud rate to 9600 as explained here: http://forum.sparkfun.com/viewtopic.php?p=94557
I manage to connect to it using the default Arduino serial terminal, ZTerm and my phone (using Amarino). In every cases, the green light on the bluetooth modem turns on, so until there it looks good.
The main problem is that my modem does not seem to be able to send/receive anything (the only time I've had any response was when I've set the baud rate to 9600).
For example, I have this code (simplified here, but the main idea is there):
int out_pin = 2;
String readLine() {
char command[100];
int i = 0;
if(Serial.available()){
delay(100);
while( Serial.available() > 0 && i< 99) {
command[i++] = Serial.read();
}
command[i++]='\0';
Serial.flush();
}
Serial.print("command: ");
Serial.println(command);
return (String) command;
}
void menu() {
if (Serial.available() <= 0) {
return;
}
String command = readLine();
// Do thing based on the command
}
void setup() {
pinMode(out_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
menu();
}
Logically, when I send something via the terminal, I should get it back (which is what happens when using the usb serial).
When I connect to the board via Bluetooth, it just stays silent.
I also tried this piece of code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Spam ...");
}
Works fine when using the usb serial, but I get nothing when using bluetooth to connect to my board.
With both codes, I also tried to use the monitor tool in Amarino to send messages to the board, but it seems that it never got it.
I've tried various other things:
- do not have the USB serial available (I powered the Arduino board via USB but using a plug wall adapter. I'll try later on with a 9V battery but I don't have it available at the moment)
- do not connect CTS-1 to RTS-0 in the modem (some tutorial tell to connect them, the other don't. So as I doubted I tried both solutions).
The only time I've had something that looked like a communication was with this setup:
Arduino - Phone connected via Bluetooth
The Amarino monitoring was on
Arduino - Computer connected via the USB serial
When uploading the new code to my board, some parts of it were displayed on the monitoring tool on the phone.
It happened one or twice and I can't reproduce it now.
I'm pretty sure I've done something wrong somewhere (it seems at least it's the most logical explanation) but I'm also wondering if it could not be a problem with the Bluetooth model (I mean, even the sample tutorials do not work).
So the questions are:
is there something I missed/forgot to do that could help me solve the
problem ?
if not: is there a simple way to check that my Bluetooth modem
works fine ?
Thanks,
Vincent

I still don't have the answer on the second question ("is there a simple way to check that my Bluetooth modem works fine ?") but I've finally be able to send/receive messages from the Bluetooth modem.
As I was guessing (at least how I understand it) that was a problem with the two serials (Bluetooth and USB) on the same board.
To solve that, I've plugged the BT TX-1 on pin digital 5, RX-0 on digital 3 and used the following code (based on the SoftwareSerial tutorial):
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(5, 3);
void setup()
{
// Start the hardware serial port
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop()
{
bluetooth.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (bluetooth.available() > 0) {
char inByte = bluetooth.read();
Serial.write(inByte);
}
}
It sends all entries received from Bluetooth on the default serial (in my case USB).
I've checked with Amarino and messages sent from my phone are displayed in the Arduino serial monitor.

Same problem here. I tried connect vice versa the 0 and 1, RX and TX(i.e. RX to RX and TX to TX) and I got some communication of gibberish as opposed to nothing at all.

Related

Spoofing an RN42 into a Wiimote

RN42 Wireshark Snapshot
Wiimote Wireshark Snapshot
All,
I am attempting to program an RN42 to spoof a nintendo Wiimote. I would like for the RN42 to connect to the Wii just as a Wiimote would. I can not seem to detect the Wii, or connect to it with the code I have so far. I used a Raspi to connect the Wiimote and RN42 in order to capture bluetooth packets. The images are attached above from wireshark. I noticed the RN42 is going into SDP protocol for some reason and not using the HID profile I set it to. I was wondering if anyone could help me fix this RN42 so I can connect it to a Wii console.
Notes:
I have utilized the command reference for the RN42 as well as Wiibrew to try and mimic a wiimote, with little success.
#include <SoftwareSerial.h>
int bluetoothTx = 3; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 2; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(200); // Short delay
bluetooth.println("SA,0"); // Set authentication to none
delay(200); // Short delay
bluetooth.println("SM,0"); // Set mode to slave
delay(200); // Short delay
bluetooth.println("SH,0100"); // Set HID flag to Joystick
delay(200); // Short delay
bluetooth.println("S~,6"); // Set HID profile
delay(200); // Short delay
bluetooth.println("SC,0000"); // Set HID profile
delay(200); // Short delay
bluetooth.println("SD,2504"); // Set HID profile
delay(200); // Short delay
bluetooth.println("R,1"); // Reboot
delay(400); // Short delay
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}
You've most likely solved or moved on already, but I'm too now trying to figure out RN-42 for xbox-360 gamepad and kinda confused with how HID flag register works.
SH command expects four hex characters which equate to 16 bits. Flag register has 10 bits. I have no idea how am I supposed to map 16 bits to 10 but your
bluetooth.println("SH,0100"); // Set HID flag to Joystick
command probably won't set it to a joystick, as all examples show it needs to be
bluetooth.println("SH,0240"); // Set HID flag to Joystick
Again, no idea why tho.

Is there any way to send this Keyboard.Press(); command wireless to my PC via Bluetooth?

I have already been able to implement the wireless data sending of my Arduino Micro board via Bluetooth. I used following code to send my sensor data from the arduino board to PC. But I want it to be able to do a Keyboard.Press(); according to the data I receive from my sensors attached to the Micro board.
Is there any way to send this Keyboard.Press(); command wireless to my PC via Bluetooth ?
I use Arduino Micro with HC-05 to send data to the PC wireless.
#include <SoftwareSerial.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU9250.h"
MPU9250 accelgyro;
I2Cdev I2C_M;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t mx, my, mz;
float Axyz[3];
const int rxPort = 8; // connected to Bluetooth TX
const int txPort = 9; // connected to Bluetooth RX
SoftwareSerial myConnection = SoftwareSerial(rxPort, txPort);
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU9250 connection successful" : "MPU9250 connection failed");
delay(1000);
myConnection.begin(9600);
}
void loop()
{
myConnection.print(mapX);
myConnection.print(" , ");
myConnection.println(mapY);
////Keyboard.press('a');
}
For bluetooth v2 you need a HID-enabled bluetooth module. Once I knew this product
https://www.sparkfun.com/products/retired/10938
but now they retired it.
If you want to move to bluetooth v4 I don't know. Searching on the web it appears that you will need to implement "HID over GATT profile" (HOGP).
I found this library for some ble chips (nRF8001, nRF2740/nRF2741) which has examples of a keyboard over bluetooth; otherwise if you already have a BT4 board try looking for libraries which implement that profile, or try porting it to your platform.
Of course this only applies to bluetooth 4 boards, since bluetooth 2 has some fixed "classes" (so you can't turn a BT-serial adapter into a BT-HID device).

Pairing bluetooth device with user defined PIN-QT

I am trying to make a bluetooth pairing application with Qt5. i want to pair with a HID device which is having default pin 1234(say), i want to enter this pin in my application.
QBluetoothLocalDevice localDevice;
localDevice.requestPairing(address, QBluetoothLocalDevice::Paired);
connect(&localDevice, SIGNAL(pairingDisplayPinCode(QBluetoothAddress,QString)),
this, SLOT(displayPinCode(QBluetoothAddress,QString)));
connect(&localDevice, SIGNAL(pairingDisplayConfirmation(QBluetoothAddress,QString)),
this, SLOT(displayParingConfirmation(QBluetoothAddress,QString)));
void BluetoothDevice::displayPinCode(const QBluetoothAddress &address, QString pin)
{
localDevice.pairingConfirmation(true);
}
void BluetoothDevice::displayParingConfirmation(const QBluetoothAddress &address, QString pin)
{
localDevice.pairingConfirmation(true);
}
this code emit some random pin.
Is there any provision to input a pin and pair device using that pin.
thanks in advance.
(i am using Ubuntu 14.10)
Please refer the files listed below from here:
bluetoothagentadaptor.cpp
bluetoothagentadaptor.h
bluetoothbaseagent.cpp
bluetoothbaseagent.h

Unable to detect Seeedstudio Bluetooth Shield

I have a problem regarding Seeedstudio Bluetooth shield http://www.seeedstudio.com/depot/Bluetooth-Shield-p-866.html
I can't detect its presence by any other devices.
The code I uploaded to Arduino is a standard example for slave device from the library:
/* Upload this sketch into Seeeduino and press reset*/
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7
#define DEBUG_ENABLED 1
SoftwareSerial blueToothSerial(RxD,TxD);
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
}
void loop()
{
char recvChar;
while(1)
{
if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
recvChar = blueToothSerial.read();
Serial.print(recvChar);
}
if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
recvChar = Serial.read();
blueToothSerial.print(recvChar);
}
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}
I've uploaded it to Arduino UNO, connected the shield and... Nothing.
LED marked as D1 is blinking green, and D2 is switched off. The device is not detected by none of the three devices I've tried (two computers and a smartphone).
By "not detected" I mean "hcitool returns nothing and OS based search for Bluetooth devices reports nothing". All three devices can detect each other without any problems.
I tried to connect it to other UNO board (in case the first one was damaged), but the result is the same.
I thought that the shield is somehow at fault, so I had it replaced by a new one - but the results are still the same.
Summing up:
3 extrnal devices
2 Arduinos
2 shields
Tested in all possible combinations, and still no success.
The device is powered up, because when I set it to send it's status to A1 analog port I always read 0 instead of a random value.
The only logical conclusion is that there is something wrong with the code above, but every google search I've made pointed me exactly to that file. It's from official wiki and in every example I've found. I've tried to contact Seeedstudio about it, but they didn't have anything of value to add ("try rebooting until it works").
Has anyone had similar problem, or has any advice what's wrong with the code?
I've managed to solve the problem on my own.
Two steps were required:
First of all I had to use hardware serial port (ports 0 and 1 on Arduino UNO), because for some reason SoftwareSerial doesn't work well with this shield.
Then I had to battery as power supply, since serial communication via ports 0 and 1 has lower priority than USB serial.
The disadvantage of this solution is that you lose USB communication with your PC, but fortunately I didn't need it.
EDIT: To avoid any confusion, here is an example of the code which worked for me:
void setup()
{
setupBlueToothConnection();
Serial.flush();
}
void loop()
{
for (char i = 0; i <= 254; i++)
{
Serial.print(i);
delay(1000);
}
}
void setupBlueToothConnection()
{
Serial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
Serial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
Serial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
Serial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
Serial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
Serial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
//Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
Serial.flush();
}
Please note that the modem sends answers to some of the commands, you should clean Serial buffer before reading from it.
Also, the jumpers on the shield should be connected like this: BT_TX to pin 0 and BT_RX to pin 1.

Sending commands from custom made Bluetooth device to android phone to control music player

I have created a simple Bluetooth device using following components
HC05 module
Arduino Uno board (with re-programmable micro-controller)
I am wondering if it is possible to send commands from my BT device, as if these commands were sent from Bluetooth headset?
What I mean is:
we send 0x00000055 keycode - and the music pauses
(KEYCODE_MEDIA_PLAY_PAUSE)
we send 0x00000058 - previous song starts playing
(KEYCODE_MEDIA_PREVIOUS)
...
Here is the full list of keycodes which android uses: http://developer.android.com/reference/android/view/KeyEvent.html
I can probably create a separate app, which will read incoming commands and simulate headset button presses, but that is not what I want. As far as I'm concerned - some of the headsets are plug-and-play, meaning that no additional apps must be installed on android device. Here is the code I am currently use to send commands to Android phone:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
enum { LED_PIN = 6 };
enum LedState { LED_ON, LED_OFF, LED_BLINK };
LedState led_state;
void setup()
{
led_state = LED_OFF;
pinMode(LED_PIN, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
const int COMMAND_MUSIC = 85;
void loop()
{
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
delay(10000);
// trying to play or pause once in 10 seconds
BTSerial.write(0x00000055);
//BTSerial.print(0x00000055, HEX);
}
Both devices are paired but music player on my phone stays unaffected by these commands..Is it possible to control music player without creating a side app for "incoming commands from BT"?
Question is if your board supports AVRCP controller BT profile?
If it does you "only" need to connect against your phones AVRCP target BT profile. When you have a AVRCP BT connection there is specified commands how to pause and skip songs.
This is how the "plug and play" headset does.
Read more about Bluetooth profiles.
http://en.wikipedia.org/wiki/Bluetooth_profile
Looking at your code you have set up a serial link towards a phone.
This link uses SPP profile and you will only be able to send raw data over that link.
If this is the only profile that your BT stack on your Arduino Uno board have you will be forced to create an application on the phone side to be able to read the raw data and do something with it e.g. pause music.
Hope this cleared things little for you.
Probably it is to late for you, but maybe I can help someone else.
Firstly, Bluetooth devices like BT headphones, keyboards etc, are known as HID (Human Interface devices). HC05 are not one of this out of the box, but there is a solution introduced by Evan Kale (link: https://www.youtube.com/watch?v=BBqsVKMYz1I) how to update one of this using serial port connection.
Other solution is to buy BT HID module, but they are more expensive (about 10 times)

Resources