doniefendilab
Posts: 3
Joined: Sat Jul 30, 2016 1:56 am
Location: Indonesia

IF Condition for Python Remi Library

Mon Aug 01, 2016 8:46 am

Hi, I use Remi library for control my GPIO pin from browser. I want to control my GPIO pin 11 and the pin will active when i input the Access code from Text Input box. but i don't get how the IF Statement coding, my code below:

Code: Select all

import remi.gui as gui
from remi import start, App
import time
import RPi.GPIO as GPIO


class MyApp(App):
    def __init__(self, *args):
        super(MyApp, self).__init__(*args)

    def main(self):
        container = gui.VBox(width = 120, height = 100)
        self.lbl = gui.Label('1')
        self.txt = gui.TextInput('1')
        self.bt = gui.Button('ActiveMe')
        
        # setting the listener for the onclick event of the button
        self.bt.set_on_click_listener(self, 'on_button_pressed')

        # appending a widget to another, the first argument is a string key
        container.append(self.lbl)
        container.append(self.txt)
        container.append(self.bt)

        # returning the root widget
        return container

    # listener function
    def on_button_pressed(self):
        if self.txt.set_text('1'):          
             GPIO.setmode(GPIO.BOARD)
             GPIO.setup(11,GPIO.OUT)
             time.sleep(5)
             GPIO.setup(11,GPIO.IN)
             GPIO.cleanup()
        else:
             self.lbl.set_text('Code Invalid!')

# starts the webserver
start(MyApp,address='192.168.88.169', port=8181, multiple_instance=False,enable_file_cache=True, update_interval=0.1, start_browser=True)
This my IF Statement but don't work, I want If I input access code 1 from input text box then press button, the PIN 11 will set OUT and then back to IN again.:

Code: Select all

if self.txt.set_text('1'):          
             GPIO.setmode(GPIO.BOARD)
             GPIO.setup(11,GPIO.OUT)
             time.sleep(5)
             GPIO.setup(11,GPIO.IN)
             GPIO.cleanup()
else:
             self.lbl.set_text('Code Invalid!')
Thank's

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: IF Condition for Python Remi Library

Mon Aug 01, 2016 9:58 am

Welcome to the forums.

Your problem does not appear to be related to the Remi library.

I think the problem is a basic misunderstanding of how to use the RPi.GPIO library correctly.

See https://sourceforge.net/p/raspberry-gpi ... asicUsage/

Typically you would use setmode() and setup() calls during the initialisation of your script. The actual toggling of the pin would be performed by using output (channel, state) calls.
State can be 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.

doniefendilab
Posts: 3
Joined: Sat Jul 30, 2016 1:56 am
Location: Indonesia

Re: IF Condition for Python Remi Library

Mon Aug 01, 2016 10:25 am

B.Goode wrote:Welcome to the forums.

Your problem does not appear to be related to the Remi library.

I think the problem is a basic misunderstanding of how to use the RPi.GPIO library correctly.

See https://sourceforge.net/p/raspberry-gpi ... asicUsage/

Typically you would use setmode() and setup() calls during the initialisation of your script. The actual toggling of the pin would be performed by using output (channel, state) calls.
State can be 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.
Hi B.Goode,
Thank's for the info. I control Relay Module 5v, if I using output (channel, state) like you said. It didn't work for me. but using this code, work for me:

Code: Select all

             
                 GPIO.setmode(GPIO.BOARD)
                 GPIO.setup(11,GPIO.OUT)
                 time.sleep(5)
                 GPIO.setup(11,GPIO.IN)
                 GPIO.cleanup()    
I'm sorry if my question make you confuse, I need IF statement for If I input access code ex:1 in Input text box Remi Library then when i press the button execute my code above.

Thank's.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: IF Condition for Python Remi Library

Mon Aug 01, 2016 10:50 am

Disclaimer: I have never used the Remi library, and I have not consulted its documentation.

For debugging purposes, ignoring the gpio handling for now, does this work:

Code: Select all

if self.txt.set_text('1'):          
             self.lbl.set_text('Code 1 sent.')
else:
             self.lbl.set_text('Code Invalid!')

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: IF Condition for Python Remi Library

