1

Iam using 2 raspberry pi, 1 raspberry( called client) has signal input which has to be forward to the 2nd raspberry (called server) which has to give an output.

On the client i made 2 scripts called check.py and send.py, when in check.py GPIO23 input is high it must call the script send.py. This script send.py connects to the server which activate the script ledup.py (ledup.py located at the 2nd raspberry) which makes his GPIO24 output high for an amount of time. In the meanwhile check.py is in a loop and keeps checking his input.

Problem is i not get it all working, i try to combine the scripts on the client to 1 script what makes more errors so i kept it split in 2 scripts. Check.py not working good, it not stays in a loop and does nothing when gpio input 23 get high. When i add print command, to check if its working standalone then i get an output. Send.py when i test standalone its working and activates ledup.py, but nothing happens when check.py has gpio23 high... I really need some help with the check.py, this is were i get stuck.

script check.py:

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
         input_state = GPIO.input(23)
         if input_state == True:
             os.sytem("python /home/pi/send.py")
             time.sleep(0.5)
         else:
             GPIO.output(24, False)
             time.sleep(0.5)
except:
    GPIO.cleanup()

script send.py:

import subprocess
import sys

HOST="pi@10.10.4.180 -p22"
ssh = subprocess.Popen(["ssh",
                        "%s" % HOST],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        universal_newlines=True,
                        bufsize=0)

ssh.stdin.write("sudo python /home/pi/ledup.py")
ssh.stdin.close()

script ledup.py:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(24,GPIO.OUT)

GPIO.output(24,GPIO.HIGH)
time.sleep(0.8)
GPIO.output(24,GPIO.LOW)

GPIO.cleanup()
B.ertoes87
  • 11
  • 2

2 Answers2

0

Try using subprocess in check.py to open the file send.py for example:

#!/usr/bin/env python
import subprocess
subprocess.call("send.py", shell=True)

For this to work check.py and send.py must be in the same folder

You can also check https://stackoverflow.com/a/11230471

=====================EDIT====================

The combined version of check.py and send.py is this:

import RPi.GPIO as GPIO
import time
import os
import subprocess
import sys

def send():

    HOST="pi@10.10.4.180 -p22"
    ssh = subprocess.Popen(["ssh",
                            "%s" % HOST],
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            universal_newlines=True,
                            bufsize=0)

    ssh.stdin.write("sudo python /home/pi/ledup.py")
    ssh.stdin.close()

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
         input_state = GPIO.input(23)
         if input_state == True:
             send()
             time.sleep(0.5)
         else:
             GPIO.output(24, False)
             time.sleep(0.5)
except:
    GPIO.cleanup()
MJCS
  • 31
  • 1
  • 6
  • thx for you response! I got it working i can see its loading, now i only i get stuck it asks for a password. I setup a keyless SSH connection using public keys, standalone i can get i working without getting asked for a password, but when i run within the script it asks a password now – B.ertoes87 Aug 21 '18 at 14:17
  • Is it possible you could combine the check.py and send.py in 1 script? – B.ertoes87 Aug 21 '18 at 20:43
  • I have updated the answer @B.ertoes87. I haven't tested out the new script yet so please let me know if it worked. – MJCS Aug 22 '18 at 11:08
  • Its working, i give every 3 to 4 seconds input, only 1 time i get output. I think the script runs one time and not stays in a loop. By the way, using remoteGPIO is not better? For timing etc. – B.ertoes87 Aug 22 '18 at 16:06
  • Try adding **print("Recieved input")** after the line check() in the while loop. Then check if it prints Recieved input when you switch GPIO23 to high. Let me know what happens. – MJCS Aug 22 '18 at 18:28
  • sorry i found a easier solution with remoteGPIO, ill post my work here under – B.ertoes87 Aug 22 '18 at 18:49
  • ah i posted under my first question sorry, so its above here :) – B.ertoes87 Aug 22 '18 at 19:01
  • Just remove the while loop in send.py – MJCS Aug 23 '18 at 08:23
0

This is working, only i need for the send.py an end. When send.py is loaded then it wont stop, i only want to run this file one time and then must stop. Led on, led off exit script?

check.py

import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.OUT)

try:
    while True:
         input_state = GPIO.input(23)
         if input_state == False:
         GPIO.output(24, True)
         subprocess.call("python send.py", shell=True)
         time.sleep(0.2)
         else:
             GPIO.output(24, False)
except:
    GPIO.cleanup()

send.py

from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep

factory3 = PiGPIOFactory(host='10.10.4.180')
led_1 = LED(24, pin_factory=factory3)

while True:
           led_1.on()
           sleep(0.2)
           led_1.off()
           sleep(0.2)
B.ertoes87
  • 11
  • 2