Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
50 improve fanpy script (#58)
Browse files Browse the repository at this point in the history
* 50 Test new fan.py script

* 50 Test mount disks

* 50 Add rc.local to mount disks

* 50 Fix playbooks

---------

Co-authored-by: veerendra2 <[email protected]>
  • Loading branch information
veerendra2 and veerendra2 authored Mar 8, 2023
1 parent 6a41d16 commit e604b7f
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 75 deletions.
64 changes: 34 additions & 30 deletions scripts/fan.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
#!/usr/bin/python3
'''
Description: A simple script to control 3pin PWM fan in raspberry pi 4
Reference: https://github.com/geekworm-com/x-c1/blob/main/fan.py
'''
import pigpio
import time
import logging

logging.basicConfig(format='%(levelname)s:%(asctime)s %(message)s', level=logging.DEBUG)
# Pin number for the fan
FAN_PIN = 18

# PWM frequency and duty cycle range
PWM_FREQ = 25000
PWM_RANGE = 100

servo = 18
# Temperature thresholds and corresponding duty cycles
TEMP_THRESHOLDS = [(75, 100), (70, 80), (60, 70), (48, 50), (30, 40)]

logging.basicConfig(format='%(levelname)s:%(asctime)s %(message)s', level=logging.DEBUG)

pwm = pigpio.pi()
pwm.set_mode(servo, pigpio.OUTPUT)
pwm.set_PWM_frequency(servo, 25000)
pwm.set_PWM_range(servo, 100)
while (True):
pwm.set_mode(FAN_PIN, pigpio.OUTPUT)
pwm.set_PWM_frequency(FAN_PIN, PWM_FREQ)
pwm.set_PWM_range(FAN_PIN, PWM_RANGE)

prev_dc = None
while True:
# Get CPU temp
with open("/sys/class/thermal/thermal_zone0/temp") as f:
temp = float(f.read()) / 1000.00
temp = float('%.2f' % temp)

if (temp > 75):
logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 100%")
pwm.set_PWM_dutycycle(servo, 100)
elif (temp > 70):
logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 80%")
pwm.set_PWM_dutycycle(servo, 80)
elif (temp > 60):
logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 70%")
pwm.set_PWM_dutycycle(servo, 70)
elif (temp > 50):
logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 50%")
pwm.set_PWM_dutycycle(servo, 50)
elif (temp > 30):
logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 40%")
pwm.set_PWM_dutycycle(servo, 40)
elif (temp < 30):
logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to 0%")
pwm.set_PWM_dutycycle(servo, 0)

time.sleep(120)
temp = float(f.read()) / 1000.00
temp = round(temp, 2)

# Determine duty cycle based on temperature
duty_cycle = 0
for threshold, dc in TEMP_THRESHOLDS:
if temp >= threshold:
duty_cycle = dc
break

# Set fan speed if duty cycle has changed
if duty_cycle != prev_dc:
pwm.set_PWM_dutycycle(FAN_PIN, duty_cycle)
logging.debug(f"Current CPU temperature is {temp}°C. Setting fan speed to {duty_cycle}%")
prev_dc = duty_cycle

# Wait for 2 minutes before checking temperature again
time.sleep(60)
6 changes: 3 additions & 3 deletions services/databases/docker-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.7"
networks:
traefik_public:
external: true
net:
internal:
driver: overlay
attachable: true
ipam:
Expand Down Expand Up @@ -46,7 +46,7 @@ services:
volumes:
- /media/disk2/postgres:/var/lib/postgresql/data:rw
networks:
- net
- internal
secrets:
- postgres_db
- postgres_password
Expand Down Expand Up @@ -77,6 +77,6 @@ services:
volumes:
- redis:/data
networks:
- net
- internal
secrets:
- redis_host_password
11 changes: 10 additions & 1 deletion services/jellyfin/docker-stack.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.0"
version: "3.2"

volumes:
config:
Expand Down Expand Up @@ -28,6 +28,15 @@ services:
hostname: jellyfin
env_file:
- .env_jellyfin
ports:
- target: 7359
published: 7359
protocol: udp
mode: host
- target: 1900
published: 1900
protocol: udp
mode: host
volumes:
- /media/disk2:/disk2:ro
- /media/disk2/jellyfin/config:/config
Expand Down
4 changes: 2 additions & 2 deletions services/nextcloud/.env_nextcloud
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POSTGRES_HOST=postgres
POSTGRES_HOST=databases_postgres
POSTGRES_DB_FILE=/run/secrets/postgres_db
POSTGRES_USER_FILE=/run/secrets/postgres_user
POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
Expand All @@ -13,5 +13,5 @@ NEXTCLOUD_TRUSTED_DOMAINS=192.168.0.120
OVERWRITECLIURL=https://192.168.0.120/nextcloud
OVERWRITEHOST=192.168.0.120
# ----- END HARDCODED NEXTCLOUD IP ------
REDIS_HOST=redis
REDIS_HOST=databases_redis
REDIS_HOST_PASSWORD_FILE=/run/secrets/redis_host_password
2 changes: 1 addition & 1 deletion services/nextcloud/.env_postgres
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POSTGRES_HOST=postgres
POSTGRES_HOST=databases_postgres
POSTGRES_DB_FILE=/run/secrets/postgres_db
POSTGRES_USER_FILE=/run/secrets/postgres_user
POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
19 changes: 8 additions & 11 deletions services/nextcloud/docker-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ version: "3.7"
networks:
traefik_public:
external: true
databases_net:
databases_internal:
external: true

secrets:
nextcloud_admin_password:
file: ../databases/secrets/nextcloud_admin_password.txt
file: secrets/nextcloud_admin_password.txt
nextcloud_admin_user:
file: ../databases/secrets/nextcloud_admin_user.txt
file: secrets/nextcloud_admin_user.txt
postgres_db:
file: ../databases/secrets/postgres_db.txt
file: secrets/postgres_db.txt
postgres_password:
file: ../databases/secrets/postgres_password.txt
file: secrets/postgres_password.txt
postgres_user:
file: ../databases/secrets/postgres_user.txt
file: secrets/postgres_user.txt
redis_host_password:
file: ../databases/secrets/redis_host_password.txt
file: secrets/redis_host_password.txt

services:
nextcloud:
Expand All @@ -42,11 +42,8 @@ services:
hostname: nextcloud
env_file:
- .env_nextcloud
depends_on:
- postgres
- redis
networks:
- databases_net
- databases_internal
- traefik_public
volumes:
- /media/disk2/nextcloud:/var/www/html
Expand Down
1 change: 1 addition & 0 deletions services/traefik/docker-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
- "traefik.http.routers.api.tls=true"
- "traefik.http.routers.api.service=api@internal"
- "traefik.http.services.dummy.loadbalancer.server.port=9999"
hostname: traefikv2
env_file:
- .env_traefik
ports:
Expand Down
6 changes: 3 additions & 3 deletions tasks/deploy-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

- name: Synchronize services directories
synchronize:
src: "services/{{ item }}"
src: "services/"
dest: "{{ services_base_dir_location }}"
delete: false
recursive: true
perms: false
with_items: "{{ services }}"

- name: Create traefik_public network
docker_stack:
state: present
name: traefik
compose:
- "{{ [services_base_dir_location, 'traefikv2', 'traefik.yml'] | path_join }}"
- "{{ [services_base_dir_location, 'traefik', 'traefik.yml'] | path_join }}"
when: "'traefik' in services"

- name: Deploy service stacks
docker_stack:
Expand Down
45 changes: 23 additions & 22 deletions tasks/prepare-pi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@
- bluetooth
- hciuart

# - name: Disable bluetooth in /boot/firmware/config.txt
# ini_file:
# path: /boot/firmware/config.txt
# section: all
# option: dtoverlay
# value: disable-bt
# mode: '0744'
# backup: yes
# become: yes
# when: disable_bluetooth|bool == true

- name: Disable HDMI Output
shell: /usr/bin/tvservice -o
become: yes
Expand All @@ -76,17 +65,7 @@
- name: Reboot Pi after upgrade
reboot:
post_reboot_delay: 10
when: reboot_after_pi_preparation|bool == true
become: yes

- name: Create mount directories
file:
path: "{{ item.value }}"
state: directory
owner: "{{ run_user }}"
group: "{{ run_user }}"
mode: "0644"
loop: "{{ lookup('dict', disk_mount_dirs) }}"
when: reboot_after_pi_preparation|bool == true and ansible_connection != 'local'
become: yes

- name: Configure disk mounts in /etc/fstab
Expand All @@ -99,3 +78,25 @@
boot: no
loop: "{{ lookup('dict', disk_mount_dirs) }}"
become: yes

- name: Change mount directories ownership
file:
path: "{{ item.value }}"
owner: "{{ run_user }}"
group: "{{ run_user }}"
mode: "0644"
loop: "{{ lookup('dict', disk_mount_dirs) }}"
become: yes

- name: Add mount disks in /etc/rc.local
copy:
content: |
#!/bin/sh -e
mount /dev/sdb1 /media/disk2
exit 0
dest: /etc/rc.local
force: false
group: root
owner: root
mode: 0755
become: yes
1 change: 1 addition & 0 deletions tasks/ufw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- 53
- 7359
- 1900
- 2377
allow_udp_ports:
- 53
- 67
Expand Down
4 changes: 2 additions & 2 deletions vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ services:
- portainer
- traefik

# Service's docker stack files copies in this location. It should not end with slash
services_base_dir_location: /opt/services
# Service's docker stack files copies in this location. It should end with slash
services_base_dir_location: /opt/services/

pkgs:
- "neofetch"
Expand Down

0 comments on commit e604b7f

Please sign in to comment.