Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import os
- import time
- import RPi.GPIO as GPIO
- import pigpio
- import datetime
- from max31865 import max31865
- from influxdb import InfluxDBClient
- # Temp config vars
- check_interval = 15 # in seconds
- max_temp = 34 # fans at max rpm
- min_temp = 26 # fans at min rpm
- # PWM settings
- pwm_pin = 12
- pwm_interval = 50
- pwm_frequency = 20000
- pwm_max_dc = 255
- pwm_min_dc = 50
- # InfluxDB setup
- client = InfluxDBClient(host = '192.168.1.200', port = 8086, username = 'user', password = 'password')
- client.switch_database(database = 'telegraf')
- # Setup objects
- rtd = max31865()
- pi = pigpio.pi()
- pi.set_mode(pwm_pin, pigpio.OUTPUT)
- pi.set_PWM_frequency(pwm_pin, pwm_frequency)
- # Main loop
- while True:
- temp_c = rtd.readTemp()
- if temp_c >= max_temp:
- pwm = pwm_max_dc
- elif temp_c <= min_temp:
- pwm = pwm_min_dc
- else:
- temp_pct_max = (temp_c - min_temp) / (max_temp - min_temp)
- temp_pwm = (temp_pct_max * (pwm_max_dc - pwm_min_dc)) + pwm_min_dc
- pwm = int(temp_pwm) - int(temp_pwm) % pwm_interval
- pi.set_PWM_dutycycle(pwm_pin, pwm)
- json_body = [
- {
- "measurement": "server_chassis",
- "tags": {"host": "tempo"},
- "time": datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
- "fields": {
- "temp_c": temp_c,
- "pwm": pwm
- }
- }
- ]
- print(json_body)
- client.write_points(json_body)
- time.sleep(check_interval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement