Skip to content

Commit

Permalink
further update with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Sep 1, 2015
1 parent 10652ce commit 2e3f25e
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pioenvs/
.cproject
.project
.sconsign.dblite
platformio.ini
8 changes: 7 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Library for using HC-05 bluetooth module with Arduino. It saves yor time by abstracting you from AT-commands, which control the module, with class Bluetooth_HC05 and it's methods. Of course it adds some overhead to your firmware, but it's the price you pay for great convinience of getting the things done with several method calls rather than writing a parser for large AT-command set.
#Bluetooth HC05 Arduino Library

A fork of the work originally done by Artem Borisovskiy ([email protected]), http://robocraft.ru

I've updated with a few minor tweaks and fixes.

Library is designed for HC-05 bluetooth module with Arduino. It saves yor time by abstracting you from AT-commands, which control the module, with class Bluetooth_HC05 and it's methods. Of course it adds some overhead to your firmware, but it's the price you pay for great convinience of getting the things done with several method calls rather than writing a parser for large AT-command set.
34 changes: 34 additions & 0 deletions examples/master_callback/master_callback.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <SoftwareSerial.h>
#include "Bluetooth_HC05.h"

SoftwareSerial bt_serial(6, 5);
Bluetooth_HC05 hc05(bt_serial);

void bluetoothDeviceFound(const BluetoothAddress &address) {
Serial.print(F("Device Found: "));
char address_str[HC05_ADDRESS_BUFSIZE];
hc05.printBluetoothAddress(address_str,address,',');
Serial.println(address_str);
}

void setup() {
Serial.begin(115200);
Serial.println(F("Callback HC05 Demo"));
/* Speed: 38400; HC-05 MODE (PIO11): pin 7 */
hc05.begin(38400, 7, HC05_MODE_COMMAND);
/* Wait until HC-05 starts */
delay(2000);
/* Allow HC-05 to initiate connections */
hc05.setRole(HC05_ROLE_MASTER);
/* Set pwd of HC-05 to all zeros */
hc05.setPassword("0000");
/* Cannot connect without this */
hc05.initSerialPortProfile();
/* set mode, num devices and time 5 * 1.28 seconds */
hc05.setInquiryMode(HC05_INQUIRY_RSSI, 9, 5);
/* Inquiring does take a bit of time, thus 10 seconds of timeout. */
hc05.inquire(bluetoothDeviceFound, 10000);
}

void loop() {
}
40 changes: 19 additions & 21 deletions examples/master_simple/master_simple.pde
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@

Bluetooth_HC05 hc05;

