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 3 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);
}
47 changes: 47 additions & 0 deletions utility/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,53 @@ 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) {
bool valid = true;
if (baud != 9600) {
_soft_serial->end();
_soft_serial->begin(9600); // shipping bluesmirf with default of 9600
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't going to work to enter command mode if the bluetooth is already set to a baud rate other than 9600, correct? (another way of asking the question - the bluetooth module remembers baud rates between boots, correct?)

I think to make this work well, you're going to have to go through multiple baud rates checking for the acknowledgement that you successfully entered command mode (usually something like OK) before you assume that you can send an AT command successfully.

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 only temporarily changes the baud rate. Once it loses power it goes back to the default, which I set to be 9600.

Choose a reason for hiding this comment

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

I'd be in favor of us setting them before we ship the modems rather than doing temp ones and having to update it each time the device boots. 95%+ of our customers will leave it as the default.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, ok. If this is a temp-only change in the baud rate I'm a bit less worried about it. I think there is some utility in being able to speed up the baud - it definitely makes high throughput data things (air-drop, etc.) more feasible...

(even though I agree 95%+ will never 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.

Yeah, so the default baud rate I set to 9600. The only time it attempts to update the baud rate is if they call beginBluetooth() with anything other than 9600.

_soft_serial->print("$"); // enter command mode
Copy link
Contributor

Choose a reason for hiding this comment

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

you should be able to do _soft_serial->print("$$$"), correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was something I forgot to change. This was how it was done in an example pass through sketch, and I just never updated it.

_soft_serial->print("$");
_soft_serial->print("$");
delay(100);

// Supported bluesmirf baud rates
switch(baud) { // Set Baud rate
case 1200: _soft_serial->println("U,1200,N"); break;
case 2400: _soft_serial->println("U,2400,N"); break;
case 4800: _soft_serial->println("U,4800,N"); break;
case 9600: _soft_serial->println("U,9600,N"); break;
case 19200: _soft_serial->println("U,192K,N"); break;
case 38400: _soft_serial->println("U,384K,N"); break;
case 57600: _soft_serial->println("U,576K,N"); break;
case 115000: _soft_serial->println("U,115K,N"); break;
Copy link
Contributor

Choose a reason for hiding this comment

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

Uno software serial doesn't work above 57600. If you do successfully manage to switch the bluetooth chip into 115000 here, you're effectively bricking it for our purposes (would require the teacher to re-wire to hardware only and/or use a bluetooth config utility and reset manually).

Also, standard ~115k baudrade is 115200, not 115000.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought I had read that software serial for arduino unos work up to 115200. But I was having trouble getting it to work. So I'll probably just drop it from the switch block. And the 115000 was a typo... I'm embarrassed by all these typos.

default: _soft_serial->println("---"); 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.

Serial.begin(baud);
_soft_serial->end();
_soft_serial->begin(baud);
} else {
Serial.begin(9600);
Serial.print(baud);
Serial.println(" is not a supported bluetooth baud rate.");
}
}
}

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