daveyman123
Posts: 119
Joined: Fri Dec 12, 2014 2:21 am

Windows to Pi with python

Sun Jan 08, 2017 7:23 pm

Hello,

How can I use windows to talk to the raspberry pi with python?

I want to write a program that will send a simple command from
a windows computer to the Pi to execute a script on the pi


Thanks,
DM123

daveyman123
Posts: 119
Joined: Fri Dec 12, 2014 2:21 am

Re: Windows to Pi with python

Sun Jan 08, 2017 7:51 pm

UPDATE:

ive managed to make a connection using this code:

server.py

Code: Select all

import socket
s=socket.socket()
host=socket.gethostname()
port=12345
s.bind((host, port))
s.listen(5)
while True:
    c, addr = s.accept()
    print 'got connection from',addr
    c.send('Thank you for connecting')
    c.close()
client.py

Code: Select all

import socket               

s = socket.socket()        
host = '192.168.0.3'# ip of raspberry pi 
port = 12345               
s.connect((host, port))
print s.recv(1024)
s.close()               

Now i just need to figure out how to send a command to execute a script

mattmiller
Posts: 2247
Joined: Thu Feb 05, 2015 11:25 pm

Re: Windows to Pi with python

Sun Jan 08, 2017 7:55 pm

Google for Python subprocess to see how to execute a script from within Python
Come back if you get stuck :)

daveyman123
Posts: 119
Joined: Fri Dec 12, 2014 2:21 am

Re: Windows to Pi with python

Sun Jan 08, 2017 8:08 pm

Thanks Matt for the reply.
I am somewhat familiar with subprocess

I added this line to the server script to run my bluetooth connection script:

Code: Select all

subprocess.call("python bluetooth2.py",shell=True)
this method seems to work!

2 questions:
how do i work around Pulseaudio not being able to run with escalated privileges?
Why is the port still in use after I close the script?

here is bluetooth2.py for reference

Code: Select all

import pexpect
from sh import bluetoothctl
import subprocess
mac = "C8:84:47:26:E6:3C"
#sh.bluetoothctl("pair", mac)
print ("stuck here")
#bluetoothctl("connect", mac)
child = pexpect.spawn('bluetoothctl')
child.sendline('power on')
child.sendline('agent on')
child.sendline('default-agent')
child.sendline('pair C8:84:47:26:E6:3C')
child.sendline('trust C8:84:47:26:E6:3C')
child.sendline('connect C8:84:47:26:E6:3C')
subprocess.call("pulseaudio --start",shell=True)
subprocess.call("pacmd set-default-sink bluez_sink.C8_84_47_26_E6_3C",shell=Tru$
subprocess.call("aplay /home/pi/bleep_01.wav", shell=True)

mattmiller
Posts: 2247
Joined: Thu Feb 05, 2015 11:25 pm

Re: Windows to Pi with python

Sun Jan 08, 2017 8:34 pm

how do i work around Pulseaudio not being able to run with escalated privileges?
I'd ask that on a new thread

port closures do not work in the manner we muggle want them too. There is dark arts stuff going on that won't allow port re-use until timeouts (that us muggles can't change despite documentation saying we can)

I'd ask this question on a new thread as well but don't expect the answers to work!

daveyman123
Posts: 119
Joined: Fri Dec 12, 2014 2:21 am

Re: Windows to Pi with python

Sun Jan 08, 2017 8:49 pm

mattmiller wrote:
how do i work around Pulseaudio not being able to run with escalated privileges?
I'd ask that on a new thread

port closures do not work in the manner we muggle want them too. There is dark arts stuff going on that won't allow port re-use until timeouts (that us muggles can't change despite documentation saying we can)

I'd ask this question on a new thread as well but don't expect the answers to work!
Id hope there are enough wizards on this board to answer the question! :D

Return to “Python”