Mon Aug 01, 2016 11:43 am

Have you run the basic Remi GUI application at https://github.com/dddomodossola/remi

Other examples are at https://github.com/dddomodossola/remi/t ... r/examples

If your query is about this specific library, have you considered using the dedicated Issues page here: https://github.com/dddomodossola/remi/issues

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: IF Condition for Python Remi Library

Mon Aug 01, 2016 3:59 pm

Hello,

It seems that you have made a little error in the if statement.
Instead of using set_text('1') you should use get_text().

Let me know if you solve this way ;-)

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: IF Condition for Python Remi Library

Mon Aug 01, 2016 5:31 pm

Hello doniefendilab,
here is your script, now should be fixed:

Code: Select all

import remi.gui as gui
from remi import start, App
import time
import RPi.GPIO as GPIO


class MyApp(App):
    def __init__(self, *args):
        super(MyApp, self).__init__(*args)

    def main(self):
        container = gui.VBox(width = 120, height = 100)
        self.lbl = gui.Label('1')
        self.txt = gui.TextInput()
        self.txt.set_text('1')
        self.bt = gui.Button('ActiveMe')
        
        # setting the listener for the onclick event of the button
        self.bt.set_on_click_listener(self, 'on_button_pressed')

        # appending a widget to another, the first argument is a string key
        container.append(self.lbl)
        container.append(self.txt)
        container.append(self.bt)

        # returning the root widget
        return container

    # listener function
    def on_button_pressed(self):
        if self.txt.get_text()=='1':
             self.lbl.set_text('ok')
             GPIO.setmode(GPIO.BOARD)
             GPIO.setup(11,GPIO.OUT)
             time.sleep(5)
             GPIO.setup(11,GPIO.IN)
             GPIO.cleanup()
        else:
             self.lbl.set_text('Code Invalid!')

# starts the webserver
start(MyApp,address='192.168.88.169', port=8181, multiple_instance=False,enable_file_cache=True, update_interval=0.1, start_browser=True)

doniefendilab
Posts: 3
Joined: Sat Jul 30, 2016 1:56 am
Location: Indonesia

Re: IF Condition for Python Remi Library

Tue Aug 02, 2016 2:03 am

dddomodossola wrote:Hello doniefendilab,
here is your script, now should be fixed:

Code: Select all

import remi.gui as gui
from remi import start, App
import time
import RPi.GPIO as GPIO


class MyApp(App):
    def __init__(self, *args):
        super(MyApp, self).__init__(*args)

    def main(self):
        container = gui.VBox(width = 120, height = 100)
        self.lbl = gui.Label('1')
        self.txt = gui.TextInput()
        self.txt.set_text('1')
        self.bt = gui.Button('ActiveMe')
        
        # setting the listener for the onclick event of the button
        self.bt.set_on_click_listener(self, 'on_button_pressed')

        # appending a widget to another, the first argument is a string key
        container.append(self.lbl)
        container.append(self.txt)
        container.append(self.bt)

        # returning the root widget
        return container

    # listener function
    def on_button_pressed(self):
        if self.txt.get_text()=='1':
             self.lbl.set_text('ok')
             GPIO.setmode(GPIO.BOARD)
             GPIO.setup(11,GPIO.OUT)
             time.sleep(5)
             GPIO.setup(11,GPIO.IN)
             GPIO.cleanup()
        else:
             self.lbl.set_text('Code Invalid!')

# starts the webserver
start(MyApp,address='192.168.88.169', port=8181, multiple_instance=False,enable_file_cache=True, update_interval=0.1, start_browser=True)
Hi dddomodossola, Thank you for your great response. Yes, that's fix my coding and running smoothly. I tried last night with your first reply, that's make me to thinking and trying to code. and I fixed it with my self but I can't reply your post replay last night. This morning i want to share the code, but you already help that to share the code. once again, thank you that's very helped. very nice Python Library, I recommended your Remi.
I just share, I made this little project for simple access control to unlock small magnetic lock for cupboard, add access it from browser.

And Thank you to B.Goode for your fast response. Very appreciate :D

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: IF Condition for Python Remi Library

Tue Aug 02, 2016 6:38 am

Glad to be helpful. ;)

Return to “Python”