Outils pour utilisateurs

Outils du site


issue122:python

Ceci est une ancienne révision du document !


Welcome back to our ‘Temp/Humidity via Bluetooth’ project. As you will remember, last month we got everything working to a smartphone. This month, we will be working on getting things up and running on the Raspberry Pi. 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, bluez-5.23-2+rpi2, bluez-firmware-1.2-3+4rpi1, pi-bluetooth-0.1.3 and pulseaudio-module-bluetooth-5.0-13 on my RPi3. I’m sure that some of those are not needed for basic bluetooth communications. There are many places on the web that can explain the process. Once you get everything installed, connect to your Arduino bluetooth device. I had to use “1234” during the pairing process. I also marked it as a trusted device.

Heureux de voir revoir sur notre projet « Température/humidité via Bluetooth ». Comme vous vous en souvenez, nous avons tout mis au point sur un smartphone. Ce mois-ci, nous allons travailler

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: hcitool scan You should see something like: pi@raspberrypi:~ $ hcitool scan Scanning … 98:D3:31:30:49:B1 HC-06 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.

Now, type: sudo rfcomm bind /dev/rfcomm1 98:D3:31:30:49:B1 Where the 98:D3:31:30:49:B1 is the mac address of the bluetooth H05/06 module on the Arduino that was obtained from the hcitool command above. Yours will be different, so use it. Now in the terminal type: rfcomm Again, you should see something like this: pi@raspberrypi:~ $ rfcomm rfcomm1: 98:D3:31:30:49:B1 channel 1 clean pi@raspberrypi:~ $ This tells you that you now have an open serial port into the Arduino bluetooth device.

Finally, in the terminal type: sudo picocom -c /dev/rfcomm1 In the terminal, you should see: rfcomm1: 98:D3:31:30:49:B1 channel 1 clean picocom v1.7 port is : /dev/rfcomm1 flowcontrol : none 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,delbs, Terminal ready This will allow you to send a “T” for temperature, an “H” for humidity, or an “A” for both. 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.

However, this series is about using Python, so, next, we’ll write a simple program to communicate with our Arduino. A simple terminal Program Below is a very simple program in Python that will allow us to communicate to the Arduino via the serial port. The first three lines import the pyserial library and set up the port. import serial port = “/dev/rfcomm1” 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, returning 0 or more characters from the port. We, however will set the timeout to 1 second. You can specify the number of seconds for the system to wait and you can use a float value.

ser = serial.Serial(port, baud, timeout=1) In the next two lines (plus the comment), we are simply verifying the serial port is open and then printing that fact. # open the serial port if ser.isOpen(): print(ser.name + ' is open…') 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(“Enter command or 'exit':”) # for Python 2 # cmd = input(“Enter command or 'exit':”) # for Python 3 if cmd == 'exit': 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. else: ser.write(cmd.encode('ascii')+'\r\n') out = ser.readlines() for l in out: print l The source (short as it is) is available on pastebin at https://pastebin.com/tXWirGCX 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.

issue122/python.1499949204.txt.gz · Dernière modification : 2017/07/13 14:33 de d52fr