Test HC05 Bluetooth adapter with NUCLEO F401RE. Includes serial command listener will echo commands from remote PC both on that connection and on local PC.

Dependencies:   mbed multi-serial-command-listener

Committer:
joeata2wh
Date:
Tue Apr 05 02:47:43 2016 +0000
Revision:
3:e985657f12b1
Parent:
2:7927c31fd7af
added current draw and model tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeata2wh 0:02b4c2dd2a55 1 /* xej-Nucleo-F401RE-and-HC05-Bluetooth.cpp
joeata2wh 0:02b4c2dd2a55 2 Test Nucleo-F401RE with HC05 Bluetooth adapter
joeata2wh 0:02b4c2dd2a55 3
joeata2wh 0:02b4c2dd2a55 4 Wanted to use it for upload of telemetry
joeata2wh 0:02b4c2dd2a55 5 type a command and hc053 port and it will be echoed back
joeata2wh 0:02b4c2dd2a55 6
joeata2wh 0:02b4c2dd2a55 7
joeata2wh 0:02b4c2dd2a55 8 pin-HC05 Pin-MBed
joeata2wh 0:02b4c2dd2a55 9 TX --- PA_12
joeata2wh 0:02b4c2dd2a55 10 RX --- PA_11
joeata2wh 0:02b4c2dd2a55 11 +5V --- +5 on CN6
joeata2wh 0:02b4c2dd2a55 12 Also worked on 3.3V on CN6
joeata2wh 0:02b4c2dd2a55 13 GND --- GND on CN6
joeata2wh 0:02b4c2dd2a55 14 STATE--- NC - not connected
joeata2wh 0:02b4c2dd2a55 15 EN --- NC - not connected
joeata2wh 0:02b4c2dd2a55 16
joeata2wh 0:02b4c2dd2a55 17 If you cycle power on mbed you may need to close
joeata2wh 0:02b4c2dd2a55 18 and re-open the port connection which
joeata2wh 0:02b4c2dd2a55 19 is pretty easy when using RealTerm.
joeata2wh 3:e985657f12b1 20
joeata2wh 3:e985657f12b1 21 tested with Honbay® Wireless Bluetooth Host Serial Transceiver Module
joeata2wh 3:e985657f12b1 22 Draws about 30mA when starting then drops back
joeata2wh 3:e985657f12b1 23 to 10mA when idle with a jump to about 20 mA
joeata2wh 3:e985657f12b1 24 when transmitting.
joeata2wh 0:02b4c2dd2a55 25
joeata2wh 0:02b4c2dd2a55 26 ***
joeata2wh 0:02b4c2dd2a55 27 * By Joseph Ellsworth CTO of A2WH
joeata2wh 0:02b4c2dd2a55 28 * Take a look at A2WH.com Producing Water from Air using Solar Energy
joeata2wh 0:02b4c2dd2a55 29 * March-2016 License: https://developer.mbed.org/handbook/MIT-Licence
joeata2wh 0:02b4c2dd2a55 30 * Please contact us http://a2wh.com for help with custom design projects.
joeata2wh 0:02b4c2dd2a55 31 ***
joeata2wh 0:02b4c2dd2a55 32
joeata2wh 0:02b4c2dd2a55 33 */
joeata2wh 0:02b4c2dd2a55 34 #include "mbed.h"
joeata2wh 0:02b4c2dd2a55 35 #include "multi-serial-command-listener.h"
joeata2wh 0:02b4c2dd2a55 36
joeata2wh 0:02b4c2dd2a55 37 char myCommand[SCMD_MAX_CMD_LEN+1];
joeata2wh 0:02b4c2dd2a55 38
joeata2wh 0:02b4c2dd2a55 39
joeata2wh 0:02b4c2dd2a55 40 //------------------------------------
joeata2wh 0:02b4c2dd2a55 41 // RealTerm or Teraterm config
joeata2wh 0:02b4c2dd2a55 42 // 9600 bauds, 8-bit data, no parity
joeata2wh 0:02b4c2dd2a55 43 //------------------------------------
joeata2wh 0:02b4c2dd2a55 44
joeata2wh 0:02b4c2dd2a55 45 //Serial hc05(D1, D0); // PA_2, PA_3 This one does not work because redirected to USBTX, USBRX
joeata2wh 0:02b4c2dd2a55 46 // // can be fixed by changing solder bridges
joeata2wh 0:02b4c2dd2a55 47 Serial hc052(D10,D2); // PB_6, PA_10 This one works
joeata2wh 0:02b4c2dd2a55 48 Serial hc053(PA_11, PA_12); // This one works
joeata2wh 0:02b4c2dd2a55 49 Serial pc(USBTX, USBRX);
joeata2wh 0:02b4c2dd2a55 50
joeata2wh 0:02b4c2dd2a55 51 DigitalOut myled(LED1);
joeata2wh 0:02b4c2dd2a55 52
joeata2wh 0:02b4c2dd2a55 53
joeata2wh 0:02b4c2dd2a55 54 void commandCallback(char *cmdIn, void *extraContext) {
joeata2wh 0:02b4c2dd2a55 55 strcpy(myCommand, cmdIn);
joeata2wh 0:02b4c2dd2a55 56 // all our commands will be recieved async in commandCallback
joeata2wh 0:02b4c2dd2a55 57 // we don't want to do time consuming things since it could
joeata2wh 0:02b4c2dd2a55 58 // block the reader and allow the uart to overflow so we simply
joeata2wh 0:02b4c2dd2a55 59 // copy it out in the callback and then process it latter.
joeata2wh 0:02b4c2dd2a55 60
joeata2wh 0:02b4c2dd2a55 61 // See data_log one of dependants of this library for example
joeata2wh 0:02b4c2dd2a55 62 // of using *extraContext
joeata2wh 0:02b4c2dd2a55 63 }
joeata2wh 0:02b4c2dd2a55 64
joeata2wh 0:02b4c2dd2a55 65 int main() {
joeata2wh 0:02b4c2dd2a55 66 pc.baud(9600);
joeata2wh 1:667ddd72c851 67 //hc05.baud(9600);
joeata2wh 0:02b4c2dd2a55 68 hc052.baud(9600);
joeata2wh 0:02b4c2dd2a55 69 hc053.baud(9600);
joeata2wh 0:02b4c2dd2a55 70 int i = 1;
joeata2wh 0:02b4c2dd2a55 71 struct SCMD *cmdProc = scMake(&hc053, commandCallback, NULL) ;
joeata2wh 0:02b4c2dd2a55 72
joeata2wh 1:667ddd72c851 73 pc.printf("Test HC05 Bluetooth Connection !\r\n");
joeata2wh 0:02b4c2dd2a55 74 while(1) {
joeata2wh 0:02b4c2dd2a55 75 wait(1);
joeata2wh 0:02b4c2dd2a55 76 //hc05.printf("d1/do this program runs since %d seconds.\r\n", i);
joeata2wh 0:02b4c2dd2a55 77 hc052.printf("d10/d2 %d seconds\r\n", i);
joeata2wh 0:02b4c2dd2a55 78 hc053.printf("PA_11/PA_12 %d seconds\r\n", i);
joeata2wh 1:667ddd72c851 79 pc.printf("This program runs since %d seconds.\r\n", i++);
joeata2wh 0:02b4c2dd2a55 80 myled = !myled;
joeata2wh 0:02b4c2dd2a55 81 if (myCommand[0] != 0) {
joeata2wh 0:02b4c2dd2a55 82 pc.printf("Command Recieved =%s\r\n", myCommand);
joeata2wh 0:02b4c2dd2a55 83 hc053.printf("\r\nCommand Recieved =%s\r\n", myCommand);
joeata2wh 1:667ddd72c851 84 if (strcmp(myCommand,"clear") == 0) {
joeata2wh 1:667ddd72c851 85 i = 0;
joeata2wh 1:667ddd72c851 86 }
joeata2wh 2:7927c31fd7af 87 myCommand[0] = 0; // clear until we recieve the next command
joeata2wh 0:02b4c2dd2a55 88 }
joeata2wh 0:02b4c2dd2a55 89 }
joeata2wh 0:02b4c2dd2a55 90 }
joeata2wh 0:02b4c2dd2a55 91