{"id":2567,"date":"2019-08-15T16:01:59","date_gmt":"2019-08-15T16:01:59","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=2567"},"modified":"2019-08-15T16:05:17","modified_gmt":"2019-08-15T16:05:17","slug":"raspberry-pi-ping-ip-address-and-toggle-led","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/raspberry-pi-ping-ip-address-and-toggle-led\/","title":{"rendered":"Raspberry Pi &#8211; Ping IP Address and Toggle LED"},"content":{"rendered":"\n<p>The following script is for monitoring if an IP address is reachable or not.  If it becomes  unavailable the script will turn on a LED that is plugged into one of the GPIO pins of the Raspberry Pi.  View pinout <a href=\"http:\/\/www.incredigeek.com\/home\/raspberry-pi-pinouts\/\">here<\/a><\/p>\n\n\n\n<p><strong>Script<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n# Script to ping ip address and turn on LED on if device is unreachable.\n                                                                                                                                                                                                 nPin=\"18\"  # Change if GPIO pin is different                                                                                                     \nledPin=\"gpio${nPin}\"                                                                                                                                                                                                                            toPing=\"8.8.8.8\"  # Change to address you want to ping\n\necho \"${nPin}\" > \/sys\/class\/gpio\/export\necho \"out\" > \/sys\/class\/gpio\/${ledPin}\/direction\n\nif ( fping -r1 $toPing | grep -v alive ); then\n         echo \"Internet unreachable\"\n         # Turn on LED\n         echo \"1\" > \/sys\/class\/gpio\/${ledPin}\/value\n else\n         # Turn off LED \n         echo \"0\" > \/sys\/class\/gpio\/${ledPin}\/value\n fi\n<\/pre>\n\n\n\n<p>Save script as ping_led.sh and make it executable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">chmod +x ping_led.sh<\/pre>\n\n\n\n<p>and run the script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sh ping_led.sh<\/pre>\n\n\n\n<p><strong>Run script in crontab<\/strong><\/p>\n\n\n\n<p>You can setup the script to run every minute using a crontab<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">crontab -e<\/pre>\n\n\n\n<p>Add the following line<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">*\/1 * * * * \/home\/pi\/ping_led.sh<\/pre>\n\n\n\n<p>Should now execute the script every minute and not need any human interaction.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following script is for monitoring if an IP address is reachable or not. If it becomes unavailable the script will turn on a LED that is plugged into one of the GPIO pins of the Raspberry Pi. View pinout &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/raspberry-pi-ping-ip-address-and-toggle-led\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,91],"tags":[210,134,105,606,92,287,173],"class_list":["post-2567","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi","tag-led","tag-monitoring","tag-pi","tag-programming","tag-raspberry-pi-2","tag-rpi","tag-script"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=2567"}],"version-history":[{"count":4,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2567\/revisions"}],"predecessor-version":[{"id":2572,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2567\/revisions\/2572"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=2567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=2567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=2567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}