Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,27 @@ void loop()
readTemperature(temp_data);
serialConnection.println(temperatureToCSV("temperature", temp_data));
}
```

To use the the wireless bluetooth module, follow
[this bluetooth setup guide](https://github.com/ArduSat/ArdusatSDK/wiki/Bluetooth-Setup-Guide). Then you
can do the following.
```
ArdusatSerial serialConnection(SERIAL_MODE_HARDWARE_AND_SOFTWARE, 2, 3);

void setup()
{
serialConnection.beginBluetooth(9600);
serialConnection.println("This message will go out on hardware serial and software serial!");
beginTemperatureSensor();
}

void loop()
{
temperature_t temp_data;
readTemperature(temp_data);
serialConnection.println(temperatureToCSV("temperature", temp_data));
}
```

#### Serial Modes
Expand Down
80 changes: 80 additions & 0 deletions examples/bluetooth/bluetooth.ino
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);
}
54 changes: 54 additions & 0 deletions utility/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

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.

Copy link
Contributor Author

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.

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

Copy link
Contributor Author

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's setup() function.

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.


_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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need breaks in here, otherwise you're writing each of these to the unit and falling to the default making valid always be false

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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");

Choose a reason for hiding this comment

The 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 9600, since valid is always false.

_soft_serial->end();
_soft_serial->begin(9600);
_soft_serial->println("Error: specified baud rate not available.");
}
}
}

void ArdusatSerial::end()
{
send_to_serial(end())
Expand Down
1 change: 1 addition & 0 deletions utility/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ArdusatSerial : public Stream
~ArdusatSerial();

void begin(unsigned long speed, bool setXbeeSpeed=false);
void beginBluetooth(unsigned long speed);
void end();

virtual int peek();
Expand Down