How to change default value (IN/OUT) of the GPIO pins?

1,256 views
Skip to first unread message

Kevin Bright

unread,
Mar 22, 2016, 8:22:30 PM3/22/16
to WebIOPi
I'm using my Raspberry Pi as a home automation tool via some relays connected to the GPIO pins.  Everything works great (especially on the most recent version 0.7.1), but when the Raspberry Pi gets shut down, I have to go back and change all of the pins back to OUT from IN, or it doesn't work.  Is there a way to change the default values so after a reboot, the GPIO pins are automatically set to OUT?  

Thanks. 

craisondigital

unread,
Mar 22, 2016, 9:49:55 PM3/22/16
to WebIOPi
In the Def setup Section of your script, you can set the pins to input or output.

def setup():
GPIO.setFunction(PinNumber, GPIO.OUT)

Kevin Bright

unread,
Mar 22, 2016, 9:55:26 PM3/22/16
to WebIOPi
Thanks... which file is this and where is it? 

Kevin Bright

unread,
Mar 23, 2016, 9:36:15 AM3/23/16
to WebIOPi
Can anyone help me out on this? 

craisondigital

unread,
Mar 23, 2016, 9:55:30 AM3/23/16
to WebIOPi
You should start with the tutorials.  Creating a custom script is one of the first things to do within webiopi.  This is where you define what happens on startup, loop and destroy.

Once you make your python script, you edit /etc/webiopi/config to point to your new script.

Kevin Bright

unread,
Mar 23, 2016, 10:01:03 AM3/23/16
to WebIOPi
Thanks... I didn't create any custom scripts...  All I did was customize the index.html file to include only the buttons that I needed.  So where would the "stock" script be located?  

craisondigital

unread,
Mar 23, 2016, 10:07:55 AM3/23/16
to WebIOPi
I dont know, but making a custom script is very easy...  Here is an example..

1.  Type sudo nano ~/script.py (This will create the script in your home folder)

2.  Type the following within script.py

import webiopi

GPIO = webiopi.GPIO

RELAY1 = 18 (Change these numbers to the GPIO numbers of your relays) 
RELAY2 = 22

def setup():


On Tuesday, March 22, 2016 at 8:22:30 PM UTC-4, Kevin Bright wrote:

craisondigital

unread,
Mar 23, 2016, 10:11:21 AM3/23/16
to WebIOPi
Sorry hit return by mistake, here is the script..

import webiopi
GPIO = webiopi.GPIO
RELAY1 = 18 (Change these numbers to the GPIO numbers of your relays) 
RELAY2 = 22
def setup():
      GPIO.setFunction(RELAY1, GPIO.OUT)
      GPIO.setFunction(RELAY2, GPIO.OUT)
def destroy():
      GPIO.digitalWrite(RELAY1, GPIO.LOW)
      GPIO.digitalWrite(RELAY2, GPIO.LOW)

Now save the file, and type:

sudo nano /etc/webiopi/config

Scroll down to the #SCRIPTS section and uncomment the last line and change it to read..


myscript = /home/pi/script.py

now restart webiopi

Kevin Bright

unread,
Mar 23, 2016, 10:21:17 AM3/23/16
to WebIOPi
Thanks for your help...  I'll give it a try when I get home.  

Kevin Bright

unread,
Mar 24, 2016, 10:27:14 AM3/24/16
to WebIOPi
I tried your suggestion, but for some reason, when I set more than one GPIO to out, I get the following error message in my browser:

"This site can't be reached 
192.168.1.100 refused to connect 
ERR_CONNECTION_REFUSED"

I cant connect via SSH using Putty, but any attempt to connect via the browser fails.  

The weird thing is, if I set only one GPIO to OUT, it works fine for that GPIO pin.   

Here's my code:

import webiopi
GPIO = webiopi.GPIO

def setup():

     GPIO.setFunction(22, GPIO.OUT)
     GPIO.setFunction(27, GPIO.OUT)
     GPIO.setFunction(23, GPIO.OUT)
     GPIO.setFunction(25, GPIO.OUT)
     GPIO.setFunction(17, GPIO.OUT)

def destroy():
     GPIO.digitalWrite(22, GPIO.LOW)
     GPIO.digitalWrite(27, GPIO.LOW)
     GPIO.digitalWrite(23, GPIO.LOW)
     GPIO.digitalWrite(25, GPIO.LOW)
     GPIO.digitalWrite(27, GPIO.LOW)

I also uncommented the last light of the SCRIPTS section in the config file and redirected it to the location of my "script.py" file.  

Any idea on why this isn't working?  

I noticed that in my "home" directory, there is another file named "script.py save".  Do you think that is messing this up? 

Thanks. 


On Wednesday, March 23, 2016 at 10:11:21 AM UTC-4, craisondigital wrote:

Kevin Bright

unread,
Mar 24, 2016, 10:38:18 AM3/24/16
to WebIOPi
After further investigation, everything works if I leave out the entire "def:destroy()" function.  What is the purpose of that function and do I need it? 

Thanks. 

Toshi Bass

unread,
Mar 24, 2016, 10:42:31 AM3/24/16
to WebIOPi

def destroy is safe close down script for gpio, its incorrect in above post, it should be :

def destroy():
     GPIO.digitalWrite(22, GPIO.IN)
     GPIO.digitalWrite(27, GPIO.IN)
     GPIO.digitalWrite(23, GPIO.IN)
     GPIO.digitalWrite(25, GPIO.IN)
     GPIO.digitalWrite(27, GPIO.IN)
