Can't get Kodi to accept keys emitted from python-uinput? - python

I'm emitting keys from a python script using python-uinput. Basic stuff such as up / down / enter / esc.
As far as I can see this works fine on my desktop, in the terminal, and with the browser. But when I run Kodi, it doesn't seem to respond at all. Is this something to do with it being a fullscreen application?
NB: I'm running Raspbian on model 3 Raspberry Pi.

Maybe you need to do: sudo modprobe uinput
The following script works for me to send Function key 12 to vice (a C64 emulator) based on a button press on a GPIO:
import uinput
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
wasPressed=False
# set up keystroke input
device = uinput.Device([uinput.KEY_F12])
while True:
button_inactive = GPIO.input(21)
if not button_inactive and not wasPressed:
device.emit_click(uinput.KEY_F12)
print "sending F12"
wasPressed=True
if button_inactive:
wasPressed=False
time.sleep(0.1)
Note that I used uinput.KEY_F12 twice. The script should be run as root.

Related

How to run Neopixel without sudo permissions for use in an alexa app

I am creating an Alexa app that will allow the user to turn on a set of NeoPixels (WS2812) LEDs using a simple voice command. The app is hosted on a raspberry pi 4 and currently works for turning on GPIO pins. The issue I am having is that the code to turn on the NeoPixels needs sudo permissions, so when using the alexa app and it will simply error instead of turning on the lights.
due to the fact that sudo is needed the only way I have been able to run the script is to
sudo python3 LEDTest2.py
which works fine
here is the implementation for the alexa app turning on the GPIO pin and calling the python program
#ask.intent('GpioIntent', mapping = {'status':'status'})
def Gpio_Intent(status,room):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
if status in STATUSON:
GPIO.output(17,GPIO.HIGH)
LEDTest2.func() ###Problem Point###
return statement('turning {} lights'.format(status))
This works fine for turning on the GPIO pin
Here is the LEDTest2.func()
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.D18,60)
def func():
print("func() in LEDtest2.py")
pixels.fill((255, 197, 143))
time.sleep(5)
pixels.fill((0,0,0))
if __name__ == "__main__": #same as file one
print("LEDtest2.py is being run directly")
for x in range(2):
pixels.fill((0, 255, 0))
time.sleep(.1)
pixels.fill((0, 0, 0))
time.sleep(.1)
else:
print("LEDtest2.py is being imported into another module")
how can i run this code without needing sudo permissions? is there an alternative way to control WS2812 LEDs using python?
Edit: Just to clarify i am not having any problems with the GPIO pins it is the NeoPixel library that cannot be used without sudo permissions
I was able to control it without sudo.
You should enable SPI interface from raspi-config.
Of course, first physically move LED connection to GPIO10 and change the pin in code.
I tried it and this works for me.

Raspberry Pi SW-420 vibration sensor and Python

I'm trying to create an application that will sense my laundry machine turning on. Then when it's done, i want it to detect that and flash my Hue bulbs. I'm using the SW-420 vibration sensor and Python for this project. I have successfully gotten it to recognize vibration, but unfortunately, it's not what I need. The following is my current code.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
def start(channel):
if GPIO.input(channel):
print "Dryer has started!"
time.sleep(1)
else:
print "Dryer has stopped"
time.sleep(1)
#f = open("status", "w") #Used to create light status file
#subprocess.call(["perl", "/home/pi/huepi/huepi", "lights"], stdout=f); # creates status file
#subprocess.call(["perl", "/home/pi/huepi/huepi", "for", "1 3 4 5 6 10 11 12", "do", "blinkonce"]); # blinks hue bulbs
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, start) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)
The issue that I'm having is even when the machine is running the sensor registers that it has stopped. I've tried using
GPIO.add_event_detect(channel, GPIO.RISING, bouncetime=300)
and
GPIO.add_event_detect(channel, GPIO.FALLING, bouncetime=300)
both with the same results. I just need it to register that the machine has started then with it stops flash the Hue bulb and wait for the next time the machine starts back up.
I'm using a Perl library called Huepl to interface with but bulbs That section of the code is working fine. If you need any more information I'll be glad to supply. Thank you in advance!
Set started via callback on your first detected vibration. Set a lastShakeTime to now via callback whenever you detect vibration.
In your main loop check if nothing was detected for 60s:
import datetime
# infinite loop
while True:
time.sleep(1)
now = datetime.datetime.now()
if started and (now - lastShakeTime).total_seconds() > 60:
# do something with your oleds
Polling vs. Eventbased:
https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/

