我正在设置一个PHP页面来控制使用树莓派的一些基本传感器。我正在处理我自己的PHP代码,但多年以来我甚至触及了编码,而且我对C语言感到茫然。我已经编译了下面的代码,它运行良好,我从传感器获取数据。我能够将数据移植到PHP中而不会出现问题。我希望能够做的是将一个参数传递给已编译的C程序,该程序将是哪个PIN读取树莓派。
define DHT_PIN 29
(数字29是我希望通过命令行参数传递的。)
C代码:
#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();
}
我尝试过阅读,但C不是我的语言。我正在寻求一些帮助来完成我的任务。
答案 0 :(得分:1)
您需要更改main
函数的原型,然后解析所提供的参数:
#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;
}
如果需要更复杂的参数解析,请参阅getopt
函数。
答案 1 :(得分:-1)
像这样使用argv [](假设参数是一个整数):
int dht_pin = atoi(argv[1])
您需要将main定义为:
int main(int argc, char *argv[])
{ ... }
atoi()是stdlib.h的一部分