Suggestion for a power boost, stable UPS

didnt work for me…obliviously i changed the pin numbers

This work! You should press for 3 seconds the button to shutdown the system

import RPi.GPIO as GPIO
import time
import os
stopcount = settingcount = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True:
    #print GPIO.input(10)
    if (GPIO.input(10) == False):
        stopcount = stopcount + 1
        #print stopcount
        if stopcount == 30:       # 3 secondi di pressione e si spegne
            #print 'reboot'
            os.system("sudo shutdown -h now")
    else:   # means GPIO10 == True
        #print 'stopcount reset'
        stopcount = settingcount = 0
    time.sleep(0.1) # is sleep(1) ok or does it need to be shorter. for example sleep(0.1)

I might have missed something, I didn’t post the whole code, I just pulled out part on my code dealing with shutdown and made one edit to simplify it. The edit is likely what messed it up. The full file is as follows and the shut down works. It’s delayed until the end of my message. I think you’ll see why I didn’t post the full file, there is a lot going on.

import os
import time, datetime
import bme680
import SI1145.SI1145 as SI1145
import RPi.GPIO as GPIO
from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
   
sense = SenseHat()
sense.set_rotation(180)
sense.set_imu_config(False, False, False)
sense.low_light = False

uvs = SI1145.SI1145()
sensor = bme680.BME680()

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)

sensor.set_gas_status(bme680.DISABLE_GAS_MEAS)
#sensor.set_gas_heater_temperature(320)
#sensor.set_gas_heater_duration(150)
#sensor.select_gas_heater_profile(0)


GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_OFF)  
GPIO.setup(16,GPIO.OUT, initial=1) #Red
GPIO.setup(19,GPIO.OUT, initial=1) #Yellow
GPIO.setup(20,GPIO.OUT, initial=1) #Green
GPIO.setup(21,GPIO.OUT, initial=1) #Blue


s=(0.1) # scroll speed
w=(0) # color all white toggle
o=(165)
L=(1) #LED's on or off
x=(2) #shutdown variable
m=(0)

def Shutdown(channel):  
    global x
    x = (0)

def readvis():
    vis = uvs.readVisible()
    vis = (round(vis))

    global w
    global o
    global L
    global m

    if vis < 270 and m == (0):
        sense.low_light = True
        w = (0)
        o = (165)
        L = (1)
    elif vis >= 270 and vis < 600 and m == (0):
        sense.low_light = False
        w = (0)
        o = (165)
        L = (0)
    elif vis >= 600 and m == (0):
        sense.low_light = False
        w = (255)
        o = (255)
        L = (0)
    
# is really stick down
def pushed_up(event):
    global L
    global m
    if event.action == ACTION_PRESSED:
       sense.low_light = True
       L = (1)
       m = (1)
        
# is really stick up
def pushed_down(event):
    global L
    global m
    if event.action == ACTION_PRESSED:
       sense.low_light = False
       L = (0)
       m = (1)

#is really stick right
def pushed_left(event):
    global w
    global o
    global m
    if event.action == ACTION_PRESSED:
        w = (255)
        o = (255)
        m = (1)
        
# is really stick left
def pushed_right(event):
    global w
    global o
    global m
    if event.action == ACTION_PRESSED:
        w = (0)
        o = (165)
        m = (1)

def pushed_middle(event):
    global m
    if event.action == ACTION_PRESSED:
        m = (0)

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right
sense.stick.direction_middle = pushed_middle

GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

while True:

    readvis()

    dateString = "%A %B %-d %-I:%M:%p"
    msg = "It is %s" % (datetime.datetime.now().strftime(dateString))
    sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, 255))


    if sensor.get_sensor_data(): 

       t = (sensor.data.temperature) 
       t = (round(t))
          
    if t <= 0: 
        tc = [w, w, 255]   # Blue
        #GPIO.output(16, 1) # Red
        #GPIO.output(19, 1) # Yellow
        #GPIO.output(20, 1) # Green
        #GPIO.output(21, L) # Blue <
    elif t > 0 and t < 13:
        tc = [255, 255, w] # Yellow
        #GPIO.output(16, 1) # Red
        #GPIO.output(19, L) # Yellow <
        #GPIO.output(20, 1) # Green
        #GPIO.output(21, 1) # Blue
    elif t >= 13 and t <= 25:
        tc = [w, 255, w]   # Green
        #GPIO.output(16, 1) # Red
        #GPIO.output(19, 1) # Yellow
        #GPIO.output(20, L) # Green <
        #GPIO.output(21, 1) # Blue
    else:
        tc = [255, w, w]   # Red
        #GPIO.output(16, L) # Red <
        #GPIO.output(19, 1) # Yellow
        #GPIO.output(20, 1) # Green
        #GPIO.output(21, 1) # Blue
        
    msg = "and %sc" % (t)
    sense.show_message(msg, scroll_speed=s, text_colour=tc)

    readvis()

    if sensor.get_sensor_data(): 
       h = (sensor.data.humidity)
       h = (round(h))

    if h < 0:
        h = 0

    if h > 100:
        h = 100

    if h < 30:
        hc = [255, w, w]  # Red
    elif h >= 30 and h <= 60:
        hc = [w, 255, w]  # Green
    elif h > 60 and h < 80:
        hc = [255, 255, w]  # Yellow
    elif h >= 80:
        hc = [255, w, w]  # Red

    msg = "with %s%% Humidity" % (h)
    sense.show_message(msg, scroll_speed=s, text_colour=hc)

    readvis()

    if sensor.get_sensor_data(): 
       p = (sensor.data.pressure) 
       p = round(p)
        
    if p > 0 and p < 985:
        pc = [255, w, w]  # Red
        GPIO.output(16, L) # Red <
        GPIO.output(19, 1) # Yellow
        GPIO.output(20, 1) # Green
        GPIO.output(21, 1) # Blue
        msg = "- Barometer is Very Low @ %smb - Storm Watch" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 985 and p < 1005:
        pc = [255, 255, w]  # Yellow
        GPIO.output(16, 1) # Red
        GPIO.output(19, L) # Yellow <
        GPIO.output(20, 1) # Green
        GPIO.output(21, 1) # Blue
        msg = "- Barometer is Low @ %smb - Possible Percipitation" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 1005 and p < 1025:
        pc = [w, 255, w]  # Green
        GPIO.output(16, 1) # Red
        GPIO.output(19, 1) # Yellow
        GPIO.output(20, L) # Green <
        GPIO.output(21, 1) # Blue
        msg = "- Barometer is Mid Range @ %smb" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 1025 and p < 1050:
        pc = [w, w, 255]  # Blue
        GPIO.output(16, 1) # Red
        GPIO.output(19, 1) # Yellow
        GPIO.output(20, 1) # Green
        GPIO.output(21, L) # Blue <
        msg = "- Barometer is High @ %smb" % (p)
        sense.show_message(msg, scroll_speed=s, text_colour=pc)
    elif p >= 1050:
        pc = [255, w, w]  # Red
        GPIO.output(16, L) # Red <
        GPIO.output(19, 1) # Yellow
        GPIO.output(20, 1) # Green
        GPIO.output(21, 1) # Blue
        msg = "- Barometer is Very High @ %smb - Expect Dry Conditions" % (p) 
        sense.show_message(msg, scroll_speed=s, text_colour=pc)

    readvis()

    uv = uvs.readUV()
    u = uv/100
    u = (round(u))

    if u > 0 and u < 3 and L == 0:
        msg = "- UV Index is Low @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, w)) # Green        
    elif u >= 3 and u < 6 and L == 0:
        msg = "- UV Index is Moderate @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, 255, w)) # Yellow        
    elif u >= 6 and u < 8 and L == 0:
        msg = "- UV Index is High @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, o, w)) # Orange       
    elif u >= 8 and u < 11 and L == 0:
        msg = "- UV Index is Very High @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, w ,w)) # Red
    elif u >= 11 and L == 0:
        msg = "- UV Index is Extreme @ %s" % (u)
        sense.show_message(msg, scroll_speed=s, text_colour=(255, w, 255)) # Violet
        
    #vis = uvs.readVisible()
    #vis = (round(vis)) 

    #msg = "and the VIS is %s" % (vis)
    #sense.show_message(msg, scroll_speed=s, text_colour=(255, w, 255))

    if x == 0:
        sense.clear()
        os.system("sudo shutdown now -P")
        time.sleep(30)
    elif x == 1:
        sense.clear()
        raise SystemExit
        time.sleep(30)

# Last edited on Jun 9th 2018 summer config, 10mm LEDs set to pressure.
# run sudo crontab -e
# add
# @reboot python3 /home/pi/THPUVBME.py &
1 Like