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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carsten_h
    Senior Member
    • Apr 2019
    • 831

    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.

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

    Originally posted by Aldoszx
    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:
    Click image for larger version

Name:	Bildschirmfoto 2020-01-24 um 21.05.05.png
Views:	1
Size:	17.2 KB
ID:	1578247
    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?
    Pi4 4GB piCorePlayer with LMS and Squeezelite for USB inside an Argon One case
    Pi3B+ (7" Display, Hifiberry DAC+ Pro) piCorePlayer with Squeezlite/Jivelite for Hifiberry inside a SmartiPi touch case
    Pi Zero W only for Bluetooth headphones
    Two Airport Express
    Ikea Symfonisk
  • paul-
    Senior Member
    • Jan 2013
    • 5781

    #2
    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

    Comment

    • carsten_h
      Senior Member
      • Apr 2019
      • 831

      #3
      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.
      Pi4 4GB piCorePlayer with LMS and Squeezelite for USB inside an Argon One case
      Pi3B+ (7" Display, Hifiberry DAC+ Pro) piCorePlayer with Squeezlite/Jivelite for Hifiberry inside a SmartiPi touch case
      Pi Zero W only for Bluetooth headphones
      Two Airport Express
      Ikea Symfonisk

      Comment

      • paul-
        Senior Member
        • Jan 2013
        • 5781

        #4
        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

        Comment

        • carsten_h
          Senior Member
          • Apr 2019
          • 831

          #5
          Originally posted by paul-
          Then shutdown is working.
          Sure? Ok. If you say it, then it have to.

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

          Originally posted by paul-
          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. :-)

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

          Thank you for your help!
          Pi4 4GB piCorePlayer with LMS and Squeezelite for USB inside an Argon One case
          Pi3B+ (7" Display, Hifiberry DAC+ Pro) piCorePlayer with Squeezlite/Jivelite for Hifiberry inside a SmartiPi touch case
          Pi Zero W only for Bluetooth headphones
          Two Airport Express
          Ikea Symfonisk

          Comment

          • paul-
            Senior Member
            • Jan 2013
            • 5781

            #6
            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

            Comment

            • carsten_h
              Senior Member
              • Apr 2019
              • 831

              #7
              Originally posted by paul-
              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
              Wow, that is great! Thank you very much for your work!
              I will test it as fast as I can.
              I saw a file: "etc/argononed.conf" in the script. I think I can save there my own definitions for the fan. Is this correct? Will this file be saved when doing a backup?
              Pi4 4GB piCorePlayer with LMS and Squeezelite for USB inside an Argon One case
              Pi3B+ (7" Display, Hifiberry DAC+ Pro) piCorePlayer with Squeezlite/Jivelite for Hifiberry inside a SmartiPi touch case
              Pi Zero W only for Bluetooth headphones
              Two Airport Express
              Ikea Symfonisk

              Comment

              • Aldoszx
                Junior Member
                • Jan 2020
                • 25

                #8
                Thanks Paul for the script.
                The shutdown works.
                Unfortunately, the fan does not start.
                I have changed the temperature threshold but no luck.
                Maybe I am doing something wrong or something is missing.

                Trying to run "python3 /home/tc/argononed.py" from ssh got me this:

                tc@piCoreTone:~$ python3 /home/tc/argononed.py
                Traceback (most recent call last):
                File "/home/tc/argononed.py", line 9, in <module>
                bus = smbus.SMBus(1)
                FileNotFoundError: [Errno 2] No such file or directory
                Last edited by Aldoszx; 2020-01-25, 21:30.

                Comment

                • carsten_h
                  Senior Member
                  • Apr 2019
                  • 831

                  #9
                  [QUOTE=Aldoszx;962149]Trying to run "python3 /home/tc/argononed.py" from ssh got me this:[QUOTE]

                  That is the same as I got. I have a Pi 4, so normally I would think that the line:
                  bus = smbus.SMBus(0)
                  would be called.

                  I tested: "python3 smbus" an got the same errormessage. So I think smbus is missing.

                  Originally there were three dependencies with smbus: "python-smbus python3-smbus i2c-tools". Is this now all in "smbus-python3.6.tcz"?

                  I found nearly the same error here: https://github.com/johnbryanmoore/VL...thon/issues/13

                  Do we also have to enable I2c?
                  Last edited by carsten_h; 2020-01-25, 21:45.
                  Pi4 4GB piCorePlayer with LMS and Squeezelite for USB inside an Argon One case
                  Pi3B+ (7" Display, Hifiberry DAC+ Pro) piCorePlayer with Squeezlite/Jivelite for Hifiberry inside a SmartiPi touch case
                  Pi Zero W only for Bluetooth headphones
                  Two Airport Express
                  Ikea Symfonisk

                  Comment

                  • Aldoszx
                    Junior Member
                    • Jan 2020
                    • 25

                    #10
                    I think i2c-tools is missing !

                    Comment

                    • paul-
                      Senior Member
                      • Jan 2013
                      • 5781

                      #11
                      Originally posted by Aldoszx
                      I think i2c-tools is missing !
                      i2c-tools is not needed. You need to manually load the i2c-dev module. This does not load automatically.

                      sudo modprobe i2c-dev
                      piCorePlayer a small player for the Raspberry Pi in RAM.
                      Homepage: https://www.picoreplayer.org

                      Please donate if you like the piCorePlayer

                      Comment

                      • carsten_h
                        Senior Member
                        • Apr 2019
                        • 831

                        #12
                        Originally posted by paul-
                        sudo modprobe i2c-dev
                        Ok. I did it. Do we have to do it every time after boot?
                        I used this command as the first user command.

                        Now it starts without any problem. I will test if the fan is starting now.
                        Last edited by carsten_h; 2020-01-25, 22:21.
                        Pi4 4GB piCorePlayer with LMS and Squeezelite for USB inside an Argon One case
                        Pi3B+ (7" Display, Hifiberry DAC+ Pro) piCorePlayer with Squeezlite/Jivelite for Hifiberry inside a SmartiPi touch case
                        Pi Zero W only for Bluetooth headphones
                        Two Airport Express
                        Ikea Symfonisk

                        Comment

                        • paul-
                          Senior Member
                          • Jan 2013
                          • 5781

                          #13
                          Originally posted by carsten_h
                          Ok. I did it. Do we have to do it every time after boot? Then I would start it in the script.

                          Then I get this error:
                          Code:
                          Exception in thread Thread-2:
                          Traceback (most recent call last):
                            File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
                              self.run()
                            File "/usr/local/lib/python3.6/threading.py", line 864, in run
                              self._target(*self._args, **self._kwargs)
                            File "argononed.py", line 81, in temp_check
                              val = float(temp.replace("'C",""))
                          ValueError: could not convert string to float: 'VCHI initialization failed\n'
                          Yes you need to load i2c-dev before starting the script. The script has to run with root permissions, as it make a call to vcgencmd, which requires it.
                          piCorePlayer a small player for the Raspberry Pi in RAM.
                          Homepage: https://www.picoreplayer.org

                          Please donate if you like the piCorePlayer

                          Comment

                          • Aldoszx
                            Junior Member
                            • Jan 2020
                            • 25

                            #14
                            Originally posted by paul-
                            Yes you need to load i2c-dev before starting the script. The script has to run with root permissions, as it make a call to vcgencmd, which requires it.
                            Thanks Paul !
                            Now it is working !
                            The fan also is on, according to temp settings !

                            Comment

                            • carsten_h
                              Senior Member
                              • Apr 2019
                              • 831

                              #15
                              Originally posted by paul-
                              The script has to run with root permissions, as it make a call to vcgencmd, which requires it.
                              Which script? The argononed.py?

                              I wrote another temperature entry inside the py script, as my Pi was only 46°C and now the fan is running. Thank you very much for your help!

                              The gpio-shutdown script from the tweaks page will be deleted when I set this to "No"?
                              I am only asking because the shutdown button is still shutting down so that the USB drives are not cleanly dismounted.

                              Oh, the fan is really loud. I think I should only use the fan when the Pi is getting really hot.
                              Pi4 4GB piCorePlayer with LMS and Squeezelite for USB inside an Argon One case
                              Pi3B+ (7" Display, Hifiberry DAC+ Pro) piCorePlayer with Squeezlite/Jivelite for Hifiberry inside a SmartiPi touch case
                              Pi Zero W only for Bluetooth headphones
                              Two Airport Express
                              Ikea Symfonisk

                              Comment

                              Working...