diff --git a/README.md b/README.md index 58a074d..9072cb5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/bluetooth/bluetooth.ino b/examples/bluetooth/bluetooth.ino new file mode 100644 index 0000000..411e367 --- /dev/null +++ b/examples/bluetooth/bluetooth.ino @@ -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 (sam@ardusat.com) + * Organization: Ardusat + * + * ===================================================================================== + */ + +/*----------------------------------------------------------------------------- + * Includes + *-----------------------------------------------------------------------------*/ +#include +#include +#include + +/*----------------------------------------------------------------------------- + * 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); +} diff --git a/utility/serial.cpp b/utility/serial.cpp index 537db3c..0a129db 100644 --- a/utility/serial.cpp +++ b/utility/serial.cpp @@ -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; \ @@ -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()) diff --git a/utility/serial.h b/utility/serial.h index c53ad49..65230f0 100644 --- a/utility/serial.h +++ b/utility/serial.h @@ -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();