从crontab运行的perl程序产生错误的i2cget raspberry pi值

从crontab运行的perl程序产生错误的i2cget raspberry pi值,perl,cron,crontab,raspberry-pi,i2c,Perl,Cron,Crontab,Raspberry Pi,I2c,我已经阅读了许多其他的问答,这些问答似乎都是相关的,但我还没有在这里找到问题所在 我在Raspberry Pi上使用了以下perl脚本。我希望将I2C传感器的温度数据记录到sqlite3数据库中。下面的程序在从命令行运行时工作,但在从cron运行时不工作 我假设i2cget的值在从cron运行时不正确,但我不确定如何确定i2cget环境的哪些部分需要正常工作 #!/usr/bin/perl #printf '%d\n' `i2cget -y 1 0x48 0x0` $temp = `i2cge

我已经阅读了许多其他的问答,这些问答似乎都是相关的,但我还没有在这里找到问题所在

我在Raspberry Pi上使用了以下perl脚本。我希望将I2C传感器的温度数据记录到sqlite3数据库中。下面的程序在从命令行运行时工作,但在从cron运行时不工作

我假设i2cget的值在从cron运行时不正确,但我不确定如何确定i2cget环境的哪些部分需要正常工作

#!/usr/bin/perl

#printf '%d\n' `i2cget -y 1 0x48 0x0`
$temp = `i2cget -y 1 0x48 0x0`;
#$temp = sprintf("%d\n", $temp);
$temp = hex($temp);
#print $temp, "\n";

use DBI;

#/home/techplex/ece331/project2_temp_data_grapher/
$dbh = DBI->connect( "dbi:SQLite:tempdata.db" ) or die "Cannot connect: $DBI::errstr";

$dbh->do( "CREATE TABLE IF NOT EXISTS temperature (timestamp datetime, temperature float);" );
$dbh->do( "INSERT INTO temperature (timestamp, temperature) VALUES (datetime('now', 'localtime'), $temp);" );

$dbh->disconnect;
我已将这一行添加到我的crontab中:

*/1 * * * * cd /home/techplex/temp_data_grapher; ./datalogger.pl

尝试分析cron环境与命令行:

set > command.lis
从命令行:

set > command.lis
暂时将其添加到crontab,在获得结果后将其删除:

* * * * * set > /home/techplex/crontab.lis
(假设/home/techplex是一个目录并且存在。并且是您的主目录,请根据需要进行更改。)

现在您有两个文件

diff crontab.lis command.lis

将向您展示您所处环境的不同之处

尝试分析cron环境与命令行:

set > command.lis
从命令行:

set > command.lis
暂时将其添加到crontab,在获得结果后将其删除:

* * * * * set > /home/techplex/crontab.lis
(假设/home/techplex是一个目录并且存在。并且是您的主目录,请根据需要进行更改。)

现在您有两个文件

diff crontab.lis command.lis

将向您展示您所处环境的不同之处

在perl脚本中需要i2c的完整路径:

$temp = `/full/path/to/i2cget -y 1 0x48 0x0`;

在perl脚本中需要i2c的完整路径:

$temp = `/full/path/to/i2cget -y 1 0x48 0x0`;