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:29:28 2016 +0000
Revision:
1:667ddd72c851
Parent:
0:02b4c2dd2a55
Child:
2:7927c31fd7af
add example of interpreting a command from remote serial device

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 0:02b4c2dd2a55 20
joeata2wh 0:02b4c2dd2a55 21 ***
joeata2wh 0:02b4c2dd2a55 22 * By Joseph Ellsworth CTO of A2WH
joeata2wh 0:02b4c2dd2a55 23 * Take a look at A2WH.com Producing Water from Air using Solar Energy
joeata2wh 0:02b4c2dd2a55 24 * March-2016 License: https://developer.mbed.org/handbook/MIT-Licence
joeata2wh 0:02b4c2dd2a55 25 * Please contact us http://a2wh.com for help with custom design projects.
joeata2wh 0:02b4c2dd2a55 26 ***
joeata2wh 0:02b4c2dd2a55 27
joeata2wh 0:02b4c2dd2a55 28 */
joeata2wh 0:02b4c2dd2a55 29 #include "mbed.h"
joeata2wh 0:02b4c2dd2a55 30 #include "multi-serial-command-listener.h"
joeata2wh 0:02b4c2dd2a55 31
joeata2wh 0:02b4c2dd2a55 32 char myCommand[SCMD_MAX_CMD_LEN+1];
joeata2wh 0:02b4c2dd2a55 33
joeata2wh 0:02b4c2dd2a55 34
joeata2wh 0:02b4c2dd2a55 35 //------------------------------------
joeata2wh 0:02b4c2dd2a55 36 // RealTerm or Teraterm config
joeata2wh 0:02b4c2dd2a55 37 // 9600 bauds, 8-bit data, no parity
joeata2wh 0:02b4c2dd2a55 38 //------------------------------------
joeata2wh 0:02b4c2dd2a55 39
joeata2wh 0:02b4c2dd2a55 40 //Serial hc05(D1, D0); // PA_2, PA_3 This one does not work because redirected to USBTX, USBRX
joeata2wh 0:02b4c2dd2a55 41 // // can be fixed by changing solder bridges
joeata2wh 0:02b4c2dd2a55 42 Serial hc052(D10,D2); // PB_6, PA_10 This one works
joeata2wh 0:02b4c2dd2a55 43 Serial hc053(PA_11, PA_12); // This one works
joeata2wh 0:02b4c2dd2a55 44 Serial pc(USBTX, USBRX);
joeata2wh 0:02b4c2dd2a55 45
joeata2wh 0:02b4c2dd2a55 46 DigitalOut myled(LED1);
joeata2wh 0:02b4c2dd2a55 47
joeata2wh 0:02b4c2dd2a55 48
joeata2wh 0:02b4c2dd2a55 49 void commandCallback(char *cmdIn, void *extraContext) {
joeata2wh 0:02b4c2dd2a55 50 strcpy(myCommand, cmdIn);
joeata2wh 0:02b4c2dd2a55 51 // all our commands will be recieved async in commandCallback
joeata2wh 0:02b4c2dd2a55 52 // we don't want to do time consuming things since it could
joeata2wh 0:02b4c2dd2a55 53 // block the reader and allow the uart to overflow so we simply
joeata2wh 0:02b4c2dd2a55 54 // copy it out in the callback and then process it latter.
joeata2wh 0:02b4c2dd2a55 55
joeata2wh 0:02b4c2dd2a55 56 // See data_log one of dependants of this library for example
joeata2wh 0:02b4c2dd2a55 57 // of using *extraContext
joeata2wh 0:02b4c2dd2a55 58 }
joeata2wh 0:02b4c2dd2a55 59
joeata2wh 0:02b4c2dd2a55 60 int main() {
joeata2wh 0:02b4c2dd2a55 61 pc.baud(9600);
joeata2wh 1:667ddd72c851 62 //hc05.baud(9600);
joeata2wh 0:02b4c2dd2a55 63 hc052.baud(9600);
joeata2wh 0:02b4c2dd2a55 64 hc053.baud(9600);
joeata2wh 0:02b4c2dd2a55 65 int i = 1;
joeata2wh 0:02b4c2dd2a55 66 struct SCMD *cmdProc = scMake(&hc053, commandCallback, NULL) ;
joeata2wh 0:02b4c2dd2a55 67
joeata2wh 1:667ddd72c851 68 pc.printf("Test HC05 Bluetooth Connection !\r\n");
joeata2wh 0:02b4c2dd2a55 69 while(1) {
joeata2wh 0:02b4c2dd2a55 70 wait(1);
joeata2wh 0:02b4c2dd2a55 71 //hc05.printf("d1/do this program runs since %d seconds.\r\n", i);
joeata2wh 0:02b4c2dd2a55 72 hc052.printf("d10/d2 %d seconds\r\n", i);
joeata2wh 0:02b4c2dd2a55 73 hc053.printf("PA_11/PA_12 %d seconds\r\n", i);
joeata2wh 1:667ddd72c851 74 pc.printf("This program runs since %d seconds.\r\n", i++);
joeata2wh 0:02b4c2dd2a55 75 myled = !myled;
joeata2wh 0:02b4c2dd2a55 76 if (myCommand[0] != 0) {
joeata2wh 0:02b4c2dd2a55 77 pc.printf("Command Recieved =%s\r\n", myCommand);
joeata2wh 0:02b4c2dd2a55 78 hc053.printf("\r\nCommand Recieved =%s\r\n", myCommand);
joeata2wh 1:667ddd72c851 79 if (strcmp(myCommand,"clear") == 0) {
joeata2wh 1:667ddd72c851 80 i = 0;
joeata2wh 1:667ddd72c851 81 }
joeata2wh 0:02b4c2dd2a55 82 myCommand[0] = 0; // clear until we recieve the next one
joeata2wh 0:02b4c2dd2a55 83 }
joeata2wh 0:02b4c2dd2a55 84 }
joeata2wh 0:02b4c2dd2a55 85 }
joeata2wh 0:02b4c2dd2a55 86