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 all 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);
}
83 changes: 83 additions & 0 deletions utility/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ const char xbee_cmd_write[] PROGMEM = "ATWR";
const char xbee_cmd_close[] PROGMEM = "ATCN";
const char xbee_baud_success[] PROGMEM = "Set XBEE baud rate to ";

const char bt_cmd_mode[] PROGMEM = "$$$";
const char bt_1200_baud_cmd[] PROGMEM = "U,1200,N";
const char bt_2400_baud_cmd[] PROGMEM = "U,2400,N";
const char bt_4800_baud_cmd[] PROGMEM = "U,4800,N";
const char bt_9600_baud_cmd[] PROGMEM = "U,9600,N";
const char bt_19200_baud_cmd[] PROGMEM = "U,192K,N";
const char bt_38400_baud_cmd[] PROGMEM = "U,384K,N";
const char bt_57600_baud_cmd[] PROGMEM = "U,576K,N";
const char bt_bad_baud_err1[] PROGMEM = " isn't a supported bluetooth baud rate.";
const char bt_bad_baud_err2[] PROGMEM = "Supported baud rates are:";
const char bt_bad_baud_err3[] PROGMEM = "1200 2400 4800 9600 19200 38400 57600";

#define send_to_serial(function) \
if (_mode == SERIAL_MODE_HARDWARE || _mode == SERIAL_MODE_HARDWARE_AND_SOFTWARE) { \
Serial.function; \
Expand Down Expand Up @@ -92,6 +104,77 @@ 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
*
* NOTE: If this function is called multiple times with different values for 'baud'
* without losing power in between calls, the baud rate will not be updated
* after the first call.
*/
void ArdusatSerial::beginBluetooth(unsigned long baud)
{
bool valid_baud = true;
char baud_cmd[9];
char cmd_mode[4];
char bad_baud_err[40];
strcpy_P(cmd_mode, bt_cmd_mode);

// Temporarily sets baud rate until power loss
switch (baud) {
case 1200: strcpy_P(baud_cmd, bt_1200_baud_cmd); break;
case 2400: strcpy_P(baud_cmd, bt_2400_baud_cmd); break;
case 4800: strcpy_P(baud_cmd, bt_4800_baud_cmd); break;
case 9600: strcpy_P(baud_cmd, bt_9600_baud_cmd); break;
case 19200: strcpy_P(baud_cmd, bt_19200_baud_cmd); break;
case 38400: strcpy_P(baud_cmd, bt_38400_baud_cmd); break;
case 57600: strcpy_P(baud_cmd, bt_57600_baud_cmd); break;
default: valid_baud = false;
}

if (valid_baud) {
if (_mode == SERIAL_MODE_HARDWARE) {
if (baud != 9600) {
Serial.begin(9600); // shipping bluesmirf with default of 9600
Serial.print(cmd_mode); // enter command mode
delay(100);
Serial.println(baud_cmd);
}
Serial.begin(baud);
}

if (_mode == SERIAL_MODE_SOFTWARE || _mode == SERIAL_MODE_HARDWARE_AND_SOFTWARE) {
if (baud != 9600) {
_soft_serial->end();
_soft_serial->begin(9600); // shipping bluesmirf with default of 9600
_soft_serial->print(cmd_mode); // enter command mode
delay(100);
_soft_serial->println(baud_cmd);
}
_soft_serial->end();
_soft_serial->begin(baud);
}

if (_mode == SERIAL_MODE_HARDWARE_AND_SOFTWARE) {
Serial.begin(baud);
}
} else {
Serial.begin(9600);
Serial.print(baud);
strcpy_P(bad_baud_err, bt_bad_baud_err1);
Serial.println(bad_baud_err);
strcpy_P(bad_baud_err, bt_bad_baud_err2);
Serial.println(bad_baud_err);
strcpy_P(bad_baud_err, bt_bad_baud_err3);
Serial.println(bad_baud_err);
}
}

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