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);
}
Related
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);
}
}
so i'm currently developing an android service which implements the HFP profile for later use with a gui , i was able to successfully and easily implement the RFCOMM part where the AT commands like ATA(accep call) are sent , but i am stuck with accepting the audio SCO Connection on the app . so basically im testing with an Iphone AG Role and an android tablet HF Role which runs my app , to open the SCO connection i have tried calling AudioMAnager.startBluetoothSco(); without any luck , and even made a bluetooth SCO socket server in C using the ndk , which listens for a connection. but the actual problem is that the Iphone doesnt seem to try to connect with the sco socket , so i dumped the trafic from the android tablet and saw that when the Iphone requests the SCO connection the android hci automatically responds with Reject of reason: Connection Rejected due to Limited Resources (0x0d) , no matter what i do, maybe im missing something? any ideas? thanks. forgot to mention that both devices are paired using the os settings app and the connection is established by connecting to the android tablet from the native settings app of ios.
AudioHandler.java
public class AudioHandler implements Runnable{
static
{
System.loadLibrary("libsco");
}
#Override
public void run()
{
AudioManager amanager = (AudioManager) Common.APPCONTEXT.getSystemService(Context.AUDIO_SERVICE);
amanager.setMode(AudioManager.MODE_IN_COMMUNICATION);
amanager.startBluetoothSco();
amanager.setBluetoothScoOn(true);
if(amanager.isBluetoothScoOn())
{
int status= this.SCOINIT();
Log.d("SCO","SCOFinished");
}
/*while(Common.isCallActive)
{
play(stream.readbytes());
}
this.SCOCLOSE();
amanager.stopBSCO();
*/
}
private native int SCOINIT();}
libsco.c
void init() {
struct sockaddr_sco addr = {0}, remoteadress = {0};
int SCOServer, SCOClient;
SCOServer = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
if (SCOServer < 0) {
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "SCO socket create failed.");
print("SCO socket create failed.");
}
addr.sco_family = AF_BLUETOOTH;
bacpy(&addr.sco_bdaddr, BDADDR_ANY);
if (bind(SCOServer, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
print("failed to bind sco.");
}
if (listen(SCOServer, 1)) {
print("Listening Failed!!");
} else {
print("Listening for SCO connection.");
}
socklen_t addrlength = sizeof(remoteadress);
SCOClient = accept(SCOServer, (struct sockaddr *) &remoteadress, &addrlength);
if (SCOClient < 0) {
print("Accept Failed!!");
close(SCOClient);
} else {
print("Conected.\n");
close(SCOClient);
}
close(SCOServer);
}
everything runs without errors and i can see the "LIstening for SCO Connection " line, but it never accepts because the android hci rejects the connection before anything can be done...
screenshot from wireshark
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.