Advertisement
Guest User

fan_control

a guest
Nov 6th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import os
  5. import time
  6. import RPi.GPIO as GPIO
  7. import pigpio
  8. import datetime
  9. from max31865 import max31865
  10. from influxdb import InfluxDBClient
  11.  
  12. # Temp config vars
  13. check_interval = 15  # in seconds
  14. max_temp = 34  # fans at max rpm
  15. min_temp = 26  # fans at min rpm
  16.  
  17. # PWM settings
  18. pwm_pin = 12
  19. pwm_interval = 50
  20. pwm_frequency = 20000
  21. pwm_max_dc = 255
  22. pwm_min_dc = 50
  23.  
  24. # InfluxDB setup
  25. client = InfluxDBClient(host = '192.168.1.200', port = 8086, username = 'user', password = 'password')
  26. client.switch_database(database = 'telegraf')
  27.  
  28. # Setup objects
  29. rtd = max31865()
  30. pi = pigpio.pi()
  31. pi.set_mode(pwm_pin, pigpio.OUTPUT)
  32. pi.set_PWM_frequency(pwm_pin, pwm_frequency)
  33.  
  34. # Main loop
  35. while True:
  36.     temp_c = rtd.readTemp()
  37.     if temp_c >= max_temp:
  38.         pwm = pwm_max_dc
  39.     elif temp_c <= min_temp:
  40.         pwm = pwm_min_dc
  41.     else:
  42.         temp_pct_max = (temp_c - min_temp) / (max_temp - min_temp)
  43.         temp_pwm = (temp_pct_max * (pwm_max_dc - pwm_min_dc)) + pwm_min_dc
  44.         pwm = int(temp_pwm) - int(temp_pwm) % pwm_interval
  45.  
  46.     pi.set_PWM_dutycycle(pwm_pin, pwm)
  47.    
  48.     json_body = [
  49.         {
  50.         "measurement": "server_chassis",
  51.         "tags": {"host": "tempo"},
  52.         "time": datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
  53.         "fields": {
  54.             "temp_c": temp_c,
  55.             "pwm": pwm
  56.             }
  57.         }
  58.     ]
  59.     print(json_body)
  60.  
  61.     client.write_points(json_body)
  62.     time.sleep(check_interval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement