Send data to Arduino on keypress Raspberry Pi
Question: I'm trying to build a car (Wild Thumper) that i can drive from my Raspberry
Pi. Currently i'm using my Raspberry Pi over SSH. It should send to data to my
Arduino so it knows when it has to go forward or when to turn.

I've tried making scripts that get called by jQuery (apache on Pi) and send an
integer over the serial port but it requires a delay and this is not ideal.
(example forwardStart.py:)

    
    
    import serial
    ser = serial.Serial('/dev/ttyACM0', 9600)
    ser.open()
    # here a delay is needed
    ser.write('4') # go forward
    ser.close()
    

To solve this i tried looking for a single python script that read my keyboard
and send the correct integer. However, all keylisteners require display and
can't be used over SSH.

Can anybody help me with the Python script or another idea that would works?

Thanks!


Answer: You should start reading from
[here](http://stackoverflow.com/questions/983354/how-do-i-make-python-to-wait-
for-a-pressed-key). The idea would be something like

    
    
    import serial
    ser = serial.Serial('/dev/ttyACM0', 9600)
    ser.open()
    # here a delay is needed
    
    try:
        while 1:
            try:
                key = sys.stdin.read(1) # wait user input
                actionKey = key2action(key) # translate key to action
                ser.write(actionKey) # go forward
            except IOError: pass
    finally:
        ser.close()
    

**Note:** this code will fail, it's more like pseudo-code to illustrate the
idea.

