pwmfan

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2018 License: MIT Imports: 10 Imported by: 0

README

pwmfan-go

A Pulse Width Modulation Fan For RaspberryPi written in Go
Fan speed is adjust by CPU temperature, to reduce fan noise and power consumption

GitHub version Go Report Card GoDoc

Warning:

This program still in EARLY development, function may missing or not work properly, use with caution.
This library still in EARLY development, APIs may change, use with caution.

Pros
  1. Easy to use, just a binary file and a simple configuration file
  2. Easy install/uninstall with handy script
  3. Run as systemd service
  4. remote fan state monitor

Hardware Requirement

  1. A RaspberryPi 1/2/3 serial
  2. A fan that support PWM signal, or you can build one by add a transistor (and resistor) to you origianl fan
Remember:

Raspberry Pi support max +5V for power pin, and use +3.3V for other gpio pin, double check before connect anything to you pi

Software Requirement

  1. (If you want to use inbox system daemon configuration,) A Systemd controlled system.
  2. (If you want to build programe yourself,) A Go SDK.

Usage

Since controller program uses pwm to control fan, it only works with root privillage.

You can get a very simple installation help when you run ./install.sh with no argument.

Install Controller From Binary Release:
  1. Download release package from Releases to your Pi
cd controller
  1. Modify configuration file, see Controller Configuration File below
  2. Use
./install.sh install

to install controller program to your system

Install Controller From Source:
  1. On your Pi
go get -v -u -d github.com/eZioPan/pwmfan-go
cd $/GOPATH/src/github.com/eZioPan/pwmfan-go/cmd/controller
./install.sh build
  1. Modify configuration file, see Controller Configuration File below
  2. Use
./install.sh install

to install controller program to your system

Controller Configuration File:

By default, the controller program use JSON style "config.json" file in the same directory of binary file, you can change it using -conifg command line parameter.
Here is an example of controller configuration file:

{
	"Pin":18,
	"CPUTempPath":"/sys/class/thermal/thermal_zone0/temp",
	"StartCount":5,
	"StopCount":5,
	"StartTemp":38,
	"LowTemp":30,
	"HighTemp":45,
	"PwmFreq":600,
	"StartCycle":50,
	"LowCycle":50,
	"HighCycle":100,
	"FullCycle":100,
	"SampleRate":2,
	"NetworkInterfaceName":"lo",
	"ListenPort":2334,
	"Token":"123456789"
}

Pin defines the gpio pin number of PWM. This uses BCM Pinout number, you can check here.
CPUTempPath defines the system file path to read cpu's temperature.
StartCount after counter reaches this count fan will start.
StopCount after counter reaches this count fan will stop.
StartTemp when temperature higher than this value, fan will start.
LowTemp when temperature lower than this value, fan will stop.
HighTemp when temperature higher than this value, fan will run in HighCycle defined speed.
PwmFreq PWM signal frequence, should be set high enough for PWM signal.
StartCycle when fan start from full stop, it needs a little higher speed to kick it start.
LowCycle is the lowest speed fan will run, before fan fully stopped.
HighCycle is the highest speed fan will run. This value shoudn't greater than FullCycle.
FullCycle is maximum speed which fan is able to run at.
SampleRate is the rate that program check CPU temperature and change fan speed in.
NetworkInterfaceName is the network interface which will be use to listen request and send fan data from
ListenPort is the network port which will be use to listen request and send fan data by
Token is the token that will be check during fan data request

Uninstall Controller:
cd controller
./install.sh uninstall
Install Monitor From Binary Release:
  1. Download release package from Releases
cd monitor
  1. Modify configuration file, see Monitor Configuration File below
  2. Use
./monitor

to monitor your fan state

Install Monitor From Source:
  1. On your Pi
go get -v -u -d github.com/eZioPan/pwmfan-go
cd $/GOPATH/src/github.com/eZioPan/pwmfan-go/cmd/monitor
go build -v -ldflags "-s -w"
  1. Modify configuration file, see Monitor Configuration File below
  2. Use
./monitor

to monitor your fan state

Monitor Configuration File:

By default, the monitor program use JSON style "config.json" file in the same directory of binary file, you can change it using -conifg command line parameter.
Here is an example of monitor configuration file:

{
	"NetworkInterface":"lo",
	"remoteHost":"localhost",
	"remotePort": 2334,
	"Token":"123456789",
	"SampleRate":1
}

NetworkInterface is the network interface which will be use to send request and receive fan data from
remoteHost is IP/hostname that your controller program exist
remotePort is the port your controller program use to listen request and send data
Token is the token that will be check during fan data request
SampleRate is the rate that monitor will request fan state in

Uninstall Monitor:

Just remove binary file and configuration file

Tips:
  1. If you want to use fan monitor in a diffrent machine, please check your network/firewall settings.
    pwmfan use UDP to send and receive message
  2. Token must be THE SAME both in controller configuration file and monitor configuration file.
    As for now, token is sent in plain text, DO NO PUT ANY SENSITIVE DATA IN THIS FIELD
  3. As for now, fan data is sent in plain text, will change in future release

For Developers

go get -v -u -d github.com/eZioPan/pwmfan-go
import "github.com/eZioPan/pwmfan-go"

Dependencices

stianeikeland/go-rpio for accessing RasberryPi gpio in pure go

TODO

  • Write fan log to system log
  • Redesign configuration for more clear layer style
  • Add support for user defined fan data
  • Linux man page for commandline
  • send network data with encryption
  • [Done in v0.2.0] Network function to read fan state
  • [Done in v0.2.0] run as a Systemd service
  • [WON'T DO] Network function to change fan parameter(Too dangerous)

Changelog

  • v0.2.0
    Support systemd service
    Support remote fan monitor
    Support SampleRate parameter

  • v0.1.0
    Reconstruct code
    Support StartCount and StopCount parameter

  • v0.0.1
    Init Release
    Basic function

Documentation

Overview

Package pwmfan is a set of toolkits to build a pwm fan control program

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleErr added in v0.2.0

func HandleErr(err error)

HandleErr will try to handle an error

func IFNameToIPv4 added in v0.2.0

func IFNameToIPv4(ifname string) (ip net.IP)

IFNameToIPv4 reads an network interface name, return an net.IP, with the first address of the network interface

func LinearClampRemap

func LinearClampRemap(input float64, opt ...float64) (output float64)

LinearClampRemap is a linear remap function that will clamp output in between opt[2] and opt[3]

func LinearRemap

func LinearRemap(input float64, opt ...float64) (output float64)

LinearRemap is a linear remap function

func PullCPUTemp

func PullCPUTemp(CPUTempPath string, Divider float64, Temp chan<- float64)

PullCPUTemp read from a system file constantanious and put temperature into a predifined channel

Note: As for raspberry Pi, CPUTempPath is /sys/class/thermal/thermal_zone0/temp Divider should be set to 1000

func ReadCPUTemperature

func ReadCPUTemperature(CPUTempPath string, Divider float64) (Temperature float64)

ReadCPUTemperature read temprature once from a file and divie raw data by a divider

Note: As for raspberry Pi, CPUTempPath is /sys/class/thermal/thermal_zone0/temp Divider should be set to 1000

func SignalProcess

func SignalProcess(Process *os.Process, SigChan <-chan os.Signal, Action func())

SignalProcess listen from a os.Notify function's output, when catch signal, Process the Action, then send original to the Process

func TriangularWave

func TriangularWave(small float64, big float64, step float64, ch chan<- float64)

TriangularWave is a linear remap will read input and generate a oscillation wave

Types

type Config

type Config struct {
	Pin                  uint
	CPUTempPath          string
	StartCount           uint
	StopCount            uint
	StartTemp            float64
	LowTemp              float64
	HighTemp             float64
	PwmFreq              uint
	StopCycle            uint
	StartCycle           uint
	LowCycle             uint
	HighCycle            uint
	FullCycle            uint
	SampleRate           uint
	NetworkInterfaceName string
	ListenPort           int
	Token                string
}

Config contain all information that define a PWM fan's user configuration TODO: try to implement String() method

func ParseJSON

func ParseJSON(cfgFilePath string) (cfg Config)

ParseJSON parse json file into a Config structure

type Fan

type Fan struct {
	State        FanState
	Cycle        uint
	Temp         float64
	Cfg          Config
	Pin          rpio.Pin
	StopCounter  uint
	StartCounter uint
	UDPAddr      *net.UDPAddr
}

Fan represent a Fan

State, Cycle, Temp should be updated 'in realtime' Pin better not be modified after call NewFan()

TODO: try to implement String() method

func NewFan

func NewFan(cfg Config) (fan *Fan)

NewFan initialize a Fan object

func (*Fan) CreateServer added in v0.2.0

func (fan *Fan) CreateServer() (udpConn *net.UDPConn)

CreateServer use fan's configuration, return a *net.UDPConn object

func (Fan) GetCfg

func (fan Fan) GetCfg() (cfg Config)

GetCfg get a fan's configuration

func (Fan) GetCycle

func (fan Fan) GetCycle() (cycle uint)

GetCycle get fan's current pwm duty cycle information

func (Fan) GetStartCounter

func (fan Fan) GetStartCounter() (num uint)

GetStartCounter get fan's current start counter

func (Fan) GetState

func (fan Fan) GetState() (state FanState)

GetState get a fan's current running state

func (Fan) GetStopCounter

func (fan Fan) GetStopCounter() (num uint)

GetStopCounter get fan's current stop counter

func (Fan) GetTemp

func (fan Fan) GetTemp() (temp float64)

GetTemp get fan's current loaded temperature information

func (Fan) GetUDPAddr added in v0.2.0

func (fan Fan) GetUDPAddr() (UDPAddr *net.UDPAddr)

GetUDPAddr get fan's udp listening address

func (*Fan) HandleRequest added in v0.2.0

func (fan *Fan) HandleRequest(udpConn *net.UDPConn)

HandleRequest handle request from network

func (*Fan) Monitor

func (fan *Fan) Monitor()

Monitor is the function to control fan's real-time stat

func (*Fan) ResolveUDPAddr added in v0.2.0

func (fan *Fan) ResolveUDPAddr()

ResolveUDPAddr resolve the listening udp address from fan config

func (*Fan) SetCfg

func (fan *Fan) SetCfg(cfg Config)

SetCfg set a fan's configuration

func (*Fan) SetCycle

func (fan *Fan) SetCycle(cycle uint)

SetCycle set fan's current pwm duty cycle information

func (*Fan) SetStartCounter

func (fan *Fan) SetStartCounter(num uint)

SetStartCounter set fan's current start counter

func (*Fan) SetState

func (fan *Fan) SetState(state FanState)

SetState set a fan's current running state

func (*Fan) SetStopCounter

func (fan *Fan) SetStopCounter(num uint)

SetStopCounter set fan's current stop counter

func (*Fan) SetTemp

func (fan *Fan) SetTemp(temp float64)

SetTemp set fan's current loaded temperature information

func (*Fan) SetUDPAddr added in v0.2.0

func (fan *Fan) SetUDPAddr(udpAddr *net.UDPAddr)

SetUDPAddr set fan's udp listening address

func (Fan) String added in v0.2.0

func (fan Fan) String() string

func (*Fan) UpdateCycleFromState

func (fan *Fan) UpdateCycleFromState(remapper RemapFunc)

UpdateCycleFromState update pwm fan's Cycle information from State information

type FanState

type FanState uint

FanState represent a PWM Fan's running state TODO: try to implement String() method

const (
	// Stop represent Pwm Fan stay in a full stop state
	Stop FanState = iota
	// Start represent Pwm Fan enter in s Start state
	Start
	// Run represent Pwm Fan stay in a running state
	Run
)

func (FanState) String added in v0.2.0

func (state FanState) String() string

type RemapFunc

type RemapFunc func(float64, ...float64) float64

RemapFunc is a function that read at least one float64 input and output a float64 data

More specific, this function should take 1st argument as an input data, then read more input, and output an data Example: LinearRemap and LinearClampRemap

Directories

Path Synopsis
cmd
controller command
monitor command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL