This a great example of how Bluetooth (BT) reads incoming chars. This program constantly check if a char has been sent over BT and then turns on or off a digital output on the K64F board. We used MIT App Inventor to download an app to a android phone. This app would connect to our HC06 BT module on the K64F. The app would send chars when certain buttons were pressed. The K64F would then receive them and take appropriate action. Our digital output would send signal to relays, that's why you see variables called relay1,2,3... The relays then allowed power to got to a power strip.
Revision 1:9ba4f4c2b414, committed 2019-04-29
- Comitter:
- jwhammel
- Date:
- Mon Apr 29 00:01:55 2019 +0000
- Parent:
- 0:2fb6b3ddfcbd
- Commit message:
- This is a great example of how Bluetooth (BT) works. The program is constantly checking to see if a char has been sent over BT signal. This used the K64F and HC06. We used MIT App Inventor to make a program that will connect to our HC06 BT module.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 2fb6b3ddfcbd -r 9ba4f4c2b414 main.cpp --- a/main.cpp Wed Jan 02 18:37:58 2019 +0000 +++ b/main.cpp Mon Apr 29 00:01:55 2019 +0000 @@ -1,34 +1,78 @@ #include "mbed.h" -CAN can1(PA_11, PA_12); -Serial bluetooth(PA_9, PA_10); //PINS TO CONECT. PA_9 WITH RX PIN HC-06 -int dato=0; + +Serial bluetooth(PTC15, PTC14); // pins to connect on HC-06 +int mydata=0; -DigitalOut led(PC_13);// LED BUILT IN BOARD STM32F103C8T6 +DigitalOut led_r(LED1); // on board LED +DigitalOut Relay1(PTC17); +DigitalOut Relay2(PTB9); +DigitalOut Relay3(PTA1); +DigitalOut Relay4(PTB23); +DigitalOut Relay5(PTA2); int main() { + Relay1 = 0; + Relay2 = 0; + Relay3 = 0; + Relay4 = 0; + Relay5 = 0; + led_r = 1; + bluetooth.baud(9600); bluetooth.printf("\nSTM32 bluetooth\n"); - while(1) { + printf("\r\nready\r\n"); + while(1) + { + + if(bluetooth.readable()) + { + mydata=bluetooth.getc(); + bluetooth.putc(mydata); + printf("mydata = %c\r\n",mydata); + } // end if - if(bluetooth.readable()) { - - dato=bluetooth.getc(); - bluetooth.putc(dato); - - - } - if (dato=='a'){ - led=0; - } - else if(dato=='b') + switch(mydata) { - led=1; - } - + case 'a': + Relay1 = 0; + break; + case 'A': + Relay1 = 1; + break; + case 'b': + Relay2 = 0; + break; + case 'B': + Relay2 = 1; + break; + case 'c': + Relay3 = 0; + break; + case 'C': + Relay3 = 1; + break; + case 'd': + Relay4 = 0; + break; + case 'D': + Relay4 = 1; + break; + case 'e': + Relay5 = 0; + break; + case 'E': + Relay5 = 1; + break; + default: + led_r = 0; + } // end switch - - } -} + //wait(1.0f); + //printf("one second has elapsed.\r\n"); + + } // end while + +} // end main