2

With Reference to pi-3

I have read the Webserver approach and have tried it. That just shows me the python code back on the browser, if I run "http://(Ip address of my Pi)/programme.py". Where the programme.py is in "www" folder of the web server.

I want that a programme that deals with controlling gpio pins according to my logic to be executed on the Pi remotely that is via the Internet and that too from an another python script that is executing on some other device connected to the Internet something like exec("the URL to the programme on Pi") and that programme on Pi gets executed where my Pi is connected to the Internet.

So, basically:

I have a python code on my Laptop and this laptop has an Internet connection. Now, have a Pi too and it has a python code that deals with controlling Gpio pins of the Pi and my Pi is also connected to the Internet. I will execute that code on the laptop manually but the code on Pi should get executed automatically by the code executing on the laptop with the help of the Internet since both the devices are connected to the Internet. Basically, It should be something like just a call to that code on Pi to get it executed. Here both the codes are Independent in functionality the only thing is one Python programme need to just start or Initiate the execution of the script on the Pi via Internet remotely.

Hope now it's clear!

Do not down vote I researched a lot to do this and tried much. Am quite new to Pi.

Found a Solution: https://github.com/Rahul14singh/Remote_raspberrypi_GPIO_Control

6
  • I do not understand what you want. Could you expand your question to include a timeline of events, i.e. doing this causes that, causes that, causes that etc?
    – joan
    Commented Jun 2, 2017 at 6:14
  • Okay so see I am connected to the Internet on my laptop and there is a python code on my laptop. There is also a python code that controls gpio pins in my Pi which is also connected to the Internet so now I want to run that python code on Pi via this python code that is on my laptop when I execute the code on laptop manually myself but that code on Pi executes automatically by this Python code. Hope now it's clear! Commented Jun 2, 2017 at 6:18
  • It's should be something like just a call to that code on Pi to get it executed both the codes are Independent in functionality the only thing is one Python programme need to just start or Initiate the execution of the script on the Pi via Internet remotely. Commented Jun 2, 2017 at 6:24
  • You should add explanations to your question, not as comments. It is still not clear. You have at least three machines. Give a time line of what you want to happen on each machine and when.
    – joan
    Commented Jun 2, 2017 at 8:00
  • Added the explanation @joan Commented Jun 2, 2017 at 8:18

2 Answers 2

2

Give something like this a shot,

import subprocess
def TerminalCommand(command='~/take_snapshot.sh ', shell=True):
proc = subprocess.Popen(
                        command,
                        shell=shell,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                       )

msg = 'running command: "{}"'.format(command).encode('utf-8')
stdout_value, stderr_value = proc.communicate(msg)

print('pass through:', repr(stdout_value.decode('utf-8')))
print('stderr      :', repr(stderr_value.decode('utf-8')))

I'm currently using it to take pictures periodically. As Anon suggests, you can pass ssh [email protected] "/path/to/python/code/code.py" as command to run your script. Hope that helps.

EDIT: If you need to ssh into the pi, you can use paramiko.

from paramiko import SSHClient, AutoAddPolicy

def Connect(ip, username='pi', pw='password'): 
    '''ssh into the pi'''
    print('connecting to {}@{}...'.format(username, ip))
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(ip, username=username, password=pw)
    print('connection status =', ssh.get_transport().is_active())
    return ssh

def SendCommand(ssh, command, pw='password'):
    '''send a terminal/bash command to the ssh'ed-into machine '''
    print('sending a command... ', command)
    stdin, stdout, stderr = ssh.exec_command( command )
    if "sudo" in command:
        stdin.write(pw+'\n')
    stdin.flush()
    print('\nstout:',stdout.read())
    print('\nsterr:',stderr.read())

myssh = Connect(ip='192.168.3.14')
SendCommand(myssh, command='~/script.py') 
2
  • How to use "ssh [email protected] "/path/to/python/code/code.py" what should I write in my python script. I want that one script executes the other script remotely where one script is on my laptop and other on my Pi. And the script on my laptop should execute the script on the Pi. Commented Jun 2, 2017 at 12:14
  • @RahulSingh I've edited my answer to explain ssh-ing and issuing commands using paramiko.
    – Chris
    Commented Jun 2, 2017 at 23:30
1

Sorry, I can't comment.

What about

    ssh [email protected] "/path/to/python/code/code.py"

You can include in the python script on your laptop a subprocess call to this command to execute remotely the python script on your pi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.