如何使用 Grove - PH 传感器套件(E-201C-蓝色)Raspberry pi 零和 GrovePi 零板获得 Java 中的 pH

how to get pH in Java using Grove - PH Sensor Kit (E-201C-Blue) Raspberry pi Zero and GrovePi Zero Board

我有GrovePi Zero(GrovePi0) from GrovePi Zero Base Kit and Grove - PH Sensor Kit (E-201C-Blue) 我在 Raspberry Pi Zerro 2 上使用 Java(我可以使用任何版本的 JDK 8...17)。 使用 GrovePi-pi4j 和 Pi4j 版本 1.4(可以使用任何版本)

我的 class 下面的 GrovePHSensor 代表 PH 传感器。

@GroveAnalogPin
public class GrovePHSensor extends GroveAnalogInputDevice<Double> {
   
public GrovePHSensor(GrovePi grovePi, int pin) throws IOException {
    super(grovePi.getAnalogIn(pin, 4));
}

@Override
public Double get(byte[] data) {
 /// WHAT TO DO HERE?
}
}

问题是有大量奇怪的代码给出了不同的结果 即使我认为我理解它的作用,我也不确定这样做是否正确。

例如这个线程非常混乱https://forum.dexterindustries.com/t/grove-ph-sensor-kit-e-201c-blue-raspberry-pi-zero/7961/13

同时 wiki 页面在 Seeed https://wiki.seeedstudio.com/Grove-PH-Sensor-kit/ 给出了具有不同公式的 Arduino 的示例代码

当我读取 4 字节[]时,我得到类似 [Pi4J IO read][0, 1, -106, -1] 如果我读取超过 4 个字节,那么末尾的所有字节都是 -1

最好能清楚地实现 public Double get(byte[] data) {} 函数 ...

@GroveAnalogPin
public class GrovePHSensor extends GroveAnalogInputDevice<Double> {
     private static final Logger l = 
     LogManager.getLogger(GrovePHSensor.class.getName());
     /*** pH values range */
     public static final double PH_RANGE = 14.0;
    /*** 
      number of possible samples with 10 bit analog to digital converter 
     */
    public static final int A2D_RANGE = 1023;

public GrovePHSensor(GrovePi grovePi, int pin) throws IOException {
    super(grovePi.getAnalogIn(pin, 4));
}

@Override
public Double get(byte[] data) {
    // the extraction of the value is taken from the sample of the 
    // RotarySensor3Led 
    // https://github.com/DexterInd/GrovePi/blob/master/Software/Java8/GrovePi-spec/src/main/java/org/iot/raspberry/grovepi/devices/GroveRotarySensor.java
    // this is how its is done there

    int[] v = GroveUtil.unsign(data);
    double sensorValue = (v[1] * 256) + v[2];

    // the Analog to Digital is 10 bit -> 1023 intervals that cover the range of pH of 0 to 14
    // hence the pH value is the sensor value that is 0 to 1024
    // times the pH per interval of the A2D 14/1023
    double ph = sensorValue * (PH_RANGE / (double) A2D_RANGE);
    l.trace("sensorValue = {}  ph={}", sensorValue, ph);
    return ph;
    }
}