Skip to content

Instantly share code, notes, and snippets.

@Neurogami
Last active November 20, 2016 04:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Neurogami/423f4d4d3c03e0414480 to your computer and use it in GitHub Desktop.
Save Neurogami/423f4d4d3c03e0414480 to your computer and use it in GitHub Desktop.
/*!
@file bleuart_datamode.ino
@author hathach, ktown (Adafruit Industries)
This demo will show you how to initialize the module in COMMAND mode then
send data in DATA mode, uses the MODE pin
Altered to work with a Teensy 3.1 using Serial2
The Bluefruit CTS pin is connected to Teensy pin 8.
The Bluefruit MOD pin is connected to Teensy pin 12.
Bluefruit is switched to DATA.
Bluefruit RTS pin is connected to GND.
Teensy rx2 goes to Bluefruit tx
Teensy tx2 goes to Bluefruit rx
*/
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BLE_HWSPI.h"
#include "Adafruit_BluefruitLE_UART.h"
// If you are using Hardware Serial
// The following macros declare the Serial port you are using. Uncomment this
// line if you are connecting the BLE to Leonardo/Micro or Flora
#define BLUEFRUIT_HWSERIAL_NAME Serial2
#define BLUEFRUIT_UART_MODE_PIN 12 // Optional but recommended, set to -1 if unused
// Sketch Settings
#define BUFSIZE 128 // Read buffer size for incoming data
#define VERBOSE_MODE true // Enables full debug output is 'true'
/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void) {
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit Command <-> Data Mode Example"));
Serial.println(F("------------------------------------------------"));
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) ) {
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
/* Perform a factory reset to make sure everything is in a known state */
Serial.print(F("Performing a factory reset: "));
if (! ble.factoryReset() ) {
error(F("Couldn't factory reset"));
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
Serial.println(F("Then Enter characters to send to Bluefruit"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}
Serial.println(F("*****************"));
// Set module to DATA mode
if (BLUEFRUIT_UART_MODE_PIN >= 0) {
Serial.println( F("Switching to DATA mode using the MODE pin!") );
ble.setModePin(BLUEFRUIT_MODE_DATA);
} else {
Serial.println( F("Switching to DATA mode using +++!") );
ble.println("+++");
delay(100);
ble.waitForOK();
}
Serial.println(F("*****************"));
}
/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void) {
// Check for user input
char n, inputs[BUFSIZE+1];
if (Serial.available()) {
n = Serial.readBytes(inputs, BUFSIZE);
inputs[n] = 0;
// Send characters to Bluefruit
Serial.print("Sending: ");
Serial.print(inputs);
// Send input data to host via Bluefruit
ble.print(inputs);
}
// Echo received data
while ( ble.available() ) {
int c = ble.read();
Serial.print((char)c);
// Hex output too, helps w/debugging!
Serial.print(" [0x");
if (c <= 0xF) Serial.print(F("0"));
Serial.print(c, HEX);
Serial.print("] ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment