Home of the Squeezebox™ & Transporter® network music players.
Results 1 to 10 of 39

Hybrid View

  1. #1
    Senior Member
    Join Date
    Apr 2019
    Location
    Wunstorf, Germany
    Posts
    325

    Using Argon One case with fan and powerbutton together with piCorePlayer?

    Hello!

    This is a new thread for the question how to use the fan and the powerbutton of the Argon One case together with piCorePlayer.
    Let's start at the beginning.

    I want to use a Raspberry Pi 4 as a LMS server with the piCorePlayer installation. This will be no problem.
    I want to place this into this case: https://www.argon40.com/catalog/prod...rry-pi-4-case/
    This case has a software for using the Power-Button together with eg. Raspbian.

    Does anyone know if this software button will also work somehow together with piCorePlayer? So is it possible to switch it off gracefully with the button?
    This case has also been available for older Raspberry Pi.

    The script for the button and the fan can be found here (for Raspbian): https://download.argon40.com/argon1.sh

    First part: "The fan"

    The installation of the script consists of a few different parts.
    First it installs the following packages: "raspi-gpio python-rpi.gpio python3-rpi.gpio python-smbus python3-smbus i2c-tools"
    Then it creates a few files:
    - daemonconfigfile (there are only temperatures and fan speeds in a "table"
    - shutdownscript (this will be called later from the daemon when the button is pressed.
    - powerbuttonscript (this is the script that will later run as a daemon)
    - daemonfanservice (this is the configfile for the service, it is placed at: /lib/systemd/system/$daemonname.service)
    - removescript (to remove the installation)
    - configscript (to configure the reactions to different temperatures, later it will write the daemonconfigfile)
    At the end it starts the service via:
    Code:
    sudo systemctl daemon-reload
    sudo systemctl enable $daemonname.service
    sudo systemctl start $daemonname.service
    This is the part of the daemon for temperature checking and switching the fan to the different speeds:
    Code:
    echo 'def temp_check():' >> $powerbuttonscript
    echo '	fanconfig = ["65=100", "60=55", "55=10"]' >> $powerbuttonscript
    echo '	tmpconfig = load_config("'$daemonconfigfile'")' >> $powerbuttonscript
    echo '	if len(tmpconfig) > 0:' >> $powerbuttonscript
    echo '		fanconfig = tmpconfig' >> $powerbuttonscript
    echo '	address=0x1a' >> $powerbuttonscript
    echo '	prevblock=0' >> $powerbuttonscript
    echo '	while True:' >> $powerbuttonscript
    echo '		temp = os.popen("vcgencmd measure_temp").readline()' >> $powerbuttonscript
    echo '		temp = temp.replace("temp=","")' >> $powerbuttonscript
    echo '		val = float(temp.replace("'"'"'C",""))' >> $powerbuttonscript
    echo '		block = get_fanspeed(val, fanconfig)' >> $powerbuttonscript
    echo '		if block < prevblock:' >> $powerbuttonscript
    echo '			time.sleep(30)' >> $powerbuttonscript
    echo '		prevblock = block' >> $powerbuttonscript
    echo '		try:' >> $powerbuttonscript
    echo '			bus.write_byte(address,block)' >> $powerbuttonscript
    echo '		except IOError:' >> $powerbuttonscript
    echo '			temp=""' >> $powerbuttonscript
    echo '		time.sleep(30)' >> $powerbuttonscript
    They are reading the temperature with "vcgencmd measure_temp" and if it reaches the next step from the config they call this:
    Code:
    bus.write_byte(address,block)
    where address is 0x1a and block is the fanspeed from config.

    I don't know what this bus.write_byte is doing. Maybe it is enough to write this a single time to have the fan running always at the same speed?

    The fan is controlled via I2c as paul has written and bus.write... is for writing to I2c.

    Is it possible to write to I2c with piCorePlayer? So I can write one byte at start to let the fan work in one defined speed.

    Quote Originally Posted by paul- View Post
    Yes you can, we can probably run the scripts(Daemon) that they use. You just can use the installation script.
    Second Part: "The powerbutton"

    Quote Originally Posted by Aldoszx View Post
    I also have this RPi4 case.
    From the script itself, i saw that: 'shutdown_pin=4'.
    I suppose that is the pin used for shutdown command.
    And, it must be pulled down: 'pull_up_down=GPIO.PUD_DOWN'
    So I used this configuration in piCorePlayer:
    Name:  Bildschirmfoto 2020-01-24 um 21.05.05.png
Views: 1104
Size:  17.2 KB
    I pressed the button to create the script and it can be found in the filesystem.

    But what happens is that I get the following messages at startup:
    Code:
    Mounting USB Drives...
    Mounting USB Drive: 5D0B-9332...
    FUSE exfat 1.2.7
    WARN: volume was not unmounted cleanly.
    Disk Mounted at /mnt/LMS.
    Mounting USB Drive: 5E28-AD65...
    FUSE exfat 1.2.7
    WARN: volume was not unmounted cleanly.
    Disk Mounted at /mnt/Musikbox.
     Done.
    So the Pi was not correctly shutdown, as the filesystems are not clean.

    Is there any possibility to see if the shutdown script is called when switching off without an attached monitor? E.g. via looking at the logfile via ssh or something like that?

  2. #2
    Senior Member paul-'s Avatar
    Join Date
    Jan 2013
    Posts
    3,184
    First, do not try to run those scripts directly.....remove anything you have tried.

    Lets talk about shutdown first. If you just configure the shutdown tweak the way you did it, does the device shutdown? Did you install the shutdown-monitor extension?
    piCorePlayer a small player for the Raspberry Pi in RAM.
    Homepage: https://www.picoreplayer.org

    Please donate if you like the piCorePlayer

  3. #3
    Senior Member
    Join Date
    Apr 2019
    Location
    Wunstorf, Germany
    Posts
    325
    I did not run those scripts, I only tried to understand how they work.

    Yes, the Pi is shutting down and yes I installed the shutdown monitor.

  4. #4
    Senior Member paul-'s Avatar
    Join Date
    Jan 2013
    Posts
    3,184
    Then shutdown is working. I’ll check out the fan speed dependencies, they should all be availiable.

    As for the disk mount errors, the dirty bit is set, it could have come from anywhere. It’s mostly harmless, how many times do you Just pull the stick from your Mac, vs ejecting it?

    You’ll have to scan/fix them. Probably easier for you to do it on your Mac.
    piCorePlayer a small player for the Raspberry Pi in RAM.
    Homepage: https://www.picoreplayer.org

    Please donate if you like the piCorePlayer

  5. #5
    Senior Member
    Join Date
    Apr 2019
    Location
    Wunstorf, Germany
    Posts
    325
    Quote Originally Posted by paul- View Post
    Then shutdown is working.
    Sure? Ok. If you say it, then it have to.

    Quote Originally Posted by paul- View Post
    I’ll check out the fan speed dependencies, they should all be availiable.
    Thank you!

    Quote Originally Posted by paul- View Post
    how many times do you Just pull the stick from your Mac, vs ejecting it?
    Never! I always ejecting them correctly on Mac and on PC. :-)

    Quote Originally Posted by paul- View Post
    Probably easier for you to do it on your Mac.
    Yes, i think so.

    Thank you for your help!

  6. #6
    Senior Member paul-'s Avatar
    Join Date
    Jan 2013
    Posts
    3,184
    First, remove the shutdown monitor and don't use that on the tweaks page....this script will do all of it a different way.

    I have uploaded the dependencies to our repo

    smbus-python3.6.tcz
    RPi-GPIO-python3.6.tcz

    Install them from the extensions web page.


    This is the actual script, save it to your home directory as /home/tc/argononed.py
    Then put in the user commands on the pcp tweaks page.

    python3 /home/tc/argononed.py

    Code:
    #!/usr/bin/python
    import smbus
    import RPi.GPIO as GPIO
    import os
    import time
    from threading import Thread
    rev = GPIO.RPI_REVISION
    if rev == 2 or rev == 3:
    	bus = smbus.SMBus(1)
    else:
    	bus = smbus.SMBus(0)
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    shutdown_pin=4
    GPIO.setup(shutdown_pin, GPIO.IN,  pull_up_down=GPIO.PUD_DOWN)
    def shutdown_check():
    	while True:
    		pulsetime = 1
    		GPIO.wait_for_edge(shutdown_pin, GPIO.RISING)
    		time.sleep(0.01)
    		while GPIO.input(shutdown_pin) == GPIO.HIGH:
    			time.sleep(0.01)
    			pulsetime += 1
    		if pulsetime >=2 and pulsetime <=3:
    			os.system("pcp rb")
    		elif pulsetime >=4 and pulsetime <=5:
    			os.system("pcp sd")
    def get_fanspeed(tempval, configlist):
    	for curconfig in configlist:
    		curpair = curconfig.split("=")
    		tempcfg = float(curpair[0])
    		fancfg = int(float(curpair[1]))
    		if tempval >= tempcfg:
    			return fancfg
    	return 0
    def load_config(fname):
    	newconfig = []
    	try:
    		with open(fname, "r") as fp:
    			for curline in fp:
    				if not curline:
    					continue
    				tmpline = curline.strip()
    				if not tmpline:
    					continue
    				if tmpline[0] == "#":
    					continue
    				tmppair = tmpline.split("=")
    				if len(tmppair) != 2:
    					continue
    				tempval = 0
    				fanval = 0
    				try:
    					tempval = float(tmppair[0])
    					if tempval < 0 or tempval > 100:
    						continue
    				except:
    					continue
    				try:
    					fanval = int(float(tmppair[1]))
    					if fanval < 0 or fanval > 100:
    						continue
    				except:
    					continue
    				newconfig.append( "{:5.1f}={}".format(tempval,fanval))
    		if len(newconfig) > 0:
    			newconfig.sort(reverse=True)
    	except:
    		return []
    	return newconfig
    def temp_check():
    	fanconfig = ["65=100", "60=55", "55=10"]
    	tmpconfig = load_config("etc/argononed.conf")
    	if len(tmpconfig) > 0:
    		fanconfig = tmpconfig
    	address=0x1a
    	prevblock=0
    	while True:
    		temp = os.popen("vcgencmd measure_temp").readline()
    		temp = temp.replace("temp=","")
    		val = float(temp.replace("'C",""))
    		block = get_fanspeed(val, fanconfig)
    		if block < prevblock:
    			time.sleep(30)
    		prevblock = block
    		try:
    			bus.write_byte(address,block)
    		except IOError:
    			temp=""
    		time.sleep(30)
    try:
    	t1 = Thread(target = shutdown_check)
    	t2 = Thread(target = temp_check)
    	t1.start()
    	t2.start()
    except:	
    	t1.stop()
    	t2.stop()
    	GPIO.cleanup()
    piCorePlayer a small player for the Raspberry Pi in RAM.
    Homepage: https://www.picoreplayer.org

    Please donate if you like the piCorePlayer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •