1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include <iostream> #include <fstream> #include <unistd.h> #include <wiringPi.h> #include <softPwm.h> #include <ctime> #define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp" #define LOG_PATH "/tmp/RaspberrypiFanSpeed.log" #define PID_PATH "/var/run/autocontrolfan.pid" #define _FANPIN 8 #define min(x,y) (x<=y?x:y) using namespace std;
void GetCpuTempera(ifstream &fin,double &temp); int initWiringPi(); void showInfo(); void SaveLog(ofstream &log, double &temp, int FanSpeed, time_t &time_cur);
int main() { showInfo(); ofstream log(LOG_PATH); if (!log.is_open()) { cout << "Can't open file : " << LOG_PATH << endl; } log.close(); ifstream fin(TEMP_PATH, ios_base::in); if (!fin.is_open()) { cout << "Can't open file : " << TEMP_PATH << endl; return -1; } ofstream pid(PID_PATH); if (!pid.is_open()) { cout << "Can't open file : " << PID_PATH << endl; } pid << getpid() << endl; pid.close(); pid.clear(); time_t time_cur; double temp = 0; int Fan_Speed = 0; bool Forty_five_Flag = false; if (initWiringPi() < 0) { return -1; }
while (true) { GetCpuTempera(fin,temp); if (temp >= 42) cout << "Cpu temperature is : \033[0;31m" << temp << "°C \033[0m" << flush; else cout << "Cpu temperature is : \033[1;32m" << temp << "°C \033[0m" << flush; if (Forty_five_Flag) { if (temp < 39.0) { Forty_five_Flag = false; Fan_Speed = 0; softPwmWrite(_FANPIN, Fan_Speed); SaveLog(log, temp, Fan_Speed, time_cur); } else { SaveLog(log, temp, Fan_Speed, time_cur); } sleep(1); } else { if (temp < 39.0) { sleep(1); Fan_Speed = 0; softPwmWrite(_FANPIN, Fan_Speed); SaveLog(log, temp, Fan_Speed, time_cur); } else if (temp >= 40.0 && temp <= 45.0) { Fan_Speed = min(((((int)temp - 40) * 10) + 60),100); softPwmWrite(_FANPIN, Fan_Speed); SaveLog(log, temp, Fan_Speed, time_cur); sleep(2); } else if (temp > 45.0) { Fan_Speed = 100; softPwmWrite(_FANPIN, Fan_Speed); Forty_five_Flag = true; SaveLog(log, temp, Fan_Speed, time_cur); sleep(5); } } sleep(1); cout << "\r"; } return 0; }
void GetCpuTempera(ifstream &fin, double &temp) { fin >> temp; temp = temp / 1000.0; fin.clear(); fin.seekg(0, ios::beg); }
int initWiringPi() { if (wiringPiSetup() != 0) { cout << "WiringPi setup failed" << flush << " \r"; return -1; } if (softPwmCreate(_FANPIN, 0, 100) != 0) { cout << "softPwmcreat setup failed" << flush << " \r"; return -2; } return 0; }
void showInfo() { cout << "-------------------------------------------------------------------------------" << endl; cout << " Project Name : AutoControlRaspberryfan " << endl; cout << " Author : BDZNH " << endl; cout << " Project URL : https://github.com/BDZNH/AutoControlRaspberryFan " << endl; cout << " what is this : Auto control raspberry fan with 5V. Turn the fan " << endl; cout << " when the temperaure is high than 45°C, turn off " << endl; cout << " fan when the CPU temperature is lower than 39°C. " << endl; cout << "-------------------------------------------------------------------------------" << endl; cout << "\n\n\n" << endl; }
void SaveLog(ofstream &log, double &temp, int Fan_Speed, time_t &time_cur) { log.open(LOG_PATH,ios_base::out); if (!log.is_open()) { cout << "Can't open file : " << LOG_PATH << endl; } time(&time_cur); log << ctime(&time_cur) << "CPU temperature is : " << temp << "°C \nSet fan speed to " << Fan_Speed << endl; log.close(); log.clear(); }
|