Home of the Squeezebox™ & Transporter® network music players.
Page 3 of 5 FirstFirst 12345 LastLast
Results 21 to 30 of 43
  1. #21
    Senior Member paul-'s Avatar
    Join Date
    Jan 2013
    Quote Originally Posted by piPlayer View Post
    I only need simple status messages such as play, pause, stop, volume, etc. to control status LEDs. Unfortunately I am very bad at programming. Therefore I used the finished PyLMS library.
    Oh, reading status is a different story. But still quite simple. The only thing not built into pcp command line tool is getting the current volume, So I just made a little script. This is based on scripting included with pCP......

    . /var/www/cgi-bin/pcp-functions
    . /var/www/cgi-bin/pcp-lms-functions
    #  The case command are entered on command line to get the result.
    case $1 in
                    REQUEST=$(pcp_lms_build_request "$NAME" "status -")
                    RESULT='\["result","mixer volume"\]'
                    REQUEST=$(pcp_lms_build_request "$NAME" "status -")
                    RESULT=$(pcp_lms_build_result result "power")
                    REQUEST=$(pcp_lms_build_request "$NAME" "status -")
                    RESULT=$(pcp_lms_build_result result "mode")
    Save the script as something like "status.sh" Then make it executable, and run "./status.sh vol"
    piCorePlayer a small player for the Raspberry Pi in RAM.
    Homepage: https://www.picoreplayer.org

    Please donate if you like the piCorePlayer

  2. #22
    Quote Originally Posted by prabbit View Post
    With this sensor and code (setting aside wiringpi), does the brightness go dimmer or darker than what can be achieved by manually setting the screen brightness to 0, which is bright enough for me during the day and too bright for nighttime?

    I would love to have a way to make my screen even dimmer. Until I do, my RPi 4 with its 7 inch touchscreen is stuck in the living room and cannot come into the bedroom.
    Sorry, prabbit, I missed your question.

    I am not sure if I get your question correctly but I am using the dimmer for the pCP in my bedroom. Its main target is to dim the display when I switch off the light and that works very well. Actually, the adaptive brightness of the display works generally very well. I am happy with the solution.

    I have already changed to a solution based on Python/smbus and will post later a description.

  3. #23

    BH1750 with Python/smbus

    I followed the recommendation of @paul- and changed to a solution based on Python/smbus. The following replaces the part with C-programming and the shell script in my previous how-to. All related to I2C is still valid.

    You just need to install the extension "smbus-python3.6.tcz". pCP will add all dependencies, i.e. all other required extensions.

    import smbus
    import time
    # Define some constants from the datasheet
    ADDRESS    = 0x23 # I2C-Adresse des BH1750 if ADDR is seto GND
    #ADDRESS    = 0x5C # I2C-Adresse des BH1750 if ADDR is seto 3.3V
    POWER_DOWN = 0x00 # No active state
    POWER_ON   = 0x01 # Power on
    RESET      = 0x07 # Reset data register value
    # Start measurement at 1 lx resolution. Time typically 120 ms
    # Start measurement at 1l x resolution. Time typically 120 ms
    # Device is automatically set to Power Down after measurement.
    # Set DEBUG to 'True' if you want to print some debug info
    DEBUG = False
    # Creating an I2C instance
    bh1750 = smbus.SMBus(1)
    def convertToNumber(data):
        # Simple function to convert 2 bytes of data
        # into a decimal number. Optional parameter 'decimals'
        # will round to specified number of decimal places.
        result=(data[1] + (256 * data[0])) / 1.2
        return (result)
    def readLight(addr=ADDRESS):
        # Read data from I2C interface
        data = bh1750.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
        return convertToNumber(data)
    def calcTargetVal(lightLevel):
        # Simple algorithm to calculate the new brightness value of the
        # display.
        MIN_THRESHOLD  = 10    # minimum LUX value to consider
        MAX_THRESHOLD  = 1010  # maximum LUX value to consider
        MIN_BRIGHTNESS = 10    # minimum brightness value of the display
        MAX_BRIGHTNESS = 255   # maximum brightness value of the display
        # STEPSIZE: actually the LUX value that represents one brightness step
        if lightLevel < MIN_THRESHOLD:
            target = MIN_BRIGHTNESS
        elif lightLevel > MAX_THRESHOLD:
            target = MAX_BRIGHTNESS
            target = MIN_BRIGHTNESS + ((lightLevel + MIN_THRESHOLD) // STEPSIZE)
        return (int(target))
    def main():
        while True:
            # Measuring the current LUX value
            if DEBUG:
                print("Light Level : " + format(lightLevel,'.2f') + " lx")
            # Calculating the target brightness of the display
            dsply_brightness = calcTargetVal(lightLevel)
            # Setting the brightness value of the display
            dsply_file = '/sys/class/backlight/rpi_backlight/brightness'
            f = open(dsply_file, 'wt')
            if DEBUG:
                print("Display: " + str(dsply_brightness))
            # Wait 5 seconds before the next calculation
    # main() is only invoked if display-control.py runs as a script
    if __name__=="__main__":
    I stored this script as "display-control.py" in the folder /home/tc/lux/.

    The Python script should be executable. You can verify it with the command "ls -l":

    tc@SB-Kitchen:~/lux$ ls -l display-control.py
    -rwxr-xr-x 1 tc staff 4261 Mar 19 21:40 display-control.py

    Important are the three 'x' for executable. If they are missing then you can change it with

    $ chmod 755 display-control.sh

    To ensure that the Python script is executed with startup of pCP, it must be started by the boot script /opt/bootlocal.sh. Together with the I2C stuff, I added the following lines to /opt/bootlocal.sh:
    ### BH1750 start ------
    # Enabling kernel module i2c-dev
    /sbin/modprobe i2c-dev
    # starting shell script to control display brightness
    ### BH1750 stop ------
    This solution runs since yesterday on the pCP in my bedroom and I am happy with it.
    Last edited by jd68; 2021-03-21 at 08:42. Reason: Adding note that the script must be executable

  4. #24
    Join Date
    Jul 2015
    jd68, there are two kinds of light sensor on market, based on BH1750 and BH1750FVI. Is it principal, what kind of sensor uses manufacturer? And I found two models of sensors: GY-30 and GY-302.
    Oh! My misstake. You are recommending another case called SmartPi Touch. I'm allredy use this case. I think it will be more difficult.
    Last edited by WadDad; 2020-07-13 at 01:49. Reason: my misstake

  5. #25
    Hi WadDad,

    I have used the following board/sensor:

    On the board is written "GY-302" and the referenced datasheet is "BH1750FVI". Hence, "GY-302" seems to be the name of the I2C board and it uses the "BH1750FVI" as light sensor. It has the dimensions 18 x 14 mm.

    But my description can also be used with other I2C light sensors, e.g. Adafruit VEML7700. They will have different I2C addresses and might use different commands to start the measurement -- but in general it will be the same procedure.

    I have chosen the BH1750 because I found some web pages which described how to use it.

  6. #26
    Quote Originally Posted by WadDad View Post
    You are recommending another case called SmartPi Touch. I'm allredy use this case. I think it will be more difficult.
    Yes, the SmartPi case is very useful together with the light sensor.

    With your case, you should check if the light sensor must point to the same direction as the display or if it can point to another direction. For example, if the sensor is placed on the back of the case and it points to the other direction than the display, then the sensor will not detect correctly the sunlight that shines directly on your display. But that is normally not an important use case for a player in the bedroom.

  7. #27
    Join Date
    Jul 2015
    I made it! But I've got some troubles. First of all, python script doesn't run. When I add "smbus-python3.6.tcz" it's moved to uninstalled list. Now I'm using .sh script. It's work. But minimal brightness sets by program even I turn on piCorePlayer and brightness in room is so low. I use next variables:
    # if the measured value is below MIN_THRESHOLD then the display is set to MIN_BRIGHTNESS
    # if the measured value is above MAX_THRESHOLD then the display is set to MAX_BRIGHTNESS
    STEPSIZE=$(echo "scale=2 ; $LUX_RANGE/$BRIGHTNESS_RANGE" | bc -l | cut -f1 -d\.);
    # time interval between two measurements
    # Debugging ...
    echo `date` > $WDIR/deleteme
    # Let's wait until pCP is up & running
    sleep 20
    while true; do
      # Measuring the current LUX value
      # Calculating the target brightness of the display
      if [ $LUX -lt $MIN_THRESHOLD ]; then
      elif [ $LUX -gt $MAX_THRESHOLD ]; then
        TRGT=$(echo "$MIN_BRIGHTNESS+(($LUX-$MIN_THRESHOLD)/$STEPSIZE)" | bc -l | cut -f1 -d\.);
        # Setting the brightness value of the display
        echo $TRGT > /sys/class/backlight/rpi_backlight/brightness
        # Uncomment for debugging & testing
    #    echo $LUX
    #    echo $TRGT
        # Wait some seconds before the next calculation
        sleep $INTERVAL

    It because my Raspberry Pi stay in equipment rack.

  8. #28
    Congratulations so far, WadDad!

    Regarding the Python repository:
    Are you sure that you have enough disk space available to download the required repositories? The "Extensions" window of pCP shows the available free space:
    Name:  pCP-extensions.png
Views: 146
Size:  87.3 KB

    "smbus-python3.6.tcz" and its dependencies require 16-17 MB. You might need to increase the size of the partition as described in the screen shot.

    To investigate your problems with the shell script, I suggest to change the following lines in the script as below:
        # Uncomment for debugging & testing
    #    echo $LUX
    #    echo $TRGT
        # Uncomment for debugging & testing
        echo $LUX
        echo $TRGT
    If you start now the script in a terminal, you will see the measured LUX value and the resulting brightness of the display ($TRGT). Now you change the brightness in your room and you see what is measured by the sensor. The measured LUX values can be used to adapt the parameters MIN_THRESHOLD and MAX_THRESHOLD.

  9. #29
    Join Date
    Jul 2015
    Hello, jd68. Thank you for answer.
    I think 337MB must be enough.
    Script running. I'm sure. I checked it by deleting symbol # at showed strings. Displayed numbers changes if I set the light near sensor.
    I think touchscreen brightness must have two stages: one for turned off PCP and another for PCP that plays music. If PCP turned off maximum and minimum brightness of screen must be lower. As I remember Touchbox had special string in menu for screen brightness called "Reduce on screensaver".
    Here is some output strings:
    Last edited by WadDad; 2020-08-03 at 08:26.

  10. #30
    OK, This is the difference in our configurations: my screensaver is set to "Switch off" when the pCP is switched off. Hence, I don't care about the adaptive brightness control when pCP is not running.

    But your wish of two stages of the adaptive brightness control can be handled by the shell script. We just need to find a way to know whether pCP is ON or OFF. Maybe @paul- can tell us if there exists a file (or something else) that tells us the state of pCP.

    When we have this information then we just add an if-else loop to the shell script to distinguish between the two states: "pCP on" or "pCP off".

Tags for this Thread

Posting Permissions

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