void setup()
{
/* Speed: 38400; HC-05 RESET: pin 2; HC-05 MODE (PIO11): pin 3 */
hc05.begin(38400, 2, 3, HC05_MODE_COMMAND);
/* Wait until HC-05 starts */
delay(700);
/* Allow HC-05 to initiate connections */
hc05.setRole(HC05_ROLE_MASTER);
/* Cannot connect without this */
hc05.initSerialPortProfile();
/* It's ridiculous: HC-05 cannot connect to anything without inquiring -
no matter will it found any device or not!
Iquiring may take pretty much time, thus 10 seconds of timeout. */
hc05.inquire(NULL, 10000);
/* Slave module says "+ADDR:11:4:290255" on "AT+ADDR?" command */
BluetoothAddress slave = { 0x00, 0x11, 0x04, 0x29, 0x02, 0x55 };
hc05.connect(slave);
void setup() {
/* Speed: 38400; HC-05 RESET: pin 2; HC-05 MODE (PIO11): pin 3 */
hc05.begin(38400, 2, 3, HC05_MODE_COMMAND);
/* Wait until HC-05 starts */
delay(700);
/* Allow HC-05 to initiate connections */
hc05.setRole(HC05_ROLE_MASTER);
/* Cannot connect without this */
hc05.initSerialPortProfile();
/* It's ridiculous: HC-05 cannot connect to anything without inquiring -
no matter will it found any device or not!
Iquiring may take pretty much time, thus 10 seconds of timeout. */
hc05.inquire(NULL, 10000);
/* Slave module says "+ADDR:11:4:290255" on "AT+ADDR?" command */
BluetoothAddress slave = { 0x00, 0x11, 0x04, 0x29, 0x02, 0x55 };
hc05.connect(slave);
}

void loop()
{
hc05.println("Blablabla");
delay(1000);
void loop() {
hc05.println("Blablabla");
delay(1000);
}
8 changes: 8 additions & 0 deletions lib/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Documentation: http://docs.platformio.org/en/latest/userguide/cmd_init.html

This directory is intended for the project specific (private) libraries.
PlatformIO will compile them to static libraries and link to executable file.

The source code of each library should be placed in separate directory.
For example, "lib/private_lib/[here are source files]".
62 changes: 32 additions & 30 deletions Bluetooth_HC05.cpp → src/Bluetooth_HC05.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ unsigned long htoul(const char *str)
#ifdef HC05_SOFTSERIAL
Bluetooth_HC05::Bluetooth_HC05(SoftwareSerial &serial):
m_uart(&serial), m_timeout(HC05_DEFAULT_TIMEOUT), m_ticksAtStart(millis()),
m_modePin(0xFF), m_errorCode(HC05_OK)
m_modePin(0xFF), m_resetPin(0xFF), m_errorCode(HC05_OK)
{
}
#else
Bluetooth_HC05::Bluetooth_HC05(HardwareSerial &serial):
m_uart(&serial), m_timeout(HC05_DEFAULT_TIMEOUT), m_ticksAtStart(millis()),
m_modePin(0xFF), m_errorCode(HC05_OK)
m_modePin(0xFF), m_resetPin(0xFF), m_errorCode(HC05_OK)
{
}
#endif
Expand All @@ -91,14 +91,27 @@ HC05_Result Bluetooth_HC05::getLastError() const
}

void Bluetooth_HC05::begin(long baud_rate, uint8_t mode_pin, HC05_Mode mode)
{
begin(baud_rate, 0xFF, mode_pin, mode);
}


void Bluetooth_HC05::begin(long baud_rate, uint8_t reset_pin,
uint8_t mode_pin, HC05_Mode mode)
{
m_uart->begin(baud_rate);

m_modePin = mode_pin;
pinMode(m_modePin, OUTPUT);
digitalWrite(m_modePin, (mode == HC05_MODE_DATA ? LOW : HIGH));

softReset();
if (reset_pin != 0xFF) {
m_resetPin = reset_pin;
pinMode(m_resetPin, OUTPUT);
digitalWrite(m_resetPin, HIGH);
hardReset();
} else
softReset();
}

void Bluetooth_HC05::changeMode(HC05_Mode mode)
Expand All @@ -116,6 +129,16 @@ bool Bluetooth_HC05::probe(unsigned long timeout)
return readOperationResult();
}


void Bluetooth_HC05::hardReset()
{
digitalWrite(m_resetPin, LOW);
delay(6);
digitalWrite(m_resetPin, HIGH);

m_errorCode = HC05_OK;
}

