For my DIY access control system, I'm running Debian on a Raspberry Pi attached to a MagStripe card reader. When a card is swiped, the reader writes the data from the magnetic stripe over the USB port by acting like a keyboard. For example, plug it into your computer, open a text editor, and swipe a card and you'll see a string of data printed out like you typed it with your keyboard.
Simple connection diagram:
Client <--ssh--> Host + card reader
The only problem is that I'm running my Python script over ssh, which doesn't directly hear the keyboard input.
How can I get Python to listen to the keyboard events coming from the MagStripe reader? (do I need to use a keylogger? PyGame's keyboard bindings?)
Thanks for the help!
On Linux, USB keyboards can be accessed via /dev/input.
See: format of /dev/input/event*? - the answers to that question include two different Python modules for getting events, and links to more documentation.
To translate from key codes to ASCII, see How can I translate Linux keycodes from /dev/input/event* to ASCII in Perl?
You will probably need to be root, or change the permissions on /dev/input.
It may be worth dividing the program into a two pieces: one as a service that starts at boot time on the raspberry pi which will get its stdin from your keyboard device(the reader) by default, and another that provides remote access functionality over ssh. This way you can avoid having to deal with devices in /dev directly.
Related
I have been trying to work logics on esp8266 and esp 32 via micro python and it turns out that micro python does not let you control the hardware fully with your keyboard.
It is inconvenient to press enter after every command on a serial port and impractical in autonomous applications like robots and drones where there is an infinite while loop running on milliseconds and asking for keypress event
If you want to use keyboards directly to your microcontroller, you either need to support USB or PS2 protocol. To understand why have a look at Ben Eaters comprehensive youtube videos on the two subjects: USB and PS/2
Your microcontroller is connected to your pc via the terminal, so do not count your computer keyboard as connected to your microcontroller.
If you want to interface with a remote drone, or other peripheral where you don't want the actual interface to be the terminal prompt (which uses the enter key as the send character), you would have to write an application that handles the keyboard interaction, or touch events on a mobile app, or a joystick/joypad input, and then sending the commands to the device afttwerwards, without waiting for enter or other control characters... that application could be written in regular python using the mentioned libraries, and the appropriate python libraries to send stuff via the terminal. You may also choose to use serial over bluetooth.
Furthermore, if you want to ensure that the wireless (wifi/bluetooth/433mhz/2.4gz) communication is not intercepted or interrupted/replayed, consider if you would need to encrypt the communication.
I have got a Cognex Advantage 100 camera connected to my PC via ethernet.
After pressing F5 in the inSight Explorer to trigger the camera I can use the captured image in a Python script.
Can I make the Python script trigger the image capture itself?
I'm not very familiar with the Advantage series, but I am quite familiar with the other In-Sight cameras. I'm going to assume the Advantage is similar to other In-Sight cameras.
You should be able to achieve a trigger from python by opening a telnet connection to the camera (on port 23), logging in (default username: admin, password: ), and sending the command 'SE8'. The camera trigger mode must be set to External, Manual or Network. If the command is successful, it will respond with a '1'. I'd suggest trying this with a telnet client before trying it in python. Suggested telnet clients: Putty or Hercules.
More information can be found in the In-Sight Explorer help file. From the contents, go to 'Communications Reference -> Native Mode Communications'.
Possibly you could simulate a key press. This answer here and this answer here, might help you do that.
Apart from that, your camera software doesn't allow you to interact with it via python, but it does supply it own method of programming the camera here. Try that instead, it seems to be the indented way of doing this.
I'm working on a program in Python that, for now, is on my iMac with OS X, but if I had to, I could move it to a Linux box on a Soekris box. Also, if there's a way to do this with a command line program or a C library I can easily access from Python (still not sure how to do that, but I hear it can be done), I'm open to that. But if it's possible to do it in Python on OS X, that would be preferred.
I have a simple (as of now) Python program for working with my modems. It handles incoming calls with caller ID info and taking faxes on my fax line. I would love to be able to add voice mail - nothing complex, but just being able to play back an outgoing message and record an incoming one.
But I'm having trouble finding any information on how to read or write audio data with a modem device on either OS X or Linux.
Is there anything good on handling audio data with modems on OS X or Linux?
I am using a Beaglebone Black (BBB) with Python and pyserial to communicate with an OBD-II reader. I am essentially trying to build a customizable digital gauge panel. Ideally I would like to use Flash for the GUI. Sadly Linux support for Flash is pretty weak. I would like to be able to send data from the BBB using Python to a OSX host computer.
I am currently using terminal to shell into the BBB to run code. I would need to be able to send data from the BBB via a USB/serial interface to the OSX computer running Flash. What would be the best method of accomplishing this?
I have not used beaglebone. I have worked with arduino's serial I/O. But this post says you have multiple serial I/O ports on BBB. Find appropriate connectors/convertors for serial to USB.
Then use the pyserial python module.
On OSX, you will find your device when connected on a path like /dev/ttyo1 where dev is my system name and ttyo1 or something similar will be your device.
import serial as s
device = "/dev/tty01"
bbb = s.Serial(device, 4800) #the second param is baudrate
while(True):
bbb.readline()
# do what you want with the output.
bbb.write('input')
This will read till the end of line character and give you a string. and then write "input" to the serial io on bbb. You will need a similar program running on BBB to read this input and do what you want to do with it.
So there will be two python programs. One on the OSX and the other on the BBB
That way you can give commands from OSX.py, let your BBB.py process and send a response. Which the OSX.py will read and do what is to be done.
You will have to design the input/output cycle properly.
Also if flash is not really necessary you can check out pyside.
How can I detect when a flash drive is plugged in? I'm using a bare Debian installation, without any GUI and want to be notified in my Python script when a new flash drive appears... I know that D-BUS distributes such information, but i dont want to use D-BUS. Is there a more bare access to that information? Shouldn't that be available under /proc or /sys? How can I "connect" to that source?
Bye
falstaff
All mayor Linux distros include udev, that allows you to write custom scripts on hardware events.
You can read uevents from kernel via a Netlink socket and look for events where "ACTION" is "add" (you can also watch if a partition from a device was mounted or unmounted, or if a device is being removed). That's as close to the source of events as one can get in user space. As far as I know, this is how udev detects inserted removable media.
But probably using D-Bus/HAL API via Python bingings will be much easier (no uevents data parsing, etc). Not sure why you are opposed to this. Since you are using Python, I suspect that resources are not really the issue.
If you are targetting an embedded device, then you can run mdev instead of udev.
Then you can write mdev rules that are quite simple and triggers a script.
Of course you are not directly monitoring from your script, mdev is doing it, but you can launch any command. You can probably do the same thing with udev, but it always looked quite complicated to me.
When an USB device is plugged in syslog writes messages concerning this to /var/log/messages. The "dmesg" command shows this log. You can check near the end of the log to see which channel the device was attached to, it is usually /dev/sd(letter)(number) depending on the partitions and number of serial disks plugged into the system.
/proc/partitions shows all the partitions known to the kernel.
I did this using zenity in a script and udev with rule on rhel6 with:
KERNEL=="sd[b-d]", DRIVERS=="usb", ACTION=="add", RUN+="/path/to/script"