Page 1 of 1

2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 2:03 am
by stasimon
Hi all,

I have problem with merge 2 python scripts for Raspberry Pi 3b+ with Retropie. 1st script control fan + led on when PI is on. 2nd script is for reset/poweroff system.

1st:

Code: Select all

#!/usr/bin/env python3

import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO
pin1 = 18
led1 = 23
maxTMP = 60
minTMP = 40

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pin1, GPIO.OUT)
    GPIO.setup(led1, GPIO.OUT)
    GPIO.setwarnings(False)
    GPIO.output(led1, GPIO.HIGH)
    return()
    
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    temp =(res.replace("temp=","").replace("'C\n",""))
   # print("temp is {0}".format(temp))
    return temp
    
def getTEMP():
    CPU_temp = float(getCPUtemperature())
    if CPU_temp>maxTMP:
        setPin1(True)
    elif CPU_temp<minTMP:
        setPin1(False)
    return()

def setPin1(mode):
    GPIO.output(pin1, mode)
    return()
    
try:
    setup()
    while True:
        getTEMP()
        sleep(5)
        
except KeyboardInterrupt:
    GPIO.cleanup()
2nd:

Code: Select all

#!/usr/bin/python3

from gpiozero import Button
from signal import pause
from subprocess import check_call
off1 = 3

held_for=0.0

def rls():
        global held_for
        if (held_for > 5.0):
                check_call(['/sbin/poweroff'])
        elif (held_for > 2.0):
                check_call(['/sbin/reboot'])
        else:
                held_for = 0.0

def hld():
        global held_for
        held_for = max(held_for, button.held_time + button.hold_time)

button=Button(off1, hold_time=1.0, hold_repeat=True)
button.when_held = hld
button.when_released = rls
pause()
Can someone merge those 2 scripts into 1? I can't get it. Maybe it can't be done?

Thanks guys for your help.

Re: 2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 2:31 am
by PhatFil
Hi

what do you want the new script to do?

Post your best effort and any errors it generates when you try to execute it..

Both are Python3 so it should be ok..

start by copying the imports from one file into another below the originals and delete any exact duplicates

then move the declarations (inc functions) over and look out for duplicate variable/function names, these will need changing for one side..

then copy the main execution block and if it looks like its doing what you want give it a whirl...

Re: 2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 3:02 am
by stasimon
Sorry, I'm totaly newbie in python, I found similar scripts on internet and only change it for me. Spent few days by method change/test untill both scripts start work. I want merge both scriots into one for my retropie project. So 1 script should do what bouth doing separate, start LED on system boot, check temperature every 5 sec and start fan when is higher than 60 and stop when is lower than 40 and reset (push in button switch longer than 2 sec) or poweroff (push in button switch longer than 5 sec)

Like I said both scripts work ok when separate, when merge it into one, after run it I don't have any error but only led + fan work, not reset/switch off. I'm on beggining with python and it's really dificult for me (never program anything before)

Re: 2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 3:38 am
by PhatFil
stasimon wrote:
Mon Dec 31, 2018 3:02 am
Sorry, I'm totaly newbie in python, I found similar scripts on internet and only change it for me. Spent few days by method change/test untill both scripts start work. I want merge both scriots into one for my retropie project. So 1 script should do what bouth doing separate, start LED on system boot, check temperature every 5 sec and start fan when is higher than 60 and stop when is lower than 40 and reset (push in button switch longer than 2 sec) or poweroff (push in button switch longer than 5 sec)

Like I said both scripts work ok when separate, when merge it into one, after run it I don't have any error but only led + fan work, not reset/switch off. I'm on beggining with python and it's really dificult for me (never program anything before)
Ok so now we have some requirements

start led on system boot (at script start, and schedule script to run at start up perhaps? ;)

monitor temp regularly (5 seconds) perhaps a little longer ??
and turn fans on when over a threshold temp..
Turn off again when cool?
and also monitor reset switch state and if pressed reboot the system.

sounds very do-able and you have broken the back of the job with 2 independent scrips so post what you have and lets see..

It might help if you write the program logic out in plain langauge

something like

Code: Select all

Turn on led

while true
         If temp > threshold
               turn on fans
         if temp < normal
               turn off fans
         if reset = pressed
                reboot system
         sleep 5 seconds
Now there is a problem here .. what if the reset button is hit durring the 5 seconds of sleep time ??? Hmmm

Code: Select all

Turn on led
time0, time1= now
while true
        if reset = pressed
                reboot system
         if time1-time0 =0
               	If temp > threshold
               		turn on fans
         	if temp < normal
               		turn off fans
        time1= now
        if time1 >= time0 +5seconds
        	time0,time1=now       		
      
         
should do it..

Re: 2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 3:45 am
by PhatFil
ok i missed out the shutdown long press.. but i hope you get the idea refine the logic before you look at actual code.. seems like you may want to have more time variables to set n check for the 5 second shutdown condition..

Re: 2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 8:45 am
by PhatFil

Code: Select all

Turn on led
time0, time1= now
ButtonStartTime =0
ButtonStopTime =0
ButtonTimerFlag=0   # 3 states 0 waiting, 1 testing , 2 awaiting resolution
while true
        if (reset = pressed ) AND (ButtonTimerFlag = 0)	
		ButtonTimerFlag =1
		ButtonStartTime= now
	elseif (reset = NOT pressed ) AND (ButtonTimerFlag = 1)
		ButtonStopTime = now		                
		ButtonTimerFlag =2

	if ButtonTimerflag=2
		ButtonTimerflag=0
		if ButtonStopTime>= buttonstarttime 5seconds
			system shutdown
		elseif ButtonStopTime>= buttonstarttime 2seconds
			system reboot

         if time1-time0 =0
               	If temp > threshold
               		turn on fans
         	if temp < normal
               		turn off fans
        time1= now
        if time1 >= time0 +5seconds
        	time0,time1=now       		
      
         
seems like the sort of thing you want with the button delay timing.

Re: 2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 9:05 am
by paddyg
maybe == in some of those conditions?!!

Re: 2 python scripts merge in 1?

Posted: Mon Dec 31, 2018 9:43 am
by PhatFil
paddyg wrote:
Mon Dec 31, 2018 9:07 am
maybe == in some of those conditions?!!

since its intended to help the OP your probably not wrong,