bastilmr / opi5-fan-control

23 stars 5 forks source link

undefined reference to `crypt' undefined reference to `pow' #2

Open StuartIanNaylor opened 1 year ago

StuartIanNaylor commented 1 year ago
orangepi@orangepi5:~/opi5-fan-control$ gcc -o fan-control fan-control.c -lwiringPi
/usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/11/../../../../lib/libwiringPi.so: undefined reference to `crypt'
/usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/11/../../../../lib/libwiringPi.so: undefined reference to `pow'
collect2: error: ld returned 1 exit status

I cloned the v0.2 version and seemed to install ok

orangepi@orangepi5:~/opi5-fan-control$ gpio readall
 +------+-----+----------+--------+---+   OPI5   +---+--------+----------+-----+------+
 | GPIO | wPi |   Name   |  Mode  | V | Physical | V |  Mode  | Name     | wPi | GPIO |
 +------+-----+----------+--------+---+----++----+---+--------+----------+-----+------+
 |      |     |     3.3V |        |   |  1 || 2  |   |        | 5V       |     |      |
 |   47 |   0 |    SDA.5 |     IN | 1 |  3 || 4  |   |        | 5V       |     |      |
 |   46 |   1 |    SCL.5 |     IN | 1 |  5 || 6  |   |        | GND      |     |      |
 |   54 |   2 |    PWM15 |     IN | 1 |  7 || 8  | 0 | IN     | RXD.0    | 3   | 131  |
 |      |     |      GND |        |   |  9 || 10 | 0 | IN     | TXD.0    | 4   | 132  |
 |  138 |   5 |  CAN1_RX |     IN | 1 | 11 || 12 | 1 | IN     | CAN2_TX  | 6   | 29   |
 |  139 |   7 |  CAN1_TX |     IN | 1 | 13 || 14 |   |        | GND      |     |      |
 |   28 |   8 |  CAN2_RX |     IN | 1 | 15 || 16 | 1 | IN     | SDA.1    | 9   | 59   |
 |      |     |     3.3V |        |   | 17 || 18 | 1 | IN     | SCL.1    | 10  | 58   |
 |   49 |  11 | SPI4_TXD |     IN | 1 | 19 || 20 |   |        | GND      |     |      |
 |   48 |  12 | SPI4_RXD |     IN | 1 | 21 || 22 | 1 | IN     | GPIO2_D4 | 13  | 92   |
 |   50 |  14 | SPI4_CLK |     IN | 1 | 23 || 24 | 1 | IN     | SPI4_CS1 | 15  | 52   |
 |      |     |      GND |        |   | 25 || 26 | 1 | IN     | PWM1     | 16  | 35   |
 +------+-----+----------+--------+---+----++----+---+--------+----------+-----+------+
 | GPIO | wPi |   Name   |  Mode  | V | Physical | V |  Mode  | Name     | wPi | GPIO |
 +------+-----+----------+--------+---+   OPI5   +---+--------+----------+-----+------+

Any idea on what the errors are as all new to me.

After a Google I tried gcc -o fan-control fan-control.c -lwiringPi -lcrypt -lm and worked

StuartIanNaylor commented 1 year ago

Also the 30mm 3pin fan I got from PiHut seems to stall under 20% which might be the PWM rate is to slow and not the mark/space but not sure how to set that. I think fans usually run at 20 or 25 Khz normally just over hearing for any coil whine. So anyway I edited to just add 0 than min pwm but still have a min/max pwm so off below min temp but ramps up quicker.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <softPwm.h>

// Define
#define FAN_PIN 3
#define MIN_TEMP 40
#define MAX_TEMP 75
#define CPU_TEMP_FILE "/sys/class/thermal/thermal_zone0/temp"
#define MIN_PWM 25
#define MAX_PWM 100

int main() {
    int temp;
    float temp_ratio, pwm_ratio;
    int pwm_value = 0;

    if (wiringPiSetup() == -1) {
        printf("wiringPiSetup failed\n");
        return 1;
    }

    pinMode(FAN_PIN, OUTPUT);
    softPwmCreate(FAN_PIN, MIN_PWM, MAX_PWM);

    while(1) {
        FILE* temp_file = fopen(CPU_TEMP_FILE, "r");

        if (temp_file == NULL) {
            printf("Failed to read CPU temperature file\n");
            return 1;
        }

        fscanf(temp_file, "%d", &temp);
        fclose(temp_file);

        temp /= 1000;

        if (temp < MIN_TEMP) {
            pwm_value = 0;
        } else if (temp > MAX_TEMP) {
            pwm_value = MAX_PWM;
        } else {
            temp_ratio = (float)(temp - MIN_TEMP) / (MAX_TEMP - MIN_TEMP);
            pwm_ratio = temp_ratio * (MAX_PWM - MIN_PWM);
            pwm_value = (int)(pwm_ratio + MIN_PWM);
        }

        softPwmWrite(FAN_PIN, pwm_value);

        printf("CPU: %d°C | PWM: %d%%\n", temp, pwm_value);

        sleep(5);
    }
homonto commented 1 year ago

@StuartIanNaylor my blabla fan starts from 50% as it is not 3 pin fan - just 2 pins ;-) but still better 50%-100% than 0% or 100%

StuartIanNaylor commented 1 year ago

@StuartIanNaylor my blabla fan starts from 50% as it is not 3 pin fan - just 2 pins ;-) but still better 50%-100% than 0% or 100%

Yeah 2 pin fans are not just DC motors as they would be perfect for PWM but likely have a built-in circuit, 3 pin usually are voltage controlled with a tacho. The 3pin GPIO fans are PWM 3.3v input control and so do work correct but likely not at 25Khz which you would prob have to dedicate a PWM to that GPIO and set the base frquency.