<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stephanie</title>
    <description>The latest articles on DEV Community by Stephanie (@stephaniecodes).</description>
    <link>https://dev.to/stephaniecodes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F869%2F5478ae12-2b97-469e-872d-a70af5951f25.jpg</url>
      <title>DEV Community: Stephanie</title>
      <link>https://dev.to/stephaniecodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stephaniecodes"/>
    <language>en</language>
    <item>
      <title>Build a safe way to shutdown a headless raspberry pi with a tiny button.</title>
      <dc:creator>Stephanie</dc:creator>
      <pubDate>Sat, 03 Aug 2019 20:06:33 +0000</pubDate>
      <link>https://dev.to/stephaniecodes/build-a-safe-way-to-shutdown-a-headless-raspberry-pi-with-a-tiny-button-2ca3</link>
      <guid>https://dev.to/stephaniecodes/build-a-safe-way-to-shutdown-a-headless-raspberry-pi-with-a-tiny-button-2ca3</guid>
      <description>&lt;p&gt;When you work with a hardware project using the raspberry pi, it is important to always shut down the pi gracefully to prevent the pi's SD card from data corruption. &lt;/p&gt;

&lt;p&gt;In my latest project, I am using a RPI 3B+ in a wearable that is attached to my body. In order to safely shut it down without having to SSH to it from my laptop, I created this little button that plugs into the GPIO header that can trigger a safe and graceful shutdown of the Pi when it's time to turn it off.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--81Xynbe8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/htiqzm2cq2262bdjmfeh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--81Xynbe8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/htiqzm2cq2262bdjmfeh.jpg" alt="raspberry pi with shutdown button attached"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Make the button:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Cut a female-female jumper cable in half.&lt;/li&gt;
&lt;li&gt;Strip the ends and solder each to one pin on one side of the button. &lt;/li&gt;
&lt;li&gt;Clip off the pins on the other side of the button.&lt;/li&gt;
&lt;li&gt;Plug ends into 1 ground and 1 GPIO pin on your pi. I used GPIO #12 &amp;amp; the ground next to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My soldered button:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ii-UW9XJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ypmdaevmbiacw31avxmw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ii-UW9XJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ypmdaevmbiacw31avxmw.jpg" alt="Soldered button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Attached to the Pi:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XDfWq6W9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v8vw600t1a81sw1nieme.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XDfWq6W9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v8vw600t1a81sw1nieme.jpg" alt="button attached to GPIO 12 and a ground on the pi"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Write a program for the button:
&lt;/h3&gt;

&lt;p&gt;Thanks to Alex Glow's &lt;a href="https://www.hackster.io/glowascii/raspberry-pi-shutdown-restart-button-d5fd07"&gt;article&lt;/a&gt; on creating a similar button, I was able to use the script mentioned and modified it for my pi.&lt;/p&gt;

&lt;p&gt;(I created this program by typing &lt;code&gt;touch shutdown_pi.py&lt;/code&gt;, then opening the file: &lt;code&gt;sudo nano shutdown_pi.py&lt;/code&gt; and pasting the following code and saving it (CTRL+X).&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python 
# Simple script for shutting down the raspberry Pi at the press of a button. 
# by Inderpreet Singh 
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;RPi.GPIO&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;GPIO&lt;/span&gt;  
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;  
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;

&lt;span class="c1"&gt;# Use the Broadcom SOC Pin numbers 
# Setup the Pin with Internal pullups enabled and PIN in reading mode. 
&lt;/span&gt;&lt;span class="n"&gt;GPIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setmode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;GPIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BCM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="n"&gt;GPIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GPIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pull_up_down&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GPIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PUD_UP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Our function on what to do when the button is pressed 
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;Shutdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
   &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"sudo shutdown -h now"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c1"&gt;# Add our function to execute when the button pressed event happens 
&lt;/span&gt;&lt;span class="n"&gt;GPIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_event_detect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GPIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FALLING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Shutdown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bouncetime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c1"&gt;# Now wait! 
&lt;/span&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
   &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Create a cronjob:
&lt;/h3&gt;

&lt;p&gt;We want this program to run on boot and run in the background &lt;br&gt;
so that when the button is pressed, the program will trigger &lt;br&gt;
the shutdown script to run. You can trigger programs to run at boot&lt;br&gt;
a few diff ways on the pi, but I already use cronjobs, so I edited that file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;crontab -e&lt;/code&gt; (if it's your first time, I'd choose option 2 and use nano to edit the file).&lt;/li&gt;
&lt;li&gt;At bottom of the file, add &lt;code&gt;@reboot python /home/pi/shutdown_pi.py &amp;amp;&lt;/code&gt; (Tip: the &lt;code&gt;&amp;amp;&lt;/code&gt; at the end tells the pi to run the program in the background)&lt;/li&gt;
&lt;li&gt;Press CTRL+X to save &amp;amp; exit.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Test it!
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;sudo shutdown -r now&lt;/code&gt; to reboot the pi.&lt;/li&gt;
&lt;li&gt;Press the button after it reboots and it should safely shutdown the pi now! 🎉&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  UPDATE (4-Aug-2019):
&lt;/h4&gt;

&lt;p&gt;I wasn't aware at the time I built this, but there is a potentially easier &lt;a href="https://www.stderr.nl/Blog/Hardware/RaspberryPi/PowerButton.html"&gt;solution&lt;/a&gt;! &lt;/p&gt;

&lt;p&gt;Solder the button, but plug it into GPIO #3 (I2C-SCL) and ground (pins 5 &amp;amp; 6). Add the line &lt;code&gt;dtoverlay=gpio-shutdown,gpio_pin=3&lt;/code&gt; to the &lt;code&gt;/boot/config.txt&lt;/code&gt; file and reboot. Should allow for booting AND shutdown. &lt;/p&gt;

&lt;p&gt;In my experience, only the booting was working, not the shutdown, so I stuck with the cronjob above! ✌️&lt;/p&gt;

</description>
      <category>raspberrypi</category>
      <category>hardware</category>
      <category>pi</category>
    </item>
    <item>
      <title>Nevertheless, Stephanie Nemeth Coded</title>
      <dc:creator>Stephanie</dc:creator>
      <pubDate>Thu, 08 Mar 2018 15:10:58 +0000</pubDate>
      <link>https://dev.to/stephaniecodes/nevertheless-stephanie-nemeth-coded--3afe</link>
      <guid>https://dev.to/stephaniecodes/nevertheless-stephanie-nemeth-coded--3afe</guid>
      <description>&lt;h2&gt;
  
  
  I started coding...
&lt;/h2&gt;

&lt;p&gt;because I needed a career change 3 years ago. I started out as a dev intern at a Ruby on Rails shop and development into a full fledged dev. I still feel like I am just treading water most of the time, but I think I've found my niche in frontend development.&lt;/p&gt;

&lt;h2&gt;
  
  
  I'm excited about...
&lt;/h2&gt;

&lt;p&gt;LEDs! No seriously, I'm excited how I've turned my weird LEDs/hardware/wearables hobby into opportunity to speak at conferences. I'm speaking at a bunch of frontend/javascript conferences this year, which is exciting, but also terrifying for me. I only spoke at my first conference last year.&lt;/p&gt;

&lt;h2&gt;
  
  
  My advice for allies to support women and non-binary folks who code is....
&lt;/h2&gt;

&lt;p&gt;Listen to them and amplify their voices.&lt;/p&gt;

</description>
      <category>wecoded</category>
    </item>
  </channel>
</rss>
