Adding extra functions

Since we now have the script monitoring the shutdown button all the time, we can add extra buttons/switches/jumpers to be monitored at the same time. This will allow us to trigger specific programs or set up particular states just by changing the inputs. The following example allows us to easily switch between automatic DHCP networking (the default networking setup) and using a direct IP address, as used in the Networking directly to a laptop or computer recipe of Chapter 1, Getting Started with a Raspberry Pi 3 Computer, for direct LAN connections.

Add the following components to the previous circuit:

  • A 470 ohm resistor
  • Two pin headers with a jumper connector (or, optionally, a switch)
  • Breadboard wire (solid core)

After adding the preceding components, our controlled shutdown circuit now looks as follows:

The controlled shutdown circuit layout, reset button, and jumper pins

In the previous script, we add an additional input to detect the status of the LAN_SWA pin (the jumper pins we added to the circuit) using the following code:

LAN_SWA = 11    #2 

Ensure that it is set up as an input (with a pull-up resistor) in the gpio_setup() function using the following code:

GPIO.setup(LAN_SWA,GPIO.IN,pull_up_down=GPIO.PUD_UP) 

Add a new function to switch between the LAN modes and read out the new IP address. The doChangeLAN() function checks whether the status of the LAN_SWA pin has changed since the last call, and if so, it sets the network adapter to DHCP or sets the direct LAN settings accordingly (and uses flite to speak the new IP setting, if available). Finally, the LAN being set for direct connection causes the LED to flash slowly while that mode is active. Use the following code to do this:

def doChangeLAN(direct): 
  if(DEBUG):print("Direct LAN: %s" % direct) 
  if GPIO.input(LAN_SWA) and direct==True: 
    if(DEBUG):print("LAN Switch OFF") 
    cmd="sudo dhclient eth0" 
    direct=False 
    GPIO.output(LED,1) 
  elif GPIO.input(LAN_SWA)==False and direct==False: 
    if(DEBUG):print("LAN Switch ON") 
    cmd="sudo ifconfig eth0 169.254.69.69" 
    direct=True 
  else: 
    return direct 
  if(DEBUG==False):os.system(cmd) 
  if(SNDON):os.system("hostname -I | flite") 
  return direct 

Add another function, flashled(), which will just toggle the state of the LED each time it is called. The code for this function is as follows:

def flashled(ledon): 
  if ledon: 
    ledon=False 
  else: 
    ledon=True 
  GPIO.output(LED,ledon) 
  return ledon

Finally, we adjust the main loop to also call doChangeLAN() and use the result to decide whether we call flashled() using ledon to keep track of the LED's previous state each time. The main() function should now be updated as follows:

def main(): 
  gpio_setup() 
  GPIO.output(LED,1) 
  directlan=False 
  ledon=True 
  while True: 
    if(DEBUG):print("Waiting for >3sec button press") 
    if GPIO.input(SHTDWN_BTN)==False: 
       doShutdown() 
    directlan= doChangeLAN(directlan) 
    if directlan: 
      flashled(ledon) 
    time.sleep(1) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.113.188