Skip to content

Commit

Permalink
bluetooth
Browse files Browse the repository at this point in the history
  • Loading branch information
aron-bordin committed Apr 8, 2015
1 parent 438a9f9 commit 770b2f8
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 35 deletions.
74 changes: 52 additions & 22 deletions Bluetooth/Bluetooth.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
/**
*
This file is part of .PNG Arduino Framework.
.PNG Arduino Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
.PNG Arduino Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with .PNG Arduino Framework. If not, see <http://www.gnu.org/licenses/>.
*/


#include "Bluetooth.h"


/**
* Get Bluetooth RX PIN
* @return int - rx PIN
Expand Down Expand Up @@ -60,8 +80,9 @@ char Bluetooth::getMessageEnd(){
* Set Bluetooth device name
* @param char[] name
*/
void Bluetooth::setName(char name[]){
strcpy(name, this->name);
void Bluetooth::setName(char new_name[]){
strcpy(name, "AT+NAME");
strcat(name, new_name);
}

/**
Expand All @@ -74,51 +95,42 @@ void Bluetooth::setupBluetooth(){

btSerial->begin(9600);

Serial.println("Setting bluetooth with 9600");
btSerial->write("AT+BAUD4");
while (btSerial->available())
delay(1100);
Serial.println("\nSetting bluetooth with 9600");
while (btSerial->available())
Serial.write(btSerial->read());

Serial.println("\nSetting Bluetooth name");
char _name[255];
strcpy(_name, "AT+NAME");
strcat(_name, name);
btSerial->write(name);
delay(1100);
Serial.print("\nSetting Bluetooth name: ");
Serial.println(name);

while (btSerial->available())
Serial.write(btSerial->read());

Serial.println("\nConfigurando PIN para 6666");
btSerial->write("AT+PIN6666");
btSerial->write(PIN);
delay(1100);
Serial.print("\nNew PIN: ");
Serial.println(PIN);
while (btSerial->available())
Serial.write(btSerial->read());

Serial.println("\nVersao do bluetooth");
btSerial->write("AT+VERSION");
delay(1100);
Serial.print("\nBluetooth version");
while (btSerial->available())
Serial.write(btSerial->read());
}

/**
* Create Bluetooth object with RX = 2 and TX = 3
*/
Bluetooth::Bluetooth(){
setrxPin(2);
settxPin(3);
btSerial = new SoftwareSerial(2, 3);
setName("PNGFramework");
}

/**
* Create Bluetooth objct with RX and TX parameters.
*/
Bluetooth::Bluetooth(int r, int t){
setrxPin(r);
settxPin(t);
setName("PNGFramework");
btSerial = new SoftwareSerial(r, t);
setPIN(6666);
}

/**
Expand Down Expand Up @@ -148,4 +160,22 @@ char * Bluetooth::read(){
*/
void Bluetooth::send(char c[]){
btSerial->print(c);
}

/**
* Get the bluetooth PIN
*/
char *Bluetooth::getPIN(){
return &PIN[0];
}

/**
* Set the bluetooth PIN
* @param pin [description]
*/
void Bluetooth::setPIN(int pin){
char _pin[32];
strcpy(PIN, "AT+PIN");
sprintf(_pin, "%d", pin);
strcat(PIN, _pin);
}
24 changes: 23 additions & 1 deletion Bluetooth/Bluetooth.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/**
*
This file is part of .PNG Arduino Framework.
.PNG Arduino Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
.PNG Arduino Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with .PNG Arduino Framework. If not, see <http://www.gnu.org/licenses/>.
*/



#ifndef bluetooth_h
#define bluetooth_h

Expand All @@ -9,23 +29,25 @@ class Bluetooth{
private:
int rxPin;
int txPin;
char PIN[32];
SoftwareSerial *btSerial;
char msg[1024];
char name[255];
char message_end;

public:
Bluetooth();
Bluetooth(int r, int t);
int getrxPin();
int gettxPin();
char * read();
char * getName();
char getMessageEnd();
char * getPIN();
void setMessageEnd(char end);
void setName(char name[]);
void setrxPin(int rx);
void settxPin(int tx);
void setPIN(int pin);
void setupBluetooth();
void send(char c[]);
};
Expand Down
12 changes: 7 additions & 5 deletions Bluetooth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ PNG Framework was developed by stutents of Unesp - University of the State of S
This module can be used to send and receive bluetooth data. If you want to connect your Arduino with a Bluetooth device, Arduino or any other, check the Helpers module to find some classes that will help you to connect them.
This module was tested with:
* BC417 Bluetooth to Serial
* Bluetooth-RS232 HC-06
* Bluetooth-RS232 HC-05

It'll probably work with similar components. If you test it a component not listed here, please add a comment in my blog, open a pull request or edit this file to share if it's working or if you are getting some error.


## Example

Inclued the libray
Include the library
```c++
#include "Bluetooth.h"
```

Create a new Bluetooth object
```c++
Bluetooth *blue = new Bluetooth();
Bluetooth *blue = new Bluetooth(5, 6);
```

Add the following method in **void setup()**:
Expand All @@ -55,14 +55,14 @@ Now you can use any method on **void loop()**:
## Documentation
**Bluetooth();** - Create a new Bluetooth object
**Bluetooth(int r, int t);** - Create a new Bluetooth object with RX and TX PINs
**int getrxPin();** - Get the RX PIN
**int gettxPin();** - Get the TX PIN
**char * getPIN();** - Get Bluetooth PIN
**char * read();** - Read and receive a message
**char * getName();** - Get the Bluetooth device name
Expand All @@ -81,6 +81,8 @@ Now you can use any method on **void loop()**:
**void send(char c[]);** - Send a message
**void setPIN(int pin);** - Set bluetooth PIN
## Contribute
Expand Down
20 changes: 19 additions & 1 deletion Bluetooth/examples/Chat/Chat.ino
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
# TODO
#include "Bluetooth.h"
#include <SoftwareSerial.h>

Bluetooth *blue = new Bluetooth(2, 3); //RX=2, TX=3

void setup(){
Serial.begin(9600);
blue->setName("Robo1-TCC");
blue->setPIN(1234);
blue->setMessageEnd('#');
blue->setupBluetooth();
}

void loop(){
String msg = blue->read();
if (msg.length() > 0) {
Serial.println(msg);
}
}
8 changes: 5 additions & 3 deletions LEDMatrix/LedMatrixObject.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/**
*
This program is free software: you can redistribute it and/or modify
This file is part of .PNG Arduino Framework.
.PNG Arduino Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
.PNG Arduino Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with .PNG Arduino Framework. If not, see <http://www.gnu.org/licenses/>.
*/


Expand Down
8 changes: 5 additions & 3 deletions LEDMatrix/LedMatrixObject.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/**
*
This program is free software: you can redistribute it and/or modify
This file is part of .PNG Arduino Framework.
.PNG Arduino Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
.PNG Arduino Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with .PNG Arduino Framework. If not, see <http://www.gnu.org/licenses/>.
*/


Expand Down

0 comments on commit 770b2f8

Please sign in to comment.