Show Fan Activation

Is there a way for me to run a code that would report that the fan is off or on? Can’t seem to find any help anywhere. I have tried running “sensors” but it does not see the fan (cooling_device).

Thanks

Fan Shim? You can likely set it up so the LED shows when its on. Might be a tall order to have something in the status bar to show on off.
GPIO 18 is the fan control pin, when its a logic low the fan is on, and off if its a logic high.
Fan SHIM at Raspberry Pi GPIO Pinout

Have a look at this, pimoroni/fanshim-python: Python library for the Fan SHIM for Raspberry Pi (github.com)
fanshim.get_fan() # returns 1 for ‘on’, 0 for ‘off’

This is what I get when I run that:

pi@fluiddpi : ~/fanshim-python/examples $ fanshim.get_fan() #

None of the commands on the link you provided works either. Comes back with:

pi@fluiddpi : ~/fanshim-python/examples $ fanshim.set_fan(False)
-bash: syntax error near unexpected token `False’

You likely have to do the following from python

from fanshim import FanShim
fanshim = FanShim()
fanshim.get_fan()
1 Like

Yep! That did it! I’m a little new at this so didn’t think to try in Python3

So, if I want to create a running log, I just need to enter those three lines somewhere in the code?

The first two lines only need to be run once, and can be above the while True: loop.
The third line will be called up in the while True: section of the code. Each time its run it will check the status of the fan.

1 Like
  1. I am trying to create a script that will create an appended log file when the FanSHIM turns off and on. This is what I have so far.
#!/usr/bin/python

import time
from fanshim import FanShim

fanshim = FanShim()

# get status
last_status = fanshim.get_fan()

while True:
    current_status = fanshim.get_fan()
    if current_status != last_status:
        # status has changed
        if current_status == 1:
            print('Fan turned on')
        else:
            print('Fan turned off')

        # update last_status
        last_status = fanshim.get_fan()

    time.sleep(1)

I have this service:

[Unit]
Description=Fan monitor

[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/usr/bin/python -u /path/to/monitor_fan.py

[Install]
WantedBy=multi-user.target

placed that in /etc/systemd/system, then run: sudo systemctl enable fan-monitor.service sudo systemctl start fan-monitor.service

Here is the failed output:

Feb 03 22:00:19 fluiddpi systemd[1]: Started Fan monitor.
Feb 03 22:02:30 fluiddpi systemd[1]: Stopping Fan monitor...
Feb 03 22:02:30 fluiddpi systemd[1]: fan-monitor.service: Main process exited, code=killed, status=15/TERM
Feb 03 22:02:30 fluiddpi systemd[1]: fan-monitor.service: Succeeded.
Feb 03 22:02:30 fluiddpi systemd[1]: Stopped Fan monitor.
Feb 03 22:03:04 fluiddpi systemd[1]: Started Fan monitor.
Feb 03 22:13:49 fluiddpi systemd[1]: Stopping Fan monitor...
Feb 03 22:13:49 fluiddpi systemd[1]: fan-monitor.service: Main process exited, code=killed, status=15/TERM
Feb 03 22:13:49 fluiddpi systemd[1]: fan-monitor.service: Succeeded.
Feb 03 22:13:49 fluiddpi systemd[1]: Stopped Fan monitor.

Any ideas on my coding?

That’s above my current skill level I’m afraid.

1 Like