I just bought a Bluetooth HM-10 module with CC2541 chip. I am powering it with Arduino Uno. I scanned for bluetooth devices with my phone (samsung j3, 2016) and found the module named as BT05. I paired the devices but I could not connect the bluetooth module with any app. I tried to connect it with AMR Voice/BT Voice Control app and LED controller.
The code I used for controlling LED through app came from here: create.arduino.cc/projecthub/user206876468/arduino-bluetooth-basic-tutorial-d8b737
I put the code here aswell:
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data == '1') // Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') // Checks whether value of data is equal to
0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
These are the errors I get:
Maybe you can try if it's work and change the code by urself
#include <SoftwareSerial.h>
SoftwareSerial BT(2, 3); //RX, TX
char val;
void setup() {
Serial.begin(9600);
BT.begin(9600);
Serial.println("BT is ready!");
}
void loop() {
if (BT.available()>0) {
val = BT.read();
Serial.print(val);
}
}
Related
I am trying to send the number "25" from arduino to android application created by using MIT AppInventor2. At the same time, I want to make "on" or "off" a LED by clicking "lock" and "unlock" buttons on the android app. There is no problem making LED status "on" and "off" but I can not read the 25 on the application. I only read 2 or 5. I read 25 very very rarely.
What is the problem about my arduino code or AppInventor blocks?
Arduino code:
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
Serial.println(25);// Here I want to send 25
delay(20);
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
// Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
Edit: Time interval for AppInventor clock is 1000.
i meet a issue. when i send a text message to my arduino board with a shield sim900 its show my message in number mode. who know what is the problem? here is my code. at the bottom i use serial monitor to see my message and it is read the message in number.
`
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
char incoming_char=0;
void setup()
{
Serial.begin(19200); // for serial monitor
SIM900.begin(19200); // for GSM shield
SIM900power(); // turn on shield
delay(25000); // give time to log on to network.
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(1000);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
// blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(1000);
}
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}
void loop()
{
// Now we simply display any text that the GSM shield sends out on the serial monitor
if(SIM900.available() >0)
{
incoming_char=SIM900.read(); //Get the character from the cellular serial port.
Serial.print(incoming_char); //Print the incoming character to the terminal.
if (incoming_char=='a')
{
digitalWrite(4,HIGH);
Serial.println(incoming_char);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
if (incoming_char=='b')
{
digitalWrite(4,LOW);
Serial.println(incoming_char);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
I'm trying to use arduino with Bluetooth module (HC-06).
And also im trying to send/receive data from android with the app (ArduDroid which is in playstore).
But i have the problem while sending program to Arduino uno after successful compilation.
The error Code is when vcc connected to 3.3V
avrdude: stk500_getsync(): not in sync: resp=0x00
When i connected to 5V sometimes error code changes to but usually same as 3.3v
avrdude: stk500_getsync(): not in sync: resp=0x45.
When i unplug the bt device sending program is successful but i cant receive or send anything.
I checked com port and board. Everything is OK.
Please help me to continue my licence project.
Best regards..
The code is as below:
int ledPin = 13;
int state = 0;
int flag = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600); // Default connection rate for my BT module
}
void loop() {
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
if (state == '0') {
digitalWrite(ledPin, LOW);
if(flag == 0){
Serial.println("LED: off");
flag = 1;
}
}
else if (state == '1') {
digitalWrite(ledPin, HIGH);
if(flag == 0){
Serial.println("LED: on");
flag = 1;
}
}
}
You need to unplug the module before uploading the code via USB.
the module uses the same serial pins than the ones used for USB serial, that's where your issue comes from.
uploading code via bt is tricky. it can be done but I've never succeeded myself... :(
I've solved the problem as the same way. Just unplug the bt device and upload. After this step, plug again and connect to dc. Thats OK. working.
The source of the problem is serial ports as i understand. BT module using 9600 port and disconnecting the connection between computer and arduino.
The Goal:
To have the ardiuno check if it's connected to the android with bluetooth. Then to perform an act if it is connected or reconnect if it's not connected.
What I am using:
Bluesmirf silver with arduino uno and note 3
What I've done so far:
[ARDUINO CODE]
The Bluesmirf is in master mode auto connect. The arduino is supposed to check if the android app is sending an H character. If it is that means its connected. If not then it needs to keep re-connecting.
#include <SoftwareSerial.h>
#include <TextFinder.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
boolean running = false;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
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
delay(100);
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
//Check If Connected
if(bluetooth.available()) // If the bluetooth sent any characters
{
//Check if bluetooth recieved an H and store in a value
char val = bluetooth.read();
if(val == 'H')
{
running = true;
}
else if(val != 'H')
{
running = false;
}
}
else if(!bluetooth.available())
{
running = false;
}
//Actions to perform if arduino is connected or not connected
if(running == true)
{
//It's connected so wait 5 seconds
delay(5000);
}
else if(running == false)
{
//It's not connected: Attempt to reconnect
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("C,30196692D7C0");
delay(500);
bluetooth.println("---");
delay(3000);
}
}
[ANDROID CODE]
And this is the method of the android app that sends an H once the app is connected.
private void sendMessage(BluetoothSocket socket, String msg) {
OutputStream outStream;
try {
outStream = socket.getOutputStream();
byte[] byteString = (msg).getBytes();
outStream.write(byteString);
} catch (IOException e) {
Log.d("BLUETOOTH_COMMS", e.getMessage());
}
}
Side Note:
I've tried so many things to get this arduino to check if its connected or not. I only just started programming 3 weeks ago so this is becoming increasingly difficult. Any help would be appreciated.
[UPDATE #1]
I've managed to send an 'h' with the android app with this snippet here:
//call send method to send this character over bluetooth
sendMessage(socket,"h");
//Method used to send 'h' over bluetooth
private void sendMessage(BluetoothSocket socket, String msg) {
OutputStream outStream;
try {
outStream = socket.getOutputStream();
//byte[] byteString = (msg).getBytes();
byte[] byteString = stringToBytesUTFCustom(msg);
outStream.write(byteString);
} catch (IOException e) {
Log.d("BLUETOOTH_COMMS", e.getMessage());
}
}
//Method used to convert
public byte[] stringToBytesUTFCustom(String str) {
char[] buffer = str.toCharArray();
byte[] b = new byte[buffer.length << 1];
for (int i = 0; i < buffer.length; i++) {
int bpos = i << 1;
b[bpos] = (byte) ((buffer[i]&0xFF00)>>8);
b[bpos + 1] = (byte) (buffer[i]&0x00FF);
}
return b;
}
And with arduino I can properly read the 'h' using this snippet.
if (bluetooth.available() > 0) { // if the data came
char incomingByte = bluetooth.read(); // read byte
if(incomingByte == 'h') {
running = true;
}
}
New Problem
I am having trouble telling when the arduino has lost connection with the android app.
I am working with Aurdino and Amarino task. I am able to connect with Aurdino and I can send data to Aurdino from Android app. In the same way how I can access the data from Aurdino which I supposed to send to Android app(accessing data from aurdino means not the same data which I have sent from android app to aurdino). Aurdino can contain different data and I want to access that. Please help me
Regards,
Krishna
If you connect the Bluetooth RX to your Arduino TX (after uploading your sketch), you should be able to the data that is coming from Android with normal Serial operations. Here is an example sketch, that will turn LED on port 13 on if '1' is received otherwise it will be turned off
see this for a tutorial
You can also use a SoftSerial port
char val;
int ledpin = 13;
void setup() {
pinMode(ledpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if( Serial.available() )
{
val = Serial.read();
Serial.println(val);
}
if( val == '1' )
{
digitalWrite(ledpin, HIGH);
} else {
digitalWrite(ledpin, LOW);
}
delay(100);
}