Herlambang_Rizky
Posts: 25
Joined: Thu Dec 28, 2017 11:07 am

Error when executing .c file in Raspi3

Sun Jan 28, 2018 8:43 am

hi guys, im new to this microcontroler thing. but sure im familiar with C code programming. its the main reason why i tried this code from :

http://www.uugear.com/portfolio/dht11-h ... or-module/ for my DHT11 sensor.

but when i did some googling search, in case using c on Rpi3. we must install the library of C on Rpi3 CMIIW. so i did install the library with following this page :
https://projects.drogon.net/raspberry-p ... d-install/ and succes all the step.

but then when i first try the code from first page up there, something wrong happen. like this in my terminal :
Image

can anyone tell me what did just something happen? i did import the library of C on my Pi3 so i think im fine with all the library. thank you for your time guys :)

B.Goode
Posts: 5803
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 9:18 am

Disclaimer: I am not a .c programmer, on RPi/Raspbian or any other platform: your experience is probably greater than mine.

But looking at your screenshot, you do not appear to have linked to the WiringPi library when you compile your .c source with gcc. That would probably result in the missing references that are reported.

User avatar
DougieLawson
Posts: 32669
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 9:27 am

To add to B.Goode's reply

gcc -o dht11 -lwiringPi dht11_test.c
should work.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Herlambang_Rizky
Posts: 25
Joined: Thu Dec 28, 2017 11:07 am

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 11:42 am

B.Goode wrote:
Sun Jan 28, 2018 9:18 am
Disclaimer: I am not a .c programmer, on RPi/Raspbian or any other platform: your experience is probably greater than mine.

But looking at your screenshot, you do not appear to have linked to the WiringPi library when you compile your .c source with gcc. That would probably result in the missing references that are reported.
thanks dude for the answer, but as i know we must use that library to run c on RPi. am i right ? but if we must not to use that lib, please tell me what should i do if this library seems appear to be unuseful for this case :)

Herlambang_Rizky
Posts: 25
Joined: Thu Dec 28, 2017 11:07 am

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 11:43 am

DougieLawson wrote:
Sun Jan 28, 2018 9:27 am
To add to B.Goode's reply

gcc -o dht11 -lwiringPi dht11_test.c
should work.
thank you mr D for the suggestion, i just did your command but nothing happened. yes there is no error messages but nothing happened. its not report or show anything :|

User avatar
DougieLawson
Posts: 32669
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 11:44 am

That means it's worked. gcc is silent until it detects a warning or error.

Run your program with ./dht11
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Herlambang_Rizky
Posts: 25
Joined: Thu Dec 28, 2017 11:07 am

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 2:18 pm

DougieLawson wrote:
Sun Jan 28, 2018 11:44 am
That means it's worked. gcc is silent until it detects a warning or error.

Run your program with ./dht11
yep its work, thank you so much mr.D . the result may be error but finaly it can executed thank you :)
Image

but last question, why we need that command "-lwiringPi"? what its suppose to ?

jahboater
Posts: 2624
Joined: Wed Feb 04, 2015 6:38 pm

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 3:30 pm

Some comments.

Please do not use sudo to edit a C source file in your own directory.
It is completely pointless and will get you into a real mess as the file may become owned by root.

You have edited the source file and then run the executable. You may have just been taking a look which is fine,
but I have a suspicion that you don't realize what the gcc compile command does. If you change the source file
you must re-compile it before running the program again. We usually use a program aptly called "make" which
worries about all this for you.

-lwiringPi tells the C compiler that it must link your program with the wiringPi library.
For example if you used the function xxxx() the compiler (the linker really) needs to know
where to look for the definition of xxxx().

User avatar
DougieLawson
Posts: 32669
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Error when executing .c file in Raspi3

Sun Jan 28, 2018 4:44 pm

Sorry I can't help with bad results from DHT11 or DHT22 sensors as I've not got one.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

Re: Error when executing .c file in Raspi3

Mon Jan 29, 2018 8:59 am

hello,
does this example work for you? IIRC, it once worked successfully:

GPIO4 P7 ——– DATA (S)

Code: Select all

/*
 *  dht11.c:
 *   Simple test program to test the wiringPi functions
 *   DHT11 test
 */

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS   85
#define DHTPIN      7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };

void read_dht11_dat()
{
   uint8_t laststate   = HIGH;
   uint8_t counter      = 0;
   uint8_t j      = 0, i;
   float   f; /* fahrenheit */

   dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;

   /* pull pin down for 18 milliseconds */
   pinMode( DHTPIN, OUTPUT );
   digitalWrite( DHTPIN, LOW );
   delay( 18 );
   /* then pull it up for 40 microseconds */
   digitalWrite( DHTPIN, HIGH );
   delayMicroseconds( 40 );
   /* prepare to read the pin */
   pinMode( DHTPIN, INPUT );

   /* detect change and read data */
   for ( i = 0; i < MAXTIMINGS; i++ )
   {
      counter = 0;
      while ( digitalRead( DHTPIN ) == laststate )
      {
         counter++;
         delayMicroseconds( 1 );
         if ( counter == 255 )
         {
            break;
         }
      }
      laststate = digitalRead( DHTPIN );

      if ( counter == 255 )
         break;

      /* ignore first 3 transitions */
      if ( (i >= 4) && (i % 2 == 0) )
      {
         /* shove each bit into the storage bytes */
         dht11_dat[j / 8] <<= 1;
         if ( counter > 16 )
            dht11_dat[j / 8] |= 1;
         j++;
      }
   }

   /*
    * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
    * print it out if data is good
    */
   if ( (j >= 40) &&
        (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )
   {
      f = dht11_dat[2] * 9. / 5. + 32;
      printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
         dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
   }else  {
      printf( "Data not good, skip\n" );
   }
}

int main( void )
{
   printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );

   if ( wiringPiSetup() == -1 )
      exit( 1 );

   while ( 1 )
   {
      read_dht11_dat();
      delay( 1000 ); /* wait 1sec to refresh */
   }

   return(0);
}
gcc -Wall -o dht11 dht11.c -lwiringPi
(admittedly I actually use Geany and build by F9 and run by F5)

