Neste vídeo, iremos configurar nosso ventilador para ligar e desligar quando a temperatura programada chegar. Adicionando um Resistor e um Transistor, nós podemos usar a porta GPIG21 para quando chegar aos 55 graus nosso ventilador irá ligar para resfriar o circuito, e quando chegar a 48 Graus, nosso ventilador irá desligar.
You will need the following components:
- 5V fan;
- NPN transistor 2N2222
- Resistance of 680 or 340 Ohns (resistance may vary depending on your fan power and quantity);
- Cables for connecting to Raspberry Pi
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
To create our script, we must create our folder, for that, you will have to run the following command:
mkdir /home/pi/scripts/
After that enter this folder, using the command:
cd /home/pi/scripts/
Only then will you be able to create your program, which will be named fan.py:
nano fan.py
The command to be used is the following:
#!/usr/bin/python
import sys
import time
from gpiozero import LED # doc: https://gpiozero.readthedocs.io/
# define o GPIO a ser controlado pelo transistor na parte de gatilhofan = LED(21)
def cpu_temp():
with open(“/sys/class/thermal/thermal_zone0/temp”, ‘r’) as f:
return float(f.read())/1000
def main():
# close fan at begining
is_close = True
fan.off()
while True:
temp = cpu_temp()
if is_close:
if temp > 55.0: # temperatura para ligar fan
print time.ctime(), temp, ‘Fan ON’
fan.on()
is_close = False
else:
if temp < 48.0: # temperatura para desligar fan
print time.ctime(), temp, ‘Fan OFF’
fan.off()
is_close = True
time.sleep(2.0)
print time.ctime(), temp
if __name__ == ‘__main__’:
main()
To check if the fan is working, use the command:
python /home/pi/scripts/fan.py
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Now comes the time to activate the service. To do this you will first have to log in as an administrator.
sudo su
After that enter the /etc/init.d folder, you will need to use the following command:
cd /etc/init.d
Finally, you will need to create our service, so use the command:
#!/bin/bash
# /etc/init.d/fan
### BEGIN INIT INFO
# Provides:fan
# Required-Start:$remote_fs $syslog
# Required-Stop:$remote_fs $syslog
# Default-Start:2 3 4 5
# Default-Stop:0 1 6
# Short-Description: fan
# Description: Fan controller auto start after boot
### END INIT INFO
case “$1” in
start)
echo “Starting Fan”
python /home/pi/scripts/fan.py &
;;
stop)
echo “Stopping Fan”
#killall ledblink.py
kill $(ps aux | grep -m 1 ‘python /home/pi/scripts/fan.py’ | awk ‘{ print $2 }’)
;;
*)
echo “Usage: service fan start|stop”
exit 1
;;
esac
exit 0
To restart all services, use the command:
chmod +x /etc/init.d/fan.service
We can start the service with the command:
sudo service fan start
To check if the service is running, use the command:
sudo service fan status
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –