Fan on C1

Post Reply
User avatar
bcclsn
Posts: 106
Joined: Thu Mar 05, 2015 12:04 am
languages_spoken: italian
ODROIDs: Odroid C1
Location: avellino, Italy
Has thanked: 2 times
Been thanked: 0
Contact:

Fan on C1

Unread post by bcclsn » Fri Aug 23, 2019 4:36 pm

hi guys,

my C1 in this period, is very hot so I decided to install a mini fan in addition to a copper heatsink. to control the fan, I realized a mini electronic circuit, using a transistor... than I wrote a simple program in C (sorry, I don't know python) in order to check the cpu temperature and to control the base of the transistor (I used a bjt) to start/stop the fan (in addition, with this system I could use a +12V fan instead a +5V... that I didn't have at home). finally I wrote a systemd service to auto-start the script at boot.

Code: Select all

[Unit]
Description=Fan Control

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/home/odroid/./.script/fan_control

[Install]
WantedBy=multi-user.target

Code: Select all

/*
 ============================================================================
 Name        : fan_control.c
 Author      : bcclsn
 Version     : 1.0
 Copyright   : null
 Description : controlla l'accensione e lo spegnimento di una ventola tramite
               transistor e pin gpio
 ============================================================================
 */
#include <wiringPi.h>

#define FTEMP     "/sys/class/thermal/thermal_zone0/temp"
#define THRESHOLD 60000                                               // first two MSD (degree)
#define FORCED_ON 12                                                  // 12 cycle
#define GPIO_PIN  1

int os_read_d(char  *fname) {                                         // thanks to vbextreme
  FILE* fd = fopen(fname, "r");

  if (fd == NULL) {
    return -1;
  }
  char inp[64];
  inp[0] = 0;
  fgets(inp, 64, fd);
  return strtoul(inp, NULL, 10);
}

void main(void) {
  int temperature;
  int counter = 0;

  wiringPiSetup();
  pinMode(GPIO_PIN, OUTPUT);

  while(1) {
     temperature = os_read_d(FTEMP);

     if (temperature >= THRESHOLD) {
        digitalWrite(GPIO_PIN, HIGH);                                 // start the fan
        counter = 0;                                                  // reset the counter
     } else {                                                         // else if temperature is under the threshold
        counter++;                                                    // start the counter
        if (counter > FORCED_ON) {                                    // after 12 cycle under the threshold (cycle * delay = one minute)
           digitalWrite(GPIO_PIN, LOW);                               // stop the fan
        }
     }
     delay(5000);
  }
}
ndr.
I compile the script using:

Code: Select all

gcc -o wpi_exam_led wpi_exam_led.c -lwiringPi -lwiringPiDev -lm -lpthread -lrt -lcrypt
source: https://wiki.odroid.com/odroid-xu4/appl ... o/wiringpi

now we came to the problem:
both when I start the script using systemd (or crontab) and when I start it manually (./fan) the fan starts correctly but after some time it stopps and the temperature goes up (+70 to +80 degree).
I don't understand the reason of the fan stop... I don't know how investigate the system.

can you help me?
thanks in advance :)

User avatar
mad_ady
Posts: 6530
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 163 times
Been thanked: 125 times
Contact:

Re: Fan on C1

Unread post by mad_ady » Fri Aug 23, 2019 4:45 pm

Modify your code to start printing on stdout the current temperature and relevant variables to see where it goes wrong. You can then compare them with what your system reports.

User avatar
bcclsn
Posts: 106
Joined: Thu Mar 05, 2015 12:04 am
languages_spoken: italian
ODROIDs: Odroid C1
Location: avellino, Italy
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Fan on C1

Unread post by bcclsn » Fri Aug 23, 2019 4:50 pm

mad_ady wrote:
Fri Aug 23, 2019 4:45 pm
Modify your code to start printing on stdout the current temperature and relevant variables to see where it goes wrong. You can then compare them with what your system reports.
I've already done it but I still didn't understand. anyway, I'm editing it again... stay tuned :)

User avatar
bcclsn
Posts: 106
Joined: Thu Mar 05, 2015 12:04 am
languages_spoken: italian
ODROIDs: Odroid C1
Location: avellino, Italy
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Fan on C1

Unread post by bcclsn » Fri Aug 23, 2019 6:26 pm

mad_ady wrote:
Fri Aug 23, 2019 4:45 pm
Modify your code to start printing on stdout the current temperature and relevant variables to see where it goes wrong. You can then compare them with what your system reports.
well done, I've found the issue!

after 1h and 34min of monitoring, the temperature read is -1...
it is obvious that there is a bug in the function written by my friend vbextreme of the archlinux forum. so, now, I have to understand how fix it

Code: Select all

int os_read_d(char  *fname) {                                         // thanks to vbextreme
  FILE* fd = fopen(fname, "r");

  if (fd == NULL) {
    return -1;
  }
  char inp[64];
  inp[0] = 0;
  fgets(inp, 64, fd);
  return strtoul(inp, NULL, 10);
}

User avatar
mad_ady
Posts: 6530
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 163 times
Been thanked: 125 times
Contact:

Re: Fan on C1

Unread post by mad_ady » Fri Aug 23, 2019 7:42 pm

I'd say he never closes the file descriptor after reading so the process runs out of file descriptors after a while. Adding a fclose after fgets should help.
These users thanked the author mad_ady for the post:
bcclsn (Fri Aug 23, 2019 8:36 pm)

User avatar
bcclsn
Posts: 106
Joined: Thu Mar 05, 2015 12:04 am
languages_spoken: italian
ODROIDs: Odroid C1
Location: avellino, Italy
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Fan on C1

Unread post by bcclsn » Fri Aug 23, 2019 8:39 pm

mad_ady wrote:I'd say he never closes the file descriptor after reading so the process runs out of file descriptors after a while. Adding a fclose after fgets should help.
mmm mey be...
I'm editing, hope for the best

User avatar
bcclsn
Posts: 106
Joined: Thu Mar 05, 2015 12:04 am
languages_spoken: italian
ODROIDs: Odroid C1
Location: avellino, Italy
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Fan on C1

Unread post by bcclsn » Sat Aug 24, 2019 1:46 am


after +4h of uptime, the fan is still working :3
need a lot of test, but it seems to be solved! :)

User avatar
mad_ady
Posts: 6530
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 163 times
Been thanked: 125 times
Contact:

Re: Fan on C1

Unread post by mad_ady » Sat Aug 24, 2019 2:06 am

Always close open files. It's a matter of curtosy.
These users thanked the author mad_ady for the post:
bcclsn (Sat Aug 24, 2019 2:27 am)

User avatar
bcclsn
Posts: 106
Joined: Thu Mar 05, 2015 12:04 am
languages_spoken: italian
ODROIDs: Odroid C1
Location: avellino, Italy
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Fan on C1

Unread post by bcclsn » Sat Aug 24, 2019 2:28 am

I know... it had completely escaped me :(

Post Reply

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 1 guest