issue122:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue122:python [2017/07/14 10:00] – d52fr | issue122:python [2017/07/16 14:10] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
The first thing we have to do is get the RPi Bluetooth connected to the Arduino. I’m going to assume that you already have the proper bluetooth software installed on your RPi. I’ve installed bluetooth-5.23-2+rpi2, | The first thing we have to do is get the RPi Bluetooth connected to the Arduino. I’m going to assume that you already have the proper bluetooth software installed on your RPi. I’ve installed bluetooth-5.23-2+rpi2, | ||
- | Heureux de voir revoir sur notre projet « Température/ | + | Heureux de vous revoir sur notre projet « Température/ |
- | La première chose que nous devons faire est de connecter le bluetooth | + | La première chose que nous devons faire est de connecter le Bluetooth |
**Once we have it paired and trusted, we need to do some command-line magic to create a virtual serial port named ‘rfcomm1’. | **Once we have it paired and trusted, we need to do some command-line magic to create a virtual serial port named ‘rfcomm1’. | ||
Ligne 23: | Ligne 23: | ||
The big thing you should take away from this is the mac address of the bluetooth device (hopefully the arduino) that you are connected to. You will be using this address in the next command.** | The big thing you should take away from this is the mac address of the bluetooth device (hopefully the arduino) that you are connected to. You will be using this address in the next command.** | ||
- | Une fois que nous avons appairé et garanti, nous devons faire un peu de magie en ligne de commande pour créer un port série virtuel appelé « rfcomm1 ». | + | Une fois que nous l'avons appairé et garanti, nous devons faire un peu de magie en ligne de commande pour créer un port série virtuel appelé « rfcomm1 ». |
Saisissez dans un terminal : | Saisissez dans un terminal : | ||
Ligne 37: | Ligne 37: | ||
pi@raspberrypi: | pi@raspberrypi: | ||
- | De tout ça, la chose importante que vous devriez mettre de côté c'est l' | + | De tout ça, la chose importante que vous devriez mettre de côté c'est l' |
**Now, type: | **Now, type: | ||
Ligne 63: | Ligne 63: | ||
sudo rfcomm bind / | sudo rfcomm bind / | ||
- | où 98: | + | où 98: |
Maintenant, tapez dans le terminal : | Maintenant, tapez dans le terminal : | ||
Ligne 77: | Ligne 77: | ||
pi@raspberrypi: | pi@raspberrypi: | ||
- | Ceci vous indique que vous disposez d'un port série ouvert dans le dispositif bluetooth de l' | + | Cela vous indique que vous disposez d'un port série ouvert dans le dispositif bluetooth de l' |
**Finally, in the terminal type: | **Finally, in the terminal type: | ||
Ligne 139: | Ligne 139: | ||
Terminal ready | Terminal ready | ||
- | Ceci vous permettra d' | + | Ceci vous permettra d' |
- | Vous aurez à répéter la plupart de ce processus | + | Vous devrez |
**However, this series is about using Python, so, next, we’ll write a simple program to communicate with our Arduino. | **However, this series is about using Python, so, next, we’ll write a simple program to communicate with our Arduino. | ||
Ligne 157: | Ligne 157: | ||
The next line uses only some of the parameters available since the others aren’t needed. The important one is the timeout parameter. If we set the timeout to “none”, the system will wait forever (blocking) until the specified number of characters have been received in the .read command. If we set the timeout to 0, the system will return immediately, | The next line uses only some of the parameters available since the others aren’t needed. The important one is the timeout parameter. If we set the timeout to “none”, the system will wait forever (blocking) until the specified number of characters have been received in the .read command. If we set the timeout to 0, the system will return immediately, | ||
+ | |||
+ | Cependant, cette série est sur l' | ||
+ | |||
+ | Un programme simple dans le terminal | ||
+ | |||
+ | Ci-dessous, ce programme très simple en Python nous permettra de communiquer avec l' | ||
+ | |||
+ | Les trois premières lignes importent la bibliothèque pyserial et paramètrent le port série. | ||
+ | |||
+ | import serial | ||
+ | |||
+ | port = "/ | ||
+ | baud = 19200 | ||
+ | |||
+ | La ligne suivante n' | ||
**ser = serial.Serial(port, | **ser = serial.Serial(port, | ||
Ligne 177: | Ligne 192: | ||
ser.close() | ser.close() | ||
exit()** | exit()** | ||
+ | | ||
+ | ser = serial.Serial(port, | ||
+ | |||
+ | Dans les deux lignes suivantes, (plus le commentaire), | ||
+ | |||
+ | # open the serial port - ouverture du port série | ||
+ | |||
+ | if ser.isOpen(): | ||
+ | | ||
+ | |||
+ | Le moment est venu pour le plat de résistance du programme. Nous commençons une boucle « éternelle », envoyant une demande d' | ||
+ | |||
+ | while True: | ||
+ | cmd = raw_input(" | ||
+ | # for Python 2 | ||
+ | # cmd = input(" | ||
+ | # for Python 3 | ||
+ | if cmd == ' | ||
+ | ser.close() | ||
+ | exit() | ||
**Now that there is something in the cmd variable, we encode it to ascii, append a CRLF to it, and then write it to the serial port. We then use the ser.readlines() command to get the data from our Arduino. We actually have several options here to read from the serial port. If we had used ser.read(), we would have had to specify the number of bytes to be received. Since that can change depending on the command we send, that option isn’t really a good one without a lot of work. We also could have used the ser.readline() command, but that only reads up to the CRLF being sent in, so while good for the “T” or “H” command, the “A” command which sends two lines, would have some problems. The timeout value we set earlier is important, since the system would block until the CRLF is returned. | **Now that there is something in the cmd variable, we encode it to ascii, append a CRLF to it, and then write it to the serial port. We then use the ser.readlines() command to get the data from our Arduino. We actually have several options here to read from the serial port. If we had used ser.read(), we would have had to specify the number of bytes to be received. Since that can change depending on the command we send, that option isn’t really a good one without a lot of work. We also could have used the ser.readline() command, but that only reads up to the CRLF being sent in, so while good for the “T” or “H” command, the “A” command which sends two lines, would have some problems. The timeout value we set earlier is important, since the system would block until the CRLF is returned. | ||
Ligne 191: | Ligne 226: | ||
We’ll see you next month.** | We’ll see you next month.** | ||
+ | |||
+ | Maintenant qu'il y a quelque chose dans la variable cmd, nous l' | ||
+ | |||
+ | else: | ||
+ | ser.write(cmd.encode(' | ||
+ | out = ser.readlines() | ||
+ | for l in out: | ||
+ | print l | ||
+ | |||
+ | Le code source (court s'il en est) est disponible sur pastebin à https:// | ||
+ | |||
+ | Voilà ! C'est tout pour ce mois-ci. Nous avons successivement créé le circuit du capteur de température et d' | ||
+ | |||
+ | Au mois prochain. | ||
issue122/python.1500019239.txt.gz · Dernière modification : 2017/07/14 10:00 de d52fr