Message has been deleted

Kevin Bright

unread,
Mar 24, 2016, 11:03:27 AM3/24/16
to WebIOPi
Thanks...

I tried that, but I got the same error message about refusing to connect.  I'm not sure why this is messing anything up since (based on my research), this method is only called when the system goes down for a reboot, so on start up it shouldn't even be getting called.  

craisondigital

unread,
Mar 24, 2016, 11:54:50 AM3/24/16
to WebIOPi
Probably a spacing issue in python.  Make sure all spacing is consistent throughout script.

Kevin Bright

unread,
Mar 24, 2016, 12:31:53 PM3/24/16
to WebIOPi
I'm at a total loss on this...  I confirmed that all spacing is consistent, but I still get the error message when I uncomment the "destroy" method.  

Kevin Bright

unread,
Mar 24, 2016, 12:37:25 PM3/24/16
to WebIOPi
Here's a screenshot of my script.py file:

http://imgur.com/r1Xm7fG

Toshi Bass

unread,
Mar 24, 2016, 12:44:49 PM3/24/16
to WebIOPi

Whoops my mistake sorry:

     GPIO.setFunction(22, GPIO.IN)
     GPIO.setFunction(27, GPIO.IN)
     GPIO.setFunction(23, GPIO.IN)
     GPIO.setFunction(25, GPIO.IN)
     GPIO.setFunction(17, GPIO.IN)


Kevin Bright

unread,
Mar 24, 2016, 12:46:09 PM3/24/16
to WebIOPi
So setFunction in the destroy method rather than digitalWrite?  

craisondigital

unread,
Mar 24, 2016, 12:48:49 PM3/24/16
to WebIOPi
Ah yes. Set function when changing from in and out.

Digital write when changing high low

Kevin Bright

unread,
Mar 24, 2016, 12:53:22 PM3/24/16
to WebIOPi
Thanks for all your help, but it's still not working even after that change.  I'm not familiar with Python, but the "destroy" method seems similar to the "onDestroy()" method in Android.  If that's the case, it should only get called when webiopi stops or the system get's a reboot.  Thus, I really don't undertand why having that code has any affect on the connecting via the ip address. 

Pete Dudash

unread,
Mar 24, 2016, 12:58:21 PM3/24/16
to WebIOPi

The tutorials online say to set the outputs low during the destroy. I suppose input would be safer in case you connect something while the Pi is running since the pin would be in a high-impedance state.

Kevin, you haven’t mentioned anything about your javascript yet. Are you still trying to change the gpio direction or states in there? Also, be sure to run in debug mode to catch any errors. To do so, enter this in your terminal sudo webiopi -d -c /etc/webiopi/config. Since you’ll be in debug mode, it would help to put some print statements in your script to verify it is running. Try adding this between your setup and destroy routines:

def loop():
    print(“Relay 1 = {}”.format(GPIO.digitalRead(RELAY1)))
    print(“Relay 2 = {}”.format(GPIO.digitalRead(RELAY2)))
    print(“Relay 3 = {}”.format(GPIO.digitalRead(RELAY3)))
    print(“Relay 4 = {}”.format(GPIO.digitalRead(RELAY4)))
    print(“Relay 5 = {}”.format(GPIO.digitalRead(RELAY5)))
    webiopi.sleep(1)

Lastly, try looking at your javascript console to see any errors that might pop up in your browser. In Chrome, press F12. It is possible that your HTML or javascript is causing problems too. Maybe if you post those files too we can find the cause of your troubles.

Toshi Bass

unread,
Mar 24, 2016, 1:56:49 PM3/24/16
to WebIOPi
The tutorials code is wrong, def destroy(): should reset any ports you have used in this program back to input mode. This prevents damage from, say, a situation where you have a port set HIGH as an output and you accidentally connect it to GND (LOW), which would short-circuit the port and possibly fry it. Inputs can handle either 0V (LOW) or 3.3V (HIGH), so it’s safer to leave ports as inputs.

Python checks syntax including spacing before running the code so if you have a error in def destroy(): even before that part of the code is called it will prevent the code from running.

As Pete says its essential to debug your python code by running   sudo webiopi -d -c /etc/webiopi/config  also if you are using dhrome browser you can debug your javascript  ctrl shift i

You say error you have is : 
"This site can't be reached 
192.168.1.100 refused to connect 
ERR_CONNECTION_REFUSED"

Are you entering 192.168.1.100 or the correct one 192.168.1.100:8000

Finally as well as the tutorials look at the example folder in the webiopi directory although there are some errors in these files also.

Kevin Bright

unread,
Mar 24, 2016, 3:20:37 PM3/24/16
to WebIOPi
Thanks... yes, I am connecting to 192.168.1.100:8000... not sure why the error message missed the 8000 part.  I will try to debug later on and see what error messages I get.  In the meantime, I'm assuming it's safe to leave everything set to OUT, since everything is hardwarded and I'm not switching anything around.  

Kevin Bright

unread,
Mar 24, 2016, 3:40:55 PM3/24/16
to WebIOPi
Thanks... I'll give that a try.  I haven't made any changes to anything except the addition of the script.py file in the /home directory and I customized the index.html file in /usr/share/webiopi/htdocs.  

craisondigital

unread,
Mar 24, 2016, 4:21:32 PM3/24/16
to WebIOPi
I wouldn't change the default index page, rather create your own, and point to it in..

/etc/webiopi/config
Reply all
Reply to author
Forward
0 new messages