Continuing from the previous article (Running openFrameworks sample program on Raspberry Pi 3 --Qiita), replace Mac for exhibition with Raspberry Pi It is an attempt to do so.
Last time, I installed oF on Raspberry Pi and ran the sample program, so this time I will connect the NFC reader to Raspberry Pi and send the NFC data read from the python program to openFrameworks with OSC.
I made it possible to use the NFC reader by relying on the reference site 1. The program I ran is Read.py on reference site 2. Since it was as per the reference site, it is omitted.
When I first tried it, I ran the program and it didn't respond to NFC tags. The site said that I had to disable the Device Tree, but I couldn't find the Device Tree in the Advanced Option even after `sudo raspi-config`
.
I wasn't sure, but I reinstalled Raspbian and it got better. I bought a micro SD card with Raspbian 2016-02-26 pre-installed, but the pre-installed one may not have been good.
I used osc to bring the python sample program run in 4. to openFrameworks. I thought it would be nice if I could read the NFC reader directly from oF, but I didn't know what to do, so I chose this method.
I tried pyOSC while looking at the reference site 3. (Python OSC (pyOSC) | Android Days).
Install pyOSC
git clone https://gitorious.org/pyosc/devel.git
cd devel
sudo python setup.py install
Reference site 3. I tried the sample program written in, but it didn't work.
git clone https://github.com/conanxin/Arduino-Meets-Blender-OSC.git
Start up two terminals and try running the sample program.
terminal1
cd Arduino-Meets-Blender-OSC/pyOSC_examples
python basic_receive.py
terminal2
cd Arduino-Meets-Blender-OSC/pyOSC_examples
python basic_send.py
When I ran send.py, I got the following error.
terminal2
Traceback (most recent call last):
File "basic_send.py", line 28, in <module>
client.sendto(msg, ('127.0.0.1', 2014)) # note that the second arg is a tupple and not two arguments
File "/usr/local/lib/python2.7/dist-packages/OSC.py", line 1174, in sendto
ret = select.select([],[self._fd], [], timeout)
AttributeError: 'OSCClient' object has no attribute '_fd'
I just didn't understand.
Take a moment and try another sample. Run the sample that was in the develop folder where you downloaded pyOSC.
terminal1
cd devel/examples
python knect-rcv.py
terminal2
cd devel/examples
python knect-snd.py
When I executed knect-snd.py, the following was displayed on the screen of knect-rcv.py.
terminal1
('Now do something with', 'user1', 3.0, 1.0, -1.0)
('Now do something with', 'user2', 4.0, 2.0, -2.0)
('Now do something with', 'user3', 3.0999999046325684, 2.0, -2.0)
('Now do something with', 'user4', 6.0, 3.200000047683716, -2.4000000953674316)
I inserted the knect-snd.py program into Dump.py.
Dump_osc.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import signal
# OSC setting
from OSC import OSCClient, OSCMessage
client = OSCClient()
client.connect(("localhost", 7110))
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
# Send OSC
client.send( OSCMessage("/user/1", [uid[0], uid[1], uid[2], uid[3]] ) )
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
The sample program is also used here. Copy the sample program oscReceiveExample to openFrameworks / apps / myApps /.
cd openFrameworks/examples/addons
cp -r oscReceiveExample /home/pi/openFrameworks/apps/myApps/
Open ofApp.h.
cd /home/pi/openFrameworks/apps/myApps/oscReceiveExample/src
vim ofApp.h
Change `# define PORT 12345``` on the 7th line where the port number is set to
`# define PORT 7110``` and save.
Go back up one folder and do make
. When the compilation is finished, make run
.
cd ..
make
make run
A blue window will open in the upper right corner of the screen. Launch another terminal and launch Dump_osc.py created in 6.
cd MFRC522-python
python Dump_osc.py
When you hold the NFC key chain attached to the NFC reader, the NFC UID is also displayed on the blue screen.
The video looks like this. (Click the image to go to YouTube)
Now that we've done this, it seems good to do the processing according to the UID received by oF.
Recommended Posts