Echo dot commands Raspberry pi to execute a script on my computer

Looking for a coding guru to explain me what direction I need to take now:
I use an Amazon Echo Dot which is currently connected to my Raspberry Pi.
I can now say "Alexa turn device on/off" which returns a true/false to my Raspberry Pi using this excellent repository: https://github.com/toddmedema/echo.
Could anyone explain me how I can replace this 'true' in this python script with a script that sends a request to execute a batch file on my windows computer using RDP?. I think it is here but it doesn't work:
def act(self, client_address, state, name):
print "State", state, "from client #", client_address
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(str(sys.argv[3]), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
GPIO.output(str(sys.argv[3]), not state) ## State is true/false
GPIO.cleanup(str(sys.argv[3]))
return True ## after this line add the command the execute?
Extra question: How do I add more voice commands?
Would really appreciate it.

Energenie Pi-Mote simple on, wait, off script not working Raspberry Pi Home Automation

I've bought an Energenie module and plug for use with the raspberry pi. I'm completely new to GPIO and managed to get it working with the demo script on their website (https://energenie4u.co.uk/res/pdfs/ENER314%20UM.pdf)
I've since followed the instruction here, installed with Python 2 (https://libraries.io/pypi/energenie) as I simply want to turn the plug on, wait a time period and then turn off, but can't get it to work using a python script I've written using the above link. What am I missing? Here's the script:
from energenie import switch_on, switch_off
print("Sockets 1-4 or 0 for all")
while True:
socket = int(raw_input('Turn socket on: '))
switch_on(socket)
socket = int(raw_input("Turn socket off: "))
switch_off(socket)
I've also tried this:
from energenie import switch_on, switch_off
from time import sleep
# turn some plug sockets on, then turn them off after 10 seconds
switch_on()
sleep(10)
switch_off()

LCD commands not working inside threads

I hooked up a 16X2 LCD to the pi via a MCP23017 GPIO expander. The problem that I am facing is that the LCD code below works inside normal functions or when I call it individually. But it does not work inside any of the threads in my program.
#!/usr/bin/python
# Example script to show usage of MCP230xx GPIO extender to drive character LCD.
from Adafruit_CharLCD import Adafruit_CharLCD
from Adafruit_MCP230xx import MCP230XX_GPIO
bus = 1 # Note you need to change the bus number to 0 if running on a revision 1 Raspberry Pi.
address = 0x20 # I2C address of the MCP230xx chip.
gpio_count = 8 # Number of GPIOs exposed by the MCP230xx chip, should be 8 or 16 depending on chip.
# Create MCP230xx GPIO adapter.
mcp = MCP230XX_GPIO(bus, address, gpio_count)
# Create LCD, passing in MCP GPIO adapter.
lcd = Adafruit_CharLCD(pin_rs=1, pin_e=2, pins_db=[3,4,5,6], GPIO=mcp)
lcd.clear()
lcd.message(" Adafruit 16x2\n Standard LCD")
links to library programs,
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_CharLCD/Adafruit_CharLCD.py
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_MCP230xx/Adafruit_MCP230xx.py
part of my actual program:
def switch():
mcp.config(0, mcp.INPUT)
mcp.pullup(1,1)
if ((mcp.input(1)>>1))
lcd.clear()
lcd.message("Switch open")
switchthread=threading.thread(target=switch)
switchthread.start()
Can anyone please tell me how to get the LCD commands working in the threads ?

Categories

Resources