0

I am setting up a PHP page to control some basic sensors using a raspberry pi. I working off my own PHP code but it has been years since I've even touched coding and I'm at a loss when it comes to C. I've compiled the code below and it runs great and I get data back from the sensor. I'll be able to get the data ported into PHP without an issue. What I would like to be able to do is pass an argument to the compiled C program that would be which PIN to read off the raspberry pi.

define DHT_PIN 29 (The number 29 is what I would like to be passed through command line arguments.)

C Code:

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define MAX_TIMINGS 85
#define DHT_PIN 29
int data[5] = { 0, 0, 0, 0, 0 };

void read_dht_data()
{
    uint8_t laststate    = HIGH;
    uint8_t counter        = 0;
    uint8_t j            = 0, i;

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

    /* pull pin down for 18 milliseconds */
    pinMode( DHT_PIN, OUTPUT );
    digitalWrite( DHT_PIN, LOW );
    delay( 18 );

    /* prepare to read the pin */
    pinMode( DHT_PIN, INPUT );

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

        if ( counter == 255 )
            break;

        /* ignore first 3 transitions */
        if ( (i >= 4) && (i % 2 == 0) )
        {
            /* shove each bit into the storage bytes */
            data[j / 8] <<= 1;
            if ( counter > 16 )
                data[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) &&
         (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
    {
        float h = (float)((data[0] << 8) + data[1]) / 10;
        if ( h > 100 )
        {
            h = data[0];    // for DHT11
        }
        float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
        if ( c > 125 )
        {
            c = data[2];    // for DHT11
        }
        if ( data[2] & 0x80 )
        {
            c = -c;
        }
        printf( "%.1f %% %.1f *C\n", h, c );
    }
    else  {
       printf( "Data not good, skip\n" );
    }
}

int main( void )
{
    printf( "DHT22 temperature/humidity test\n" );

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

    read_dht_data();
}

I've tried reading but C is not my language. I'm asking for some help to accomplish my task.

4
  • Can't really do this (#define) via command line arguments as macros are expanded at compile time. Could initialize a static variable instead. Or could pass "-DDHT_PIN=29` when compiling.
    – MFisherKDX
    Commented Feb 23, 2018 at 22:52
  • @MFisherKDX The OP clearly stated "pass an argument to the compiled C program", both options you present are incorrect. The application needs to be altered to parse the command line argument provided by the user at run time.
    – Geoffrey
    Commented Feb 23, 2018 at 22:57
  • @EricPaquette please add what you have tried so far. Commented Feb 23, 2018 at 22:58
  • @Geoffrey ... "initialize a variable" would satisfy the OPs requirement. All he has to do is parse it from argv and use a variable instead if a macro. Not sure what you are arguing about.
    – MFisherKDX
    Commented Feb 23, 2018 at 23:25

2 Answers 2

1

You need to alter your prototype for your main function and then parse the arguments provided:

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define MAX_TIMINGS 85
int data[5] = { 0, 0, 0, 0, 0 };

void read_dht_data(long dhtPin)
{
    uint8_t laststate    = HIGH;
    uint8_t counter        = 0;
    uint8_t j            = 0, i;

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

    /* pull pin down for 18 milliseconds */
    pinMode( dhtPin, OUTPUT );
    digitalWrite( dhtPin, LOW );
    delay( 18 );

    /* prepare to read the pin */
    pinMode( dhtPin, INPUT );

    /* detect change and read data */
    for ( i = 0; i < MAX_TIMINGS; 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 */
            data[j / 8] <<= 1;
            if ( counter > 16 )
                data[j / 8] |= 1;
            j++;
        }
    }
}

int main(int argc, char * argv[])
{
  if (argc != 2)
  {
    printf("You must specify the DHT pin\n");
    return 1;
  }

  char *end;
  long dhtPin = strtol(argv[1], &end, 10);
  if (end == argv[1])
  {
    printf("Invalid number\n");
    return 1;
  }

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

  read_dht_data(dhtPin);
  return 0;
}

If you need more complex argument parsing see the getopt function.

1
  • Thanks for the quick reply. This worked like a charm. Had to add the print out as it was ommited from your answer. But it was an easy alteration. Thanks again. Commented Feb 23, 2018 at 23:12
-1

Use argv[] like so (assuming the argument is an integer):

int dht_pin = atoi(argv[1])

You'll need to define main as:

int main(int argc, char *argv[])
{ ... }

atoi() is part of stdlib.h

1
  • Don't advocate the usage of atoi, it is impossible to detect failure to parse, espesially when zero is a valid input, instead strtol should be used.
    – Geoffrey
    Commented Feb 23, 2018 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.