I have Arch Linux ARM running on a Raspberry Pi. On boot, an X application is started (navit).
After system is booted, I ssh into it, and try the following python code:
from evdev import UInput, ecodes
inp = UInput()
inp.write(ecodes.EV_KEY, ecodes.KEY_UP, 1)
inp.write(ecode.EV_KEY, ecodes.KEY_UP, 0)
inp.syn()
The problem- the application which is started on boot does not receive this keypress.
The following does work from ssh session- application receives arrow up keypress.
DISPLAY=:0 xdotool key Up
uinput kernel module is loaded, Xorg configuration is left at defaults (hotplugging devices should be enabled- although, I cannot verify it).
dmesg shows that input device is registered:
[ 28.725976] input: py-evdev-uinput as /devices/virtual/input/input0
I alsso verified that the application I started is currently focused, by using:
DISPLAY=:0 xdotool getwindowfocus getwindowpid
Any ideas where to look next to debug this issue?
Turns out evdev module was not loaded. I though loading uinput module should suffice, but it's not.
I discovered it by noticing that the code did work when external keyboard was plugged in.
Related
I am running a main python program on a Windows PC that is hooked to equipment that cannot be ran on an Raspberry pi. At a certain point in the main program, I want to call/execute a Rpi program to run. I need the GPIO pins from the Rpi to turn on a relay/s. Is there a way to wirelessly(or serially) open and run the program on the raspberry pi from the main program already running on the Windows PC?
Maybe I am not thinking of something, is there an easier and just as cheap solution to turn on a relay from the Windows PC program?
Any points in the right direction would be greatly appreciated.
The easiest way to do this is with Remote GPIO which is all documented at that link. However, the gist of it is pretty simple as follows.
On the Raspberry Pi, run these steps:
sudo apt install pigpio
sudo raspi-config and enable "Remote GPIO"
sudo systemctl enable pigpiod
On the Windows PC, run these steps:
pip install gpiozero pigpio
Assuming your RaspberryPi has IP address 192.168.1.3, run PIGPIO_ADDR=192.168.1.3 python3 YourScript.py
Your script on Windows PC would then look like this:
from gpiozero import LED
from time import sleep
red = LED(17)
while True:
red.on()
sleep(1)
red.off()
sleep(1)
depending on security requirements. Assuming that the Desktop PC and raspberry pi are on the same network, you could create an HTTP REST endpoint on the pi, you could use flask or fastapi for this. then call that from the app running on the desktop. for help with flask see https://flask.palletsprojects.com/en/2.0.x/ if you are familiar with python flask is fairly simple to get started with.
I am trying to simulate tap and swipe gesture from adb to my android device. I've tried to run the following command
adb shell input tap 500 500 and adb shell input touchscreen tap 500 500 from windows command prompt. It is giving me the following error,
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.os.Parcel.createException(Parcel.java:2074)
at android.os.Parcel.readException(Parcel.java:2042)
at android.os.Parcel.readException(Parcel.java:1990)
at android.hardware.input.IInputManager$Stub$Proxy.injectInputEvent(IInputManager.java:925)
at android.hardware.input.InputManager.injectInputEvent(InputManager.java:886)
at com.android.commands.input.Input.injectMotionEvent(Input.java:428)
at com.android.commands.input.Input.access$200(Input.java:41)
at com.android.commands.input.Input$InputTap.sendTap(Input.java:224)
at com.android.commands.input.Input$InputTap.run(Input.java:218)
at com.android.commands.input.Input.onRun(Input.java:108)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:56)
at com.android.commands.input.Input.main(Input.java:71)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:380)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.input.InputManagerService.injectInputEventInternal(InputManagerService.java:732)
at com.android.server.input.InputManagerService.injectInputEvent(InputManagerService.java:706)
at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:422)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
I've also tried to execute the same command from python script with the pure-python-adb package. The following is script I've exucted.
from ppadb.client import Client
adb = Client()
devices = adb.devices()
if len(devices) == 0:
print("No devices attached")
quit()
device = devices[0]
print(device)
device.shell('input touchscreen tap 700 1453')
I got the following output with no tap noticed on device and no error from the script.
<ppadb.device.Device object at 0x02B30E50>
Am I missing anything (Might be in developer's option setting for android)?
I am on windows 10 machine with adb version 1.0.41. And using an android device with Android 10 (Android Q) over a usb.
Look for a "USB debugging (Security Settings)" or something similar in your developer options in the debugging section -- This is where you grant ADB special permissions and allow input.
Best of luck; I hope you find it.
I tried this to tap a xy coordinate using adb command. Enabling the "USB debugging (Security Settings)" is worked for me. But the phone should connect to the internet inorder to enable the settings.
Thanks #Meer
My question regards SSH on Raspberry Pi.
I am able to successfully ssh on to the Pi using the command:
sudo ssh pi#<ipaddress>
and then entering the password.
Let's say I have a Python script file on the Pi that I execute over SSH. Let's say the script reads:
import time
while True:
print('Hello')
time.sleep(1)
This will print 'Hello' every second whilst the terminal/command prompt window is still open (that is, the computer I am using to access the Pi is running and the SSH session remains open). If I close the connection, then the code will stop being executed on the Pi.
Is there a way I can use SSH to keep the code running on the Pi even when I close the window running SSH on the computer I am using to access the Pi? As in the Pi will keep printing 'Hello' even after I shut down my computer. Maybe by entering a command to open a terminal window on the Pi itself and running the script in that terminal window?
Is there a way this can be done?
Thanks
There are two options I can think of:
create a cron job. This method is usually used to execute scripts/programs repeatedly. The job is triggered by the cron program, so it doesn't matter whether or not you are connected to the Pi, as long as it runs. You just have to connect once and setup the job (typically using crontab -e).
use screen (on Wikipedia) or tmux (on Wikipedia). Those are called terminal multiplexers, and allow you to keep shells (and thus any script/program) running although you aren't connected. Note that, in this case, you will have to manually start your script each time, so this solution is well-suited to scripts that run for a long time but are not started too often.
I need to run GUI application on remote windows host and then do some actions with mouse and keyboard. This should be done from local pc without opening any GUI application (for example "Remote Desktop Connection").
So I have a python script on remote server which does all the actions I need (tested on local pc) and I run the script via psexec which successfully opens the GUI application on server.
The problem is that when python tries to programatically move and click mouse it throws an exception because there is no screen.
Actions with keyboard (Ctrl-A, Ctrl-C, Ctrl-V) can be done successfully.
Here is the code that I use to simulate mouse click but as I mentioned it doesn't work on server because there is no actual screen.
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
Can anyone suggest me a solution?
Try connecting directly to user32.dll through ctypes and use mouse controlling functions from there. Perhaps win32types problem is that it is mostly statically linked to MS API. So it is programmed for desktop environment.
Also, you can try pymouse module that you can find on pypi.python.org, of course, which does the same as I suggested above.
I've a script to run on boot and I'd like to use the keyboard to interact with the script. I've successful set this up to run in crontab; however, the script runs in the background and I can't use the keyboard to interact with the script. Here's a simplified example of the script:
def write_to_txt(item_to_write):
with open("my_txt_file.txt", "a") as myfile:
myfile.write('\n'+str(item_to_write))
while True:
keys_to_enter = raw_input()
write_to_txt(keys_to_enter)
Please could someone point me in the right direction?
I found out how to run the script on boot and allow the keyboard to interact with the program. To the ~/.bashrc file, I appended:
sudo python /home/pi/example.py
If I understand correctly you want your program to attach its stdin to tty1? I.e. the terminal which you see on screen if you have a display hooked up - this is where by default keyboard input would end up if X windows is not installed or the tty is not switched with Ctrl+Alt+Fx?
Is moving the ownership of the background script process to the shell on tty1 an option? If so, the easiest may be to auto-login the Pi (or the user will need to login with the keyboard on startup). Then auto-start the program on tty1 so its stdin/stdout is tied to tty1.
To achieve the latter, I think you can put its invocation into one of the bash startup scripts, something like what is suggested here: https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=7192
You can run a script in foreground at boot by adding a line to /etc/rc.local
This works in my experience, in particular if the Raspberry pi is configured to wait for network to be available when booting