1

For New Year's Eve we a are building a launch pad for rockets.

We are using a Raspberry Pi and a self built board to drive relays using GPIO. For a GUI we set up a simple webserver with WebPy and a Javascript application. This application can then send a POST request to the Pi to set a pin to true, and after a defined time, the pin is set to false.

At the same time, the status of the pins should be read using a timeout of 1s.

Whenever I try to read the status, the pin is reset to false.

import RPi.GPIO as GPIO
import web
import json
from time import sleep

render = web.template.render('templates/')

class index:
    def __init__(self):
        GPIO.setmode(GPIO.BOARD)
        self.pins = [13, 11, 7, 15, 19]
        for pin in self.pins:
            GPIO.setup(pin, GPIO.OUT)

    def GET(self, page):
        if page == '':
            return render.index()
        if page == 'status':
            stati = []
            for pinNo in self.pins:
                GPIO.setup(pinNo, GPIO.IN)
                status = pin(pinNo, GPIO.input(pinNo))
                stati.append(status)
            # return the stati

    def POST(self, page):
        if page == 'shoot':
            params = json.loads(web.data())
            pin = int(params['pin'])
            time = int(params['time'])
            if pin in self.pins and time < 25:
                GPIO.setup(pin, GPIO.OUT)
                GPIO.output(pin, True)
                sleep(time)
                GPIO.setup(pin, GPIO.OUT)
                GPIO.output(pin, False)
            #return some data

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()
3

I know I'm answering an old post, but this is pretty high in Google, so I wanted to share the solution to the issue ...

The problem is that you are changing the mode of the pin before checking it's status.

To read the status of the pin, you should not change the mode to input, but rather set it as output and call input on that pin. Changing the mode from output to input resets the pin.

The code should read:

    if page == 'status':
        stati = []
        for pinNo in self.pins:
            #set the pin as output, not input
            GPIO.setup(pinNo, GPIO.OUT) 
            status = pin(pinNo, GPIO.input(pinNo))
            stati.append(status)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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