Page 1 of 1

Issue with Script

Posted: Wed Jun 21, 2017 2:47 pm
by gavO852
So I am new at python coding. I decided to try a project. I followed instructions for a project call fauxmo. I have it setup for when I tell Alexa to turn on the Xbox it should run a script that sends power on packets to the Xbox one. How ever, when I run fauxmo it gives me errors. I have specified were the power on packet scrip is but says there is no such file or directory. Please help. Images linked below
https://ibb.co/eJbmNk
https://ibb.co/gXwxTQ
https://ibb.co/nO1mNk

Re: Issue with Script

Posted: Wed Jun 21, 2017 3:32 pm
by DirkS
Try using the full path to the script: /home/pi/echoxbox/... instead of ~/echoxbox/...

Re: Issue with Script

Posted: Wed Jun 21, 2017 4:49 pm
by gavO852
Ok will try and report back!

Re: Issue with Script

Posted: Wed Jun 21, 2017 5:28 pm
by gavO852
Ok. I still got an error. But now it is saying "python: can't open file 'xbox-remote-power.py': [Errno 2] no such file or directory. Have have the full file path pointed to the .sh file, so I don't know why it would have an error for the .py file. The file is in the same directory and folder. Also, when I Chang the .sh file over to the .py, I get Error 13 permission denied. Please respond back.

Re: Issue with Script

Posted: Wed Jun 21, 2017 5:58 pm
by DirkS
gavO852 wrote:"python: can't open file 'xbox-remote-power.py': [Errno 2] no such file or directory. Have have the full file path pointed to the .sh file, so I don't know why it would have an error for the .py file. The file is in the same directory and folder. Also, when I
The working directory does not change when you start the .sh script. You have two options: cd to the directory with the script or (again) use the full path to the python script
Chang the .sh file over to the .py, I get Error 13 permission denied.
Sorry, I don't know what you mean with that...
Did you change the extension from .sh to .py?

Re: Issue with Script

Posted: Wed Jun 21, 2017 6:39 pm
by gavO852
Ok. What I mean is when I tell it to run the .py file that is in the same folder I get error 13. I don't how I could point it to the .py file. I will send a pic once again to show. The picture is a pic of the code that tells it what to do when it receives an "on" from Alexa. How could I point the .py file at the same time?
IMAGE
https://ibb.co/nAakXk
(The underlined code is the file path for the .sh file only)

Re: Issue with Script

Posted: Wed Jun 21, 2017 6:57 pm
by DirkS
Remove the dot at the start of the path.
It makes it a relative path to whichever directory you are in at the time you issue the command.

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:07 pm
by gavO852
When I do that I still get error 2 python can't open file no such file or directory exists. I am way confused at this point. All I want to happen is have the .sh script run when Alexa tells the fake wemo switch on. Images are below.
https://ibb.co/ithJJQ
https://ibb.co/dcCkyQ

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:14 pm
by DirkS
Do you use full paths in your shell script?
Can you post it here.

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:19 pm
by Paeryn
Python is telling you it can't open xbox-remote-power.py but the code you are posting is calling a shell script xbox-remote-power.sh where is the code that is trying to open the .py file? Is it in the shell script?

Pictures aren't very good for displaying code, it's much better if you copy-paste the code and/or messages directly into your post putting it between code tags, e.g.

Code: Select all

Explanation of problem...
[code]
Your code or error messages here
[/code]
The code button just above the text entry box will insert the tags for you.

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:20 pm
by gavO852
In the .sh file, all it is "#!/bin/bash

