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

Dependencies:   mbed

main.cpp

Committer:
GerritPathuis
Date:
2017-12-03
Revision:
1:e73a7c99f767
Parent:
0:0e236e004748
Child:
2:cbc675b0abd5

File content as of revision 1:e73a7c99f767:

/*
 * Author: G. Pathuis
 * Date: 02-12-17
 * Notes: HC05 connected to a KL25Z
 * Pair with PC use password 1234
 *
 * Four wire connection
 * HC05 to KL25Z
 * GND to GND
 * +5V to 5V
 * TX to pin PTE0
 * RX to pin PTE1
 *
 * Open Tera Term session to connect to KL25Z
 * and
 * Open second Tera Term session to read the Bluetooth
 * dongle in the PC
 *
 * Make sute Tera Term uses 9600 baud
 * http://www.wavesen.com
*/

#include "mbed.h"
#include "MODSERIAL.h"

MODSERIAL  pc(USBTX, USBRX);
MODSERIAL  blue(PTE0, PTE1);          // TX, RX

DigitalOut myled1(LED1);    //Blue
DigitalOut myled2(LED2);    //Green

// This function is called when a character received from PC
void pc_rxCallback(MODSERIAL_IRQ_INFO *q)
{
    char c;

    c= pc.getc();
    blue.putc(c);   // Send from PC to Blue
    pc.putc(c);     // Echo to PC
}

// This function is called when a character received from Bluetooth
void blue_rxCallback(MODSERIAL_IRQ_INFO *q)
{
    char b;

    myled1 = !myled1;
    b= blue.getc();
    pc.putc(b);
}

int main()
{
    pc.baud(115200);
    blue.baud(38400);                // Default Bluetooth Baudrate

    pc.printf("Bluetooth HC-05\r\n");
    pc.printf("Make sure the terminal programm ends with CR-LF\r\n");
    pc.printf("\r\n");

    wait_ms(100);

    // Char from PC, interrupt service routine
    pc.attach(&pc_rxCallback, MODSERIAL::RxIrq);

    // Char from Bluetooth, interrupt service routine
    blue.attach(&blue_rxCallback, MODSERIAL::RxIrq);


    // Set up
    pc.puts("AT=");
    blue.puts("AT\r\n");
    wait_ms(100);

    pc.puts("AT+VERSION?= ");
    blue.puts("AT+VERSION?\r\n");
    wait_ms(100);

    pc.puts("AT+ADDR?= ");
    blue.puts("AT+ADDR?\r\n");
    wait_ms(100);

    pc.puts("AT+NAME?= ");
    blue.puts("AT+NAME?\r\n");
    wait_ms(100);

    pc.puts("AT+Role?= ");
    blue.puts("AT+Role? \r\n");
    wait_ms(100);

    // Echo back characters
    while (1) {
        if (blue.readable()) {
            pc.putc(blue.getc());
        }
        if (pc.readable()) {
            blue.putc(pc.getc());
        }
    }

}