简体   繁体   中英

bluetooth error(11,resource are temporarily unavailable)

when I running program on raspberry pi to send data to Arduino it work properly but stopped sending data suddenly and return back an error

error message "

socket.send('0') 
bluetooth error(11,resource are temporarily unavailable)

this program aims to send 0 to Arduino if Arduino receives 0 buzzers will not alarm else it alarm ..for 2 minutes all things going well but suddenly buzzer alarm but the 2 Bluetooth in 'pi' and 'Arduino' still connected not disconnected.

I search for the error and find it is because the buffer in pi is full and it turns into the block but I can't solve the problem anyone can help me? thanks.

here is the code

import bluetooth
import time
bd_addr = "98:D3:31:FB:19:AF"
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
sock.connect((bd_addr,port)) 
while 1:
        sock.send("0")
time.sleep(2)
sock.close()

arduino code

#include <SoftwareSerial.h>
SoftwareSerial bt (5,6);  
int LEDPin = 13; //LED PIN on Arduino 
int btdata; // the data given from the computer  
void setup()  {   bt.begin(9600);   pinMode (LEDPin, OUTPUT); }
void loop() {   
    if (bt.available()) {
        btdata = bt.read();
        if (btdata == '1') {
            //if 1
            digitalWrite (LEDPin,HIGH);
            bt.println ("LED OFF!");
         }
         else {
             //if 0
             digitalWrite (LEDPin,LOW);
             bt.println ("LED ON!");
         }   
    } else {   digitalWrite(LEDPin, HIGH);
            delay (100); //prepare for data   
    } 
}

I think you are flooding it, as you have no delay in your while. You are generating data to send faster than it can actually send them, which eventually fills up the buffer. Simply add a time.sleep(0.5) in your while and decrease the value 0.5 by testing which one works best without filling up the buffer.

This is my try at making your code more resilient :

import bluetooth
import time
bd_addr = "98:D3:31:FB:19:AF"
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
sock.connect((bd_addr,port)) 
while 1:
    try:
        sock.send("0")
        time.sleep(0.1)
        sock.recv(1024)
    except bluetooth.btcommon.BluetoothError as error:
        print "Caught BluetoothError: ", error
        time.sleep(5)
        sock.connect((bd_addr,port)) 
time.sleep(2)
sock.close()

What this does is :

  • Wait a little before sending a new packet : Prevent your computer from generating data faster than it can send it, which eventually fills the buffer

  • Read the incomming data, thus consuming it from the inbound buffer : the arduino actually answers your requests, and that fills up the inbound buffer. If you don't empty it once in a while, it will overflow and your socket will be rendered unusable

  • Monitor for connection errors, and tries to recover from them by closing and reopening the socket

I would also modify the arduino code like this :

#include <SoftwareSerial.h>
SoftwareSerial bt (5,6);  
int LEDPin = 13; //LED PIN on Arduino 
int btdata; // the data given from the computer  
void setup()  {   bt.begin(9600);   pinMode (LEDPin, OUTPUT); }
void loop() {   
    if (bt.available()) {
        btdata = bt.read();
        if (btdata == '1') {
            //if 1
            digitalWrite (LEDPin,HIGH);
            bt.println ("LED OFF!");
         }
         else {
             //if 0
             digitalWrite (LEDPin,LOW);
             bt.println ("LED ON!");
         }
         delay(1000);   
    } else {   digitalWrite(LEDPin, HIGH);
            delay (10); //prepare for data   
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM