howdy all!
i found tricky find decent documentation of full arduino example using rn 4020 board , explaining everything. made one.
i had other tricky parts made using tricky, demonstrate how use software serial, , how pair peripheral bluetooth heart rate monitor!
i documented in code, , have step-by-step details
lemme know if have better suggestions or whatnot. stuff until things work, , wanted share!
most updated at:
https://github.com/comingle/comingle_example_sketches/tree/master/mod/bluetooth/bluetooth_readheartrate
i found tricky find decent documentation of full arduino example using rn 4020 board , explaining everything. made one.
i had other tricky parts made using tricky, demonstrate how use software serial, , how pair peripheral bluetooth heart rate monitor!
i documented in code, , have step-by-step details
lemme know if have better suggestions or whatnot. stuff until things work, , wanted share!
most updated at:
https://github.com/comingle/comingle_example_sketches/tree/master/mod/bluetooth/bluetooth_readheartrate
code: [select]
/*
bluetooth lte 4.0 rn4020 dilduino (arduino) example
2015-04-06
{♥} comingle -by a. quitmeyer
share alike please, tossing public domain
this example shows how running bt 4.0
and how go through sorts of other tricky processes like
** connecting rn 4020 module
** doing on software serial instead of hardware serial
** pairing peripheral (bt heart rate monitor)
** getting notifications of data peripheral
what's bt?
it's weird fancy new bt that, newer phones have (my comp don't have) , operates posting , sending services
more regular serial. there's tricky stuff that's not too
well documented yet, helps out that!
general documentation tom igoe here: https://github.com/tigoe/bledocs/wiki/introduction-to-bluetooth-le
*/
//this example won't use ossex
//#include <ossex.h>
#include <softwareserial.h>
//this software serial ports on dilduino (like leonardo)
softwareserial myserial(15, 14); // rx (15 has rx), tx
int led = 13;
byte byteread;
char mychar ;
void setup()
{
//set mux on dilduino in serial mode
pinmode(8, output);
pinmode(12, output);
digitalwrite(8, low);
digitalwrite(12, high);
//no ossex none of these below
// toy.setid(beta);
// toy.sethackerport(hacker_port_serial);
//
//setup serials
//hardware serial (uart)
//we need same soft serial or else doesn't work me
serial.begin(115200);
// set data rate softwareserial port
myserial.begin(115200); //default rate of bluetooth rn4020 115200, changed more reliable data on software serial
//there's chance might need run commands first time setting 115200 since default
/*
initialize bluetooth baud lower speed software serial
myserial.begin(115200); //default rate of bluetooth rn4020 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 let chill 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 central or peripheral device
//myserial.println("sr,20000000"); //set device in peripheral mode , set turn on auto advertising
myserial.println("sr,92000000");// set device central, support mldp , enable
delay(500);
//change name that's displayed, first thing check in debugging
myserial.println("s-,comingleizcool");
delay(500);
//reboot sucker make changes stick. light should turn off when reboot (also debugging)
myserial.println("r,1"); //reboot
delay(1000);
//make device show on scanners
myserial.println("a"); //start advertising
delay(500);
//list details buetooth device (what it's connected to, it's mac address, etc.
myserial.println("d"); //get details of rn4020 chip's current status
pinmode(led, output);
}
void loop()
{
//this code below let on arduino's software serial talk hardware serial
//so can see through
while (myserial.available()) {
mychar = myserial.read();
serial.print(mychar);
}
// delay(500); // wait second
while (serial.available()) {
mychar = serial.read();
myserial.print(mychar);
}
}
note: here's code chopped out of post meet character limit requirements.
this shows messages send things connecting, , subscribing notifications, , parsing data.
this shows messages send things connecting, , subscribing notifications, , parsing data.
code: [select]
/*
//if works out well, @ point should have device setup
//for issuing , recieveing commands on serial monitor
//to test if things working
//type "d" , hit enter on serial monitor
this gives dump of details of bluetooth chips status
6c:94:f8:df:f2:75 /mac address
comingleizcool //name set
central // role set
connected: no //
bonded: no
server services: 80000000
if getting that, work, things seem communicating, let's try
more advanced stuff next
*/
/*
-----------------------------
connecting , getting information
-----------------------------
how connect bt
type command
f
this starts scanning nearby bluetooth things
it list mac addresses , stuff
when see correct device
use "e" command connect it
my bt heart rate monitor's device number fe9693ddab49
so typed in connect
e,1,fe9693ddab49 // connect
//*note if doesn't work try e,0, fe9693ddab49
//this has "random" , "public" address types
once connected
you can @ different information connected device can offer
by sending command
lc
this lists client services
you looks returned on serial
180d // stuff heart rate monitor
2a37,000b,00
2a37,000c,02
2a38,000e,10
2a39,0010,08
180f // stuff battery
2a19,0018,02
2a19,0019,10
this looks bunch of crazy garbage, it's how bluetooth 4.0 shares data
data gets posted little bulletin areas. , there "official" numbers
designate types of data
"180f" instance battery state on bt device
"180d" official designation heart rate information (you can these online)
the indented lines below different messages available service.
these messages have specific styles of reading information tied each of them
if want read data characteristic, has end in "02"
for instance, can @ battery status , see "0018" ends "02"
to read on rn4020
type
chr,0018
and message says:
r,64 //(which showss 100% battery life in hexadecimal)
for more advanced information, heart rate, have subscribe notified of data
data notify ends in "10"
so heart rate (bpm) nice cheap bt heart rate monitor
(you can clones of polar's h7 $15-25)
do this
chw,000e,0100
it sends notify messages this
notify,000e,0054
the last number there hexadecimal heart rate
here's rough example of doing data
we can convert human numbers
int bpm;
string str;
string valstring;
while (myserial.available()) {
str = myserial.readstringuntil('\n');
if(str.startswith("notify,000e"){ //make sure 1 targetting
valstring = getstringpartbynr(str, ',', 2)); // third part - 0054
bpm = hextodec(valstring);
}
}
//function split tokens
// https://github.com/bentommye/arduino_getstringpartbynr/blob/master/getstringpartbynr.ino
//
string getstringpartbynr(string data, char separator, int index)
{
// spliting string , return part nr index
// split separator
int stringdata = 0; //variable count data part nr
string datapart = ""; //variable hole return text
for(int = 0; i<data.length()-1; i++) { //walk through text 1 letter @ time
if(data[i]==separator) {
//count number of times separator character appears in text
stringdata++;
}else if(stringdata==index) {
//get text when separator rignt one
datapart.concat(data[i]);
}else if(stringdata>index) {
//return text , stop if next separator appears - save cpu-time
return datapart;
break;
}
}
//return text if last part
return datapart;
}
// converting hex decimal:
// note: function can handle positive hex value 0 - 65,535 (a 4 digit hex string).
// larger/longer values, change "unsigned int" "long" in both places.
// https://github.com/benrugg/arduino-hex-decimal-conversion/blob/master/hex_dec.ino
unsigned int hextodec(string hexstring) {
unsigned int decvalue = 0;
int nextint;
for (int = 0; < hexstring.length(); i++) {
nextint = int(hexstring.charat(i));
if (nextint >= 48 && nextint <= 57) nextint = map(nextint, 48, 57, 0, 9);
if (nextint >= 65 && nextint <= 70) nextint = map(nextint, 65, 70, 10, 15);
if (nextint >= 97 && nextint <= 102) nextint = map(nextint, 97, 102, 10, 15);
nextint = constrain(nextint, 0, 15);
decvalue = (decvalue * 16) + nextint;
}
return decvalue;
}
*/
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Bluetooth LTE RN4020 with Software Serial and Arduino to Peripheral
arduino
Comments
Post a Comment