California
Posts: 142
Joined: Sat Jun 06, 2015 2:33 pm

Simple script optimisation and starting at boot

Sun Aug 06, 2017 8:53 am

Hello, here is a very simple script for controlling CPU fan and generating square pulses for hardware watchdog circuit. It consumes ~2% of CPU time and it works as expected.
The question 1: is possible to optimise it somehow, to spend less CPU time?
The question 2: how to make it run automatically at start?
Thanks in advance.
Vlado

simple_script.py

Code: Select all

import RPi.GPIO as GPIO
from subprocess import call
import os
from sys import exit
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)   #Stevilka dejanskega pina se uporabi v kodi

GPIO.setup(13, GPIO.OUT)  # WatchDog
GPIO.setup(15,GPIO.OUT)  # CPU cooler


# WATCHDOG pulse
def dig_out_13():
    GPIO.output(13, not GPIO.input(13))

# Return CPU temperature as float
def getCPUtemp():
    cTemp = os.popen('vcgencmd measure_temp').readline()
    return float(cTemp.replace("temp=","").replace("'C\n",""))

# Command for CPU cooler
def set_cooler():
    CPU_temp = getCPUtemp()
    if CPU_temp > 47.0:
        GPIO.output(15, GPIO.HIGH)
    elif CPU_temp < 45.0:
        GPIO.output(15, GPIO.LOW)

while True:
    set_cooler()
    dig_out_13()
    time.sleep(0.5)

User avatar
DougieLawson
Posts: 28819
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Simple script optimisation and starting at boot

Sun Aug 06, 2017 9:13 am

California wrote:
Sun Aug 06, 2017 8:53 am

The question 1: is possible to optimise it somehow, to spend less CPU time?
The question 2: how to make it run automatically at start?
1. Make the sleep longer, or don't bother running it as it does nothing useful except make the CPU run ever hotter.
2. Write a systemd service file.

viewtopic.php?f=63&t=138861#p921354
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

California
Posts: 142
Joined: Sat Jun 06, 2015 2:33 pm

Re: Simple script optimisation and starting at boot

Mon Aug 07, 2017 10:31 am

Thanks for your help.
Vlado

Massi
Posts: 1592
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Simple script optimisation and starting at boot

Mon Aug 07, 2017 11:40 am

California wrote:
Sun Aug 06, 2017 8:53 am
The question 1: is possible to optimise it somehow, to spend less CPU time?
The question 2: how to make it run automatically at start?
I'd try using pigpio's waves for the watchdog square wave.
for temperature, i'm with dougie. Not really useful, but if you are doing that for testing.. well half a second is ridicously too fast for this kind of checks, and since we are only learning something new, you could drive the fan with a pwm output depending on temperature (always with pigpio)

California
Posts: 142
Joined: Sat Jun 06, 2015 2:33 pm

Re: Simple script optimisation and starting at boot

Tue Aug 08, 2017 9:06 am

Massi wrote:
Mon Aug 07, 2017 11:40 am
California wrote:
Sun Aug 06, 2017 8:53 am
The question 1: is possible to optimise it somehow, to spend less CPU time?
The question 2: how to make it run automatically at start?
I'd try using pigpio's waves for the watchdog square wave.
for temperature, i'm with dougie. Not really useful, but if you are doing that for testing.. well half a second is ridicously too fast for this kind of checks, and since we are only learning something new, you could drive the fan with a pwm output depending on temperature (always with pigpio)
With time sleep 0,8s only 1% of CPU time is used, so it's ok. I can increase this time if needed with correction of watchdog circuit. I am taking care about temperature because I am not the only one which will use RPi, that's why small colling fan is added to housing responsible for non overheating of the RPi.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 16 guests