Added Relay Bank Control to Webiopi Pi

Skip to first unread message

Chi Ques

Jul 15, 2019, 3:59:52 PM7/15/19
to WebIOPi

 Hello Community,

I’d like to first start off that I struggle understanding how HTML, JSCRIPT and REST work so in no way I try to sell myself as a pro. For the most part I have been able to take the templates provided by and get them to work for what I need...until now.

I added a relay bank to my Webiopi system. My relay’s work great, they control what I need with the Rpi libraries. The problem is when I send a command to the Pi from a script outside of the webiopi, the whole webiopi seems to loose the ‘link’ which updates the pin’s state.

From what I understand webiopi has a way to create functions in the . My goal is to create an instruction which runs my ‘’ script for 2 seconds after 8pm.
import RPi.GPIO as GPIO

import time

= 9

# Pin Setup:

.setmode(GPIO.BCM) # Broadcom pin-numbering scheme

.setup(relayPin, GPIO.OUT) # relay pin as output

.output(relayPin, GPIO.LOW)


.output(relayPin, GPIO.HIGH)

# GPIO.cleanup()

How do I add the above io control to to:

import webiopi
import datetime

GPIO = webiopi.GPIO

LIGHT = 17 # GPIO pin using BCM numbering

HOUR_ON  = 8  # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00

# setup function is automatically called at WebIOPi startup
def setup():
    # set the GPIO used by the light to output
    GPIO.setFunction(LIGHT, GPIO.OUT)

    # retrieve current datetime
    now =

    # test if we are between ON time and tun the light ON
    if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
        GPIO.digitalWrite(LIGHT, GPIO.HIGH)

# loop function is repeatedly called by WebIOPi 
def loop():
    # retrieve current datetime
    now =

    # toggle light ON all days at the correct time
    if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
        if (GPIO.digitalRead(LIGHT) == GPIO.LOW):
            GPIO.digitalWrite(LIGHT, GPIO.HIGH)

    # toggle light OFF
    if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
        if (GPIO.digitalRead(LIGHT) == GPIO.HIGH):
            GPIO.digitalWrite(LIGHT, GPIO.LOW)

    # gives CPU some time before looping again

# destroy function is called at WebIOPi shutdown
def destroy():
    GPIO.digitalWrite(LIGHT, GPIO.LOW)

Chi Ques

Jul 16, 2019, 8:20:31 PM7/16/19
to WebIOPi
I figured it out. Using the @webiopi.macro to create a function does the trick.
Reply all
Reply to author
0 new messages