0

I am working on a Bluetooth project involving one Arduino (with Seeed bluetooth shield v2.0) and one ubuntu laptop. Basically, I want message exchanges between the Arduino and the laptop. I paired the Arduino bluetooth shield with the laptop. Then I use the code below (on the laptop) to test. The Arduino is set as a Slave. And the laptop sends a test message.

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv){
  struct sockaddr_rc addr = {0};
  int s, status;
  char buf[1024] = {0};
  char dest[18] = "00:0E:EA:CF:1E:62";

  for (size_t i = 1; i <= 30; i++) {
    addr.rc_channel = i;
    str2ba(dest, &addr.rc_bdaddr);
    // connect to server
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

    if(status == 0) {
      status = send(s, "Hello!", 6, 0);
      status = recv(s, buf, sizeof(buf), 0);
      if(status > 0)
        printf("received %s\n", buf);

      break;
    }
  }

  if(status < 0)
    perror("send error");

  close(s);
  return 0;
}

Below is the test code at Arduino side.

#include <SoftwareSerial.h>   //Software Serial Port
#define RxD 7
#define TxD 6

SoftwareSerial bt(RxD,TxD);
char buf[100];
size_t idx;

void setup() {
  Serial.begin(9600);
  bt.begin(9600); 
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
}

void loop() {
  Serial.println("Waiting ...");
  idx = 0;
  memset(buf, sizeof(buf), 0);
  while(bt.available()){
    buf[idx] = bt.read();
    idx++;
  }

 while(idx >= 0){
    bt.write(buf[idx]);
    idx--;
  }

  delay(1000);
}

void setupBlueToothConnection() { 
  bt.print("AT");
  delay(400); 

  bt.print("AT+DEFAULT"); // Restore all setup value to factory setup
  delay(2000); 

  bt.print("AT+LADD?"); // Restore all setup value to factory setup
  delay(2000);

  bt.print("AT+NAMEProver"); // set the bluetooth name as "SeeedBTSlave"
  delay(400);

  bt.print("AT+PIN0000"); // set the pair code to connect 
  delay(400);

  bt.print("AT+ROLE?");  
  delay(400);

  bt.print("AT+AUTH0");  
  delay(400);      

  bt.flush();
}

I receive error message: "send error: Connection refused". What is the problem? Can some help me with this? Thanks!

Update: I guess it might be the problem with port number. But I checked the datasheet for Seeed Bluetooth shield v2.0 and has not found any clue regarding to setup port number.

Sissi
  • 65
  • 1
  • 10
  • Either your server isn't there or there is no route to it. There's not nearly enough information about your entire system here and it's most likely a networking issue. – xaxxon Sep 21 '16 at 02:55

1 Answers1

0

Most common problem with Bluetooth on Arduino except for code is having Arduino connected to your PC over USB cable and trying to use Bluetooth, as far as I am aware most of the shields connect directly to hardware RX and TX of the Arduino board, which are the same ports used for USB communication to your PC.

So is your Arduino connected over a USB port to your PC?

  • After uploading the sketch to the Arduino, I unplug the Arduino from the PC and use a wall outlet to power it. But I still get same issue. I am getting lost. – Sissi Sep 21 '16 at 22:03