Wirelessly connecting Arduino and Raspberry Pi
Using LLAP
Although the WIK graphical tool makes it easy to get started with RasWIK, you might find the graphical environment's capabilities rather limited. Fortunately, Slice of Radio and XinoRF support the Lightweight Local Application Protocol (LLAP), which allows the components to exchange wireless messages. In practical terms, this means you can control the XinoRF board programmatically from Raspberry Pi using Python scripts. In the most simple case, you can control the LED on pin 11 using the script in Listing 1 instead of the WIK tool.
Listing 1
Simple Python Script to Control LED
01 #!/usr/bin/env python 02 # -*- coding: utf-8 -*- 03 import serial 04 from time import sleep 05 port = '/dev/ttyAMA0' 06 baud = 9600 07 serial_connection = serial.Serial(port=port, baudrate=baud) 08 sleep(0.2) 09 serial_connection.write('a--D11HIGH--') 10 sleep(3) 11 serial_connection.write('a--D11LOW--') 12 serial_connection.close
The script specifies the port and speed values, opens a serial connection using the defined values, and then sends write messages to the board. In this case, the messages are a--D11HIGH--
and a--D11LOW--
, which set pin 11 to HIGH and LOW, thus turning the LED on and off. Reading values from a pin on XinoRF is equally easy. The following two statements
reading = serial_connection.read(12) print (reading)
read 12 characters from the serial port and print the obtained reading.
Practical Examples
You can put the ability to read values programmatically from XinoRF pins to a variety of practical uses. For example, you can connect the supplied light-dependent resistor (LDR) and a regular 10K resistor to the board, then continuously read data from the pin and write the obtained values to a text file. Start with wiring the LDR and the resistor as shown in Figure 7.
Next, create an empty text file and paste the code from Listing 2 into it [6]. Save the script as read_write_values.py
and make it executable using the chmod +x read_write_values.py
command. Before you run the script, I'll explain what it actually does. As in the previous example, the script starts by initializing and establishing a serial connection. Using the infinite while
loop, the script reads values from pin 0.
Listing 2
More Advanced Python Example
01 #!/usr/bin/env python 02 # -*- coding: utf-8 -*- 03 import serial 04 import sys 05 from time import sleep 06 port = '/dev/ttyAMA0' 07 baud = 9600 08 serial_connection = serial.Serial(port=port, baudrate=baud) 09 sleep(0.2) 10 while True: 11 try: 12 serial_connection.write('a--A00READ--') 13 sleep(0.2) 14 rawdata = serial_connection.read(12) 15 value = rawdata[7:] 16 value = value.strip('-') 17 print value 18 with open("values.txt", "a") as f: 19 f.write(value +"\n") 20 sleep (3) 21 except KeyboardInterrupt: 22 sys.exit(0)
The obtained raw data, which looks like a--A0+593—, is then parsed to extract the actual value by discarding the first seven characters and stripping dashes. The parsed value is then added to the values.txt
file. The sleep
statement in the loop determines intervals between each read/write action. The loop runs until it encounters an error, such as a keyboard interrupt (i.e., Ctrl+C).
The "Controlling Legacy Devices" article [7] in the previous issue explained how to create a film SLR camera trigger based on Raspberry Pi and a simple transistor switch. For this trigger to work, Raspberry Pi must be physically connected to the camera, which makes this setup less flexible. RasWIK can help solve this problem: You can connect the camera to a XinoRF-based transistor switch that can be controlled wirelessly from Raspberry Pi.
For the transistor switch, you can use the 2N3906 PNP transistor that comes with RasWIK and a 1K resistor. Follow the diagram in Figure 8 to wire the transistor switch to the XinoRF board. To control the switch from Raspberry Pi, you can use the Python script in Listing 1, but you might need to set the value in the second sleep
statement to 0.5
. Connect the camera to the transistor switch, power the XinoRF board, and run the script on Raspberry Pi to trigger the camera shutter.
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)