0

I have two python scripts that both need to run to do what I want. Basically the first script is for a server that handles POST requests. It looks like this.

from bottle import Bottle, run, template, get, post, request
app = Bottle()

@app.route('/rotation', method='POST')
def set_rotation():
    rotation = request.forms.get('count')
    return rotation

if __name__ == '__main__':
    run(app, host='145.24.226.121', port=8080)

The second script is to make a steppermotor run. This is the script.

# importeer de GPIO bibliotheek.
import RPi.GPIO as GPIO
# Importeer de time biblotheek voor tijdfuncties.
from time import sleep
# Zet de pinmode op Broadcom SOC.
GPIO.setmode(GPIO.BCM)
# Zet waarschuwingen uit.
GPIO.setwarnings(False)
# Stel de GPIO pinnen in voor de stappenmotor:
StepPins = [4,17,27,22]

# Set alle pinnen als uitgang.
for pin in StepPins:
  print "Setup pins"
  GPIO.setup(pin,GPIO.OUT)
  GPIO.output(pin, False)

# Definieer variabelen.
StepCounter = 0

# Definieer simpele volgorde
StepCount1 = 4
Seq1 = []
Seq1 = range(0, StepCount1)
Seq1[0] = [1,0,0,0]
Seq1[1] = [0,1,0,0]
Seq1[2] = [0,0,1,0]
Seq1[3] = [0,0,0,1]

# Definieer geadvanceerde volgorde (volgens de datasheet)
StepCount2 = 8
Seq2 = []
Seq2 = range(0, StepCount2)
Seq2[0] = [1,0,0,0]
Seq2[1] = [1,1,0,0]
Seq2[2] = [0,1,0,0]
Seq2[3] = [0,1,1,0]
Seq2[4] = [0,0,1,0]
Seq2[5] = [0,0,1,1]
Seq2[6] = [0,0,0,1]
Seq2[7] = [1,0,0,1]

# Welke stappenvolgorde gaan we hanteren?
Seq = Seq2
StepCount = StepCount2

try:
  while True:
    for pin in range(0, 4):
      xpin = StepPins[pin]
      if Seq[StepCounter][pin]!=0:
        print "Stap: %i GPIO Actief: %i" %(StepCounter,xpin)
        GPIO.output(xpin, True)
      else:
        GPIO.output(xpin, False)

    StepCounter += 1

    # Als we aan het einde van de stappenvolgorde zijn beland start dan opnieuw
    if (StepCounter==StepCount): StepCounter = 0
    if (StepCounter<0): StepCounter = StepCount

    # Wacht voor de volgende stap (lager = snellere draaisnelheid)
    sleep(.01)

except KeyboardInterrupt:  
  # GPIO netjes afsluiten
  GPIO.cleanup()

I need the rotation value from the first script in the sleep function in the second script. Both scripts need to run so the first script can receive POST requests and the second script can make the steppermotor work. What is the best way to do this and is it even possible with this approach?

Jaimy
  • 517
  • 4
  • 20
  • 2
    Why do they have to be different scripts? Can you move the steppermotor code into the webserver? – fafl Apr 20 '16 at 08:29
  • I don't know if this is possible. I'm totally new to python. – Jaimy Apr 20 '16 at 08:31
  • if you want to get something from the server part, just define a GET route that returns `rotation` value and fetch it in the second script. is that what you're trying to do? – Aboud Zakaria Apr 20 '16 at 08:35
  • Yeah that's what I'm trying to do. I thought that by returning the rotation value in the set_rotation function, I could just use that in the other script. But I understand that's not the case? As I said, I'm totally new to python so I have no idea as to what is possible and what isn't – Jaimy Apr 20 '16 at 08:37

2 Answers2

0

maybe you can call the script that controll motor in the first script,

or if you have problem calling directly python function from the http script you can call trought shell with subprocess(https://docs.python.org/2/library/subprocess.html) or similar

i hope have well understand your question

or you can merge the two script like

from bottle import Bottle, run, template, get, post, request

# importeer de GPIO bibliotheek.
import RPi.GPIO as GPIO
# Importeer de time biblotheek voor tijdfuncties.
from time import sleep

app = Bottle()


@app.route('/rotation', method='POST')
def set_rotation():
    rotation = request.forms.get('count')
    rotate(rotation_to_do=rotation)
    return rotation


StepPins = [4, 17, 27, 22]


def inizialize_pin():
    # Zet de pinmode op Broadcom SOC.
    GPIO.setmode(GPIO.BCM)
    # Zet waarschuwingen uit.
    GPIO.setwarnings(False)
    # Stel de GPIO pinnen in voor de stappenmotor:

    # Set alle pinnen als uitgang.
    for pin in StepPins:
        print "Setup pins"
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, False)

# Definieer variabelen.
StepCounter = 0

# Definieer simpele volgorde
StepCount1 = 4
Seq1 = []
Seq1 = range(0, StepCount1)
Seq1[0] = [1, 0, 0, 0]
Seq1[1] = [0, 1, 0, 0]
Seq1[2] = [0, 0, 1, 0]
Seq1[3] = [0, 0, 0, 1]

# Definieer geadvanceerde volgorde (volgens de datasheet)
StepCount2 = 8
Seq2 = []
Seq2 = range(0, StepCount2)
Seq2[0] = [1, 0, 0, 0]
Seq2[1] = [1, 1, 0, 0]
Seq2[2] = [0, 1, 0, 0]
Seq2[3] = [0, 1, 1, 0]
Seq2[4] = [0, 0, 1, 0]
Seq2[5] = [0, 0, 1, 1]
Seq2[6] = [0, 0, 0, 1]
Seq2[7] = [1, 0, 0, 1]

# Welke stappenvolgorde gaan we hanteren?
Seq = Seq2
StepCount = StepCount2


# Definieer variabelen.
StepCounter = 0


def rotate(rotation_to_do=0):
    try:
        while rotation_to_do > 1:
            for pin in range(0, 4):
                xpin = StepPins[pin]
                if Seq[StepCounter][pin] != 0:
                    print "Stap: %i GPIO Actief: %i" % (StepCounter, xpin)
                    GPIO.output(xpin, True)
            else:
                GPIO.output(xpin, False)

            global StepCounter += 1

            # Als we aan het einde van de stappenvolgorde zijn beland start dan opnieuw
            if (StepCounter == StepCount):
                StepCounter = 0
                if (StepCounter < 0):
                    StepCounter = StepCount

            # Wacht voor de volgende stap (lager = snellere draaisnelheid)
            sleep(.01)
            rotation_to_do -= 1

    except KeyboardInterrupt:
        # GPIO netjes afsluiten
        GPIO.cleanup()


if __name__ == '__main__':
    inizialize_pin()
    run(app, host='145.24.226.121', port=8080)
0

I was thinking of using my PI for something similar, collecting data and then send it to another server.

Haven't got there yet but preliminary thoughts were to use pythons multi-threading ability

Check out these links

How to use threading in Python?

http://www.tutorialspoint.com/python/python_multithreading.htm

I will be interested to see how you get on

Community
  • 1
  • 1
hounded
  • 666
  • 10
  • 21