EVNBluetooth#
The Bluetooth Module Standard Peripheral uses the HC-05 Bluetooth Module.
This module operates over the Serial UART Communication Protocol, and can be set into 2 modes: Host & Remote.
Running as a Remote device means that a Bluetooth host (such as your laptop or phone) can connect to it and transmit commands or receive data.
Running as a Host device allows you to connect to other Remote devices, which can be used for communication between 2 Bluetooth Modules (1 Host & 1 Remote).
The module’s settings are stored on the module. To access and edit those settings, users must hold a button to start it in Program Mode (AT mode) and send serial commands.
This library is intended to simplify the process of confuguring settings like baud rate and host/remote mode, such that manual text commands are not needed.
If the module is started in Program Mode, this library can configure settings and retrieve some useful information. Otherwise, this library does nothing.
Normal usage of the module (in both modes) is via Serial1 or Serial2 (depending on which Serial port is used), which have the same functions as Serial.
Wiring (Serial)#
Host |
Peripheral |
Description |
|---|---|---|
3V3 |
VCC |
3.3V Power |
GND |
GND |
Ground (0V) |
RX |
TX |
Host Receive |
TX |
RX |
Host Transmit |
Starting Module in Program Mode#
This only needs to be done once to set the proper settings.
Set EVN into Off mode using the On/Off button.
Press and hold the Bluetooth Module’s onboard button.
Set EVN into On mode using the On/Off button.
Hold the button for 2s before releasing it. If the Bluetooth Module’s red LED starts blinking once every 2 seconds (rate of 0.5Hz), it has successfully entered program mode.
Now that our module has been set into Program Mode, you most likely need to restart your program, so that our library can configure the module. Do this by pressing the Reset button.
Constructor#
- EVNBluetooth(uint8_t serial_port, uint32_t baud_rate = 9600, char* name = (char*)"EVN Bluetooth", uint8_t mode = BT_REMOTE, char* addr = nullptr);
- Parameters
serial_port – Serial port the sensor is connected to (1 or 2)
baud_rate – Baud rate used when not in Program Mode. Defaults to 9600. Supported baud rates: 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400
name – Name of Bluetooth device. Defaults to
(char*)"EVN Bluetooth". Store in a char array, or cast tochar*before passing into this constructor.mode –
Mode to run module in. Defaults to
BT_REMOTEBT_HOST: Host ModeBT_REMOTE: Remote Mode
addr – Bluetooth Address of the device to connect to. This is only used in Host Mode. Store in a char array, or cast to
char*before passing into this constructor. Defaults tonullptr.
EVNBluetooth host(2, 9600, (char*)"EVN Bluetooth Host", BT_HOST, (char*)"19:8:34FE");
Functions#
All of these functions only do something when the module is in Program Mode. In normal operation, use Serial1 or Serial2 to transmit and receive data, like a normal Serial connection.
- bool begin(bool exit_program_mode = true)
If the module is set to Program Mode, this function writes all settings to the module, before exiting program mode depending on user input.
- Parameters
exit_program_mode – Whether to exit Program Mode after updating settings. Defaults to
true
bt.begin();
- bool inProgramMode()
- Returns
truethe module is in Program Mode,falseotherwise
bt.inProgramMode();
- bool exitProgramMode()
Exits Program Mode for normal operation
- Returns
trueif module is not in Program Mode,falseotherwise
bt.exitProgramMode();
- bool factoryReset()
Resets all settings stored in the module to their factory defaults. This function is useful for resetting more advanced settings which are not exposed by this library.
- Returns
trueif module was in Program Mode & successfully reset,falseotherwise
bt.factoryReset();
Retrieving Module Info#
These functions only work when the module is in Program Mode.
- bool getAddress(char* array)
Writes address of Bluetooth Module (just the address, no ``Address: `` prefix) to given array.
- Parameters
array – memory address of character array to write address to
- Returns
trueif module was in Program Mode and address was successfully retrieved,falseotherwise
char address[32] = {0}; bt.getAddress(address);
- void printAddress()
Prints address of Bluetooth Module using
Serial. Write this address to the constructor of another EVNBluetooth object in Host Mode to connect to it.- Returns
trueif module was in Program Mode and address was successfully retrieved,falseotherwise
bt.printAddress();
Example Output on Serial Monitor:
Version: 19:8:34FE //If you are connecting to this Module with another Module in Host Mode, place (char*) "19:8:34FE" in the constructor for the Host Module.
- void getVersion(char* array)
Writes firmware version of Bluetooth Module (just the version, no ``Version: `` prefix) to given array.
- Parameters
array – memory address of character array to write address to
- Returns
trueif module was in Program Mode and version was successfully retrieved,falseotherwise
char version[32] = {0}; bt.getVersion(version);
- void printVersion()
Prints firmware version of Bluetooth Module using
Serial.- Returns
trueif module was in Program Mode and version was successfully retrieved,falseotherwise
bt.printVersion();
Example Output on Serial Monitor:
Version: 3.0-20170601
Directly Programming the Module in Program Mode#
A example sketch for using the Serial Monitor to program the Bluetooth Module using AT commands can be found in Examples (Examples > EVN > Others > Debug > bluetoothAT).