Howdy all!
I found it tricky to find decent documentation of a full Arduino Example using the RN 4020 board and explaining everything. So i made one.
I also had other tricky parts that made using it extra tricky, but will demonstrate how to use it with the Software Serial, and how to pair with a peripheral like a Bluetooth Heart rate Monitor!
I documented everything in the code, and have step-by-step details
lemme know if you have better suggestions or whatnot. I just do stuff until things work, and wanted to share!
Most updated at:
/*
Bluetooth LTE 4.0 RN4020 to Dilduino (Arduino) Example
2015-04-06
{♥} COMINGLE -by A. Quitmeyer
Share alike please, but also just tossing into Public Domain
This example shows you how to get running with BT 4.0
and also how to go through all sorts of other tricky processes like
** Connecting with the RN 4020 Module
** Doing all this over Software Serial instead of hardware Serial
** Pairing with a peripheral (BT Heart Rate Monitor)
** Getting notifications of data from the peripheral
What's this BT?
It's that Weird fancy new BT that, only newer phones have (my comp don't have) and it operates with posting and sending services
more than just like a regular serial. There's some tricky stuff about it that's not too
well documented yet, so hopefully this helps out with that!
General documentation from Tom Igoe is here: https://github.com/tigoe/BLEDocs/wiki/Introduction-to-Bluetooth-LE
*/
//This example won't use any OSSEX
//#include <OSSex.h>
#include <SoftwareSerial.h>
//This is the software serial ports on the Dilduino (like Leonardo)
SoftwareSerial mySerial(15, 14); // RX (15 has to be RX), TX
int led = 13;
byte byteRead;
char myChar ;
void setup()
{
//Set the Mux on the Dilduino in serial mode
pinMode(8, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(12, HIGH);
//No OSSEX so none of these below
// Toy.setID(BETA);
// Toy.setHackerPort(HACKER_PORT_SERIAL);
//
//SETUP ALL THE SERIALS
//Hardware Serial (UART)
//We need this to be the same as the Soft Serial or else it doesn't work for me
Serial.begin(115200);
// set the data rate for the SoftwareSerial port
mySerial.begin(115200); //default rate of bluetooth RN4020 is 115200, we changed it for more reliable data over Software serial
//There's a good chance you might need to run the commands the first time by setting this to 115200 since that is default
/*
Initialize Bluetooth Baud to lower speed for Software Serial
mySerial.begin(115200); //default rate of bluetooth RN4020 is 115200,
delay(500);
mySerial.println("SB,0"); // 0 = 2400 Baud 1 = 9600 baud 2= 19200 baud 4 = 115200 baud (default)
delay(500);
mySerial.println("R,1"); //Reboot
delay(1000);
*/
//Always nice to let it chill a bit
delay(1000);
//BLUETOOTH SETUP
//Initialize BT RN4020
mySerial.println("SB,0"); // 0 = 2400 Baud 1 = 9600 baud 2= 19200 baud 4 = 115200 baud (default)
delay(500);
mySerial.println("R,1"); //Reboot
delay(1000);
//Set as central or as a peripheral device
//mySerial.println("SR,20000000"); //Set device in peripheral mode and set turn on auto advertising
mySerial.println("SR,92000000");// Set device as central, support MLDP and enable
delay(500);
//Change the name that's displayed, this is always a good first thing to check in your debugging
mySerial.println("S-,ComingleIZCool");
delay(500);
//Reboot the sucker to make the changes stick. the light should turn off when you reboot (also good for debugging)
mySerial.println("R,1"); //Reboot
delay(1000);
//Make the device show up on scanners
mySerial.println("A"); //Start advertising
delay(500);
//List all the details about the Buetooth device (what it's connected to, it's Mac address, etc.
mySerial.println("D"); //Get details of the RN4020 chip's current status
pinMode(led, OUTPUT);
}
void loop()
{
//This code below will let everything on your arduino's Software Serial talk to the hardware serial
//So you can see it all through
while (mySerial.available()) {
myChar = mySerial.read();
Serial.print(myChar);
}
// delay(500); // wait for a second
while (Serial.available()) {
myChar = Serial.read();
mySerial.print(myChar);
}
}