Outils pour utilisateurs


Connecter un arduino a un PC avec un liaison bluetooth sous python

Installation

J' ai commencé par installer pybluez .

Premier test réception

Puis j'ai mis le code suivant dans le arduino

bluetooth.pde
/*
simple test
*/
 
 
void setup()
{
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
 
delay(1000);                  // waits for a second  
Serial.println("13 off");
  }

J' ai utlisé python asynchronous-inquiry.py fourni avec pybluez dans examples/simple , qui ma retourné :

00:06:66:42:1F:D8
RN42-1FD8
Uncategorized
services:

Pour l' instant j' arrive a me connecter avec le code suivant :

ArduinoBT.py
# file: ArduinoBT.py
# desc: Demo for Arduino bluetooth module.
# $Id: ArduinoBT.py 524 2011-12-20 15:04:52 Zebulon $
 
import sys
import bluetooth
 
sock=bluetooth.BluetoothSocket()
 
if len(sys.argv) < 2:
    print "usage: ArduinoBT.py <addr>"
    sys.exit(2)
 
bt_addr=sys.argv[1]
port = 1
 
print "trying to connect to %s on PSM 0x%X" % (bt_addr, port)
 
sock.connect((bt_addr, port))
 
print "connected.  type stuff"
while True:
    #data = raw_input()
    #if(len(data) == 0): break
    #sock.send(data)
 
    data = sock.recv(1000)
    print "Data received:",data
 
sock.close()

Ce qui me renvoie :

Data received: 13 off
Data received: 13
Data received:  off
Data received: 1
Data received: 3 off
Data received: 13 off
Data received: 

Et ainsi de suite.

Deuxième test reception

En envoyant 1 ou 0 on allume la LED 13

BtPrint.pde
/*
simple LED test
*/
 
char val;         // variable to receive data from the serial port
int ledpin = 2;  // LED connected to pin 2 (on-board LED)
 
void setup()
{
  pinMode(ledpin = 13, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
 
    val = Serial.read();         // read it and store it in 'val'
 
  if( val == '0' )               // if '0' was received led 13 is switched off
 
  {
   digitalWrite(ledpin, LOW);    // turn Off pin 13 off
   delay(1000);                  // waits for a second  
//Serial.println("13 off");
  }
 
if( val == '1' )               // if '1' was received led 13 on
 {
    digitalWrite(ledpin, HIGH);  // turn ON pin 13 on
    delay(1000);                  // waits for a second
 //   Serial.println("13 on");
  }
}

Le code python est le même seul deux ou trois lignes avec # ont été interverties .

ArduinoBT2.py
# file: ArduinoBT.py
# desc: Demo for Arduino bluetooth module.
# $Id: ArduinoBT.py 524 2011-12-20 15:04:52 Zebulon $
 
import sys
import bluetooth
 
sock=bluetooth.BluetoothSocket()
 
if len(sys.argv) < 2:
    print "usage: ArduinoBT.py <addr>"
    sys.exit(2)
 
bt_addr=sys.argv[1]
port = 1
 
print "trying to connect to %s on PSM 0x%X" % (bt_addr, port)
 
sock.connect((bt_addr, port))
 
print "connected.  type stuff"
while True:
    data = raw_input()
    if(len(data) == 0): break
    sock.send(data)
 
    #data = sock.recv(1000)
    #print "Data received:",data
 
sock.close()