bool Bluetooth_HC05::softReset(unsigned long timeout)
{
PGM_STRING_MAPPED_TO_RAM(reset_cmd, "RESET");
Expand Down Expand Up @@ -1198,28 +1221,21 @@ bool Bluetooth_HC05::parseBluetoothAddress(
return false;

char *digits_ptr = const_cast<char*>(address_str);
uint8_t NAP[2];
*((uint16_t*)NAP) = htoul(digits_ptr);

address.NAP = (long) htoul(digits_ptr);

digits_ptr = strchrnul(digits_ptr, delimiter);

if (*digits_ptr != delimiter)
return false;

uint8_t UAP = htoul(++digits_ptr);
address.UAP = (long) htoul(++digits_ptr);
digits_ptr = strchrnul(digits_ptr, delimiter);

if (*digits_ptr != delimiter)
return false;

uint8_t LAP[4];
*((uint32_t*)LAP) = htoul(++digits_ptr);

address.bytes[0] = NAP[1];
address.bytes[1] = NAP[0];
address.bytes[2] = UAP;
address.bytes[3] = LAP[2];
address.bytes[4] = LAP[1];
address.bytes[5] = LAP[0];
address.LAP = (long) htoul(++digits_ptr);

return true;
}
Expand All @@ -1231,24 +1247,10 @@ int Bluetooth_HC05::printBluetoothAddress(char *address_str,
if (!address.bytes || !address_str)
return 0;

uint8_t NAP[2];
NAP[0] = address.bytes[1];
NAP[1] = address.bytes[0];

uint8_t UAP = address.bytes[2];

uint8_t LAP[4];
LAP[0] = address.bytes[5];
LAP[1] = address.bytes[4];
LAP[2] = address.bytes[3];
LAP[3] = 0;

PGM_STRING_MAPPED_TO_RAM(format, "%x%c%x%c%lx");

int written = snprintf(address_str, HC05_ADDRESS_BUFSIZE, format,
*reinterpret_cast<const uint16_t*>(NAP),
delimiter, UAP, delimiter,
*reinterpret_cast<const uint32_t*>(LAP));
address.NAP,delimiter, address.UAP, delimiter,address.LAP);

return written;
}
Expand Down
21 changes: 14 additions & 7 deletions Bluetooth_HC05.h → src/Bluetooth_HC05.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@
#include "WProgram.h"
#endif

//#define HC05_DEBUG 1 // uncomment for debug messages
/* Use the following defines to enable/disable debug or serial.
* #define HC05_DEBUG 1
* #define HC05_SOFTSERIAL 1
*/
#define HC05_SOFTSERIAL 1
#define HC05_DEBUG 1

#ifdef HC05_SOFTSERIAL
#include <SoftwareSerial.h>
#endif
#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>

enum
{
Expand All @@ -51,15 +56,14 @@ enum
HC05_ADDRESS_BUFSIZE = HC05_ADDRESS_MAXLEN + 1,
};

typedef union
{
typedef union {
uint8_t bytes[6];
struct
{
uint8_t nap[2];
uint8_t uap;
uint8_t lap[3];
} fields;
uint16_t NAP;
uint8_t UAP :8;
uint32_t LAP :24;
};

} BluetoothAddress;

Expand Down Expand Up @@ -141,8 +145,10 @@ class Bluetooth_HC05: public Print
virtual ~Bluetooth_HC05();

void begin(long baud_rate = 38400, uint8_t mode_pin = 0xFF, HC05_Mode mode = HC05_MODE_COMMAND);
void begin(long baud_rate = 38400, uint8_t reset_pin = 0xFF, uint8_t mode_pin = 0xFF, HC05_Mode mode = HC05_MODE_COMMAND);
void changeMode(HC05_Mode mode = HC05_MODE_DATA);
bool probe(unsigned long timeout = HC05_DEFAULT_TIMEOUT);
void hardReset();
bool softReset(unsigned long timeout = HC05_DEFAULT_TIMEOUT);
bool getVersion(char *buffer, size_t buffer_size, unsigned long timeout = HC05_DEFAULT_TIMEOUT);
bool restoreDefaults(unsigned long timeout = HC05_DEFAULT_TIMEOUT);
Expand Down Expand Up @@ -208,6 +214,7 @@ class Bluetooth_HC05: public Print
unsigned long m_ticksAtStart;

uint8_t m_modePin;
uint8_t m_resetPin;
HC05_Result m_errorCode;

bool readAddressWithCommand(BluetoothAddress &address,
Expand Down

0 comments on commit 2e3f25e

Please sign in to comment.