Bluetooth HC05 + KL25Z communicate with Bluetooth dongle in PC in both directions.

Dependencies:   mbed

Committer:
GerritPathuis
Date:
Sun Dec 03 13:49:58 2017 +0000
Revision:
1:e73a7c99f767
Parent:
0:0e236e004748
Child:
2:cbc675b0abd5
This version works with interrupt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GerritPathuis 0:0e236e004748 1 /*
GerritPathuis 0:0e236e004748 2 * Author: G. Pathuis
GerritPathuis 0:0e236e004748 3 * Date: 02-12-17
GerritPathuis 0:0e236e004748 4 * Notes: HC05 connected to a KL25Z
GerritPathuis 0:0e236e004748 5 * Pair with PC use password 1234
GerritPathuis 0:0e236e004748 6 *
GerritPathuis 0:0e236e004748 7 * Four wire connection
GerritPathuis 0:0e236e004748 8 * HC05 to KL25Z
GerritPathuis 0:0e236e004748 9 * GND to GND
GerritPathuis 0:0e236e004748 10 * +5V to 5V
GerritPathuis 0:0e236e004748 11 * TX to pin PTE0
GerritPathuis 0:0e236e004748 12 * RX to pin PTE1
GerritPathuis 0:0e236e004748 13 *
GerritPathuis 0:0e236e004748 14 * Open Tera Term session to connect to KL25Z
GerritPathuis 0:0e236e004748 15 * and
GerritPathuis 0:0e236e004748 16 * Open second Tera Term session to read the Bluetooth
GerritPathuis 0:0e236e004748 17 * dongle in the PC
GerritPathuis 0:0e236e004748 18 *
GerritPathuis 0:0e236e004748 19 * Make sute Tera Term uses 9600 baud
GerritPathuis 1:e73a7c99f767 20 * http://www.wavesen.com
GerritPathuis 0:0e236e004748 21 */
GerritPathuis 0:0e236e004748 22
GerritPathuis 0:0e236e004748 23 #include "mbed.h"
GerritPathuis 1:e73a7c99f767 24 #include "MODSERIAL.h"
GerritPathuis 0:0e236e004748 25
GerritPathuis 1:e73a7c99f767 26 MODSERIAL pc(USBTX, USBRX);
GerritPathuis 1:e73a7c99f767 27 MODSERIAL blue(PTE0, PTE1); // TX, RX
GerritPathuis 1:e73a7c99f767 28
GerritPathuis 1:e73a7c99f767 29 DigitalOut myled1(LED1); //Blue
GerritPathuis 1:e73a7c99f767 30 DigitalOut myled2(LED2); //Green
GerritPathuis 1:e73a7c99f767 31
GerritPathuis 1:e73a7c99f767 32 // This function is called when a character received from PC
GerritPathuis 1:e73a7c99f767 33 void pc_rxCallback(MODSERIAL_IRQ_INFO *q)
GerritPathuis 1:e73a7c99f767 34 {
GerritPathuis 1:e73a7c99f767 35 char c;
GerritPathuis 0:0e236e004748 36
GerritPathuis 1:e73a7c99f767 37 c= pc.getc();
GerritPathuis 1:e73a7c99f767 38 blue.putc(c); // Send from PC to Blue
GerritPathuis 1:e73a7c99f767 39 pc.putc(c); // Echo to PC
GerritPathuis 1:e73a7c99f767 40 }
GerritPathuis 0:0e236e004748 41
GerritPathuis 1:e73a7c99f767 42 // This function is called when a character received from Bluetooth
GerritPathuis 1:e73a7c99f767 43 void blue_rxCallback(MODSERIAL_IRQ_INFO *q)
GerritPathuis 1:e73a7c99f767 44 {
GerritPathuis 1:e73a7c99f767 45 char b;
GerritPathuis 0:0e236e004748 46
GerritPathuis 1:e73a7c99f767 47 myled1 = !myled1;
GerritPathuis 1:e73a7c99f767 48 b= blue.getc();
GerritPathuis 1:e73a7c99f767 49 pc.putc(b);
GerritPathuis 1:e73a7c99f767 50 }
GerritPathuis 0:0e236e004748 51
GerritPathuis 0:0e236e004748 52 int main()
GerritPathuis 0:0e236e004748 53 {
GerritPathuis 1:e73a7c99f767 54 pc.baud(115200);
GerritPathuis 1:e73a7c99f767 55 blue.baud(38400); // Default Bluetooth Baudrate
GerritPathuis 1:e73a7c99f767 56
GerritPathuis 1:e73a7c99f767 57 pc.printf("Bluetooth HC-05\r\n");
GerritPathuis 1:e73a7c99f767 58 pc.printf("Make sure the terminal programm ends with CR-LF\r\n");
GerritPathuis 1:e73a7c99f767 59 pc.printf("\r\n");
GerritPathuis 0:0e236e004748 60
GerritPathuis 1:e73a7c99f767 61 wait_ms(100);
GerritPathuis 1:e73a7c99f767 62
GerritPathuis 1:e73a7c99f767 63 // Char from PC, interrupt service routine
GerritPathuis 1:e73a7c99f767 64 pc.attach(&pc_rxCallback, MODSERIAL::RxIrq);
GerritPathuis 1:e73a7c99f767 65
GerritPathuis 1:e73a7c99f767 66 // Char from Bluetooth, interrupt service routine
GerritPathuis 1:e73a7c99f767 67 blue.attach(&blue_rxCallback, MODSERIAL::RxIrq);
GerritPathuis 1:e73a7c99f767 68
GerritPathuis 0:0e236e004748 69
GerritPathuis 1:e73a7c99f767 70 // Set up
GerritPathuis 1:e73a7c99f767 71 pc.puts("AT=");
GerritPathuis 1:e73a7c99f767 72 blue.puts("AT\r\n");
GerritPathuis 1:e73a7c99f767 73 wait_ms(100);
GerritPathuis 1:e73a7c99f767 74
GerritPathuis 1:e73a7c99f767 75 pc.puts("AT+VERSION?= ");
GerritPathuis 1:e73a7c99f767 76 blue.puts("AT+VERSION?\r\n");
GerritPathuis 1:e73a7c99f767 77 wait_ms(100);
GerritPathuis 0:0e236e004748 78
GerritPathuis 1:e73a7c99f767 79 pc.puts("AT+ADDR?= ");
GerritPathuis 1:e73a7c99f767 80 blue.puts("AT+ADDR?\r\n");
GerritPathuis 1:e73a7c99f767 81 wait_ms(100);
GerritPathuis 1:e73a7c99f767 82
GerritPathuis 1:e73a7c99f767 83 pc.puts("AT+NAME?= ");
GerritPathuis 1:e73a7c99f767 84 blue.puts("AT+NAME?\r\n");
GerritPathuis 1:e73a7c99f767 85 wait_ms(100);
GerritPathuis 1:e73a7c99f767 86
GerritPathuis 1:e73a7c99f767 87 pc.puts("AT+Role?= ");
GerritPathuis 1:e73a7c99f767 88 blue.puts("AT+Role? \r\n");
GerritPathuis 1:e73a7c99f767 89 wait_ms(100);
GerritPathuis 1:e73a7c99f767 90
GerritPathuis 1:e73a7c99f767 91 // Echo back characters
GerritPathuis 0:0e236e004748 92 while (1) {
GerritPathuis 0:0e236e004748 93 if (blue.readable()) {
GerritPathuis 0:0e236e004748 94 pc.putc(blue.getc());
GerritPathuis 0:0e236e004748 95 }
GerritPathuis 0:0e236e004748 96 if (pc.readable()) {
GerritPathuis 0:0e236e004748 97 blue.putc(pc.getc());
GerritPathuis 0:0e236e004748 98 }
GerritPathuis 0:0e236e004748 99 }
GerritPathuis 1:e73a7c99f767 100
GerritPathuis 0:0e236e004748 101 }