-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Bluetooth support to SDK #10
base: master
Are you sure you want to change the base?
Changes from 2 commits
fad0135
bb8532e
afbf712
98b05b3
924f06c
7cb8920
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: bluetooth.ino | ||
* | ||
* Description: Outputs the acceleration sensor readings in a JSON format over | ||
* the Sparkfun BlueSMiRF module. | ||
* | ||
* This example uses many third-party libraries available from | ||
* Adafruit (https://github.com/adafruit). These libraries are | ||
* mostly under an Apache License, Version 2.0. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Version: 1.0 | ||
* Created: 10/29/2014 | ||
* Revision: none | ||
* Compiler: Arduino | ||
* | ||
* Author: Sam Olds ([email protected]) | ||
* Organization: Ardusat | ||
* | ||
* ===================================================================================== | ||
*/ | ||
|
||
/*----------------------------------------------------------------------------- | ||
* Includes | ||
*-----------------------------------------------------------------------------*/ | ||
#include <Arduino.h> | ||
#include <Wire.h> | ||
#include <ArdusatSDK.h> | ||
|
||
/*----------------------------------------------------------------------------- | ||
* Setup Software Serial to allow for both RF communication and USB communication | ||
* RX is digital pin 10 (connect to TX/DOUT of RF Device) | ||
* TX is digital pin 11 (connect to RX/DIN of RF Device) | ||
*-----------------------------------------------------------------------------*/ | ||
ArdusatSerial serialConnection(SERIAL_MODE_HARDWARE_AND_SOFTWARE, 2, 3); | ||
|
||
/*----------------------------------------------------------------------------- | ||
* Constant Definitions | ||
*-----------------------------------------------------------------------------*/ | ||
acceleration_t accel; | ||
|
||
/* | ||
* === FUNCTION ====================================================================== | ||
* Name: setup | ||
* Description: This function runs when the Arduino first turns on/resets. This is | ||
* our chance to take care of all one-time configuration tasks to get | ||
* the program ready to begin logging data. | ||
* ===================================================================================== | ||
*/ | ||
void setup(void) | ||
{ | ||
serialConnection.beginBluetooth(9600); | ||
|
||
if (!beginAccelerationSensor()) { | ||
serialConnection.println("Can't initialize IMU! Check your wiring."); | ||
} | ||
|
||
/* We're ready to go! */ | ||
serialConnection.println(""); | ||
} | ||
|
||
/* | ||
* === FUNCTION ====================================================================== | ||
* Name: loop | ||
* Description: After setup runs, this loop function runs until the Arduino loses | ||
* power or resets. We go through and update each of the attached | ||
* sensors, write out the updated values in JSON format, then delay | ||
* before repeating the loop again. | ||
* ===================================================================================== | ||
*/ | ||
void loop(void) | ||
{ | ||
readAcceleration(accel); | ||
serialConnection.println(accelerationToJSON("acceleration", accel)); | ||
|
||
delay(1000); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,60 @@ void ArdusatSerial::begin(unsigned long baud, bool setXbeeSpeed) | |
} | ||
} | ||
|
||
/** | ||
* Begin serial communications with a Sparkfun BlueSMiRF module at the specified baud | ||
* rate. | ||
* | ||
* Note that baud rates above ~57600 are not well-supported by SoftwareSerial, and | ||
* even 57600 may cause some bugs. | ||
* | ||
* @param baud rate for serial communications | ||
*/ | ||
void ArdusatSerial::beginBluetooth(unsigned long baud) | ||
{ | ||
if (_mode == SERIAL_MODE_HARDWARE || _mode == SERIAL_MODE_HARDWARE_AND_SOFTWARE) { | ||
Serial.begin(baud); | ||
|
||
_soft_serial->end(); | ||
_soft_serial->begin(115200); // bluesmirf defaults to baudrate of 115200 | ||
|
||
_soft_serial->print("$"); // enter command mode | ||
_soft_serial->print("$"); | ||
_soft_serial->print("$"); | ||
delay(100); | ||
|
||
_soft_serial->println("S~,0"); // Set to serial port profile | ||
|
||
bool valid = true; | ||
|
||
// Supported bluesmirf baud rates | ||
switch(baud) { // Set Baud rate | ||
case 1200: _soft_serial->println("U,1200,N"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heh. nice catch. I should be able to write a proper switch block... |
||
case 2400: _soft_serial->println("U,2400,N"); | ||
case 4800: _soft_serial->println("U,4800,N"); | ||
case 9600: _soft_serial->println("U,9600,N"); | ||
case 19200: _soft_serial->println("U,192K,N"); | ||
case 38400: _soft_serial->println("U,384K,N"); | ||
case 57600: _soft_serial->println("U,576K,N"); | ||
case 115000: _soft_serial->println("U,115K,N"); | ||
case 230000: _soft_serial->println("U,230K,N"); | ||
case 460000: _soft_serial->println("U,460K,N"); | ||
case 921000: _soft_serial->println("U,921K,N"); | ||
default: valid = false; | ||
} | ||
|
||
if (valid) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to manually exit AT command mode? I assume there's a timeout, but it'd be good to make sure we're not in command mode after the change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "U,xxxx,N" AT command sets the baud rate and the immediately transitions out of command mode. |
||
_soft_serial->end(); | ||
_soft_serial->begin(baud); | ||
} else { | ||
_soft_serial->println("U,9600,N"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably a bug why this works for you using a value of |
||
_soft_serial->end(); | ||
_soft_serial->begin(9600); | ||
_soft_serial->println("Error: specified baud rate not available."); | ||
} | ||
} | ||
} | ||
|
||
void ArdusatSerial::end() | ||
{ | ||
send_to_serial(end()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this happen on each boot? Seems like this is the implication here when I would expect that once it's set, it is likely persisted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this allows for the user to change the baudrate of the bluetooth device just through the Arduino sketch. The "U,xxxx,N" AT command doesn't persist the baudrate. It defaults to a baudrate of 115200 on boot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just surprised the module doesn't persist that, anything else I've played with that I can recall saves that between boots. Weird
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a set baudrate command that persists. But this is the best way I found that still allows the user to switch the baudrate in a similar fashion to
Serial.begin(xxxx);
from the Arduino'ssetup()
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood. However, if I recall correctly if the user is on softwareserial, that often has issues at higher baud rates. Have you seen any issues with this when over software serial? I mostly just wonder if we should provide a sensible default (ship them with 9600 baud) that works with both and let them use the overloaded begin if they have changed it themselves.