There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

czardestructo OP ,
@czardestructo@lemmy.world avatar

Sorry, I forgot to post the scripts. I’m a meathead electrical engineer so I don’t use GIT or anything so here is the code dump. To summarize the setup’s software:

  • cron to run the script that turns the ethernet on and runs rsync to pull data from the server. I have 12 cron entries for the various months/dates/times to run.
  • python script to monitor the button presses for manually running a backup or turning the ethernet port back on
  • bash script that runs the rsync job to pull data from the primary server

The backup script is fairly boring, just runs rsync and pushes the rsync log files back to the primary server. If it fails it sends me an email before turning the ethernet back off and going black.

#So here is my python code that runs the button press:

<pre style="background-color:#ffffff;">
<span style="color:#323232;">#!/usr/bin/env python
</span><span style="color:#323232;">
</span><span style="color:#323232;">import RPi.GPIO as GPIO
</span><span style="color:#323232;">import subprocess
</span><span style="color:#323232;">import time
</span><span style="color:#323232;">from multiprocessing import Process
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">#when this script first runs, at boot, disable ethernet
</span><span style="color:#323232;">time.sleep(5) #wait 5 seconds for system to boot, then try and disable ethernet.
</span><span style="color:#323232;">subprocess.call(['/home/pi/ethernet_updown.sh'], shell=False)
</span><span style="color:#323232;">
</span><span style="color:#323232;">GPIO.setmode(GPIO.BCM)
</span><span style="color:#323232;">GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
</span><span style="color:#323232;">GPIO.setup(22, GPIO.OUT) #controls TFT display backlight
</span><span style="color:#323232;">GPIO.setup(23, GPIO.IN) #pull up or down is optional, the TFT display buttons have a hardware 10k pull up. Measure low tranisitions 
</span><span style="color:#323232;">GPIO.setup(24, GPIO.IN)
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">#watches the button mounted above the USB port, in the Pi's case. 
</span><span style="color:#323232;">def case_button_watch():
</span><span style="color:#323232;">    while True:
</span><span style="color:#323232;">        GPIO.wait_for_edge(3, GPIO.FALLING)
</span><span style="color:#323232;">        #wait 100ms then check if its still low, debounce timer
</span><span style="color:#323232;">        time.sleep(.100)
</span><span style="color:#323232;">        if GPIO.input(3) == GPIO.LOW:
</span><span style="color:#323232;">            #do something as it's a button press
</span><span style="color:#323232;">            print('Button is pressed!')
</span><span style="color:#323232;">            time.sleep(.900)
</span><span style="color:#323232;">            if GPIO.input(3) == GPIO.LOW:
</span><span style="color:#323232;">                #if the button is pressed for over 1 second its a long press. Run the backup script
</span><span style="color:#323232;">                print('Button long press (greater than 1 second), running an unscheduled backup')
</span><span style="color:#323232;">                subprocess.call(['/home/pi/backup.sh'], shell=False)
</span><span style="color:#323232;">            else:
</span><span style="color:#323232;">                #the press was greater than 100mS but less than 1000mS, just toggle the ethernet
</span><span style="color:#323232;">                print('Button short press (less than 1 second), toggling the ethernet')
</span><span style="color:#323232;">                subprocess.call(['/home/pi/ethernet_updown.sh'], shell=False)
</span><span style="color:#323232;">        else:
</span><span style="color:#323232;">            #do nothing as its interference
</span><span style="color:#323232;">            print('GPIO3 debounce failed, it was noise')
</span><span style="color:#323232;">
</span><span style="color:#323232;">#watches the buttons in the TFT display 
</span><span style="color:#323232;">def TFT_display_button1():
</span><span style="color:#323232;">    while True:
</span><span style="color:#323232;">        GPIO.wait_for_edge(23, GPIO.FALLING)
</span><span style="color:#323232;">        #wait 100ms then check if its still low, debounce timer
</span><span style="color:#323232;">        time.sleep(.100)
</span><span style="color:#323232;">        if GPIO.input(23) == GPIO.LOW:
</span><span style="color:#323232;">            #do something as it's a button press
</span><span style="color:#323232;">            print('Button GPIO23 is pressed!')
</span><span style="color:#323232;">            GPIO.output(22, GPIO.HIGH) #turn the backlight ON
</span><span style="color:#323232;">        else:
</span><span style="color:#323232;">            #do nothing as its interference
</span><span style="color:#323232;">            print('GPIO23 debounce failed, it was noise')
</span><span style="color:#323232;">
</span><span style="color:#323232;">#watches the buttons in the TFT display
</span><span style="color:#323232;">def TFT_display_button2():
</span><span style="color:#323232;">    while True:
</span><span style="color:#323232;">        GPIO.wait_for_edge(24, GPIO.FALLING)
</span><span style="color:#323232;">        #wait 100ms then check if its still low, debounce timer
</span><span style="color:#323232;">        time.sleep(.100)
</span><span style="color:#323232;">        if GPIO.input(24) == GPIO.LOW:
</span><span style="color:#323232;">            #do something as it's a button press
</span><span style="color:#323232;">            print('Button GPIO24 is pressed!')
</span><span style="color:#323232;">            GPIO.output(22, GPIO.LOW) #turn the backlight OFF
</span><span style="color:#323232;">        else:
</span><span style="color:#323232;">            #do nothing as its interference
</span><span style="color:#323232;">            print('GPIO24 debounce failed, it was noise')
</span><span style="color:#323232;">
</span><span style="color:#323232;">if __name__ == '__main__':
</span><span style="color:#323232;">
</span><span style="color:#323232;">    #run three parallel processes to watch all three buttons with software debounce
</span><span style="color:#323232;">    proc1 = Process(target=case_button_watch)
</span><span style="color:#323232;">    proc1.start()
</span><span style="color:#323232;">
</span><span style="color:#323232;">    proc2 = Process(target=TFT_display_button1)
</span><span style="color:#323232;">    proc2.start()
</span><span style="color:#323232;">
</span><span style="color:#323232;">    proc3 = Process(target=TFT_display_button2)
</span><span style="color:#323232;">    proc3.start()
</span>

#bash script that toggles the ethernet - if its on, it turns it off. if its off, it turns it on:

<pre style="background-color:#ffffff;">
<span style="color:#323232;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;">if sudo ifconfig | grep 'eth0' | grep 'RUNNING' > /dev/null; 
</span><span style="color:#323232;">then 
</span><span style="color:#323232;">    wall -n "$(date +"%Y%m%d_%H%M%S"):Ethernet going down"
</span><span style="color:#323232;">    sudo ifconfig eth0 down	
</span><span style="color:#323232;">else 
</span><span style="color:#323232;">    wall -n "$(date +"%Y%m%d_%H%M%S"):Ethernet going up"
</span><span style="color:#323232;">    sudo ifconfig eth0 up
</span><span style="color:#323232;">fi
</span>
  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines