I'm a new bie on arduino projects. I would like to ask you for some help. I bought a BLE Shield for Arduino from ( http://imall.iteadstudio.com/development-platform/arduino/shields/im130704001.html ). They made this shield using Hm-10 Bluetooth module(http://www.jnhuamao.cn/bluetooth.asp?ID=1). Itead Studio has no sample codes using this shield. I have no idea on how to program it or send AT commands from Arduino.
I read the “AT commands” at the data sheet (ftp://imall.iteadstudio.com/Shield/IM130704001_ITEAD_BLE_Shield/DS_IM130704001_ITEAD_BLE_Shield.pdf) and I tried to send "AT commands” from arduino to BLE shield using this code ( http://arduino.cc/en/Reference/SoftwareSerial ) but I only received the commands back.
Did anybody here ever use this HM-10 bluetooth module ?
I need some arduino sketch for help !
#include <SoftwareSerial.h>
int led = 13;
int bluetoothTx = 2;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] ={4800,9600,14400,19200,28800,38400,57600,115200};
int i = 1;
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
while(!Serial){}
Serial.write("AT sent");
delay(500);
bluetooth.write("AT+NAME?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available())
{
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+NAMEFlightline"); //Check Status
delay(500);
while (bluetooth.available()) {
Serial.write((char)bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+CHAR0x2901"); //add charicteristic
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+RELI0");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+SHOW1");
delay(100);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
pinMode(led,OUTPUT);
digitalWrite(led,HIGH);
}
void testAllBaudRates(){
for(int j=0; j<8; j++)
{
bluetooth.begin(baudrate[j]);
delay(100);
Serial.println("boud rate " + String(baudrate[j],DEC) +" i-> "+String(j,DEC));
// Serial.println("");
bluetooth.write("AT");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
Serial.println();
}
delay(100);
}
}
// and now a few blinks of the LED,
// so that we know the program is running
void loop()
{
//Read from bluetooth and write to usb serial
while(bluetooth.available())
{
char toSend = (char)bluetooth.read();
if(toSend == 'x'){
digitalWrite(led,HIGH);
Serial.println("set high");
bluetooth.write("RXOK");
}else if(toSend == 'y'){
digitalWrite(led,LOW);
Serial.println("set low");
bluetooth.write("RXOK");
}
Serial.print(toSend);
}
//Read from usb serial to bluetooth
while(Serial.available())
{
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
}
Have a look at my sketch above I have a few things to point out that I wasted time on.
make sure you have the line
while(!Serial){}
Or you may get have a working shield but miss the responses as the serial monitor is no ready.
remember that you wont get a response from the blue-tooth module, with a command from the Serial Monitor if it is connected to a device. It is connected to the device when the light stops flashing.
if you run this sketch you should get this output
AT sent
OK+Set:3
OK+Get:0x2901 <- this may be blank the first time you run it
OK+Set:Flightline
OK+Set:0x2901
OK+Set:0
OK+Set:1
the most comprehensive list of AT commands can be found here
[All the AT commands and a good explanation][1]
You will need to at Characteristics to the device as I have done here
bluetooth.write("AT+CHAR?");
or you will find it to connect to iOS and Android
If you are connecting to Android use the BluetoothLE Classes not Bluetooth ones.
You can use this sketch with baud rate autodetect to control your HM-10. This is a part of Apploader project that allows to upload to Arduino board over BLE.
This is a little late too, but try the following code, if you send it "AT" it should give you back an "OK":
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}
Related
I am trying to connect my MSP432 to my HC-05 Bluetooth module however when I connect to the module using a Bluetooth terminal, I receive no feedback. I've attached my code as well. Is the RX pin P3.2 and TX P3.3?
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
pinMode(12, OUTPUT);
}
void loop() {
if (Serial.available()) /* If data is available on serial port */
{
char data_received;
data_received = Serial.read(); /* Data received from bluetooth */
if (data_received == '1')
{
digitalWrite(12, HIGH);
Serial.write("LED turned ON");
}
else if (data_received == '2')
{
digitalWrite(12, LOW);
Serial.write("LED turned OFF");
}
else
{
Serial.write("Select either 1 or 2");
}
}
}
wondering if anyone would know why is this happening.
I have HM-10 Bluetooth module that is connected to Arduino. I use Serial Bluetooth Terminal for communicating with the HM-10 module.
The code below works perfectly fine with Arduino UNO,
the LED is turned ON/OFF
I receive messages in Serial monitor
I receive messages on my mobile phone
However if I use the same sketch and the same scheme with Arduino NANO
the LED is turned ON/OFF
I receive messages in Serial monitor
I do NOT receive any message on my mobile phone. I tried almost all other pins including the TX1 & RX0pins but without any luck. It simply doesn't send any data to the RXD pin of the HM-10 module.
Is this some kind of limitation of Arduino Nano or do I have a faulty one?
// Arduino Bluetooth modul HM-10
#define RX 11
#define TX 10
#define pinLED 13
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(TX, RX);
void setup() {
Serial.begin(9600);
Serial.println("Arduino on");
bluetooth.begin(9600);
bluetooth.print("Arduino ON");
pinMode(pinLED, OUTPUT);
}
void loop() {
byte BluetoothData;
if (bluetooth.available() > 0) {
BluetoothData=bluetooth.read();
switch (BluetoothData) {
case '0':
digitalWrite(pinLED, LOW);
Serial.println("LED turned OFF");
bluetooth.println("LED turned OFF");
break;
case '1':
digitalWrite(pinLED, HIGH);
Serial.println("LED turned ON");
bluetooth.println("LED turned ON");
break;
default:
Serial.println("Unknown command");
bluetooth.println("Unknown command");
}
}
delay(100);
}
I'm facing problem with my Arduino Bluetooth controller car.
I'm only getting reverse question marks.
I'm using an HC-05 Bluetooth module. The pins are connected as following:
HC 05 -> Arduino
RX -> TX
TX -> RX
5V -> 5V
GND -> GND
Ardunio code:
#include <AFMotor.h>
AF_DCMotor right_motor(3, MOTOR12_8KHZ);
AF_DCMotor left_motor(4, MOTOR12_8KHZ);
String readString;
void setup() {
Serial.begin(9600);
right_motor.setSpeed(250);
left_motor.setSpeed(250);
}
void loop() {
while(Serial.available()){
delay(50);
char c=Serial.read();
readString+=c;
}
if(readString.length()>0){
Serial.println(readString);
if (readString =="FORWARD"){
right_motor.run (FORWARD);
left_motor.run (FORWARD);
delay(500);
}
if (readString =="BACKWARD"){
right_motor.run (BACKWARD);
left_motor.run (BACKWARD);
delay(500);
}
if (readString =="LEFT"){
right_motor.run (FORWARD);
left_motor.run (BACKWARD);
delay(500);
}
if (readString =="RIGHT"){
right_motor.run (BACKWARD);
left_motor.run (FORWARD);
delay(500);
}
if (readString =="STOP"){
right_motor.run (RELEASE);
left_motor.run (RELEASE);
delay(500);
}
readString="";
}
}
Serial monitor:
I have tried many types of changes in the code, but they are not working.
The baud rates must be identical between Bluetooth module and Arduino Serial port. You can check the Bluetooth module baud rate by entering the following AT command:
AT+UART?, also, you can change it using the following AT command: AT+UART=desired_baud_rate, stop_bit, parity_bit,\r\n.
Moreover, you can't use Serial.read() then compare the output with "STRING"! Instead, use Serial.readString();.
According to most documentation, the default baud rate for HC-05 is 9600, or 38400 for AT command mode. Try:
Serial.begin(57600);
I've got a half dozen HC-05 which all shipped with a default set to 57600 baud.
(I'm posting this answer in part because I just spent hours re-discovering this.)
I'm having trouble communicating between Tera Term and an Arduino Mega over a bluetooth connection. My goal is to be able to set up the Mega so it can be later used to exchange text commands with a C++ application. Using the code I've found on this site, I can use the Arduino IDE Serial Monitor to send text to the Tera Term terminal, but I cannot send text from the Tera Term terminal to the Arduino. It never recognizes text was sent from the terminal. The bluetooth module I am using is the Bluetooth Mate Gold from SparkFun. The code's purpose is to detect incoming chars and then activate an LED. My code is shown below:
#include <SoftwareSerial.h>
int bluetoothTx = 15;
int bluetoothRx = 14;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
pinMode(13, OUTPUT);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(115200);
bluetooth.print("$$$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
}
void loop() {
//Read from bluetooth and write to usb serial
if(bluetooth.available()) {
char toSend = (char)bluetooth.read();
Serial.print(toSend);
flashLED();
}
//Read from usb serial to bluetooth
if(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.print(toSend);
flashLED();
}
}
void flashLED() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
}
The only thing that seems to work from Tera Term is entering command mode using "$$$." Doing that, I can run commands such as "D." I'm not sure why I can't send chars from Tera Term to the Arduino and have them be read. Any suggestions are appreciated.
I just built the same thing and i had to add a delay of 500 ms before configuring the module. I also had problems with receiving data since i used pin that did not support the PCINT interrupt.
delay(500);
bluetooth.begin(115200);
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
Its my first time to use gsm shield to my arduino so I'm kinda confused so i need directions. My aim is to read the message sent to my gsm shield then compare that message into a specific string. if they are the same, arduino will do something. Example is, the GSM shield received a text message containing STATUS, the arduino will do something. The problem I'm stuck up with now is how to read the incoming characters from the gsm module into a single string then compare that string into a specific word. I have this code as of now.
#include <SoftwareSerial.h>
#include <String.h>
char inchar[255];
SoftwareSerial cell(2,3);
int led1 = 22;
#define powerOn 4
int i;
//char comparestring[160];
char command[]={'S','T','A','T','U','S','\0'}; // this is a string for command ended with null terminator
void setup()
{
// ilagay sa loob ng setup
digitalWrite(powerOn, HIGH);
delay(1500);
digitalWrite(powerOn, LOW);
delay(5000);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
Serial.begin(9600);
cell.b-egin(9600);
delay(30000);
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=1,2,0,0,0 "); // set module to send SMS data to serial out upon receipt
delay(200);
Serial.println("GSM SHIELD IS NOW OK AND READY");
}
void loop()
{
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
{
digitalWrite(led1, HIGH);
cell.write("AT+CMGS=\"");
cell.write("09267955775");
cell.write("\"\r");
delay(1000);
cell.write("\nTerminal Monitoring System");
delay(1000);
cell.write(0x1A); // End the SMS with a control-z
}
else
{
Serial.println("\nInvalid Keyword! Type ?");
dig-italWrite(led1, LOW);
}
}
}
these codes are the codes that supposed to do the trick but i think its not working. I hope you can teach me the right way. Thank you!
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
inchar is a string so try using strcmp(inchar,command) for comparing the two.