{"id":102,"date":"2015-05-12T01:15:12","date_gmt":"2015-05-12T00:15:12","guid":{"rendered":"http:\/\/www.adienicholls.co.uk\/blogs\/?p=102"},"modified":"2017-03-11T19:50:19","modified_gmt":"2017-03-11T19:50:19","slug":"raspberry-pi-gpio-start-up-script","status":"publish","type":"post","link":"http:\/\/www.adienicholls.co.uk\/blog\/raspberry-pi-gpio-start-up-script\/","title":{"rendered":"Raspberry Pi GPIO Start-up Script"},"content":{"rendered":"<p>On powering\/reset of the Raspberry Pi connected to a relay board, the state of the relays needs to be defined or they just sit in limbo until their first use, this might be fine in some application but not others..<\/p>\n<p>For my Raspberry Pi\u00a0 aquarium system, the &#8216;<a href=\"http:\/\/www.adienicholls.co.uk\/blogs\/pitank-power-bar\/\">original<\/a>&#8216; Web UI does have a script on loading the webpage to define the pin modes but if there is a power cut and the RPi is restarted, we need these values set and activated when the RPi boots in case nobody loads the piTank webpage directly after a reboot &#8211; thus critical components wont be turned on.<\/p>\n<p>The fix to this issue is gpiobootscript.sh<\/p>\n<p><a href=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/05\/gpioreadall.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-106 size-medium\" src=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/05\/gpioreadall-600x448.png\" alt=\"GPIO Readall\" width=\"600\" height=\"448\" srcset=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/05\/gpioreadall-600x448.png 600w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/05\/gpioreadall.png 675w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p>You will need to change the pin numbers below to what you need, This script is controlling an 8 way relay and 1 pin for a temperature\u00a0 probe. I am using pins 0-6 and pin 21 for the relays with pin 7 being used for the temp probes.<\/p>\n<p>IMPORTANT: The pin numbers below are GPIO.X and not bcm or board.<\/p>\n<p>As this script is being run before login you will need to enclose the absolute paths of your folder structure as the OS might not be fully aware of the &#8216;short&#8217; paths within the boot process.<\/p>\n<pre>#!\/bin\/bash\r\n\r\n# this script is run on boot of the Pi to set \r\n# the initial state of the GPIO pins before the # first loading of index.php\r\n\r\n#set gpio.7 to input mode\r\n\/usr\/local\/bin\/gpio mode 7 in;\r\n\r\n#set mode to output\r\nfor i in 0 1 2 3 4 5 6 21; do \/usr\/local\/bin\/gpio mode $i out; done;\r\n\r\n#turn OFF lights\/rain\/spare\r\nfor i in 0 1 2 3 4 21; do \/usr\/local\/bin\/gpio write $i 1; done;\r\n\r\n#turn ON filter and heater\r\nfor i in 5 6; do \/usr\/local\/bin\/gpio write $i 0; done;<\/pre>\n<p>This script is saved in the Pi&#8217;s home folder located at \/home\/pi\/scripts\/.\u00a0 Set the file as executable (seen as this is only a &#8216;.local&#8217; thing I give the file all permissions&#8230;)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-105 size-full\" src=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/05\/rpipermissions.png\" alt=\"\" width=\"373\" height=\"454\" \/><\/p>\n<p>Define when the file is be executed using Crontab, this file is to be run using the &#8216;@reboot&#8217; command (which also works for the initial boot).<\/p>\n<p>As this file is to be run before the actual login, Crontab needs to run as &#8216;sudo&#8217;;<\/p>\n<pre>sudo crontab -e<\/pre>\n<p>Add the below string to the last line of the file that opens using the above command. Make sure there are no empty lines at the bottom of the file.<\/p>\n<pre>@reboot bash \/home\/pi\/scripts\/gpiobootscript.sh &amp;<\/pre>\n<p>To save the change of crontab use Ctrl + O<\/p>\n<p>To exit the text editor after saving the file use Ctrl + X<\/p>\n<p>Reboot the Pi.<\/p>\n<p>Using the &#8216;GPIO readall&#8217; command, check to see if the outcome is as expected &#8211; input ports are set how you want and out ports set with the state you wanted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On powering\/reset of the Raspberry Pi connected to a relay board, the state of the relays needs to be defined or they just sit in limbo until their first use, this might be fine in some application but not others.. For my Raspberry Pi\u00a0 aquarium system, the &#8216;original&#8216; Web UI does have a script on &hellip; <a href=\"http:\/\/www.adienicholls.co.uk\/blog\/raspberry-pi-gpio-start-up-script\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Raspberry Pi GPIO Start-up Script<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[],"_links":{"self":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/102"}],"collection":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":7,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":448,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/448"}],"wp:attachment":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}