samedi 5 avril 2014

arduinoBT rfcomm serial communication


Vote count:

0




I am experiencing problems while trying to send serial data via bluetooth from ArduinoBT to a computer with a bluetooth dongle connected and running Ubuntu 12.04.


On the computer i created a virtual serial port using rfcomm which creates a device /dev/rfcomm0. Programming the arduinoBT borad through via bluetooth works without a problem (also using the /dev/rfcomm0 serial port).


On the Arduino, i am reading an analog value from the ADC and send it via serial. I used this example in order to establish the communication between ArduinoBT and computer. The code was modified a bit in order to actually send the value read from the ADC.



int sensorValue=0;
int sensorPin=A0;

void setup()
{
Serial.begin( 115200 );
}

void loop()
{
// Listen for data on the serial connection
if ( Serial.available() > 1 )
{

sensorValue = analogRead(sensorPin);
Serial.println(sensorValue );
delay(100);
}
}


Python code was also modified (skip writing, just read from device)



import serial
bluetoothSerial = serial.Serial( "/dev/rfcomm0", baudrate=115200 )
while True:
print bluetoothSerial.readline()


I run the python code and nothing happened. The program seems to run, but no data is printed. Runnning just the rfcomm command in another terminal shows that the serial port is connected



rfcomm0: xx:xx:xx:xx:xx:xx channel 1 connected [tty-attached]


Where xx:xx:xx:xx:xx:xx is the bluetooth mac address.


I get a similar response if i just use cat /dev/rfcomm0 It just returns after a second or so. Again, in another terminal, i tried to run the rfcomm command 2-3 times during that second, and the result was



rfcomm0: xx:xx:xx:xx:xx:xx channel 1 connecting [tty-attached]
rfcomm0: xx:xx:xx:xx:xx:xx channel 1 closed


However, if i use the



minicom -s


and configure to read the /dev/rfcomm0, the data starts to stream in. Once the connection is closed, the same process cannot be repeated without resetting the arduinoBT board first. The response i get if i try this is



minicom: Cannot open /dev/rfcomm0: No such file or directory


I get a similar response when running the python script.


It seems like the problem is on the ArduinoBT side, as if the bluetooth module is not configured correctly, since once the arduino is reset, the process can be repeated, and, ith minicom, the data sreams in again.


I also found another thing that confirms the problem is on the arduino side. If i use



hcitool scan


The arduino bluetooth is visible. However, if i modify the arduino code to



void loop()
{
// Listen for data on the serial connection
//if ( Serial.available() > 1 )
//{

sensorValue = analogRead(sensorPin);
Serial.println(sensorValue );
delay(100);
//}
}


commenting out the if(Serial.available() > 1), arduino is only visible after reset for a while. Also cat /dev/rfcomm0


gives a different result, it returns an error saying either host is down or connection refused. Similar thing happens with the python code.


So my question is, how should the bluetooth/arduino/serial (or whatever else is causing the problem) be configured in order to have a smooth serial communication without having to reset the arduino every time and to make serial work also with python or cat.



asked 1 min ago

Jaka

70





Aucun commentaire:

Enregistrer un commentaire