IP address of Xbox
Live id of Xbox (can't share this here)
Python xbox-remote-power.py and then the ip and id. The .py file takes all that and then sends some kind of wol packet to the Xbox one. I need a way to point the .sh path and the .py path so it can find or see it. Right now, it sees the .sh but it needs the .py file to send the packets.

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:26 pm
by Paeryn
gavO852 wrote:In the .sh file, all it is "#!/bin/bash

IP address of Xbox
Live id of Xbox (can't share this here)
Python xbox-remote-power.py and then the ip and id. The .py file takes all that and then sends some kind of wol packet to the Xbox one. I need a way to point the .sh path and the .py path so it can find or see it. Right now, it sees the .sh but it needs the .py file to send the packets.
You need the full path to the python file in the shell script e.g.

Code: Select all

python /home/pi/echoxbox/xbox-remote-power-master/xbox-remote-power.py
I may have gotten the path wrong, it's hard to read the writing in the picture on my phone.

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:32 pm
by gavO852
Code for .sh file
#!/bin/bash

IP_ADDR=192.168.1.101
LIVE_ID=FD008DA9A799XXXX (I have blocked the last 4 digits of my live id for security reasons.)

python xbox-remote-power.py -a $IP_ADDR -i $LIVE_ID

Code for .py file

#!/usr/bin/env python
import sys, socket, select, time
from argparse import ArgumentParser

XBOX_PORT = 5050
XBOX_PING = "dd00000a000000000000000400000002"

py3 = sys.version_info[0] > 2


def main():
parser = ArgumentParser(description="Send power on packets to a Xbox One.")
parser.add_argument('-a', '--address', dest='ip_addr', help="IP Address of Xbox One", default='')
parser.add_argument('-i', '--id', dest='live_id', help="Live ID of Xbox One", default='')
parser.add_argument('-f', dest='forever', help="Send packets until Xbox is on", action='store_true')
args = parser.parse_args()

if not args.ip_addr:
args.ip_addr = user_input("Enter the IP address: 192.168.1.101")

if not args.live_id:
args.live_id = user_input("Enter the Live ID: FD008DA9A799XXXX")

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setblocking(0)
s.bind(("", 0))
s.connect((args.ip_addr, XBOX_PORT))

if isinstance(args.live_id, str):
live_id = args.live_id.encode()
else:
live_id = args.live_id

power_payload = b'\x00' + chr(len(live_id)).encode() + live_id + b'\x00'
power_header = b'\xdd\x02\x00' + chr(len(power_payload)).encode() + b'\x00\x00'
power_packet = power_header + power_payload
print("Sending power on packets to {0}...".format(args.ip_addr))
send_power(s, power_packet)

print("Xbox should turn on now, pinging to make sure...")
ping_result = send_ping(s)

if ping_result:
print("Ping successful!")
else:
print("Failed to ping Xbox :(")
result = ""
if not args.forever:
while result not in ("y", "n"):
result = user_input("Do you wish to keep trying? (y/n): ").lower()
if args.forever or result == "y":
print("Sending power packets and pinging until Xbox is on...")
while not ping_result:
send_power(s, power_packet)
ping_result = send_ping(s)
print("Ping successful!")

s.close()


def send_power(s, data, times=5):
for i in range(0, times):
s.send(data)
time.sleep(1)


def send_ping(s):
s.send(bytearray.fromhex(XBOX_PING))
return select.select(, [], [], 5)[0]


def user_input(text):
response = ""

while response == "":
if py3:
response = input(text)
else:
response = raw_input(text)

return response

if __name__ == "__main__":
main()


And the code for running the fake wemo switch server so echo can see and control

""" fauxmo_minimal.py - Fabricate.IO

This is a demo python file showing what can be done with the debounce_handler.
The handler prints True when you say "Alexa, device on" and False when you say
"Alexa, device off".

If you have two or more Echos, it only handles the one that hears you more clearly.
You can have an Echo per room and not worry about your handlers triggering for
those other rooms.

The IP of the triggering Echo is also passed into the act() function, so you can
do different things based on which Echo triggered the handler.
"""

import fauxmo
import logging
import time

from debounce_handler import debounce_handler

logging.basicConfig(level=logging.DEBUG)

class device_handler(debounce_handler):
"""Publishes the on/off state requested,
and the IP address of the Echo making the request.
"""
TRIGGERS = {"xbox": 52000}

def act(self, client_address, state, name):
print "State", state, "on ", name, "from client @", client_address
import subprocess
subprocess.call(['/home/pi/echoxbox/xbox-remote-power-master/xbox-remote-power.sh'])
return True

if __name__ == "__main__":
# Startup the fauxmo server
fauxmo.DEBUG = True
p = fauxmo.poller()
u = fauxmo.upnp_broadcast_responder()
u.init_socket()
p.add(u)

# Register the device callback as a fauxmo handler
d = device_handler()
for trig, port in d.TRIGGERS.items():
fauxmo.fauxmo(trig, u, p, None, port, d)

# Loop and poll for incoming Echo requests
logging.debug("Entering fauxmo polling loop,Ask Alexa to find my devices")
while True:
try:
# Allow time for a ctrl-c to stop the process
p.poll(100)
time.sleep(0.1)
except Exception, e:
logging.critical("Critical exception: " + str(e))
break

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:34 pm
by DirkS
gavO852 wrote:python xbox-remote-power.py -a $IP_ADDR -i $LIVE_ID
See Paeryn's comment above about using the full path to the python script

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:38 pm
by gavO852
When I add the .py file to the .sh file I still get the error but it says "Enter ip: 192.168.1.101"
If I hit enter the same thing pops up. I have to type the ip again and then it goes on the the live id and does the same thing. It already has the ip and id but it's asking me to type it again anyway.

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:43 pm
by gavO852
Now it will ping and turn on Xbox! I just need to adjust a little bit on how much it should ping. Thanks for everyone's help!

Re: Issue with Script

Posted: Wed Jun 21, 2017 7:47 pm
by DirkS
Good to see you got it working!