issue122:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue122:python [2017/07/04 15:26] – créée auntiee | issue122:python [2017/07/16 14:10] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Welcome back to our ‘Temp/ | + | **Welcome back to our ‘Temp/ |
- | 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 vous revoir sur notre projet « Température/ | ||
+ | |||
+ | La première chose que nous devons faire est de connecter le Bluetooth du RPi à l' | ||
- | 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’. |
In a terminal type: | In a terminal type: | ||
Ligne 17: | Ligne 21: | ||
pi@raspberrypi: | pi@raspberrypi: | ||
- | 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 l' | ||
+ | |||
+ | Saisissez dans un terminal : | ||
+ | |||
+ | hcitool scan | ||
+ | |||
+ | Vous devriez voir quelque chose comme : | ||
+ | |||
+ | pi@raspberrypi: | ||
+ | |||
+ | Scanning ... | ||
+ | 98: | ||
+ | pi@raspberrypi: | ||
+ | |||
+ | De tout ça, la chose importante que vous devriez mettre de côté c'est l' | ||
- | Now, type: | + | **Now, type: |
sudo rfcomm bind / | sudo rfcomm bind / | ||
Ligne 37: | Ligne 57: | ||
pi@raspberrypi: | pi@raspberrypi: | ||
- | This tells you that you now have an open serial port into the Arduino bluetooth device. | + | This tells you that you now have an open serial port into the Arduino bluetooth device.** |
+ | |||
+ | Maintenant, tapez : | ||
+ | |||
+ | sudo rfcomm bind / | ||
+ | |||
+ | où 98: | ||
+ | |||
+ | Maintenant, tapez dans le terminal : | ||
+ | |||
+ | rfcomm | ||
+ | |||
+ | À nouveau, vous devriez voir quelque chose comme ceci : | ||
+ | |||
+ | pi@raspberrypi: | ||
+ | |||
+ | rfcomm1: 98: | ||
+ | |||
+ | pi@raspberrypi: | ||
+ | |||
+ | 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: |
sudo picocom -c / | sudo picocom -c / | ||
Ligne 69: | Ligne 109: | ||
This will allow you to send a “T” for temperature, | This will allow you to send a “T” for temperature, | ||
- | You will have to repeat most of this process each time you reboot the RPi. I made a shell script that contained those lines so I could run it as needed. | + | You will have to repeat most of this process each time you reboot the RPi. I made a shell script that contained those lines so I could run it as needed.** |
+ | |||
+ | Enfin, saisissez dans le terminal : | ||
+ | |||
+ | sudo picocom -c / | ||
+ | |||
+ | Dans le terminal, vous devriez voir : | ||
+ | |||
+ | rfcomm1: 98: | ||
+ | |||
+ | picocom v1.7 | ||
+ | |||
+ | port is : / | ||
+ | flowcontrol | ||
+ | baudrate is : 9600 | ||
+ | parity is : none | ||
+ | databits are : 8 | ||
+ | escape is : C-a | ||
+ | local echo is : yes | ||
+ | noinit is : no | ||
+ | noreset is : no | ||
+ | nolock is : no | ||
+ | send_cmd is : sz -vv | ||
+ | receive_cmd is : rz -vv | ||
+ | imap is : | ||
+ | omap is : | ||
+ | emap is : crcrlf, | ||
+ | |||
+ | Terminal ready | ||
+ | |||
+ | Ceci vous permettra d' | ||
+ | |||
+ | Vous devrez répéter la plupart de ce processus chaque fois que vous redémarrerez le RPi. J'ai fait un script shell qui contient ces lignes, de sorte que je peux le lancer à la demande. | ||
- | 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. |
A simple terminal Program | A simple terminal Program | ||
Ligne 84: | Ligne 156: | ||
baud = 19200 | baud = 19200 | ||
- | 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 | ||
- | ser = serial.Serial(port, | + | port = "/ |
+ | baud = 19200 | ||
+ | |||
+ | La ligne suivante n' | ||
+ | |||
+ | **ser = serial.Serial(port, | ||
In the next two lines (plus the comment), we are simply verifying the serial port is open and then printing that fact. | In the next two lines (plus the comment), we are simply verifying the serial port is open and then printing that fact. | ||
Ligne 96: | Ligne 183: | ||
Now comes the meat of the program. We start a “forever” loop, sending out a prompt to enter a command or to enter ‘exit’ to quit, and wait for input from the user. This works perfectly for our purposes, since the Arduino is also waiting for a command. It also allows us to terminate the program properly. | Now comes the meat of the program. We start a “forever” loop, sending out a prompt to enter a command or to enter ‘exit’ to quit, and wait for input from the user. This works perfectly for our purposes, since the Arduino is also waiting for a command. It also allows us to terminate the program properly. | ||
+ | |||
+ | while True: | ||
+ | cmd = raw_input(" | ||
+ | # for Python 2 | ||
+ | # cmd = input(" | ||
+ | # for Python 3 | ||
+ | if cmd == ' | ||
+ | ser.close() | ||
+ | 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: | while True: | ||
Ligne 106: | Ligne 213: | ||
exit() | 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. |
else: | else: | ||
Ligne 118: | Ligne 225: | ||
So, that’s it for this month. We’ve successfully created the Temp and Humidity sensor circuit and are now able to read its values with a remote Raspberry Pi using Python. | So, that’s it for this month. We’ve successfully created the Temp and Humidity sensor circuit and are now able to read its values with a remote Raspberry Pi using Python. | ||
- | 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.1499174814.txt.gz · Dernière modification : 2017/07/04 15:26 de auntiee