gpt4 book ai didi

java - 如何使用 Java 运行带参数的 python 代码,./AdafruitDHT.py 22 4

转载 作者:太空宇宙 更新时间:2023-11-04 10:33:42 24 4
gpt4 key购买 nike

你好。我正在尝试将 raspbian 设置为与传感器(如 DHT22 温度和湿度)一起使用。找到了用 Python 编写的驱动程序库 https://github.com/adafruit/Adafruit_Python_DHT并安装到我的 Raspberry 3 B+ 中,并提供了必要的库来工作。要通过 LXTerminal 运行此传感器,必须输入

cd /home/pi/Adafruit_Python_DHT/examples
./AdafruitDHT.py 22 4

这些数字是需要工作的参数:22 指定我现在使用哪个 Controller ,例如 22 - DHT22,4 指定我现在使用的raspberrypi的GPIO引脚

只要我使用这种方式,就可以返回“Temp=20.1° Humidity=59.8%”。但需要将这些控制台线放入简单的java程序中才能工作。找到了类似这个脚本的东西

测试.java

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(final String[] args) throws IOException, InterruptedException {
//Build command
List<String> commands = new ArrayList<String>();
commands.add("./Adafruit.py");
//Add arguments
commands.add("22 4");
System.out.println(commands);

//Run macro on target
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory(new File("/home/pi/Adafruit_Python_DHT/examples"));
pb.redirectErrorStream(true);
Process process = pb.start();

//Read output
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null, previous = null;
while ((line = br.readLine()) != null)
if (!line.equals(previous)) {
previous = line;
out.append(line).append('\n');
System.out.println(line);
}

//Check result
if (process.waitFor() == 0) {
System.out.println("Success!");
System.exit(0);
}

//Abnormal termination: Log command parameters and output and throw ExecutionException
System.err.println(commands);
System.err.println(out.toString());
System.exit(1);
}
}

通过这种方式,我知道它实际上可以工作路径部分和部分 AdafruitDHT.py (它返回字符串以使用正确的命令,例如

./AdafruitDHT.py 22 4

我不能做类似的事情

commands.add("./Adafruit.py 22 4");

因为它返回没有像这样的程序。

长话短说 - 需要重新编辑此代码才能与我添加的参数正常工作,但是......实际上有第二种方法可以修复它,但需要重新编辑此 AdafruitDHT.py 文件以获得常量参数(22 4),但不知道该怎么做。脚本包含代码:

AdafruitDHT.py

import sys

import Adafruit_DHT


# Parse command line parameters. sensor_args = {
'11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 } if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2] else:
print('usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#')
print('example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4')
sys.exit(1)

# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry). humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32

# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again! if humidity is not None and temperature is not None:
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)) else:
print('Failed to get reading. Try again!')
sys.exit(1)

最佳答案

好的,这个脚本正在运行;)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
public static void main(String[] args) throws IOException {
String[] command = new String[]{"/home/pi/Desktop/dht22/examples/AdafruitDHT.py","22","4"};

Process proc = new ProcessBuilder(command).start();
try {
proc = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}

BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));

String line = "";
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}

try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

关于java - 如何使用 Java 运行带参数的 python 代码,./AdafruitDHT.py 22 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49722474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com