if that also won't work for you:
how did you install wiringPi? via apt-get install or via git clone? Are you sure you don't have 2 conflicting installations?

Herlambang_Rizky
Posts: 25
Joined: Thu Dec 28, 2017 11:07 am

Re: Error when executing .c file in Raspi3

Mon Jan 29, 2018 12:11 pm

jahboater wrote:
Sun Jan 28, 2018 3:30 pm
Some comments.

Please do not use sudo to edit a C source file in your own directory.
It is completely pointless and will get you into a real mess as the file may become owned by root.

You have edited the source file and then run the executable. You may have just been taking a look which is fine,
but I have a suspicion that you don't realize what the gcc compile command does. If you change the source file
you must re-compile it before running the program again. We usually use a program aptly called "make" which
worries about all this for you.

-lwiringPi tells the C compiler that it must link your program with the wiringPi library.
For example if you used the function xxxx() the compiler (the linker really) needs to know
where to look for the definition of xxxx().
thank you for the information, i have no idea why sudo would be a problem when i do some edit on terminal. really appreciate it :)

and the -lwiringPi thing, really good explanation. im completely understand so much thank for that :)

Herlambang_Rizky
Posts: 25
Joined: Thu Dec 28, 2017 11:07 am

Re: Error when executing .c file in Raspi3

Mon Jan 29, 2018 12:13 pm

DougieLawson wrote:
Sun Jan 28, 2018 4:44 pm
Sorry I can't help with bad results from DHT11 or DHT22 sensors as I've not got one.
nope mr.d thank you for your help before. i think and absolutely sure my sensor is broke. i've done severals code on python and C today but sadly nothing work. and again , thank you :D

User avatar
joan
Posts: 13347
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Error when executing .c file in Raspi3

Mon Jan 29, 2018 12:16 pm

Try http://abyz.me.uk/rpi/pigpio/examples.html#pdif2_DHTXXD

(needs the pigpio daemon to be running, sudo pigpiod before running).

Herlambang_Rizky
Posts: 25
Joined: Thu Dec 28, 2017 11:07 am

Re: Error when executing .c file in Raspi3

Mon Jan 29, 2018 12:17 pm

Stiller wrote:
Mon Jan 29, 2018 8:59 am
hello,
does this example work for you? IIRC, it once worked successfully:

GPIO4 P7 ——– DATA (S)

Code: Select all

/*
 *  dht11.c:
 *   Simple test program to test the wiringPi functions
 *   DHT11 test
 */

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS   85
#define DHTPIN      7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };

void read_dht11_dat()
{
   uint8_t laststate   = HIGH;
   uint8_t counter      = 0;
   uint8_t j      = 0, i;
   float   f; /* fahrenheit */

   dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;

   /* pull pin down for 18 milliseconds */
   pinMode( DHTPIN, OUTPUT );
   digitalWrite( DHTPIN, LOW );
   delay( 18 );
   /* then pull it up for 40 microseconds */
   digitalWrite( DHTPIN, HIGH );
   delayMicroseconds( 40 );
   /* prepare to read the pin */
   pinMode( DHTPIN, INPUT );

   /* detect change and read data */
   for ( i = 0; i < MAXTIMINGS; i++ )
   {
      counter = 0;
      while ( digitalRead( DHTPIN ) == laststate )
      {
         counter++;
         delayMicroseconds( 1 );
         if ( counter == 255 )
         {
            break;
         }
      }
      laststate = digitalRead( DHTPIN );

      if ( counter == 255 )
         break;

      /* ignore first 3 transitions */
      if ( (i >= 4) && (i % 2 == 0) )
      {
         /* shove each bit into the storage bytes */
         dht11_dat[j / 8] <<= 1;
         if ( counter > 16 )
            dht11_dat[j / 8] |= 1;
         j++;
      }
   }

   /*
    * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
    * print it out if data is good
    */
   if ( (j >= 40) &&
        (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )
   {
      f = dht11_dat[2] * 9. / 5. + 32;
      printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
         dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
   }else  {
      printf( "Data not good, skip\n" );
   }
}

int main( void )
{
   printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );

   if ( wiringPiSetup() == -1 )
      exit( 1 );

   while ( 1 )
   {
      read_dht11_dat();
      delay( 1000 ); /* wait 1sec to refresh */
   }

   return(0);
}
gcc -Wall -o dht11 dht11.c -lwiringPi
(admittedly I actually use Geany and build by F9 and run by F5)

if that also won't work for you:
how did you install wiringPi? via apt-get install or via git clone? Are you sure you don't have 2 conflicting installations?
this code were actualy i use on my Pi. and as long as i remember , i just did one time installing wiringPi by apt-get install.
:D

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 2 guests