Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTH11温湿度读取失败 #222

Open
jiaozg22 opened this issue May 13, 2022 · 2 comments
Open

DTH11温湿度读取失败 #222

jiaozg22 opened this issue May 13, 2022 · 2 comments

Comments

@jiaozg22
Copy link

您好,我用树莓派4b,连接到DTH11温湿度模块上,通过控制微妙数操作读取温湿度失败。具体如下:
public String read(int[] oneData, int poist) {
count = 0;
firstLow = true;
firstHigh = true;
int PIN_LED = poist;

    var pi4j = Pi4J.newAutoContext();
    logger.info("pcm针脚:{},读取数据", poist);

    var inputConfig = DigitalInput.newConfigBuilder(pi4j)
            .id("input")
            .name("input dth11")
            .address(PIN_LED)
            .provider("pigpio-digital-input");

    var input = pi4j.create(inputConfig);

    input.addListener(new DigitalStateChangeListener() {
        long start = System.nanoTime();

        @Override
        public void onDigitalStateChange(DigitalStateChangeEvent event) {
            logger.info("与上次的时间差:{}", System.nanoTime() - start);
            start = System.nanoTime();
            //5.监听ddata针脚为高电平,并且持续80us,开始读取

            if (event.state().equals(DigitalState.LOW) && firstLow) {
                //第一次监测到低电平,且持续80us
                logger.info("第一次低电平,开始时间:{},持续时间:{}", System.nanoTime(), System.nanoTime() - start);
                firstLow = false;
            }


            if (event.state().equals(DigitalState.LOW) && firstLow) {
                //第一次监测到低电平,且持续80us
                logger.info("第一次低电平,开始时间:{},持续时间:{}", System.nanoTime(), System.nanoTime() - start);
                firstLow = false;
                if (event.state().equals(DigitalState.HIGH) && firstHigh) {
                    logger.info("第一次高电平,接下来开始数据传送,开始时间:{},持续时间:{}", System.nanoTime(), System.nanoTime() - start);
                    firstHigh = false;
                }
            }

            count++;
            logger.info("针脚变化的次数:{},持续时间:{}", count, System.nanoTime() - start);
            start = System.nanoTime();
        }
    });

//
StringBuffer result = new StringBuffer("");
//每次读取是上一次的温湿度值,不是本次的值。
int i = 0;
if (oneData == null) {
oneData = new int[40];
}

    //湿度
    int[] humidity_bit = new int[8];//湿度整数位
    int[] humidity_point_bit = new int[8];//湿度小数位

    //温度
    int[] temperature_bit = new int[8];//温度整数位
    int[] temperature_point_bit = new int[8];//温度小数位

    //校验位
    int[] check_bit = new int[8];//校验位
    int check = 0;//校验结果

    //1.data针脚等待状态,输出高电平
    var ledConfig = DigitalOutput.newConfigBuilder(pi4j)
            .id("led")
            .name("LED Flasher")
            .address(PIN_LED)
            .shutdown(DigitalState.LOW)
            .initial(DigitalState.HIGH)
            .provider("pigpio-digital-output");

    var led = pi4j.create(ledConfig);
    logger.info("数据针脚电阻拉高,是否为高电平:{}", led.isHigh());
    //等待监听事件结束
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    logger.info("稳定一秒钟:{}", led.isHigh());
    logger.info("读取17gpio的值::{}", PIGPIO.gpioRead(17));;

    //2.主机发送信号到dht11,(输出低电平,持续时间大于18ms)
    led.low();

    final long INTERVAL = 20 * 1000 * 1000;

//
final long start = System.nanoTime();
//
long end = 0;

    do {
        end = System.nanoTime();

    } while (start + INTERVAL >= end);

    logger.info("主机发送开始信号 到dht11,,具体为低电平,持续18ms以上;实际状态为:{},实际时间为:{}", led.isLow(), end - start);

    //3.主机拉高data针脚电平,表示开始信号结束
    led.high();
    end = System.nanoTime();
    logger.info("主机拉高data针脚电平,表示开始信号结束;实际状态为:{},实际时间为:{}", led.isHigh(), end - start);

    //4.设置针脚为输入,并监测事件,持续20-40us
    long inputStart = System.nanoTime();
    final long dealy = 30 * 1000;


    do {
        end = System.nanoTime();

    } while (inputStart + dealy >= end);

    logger.info("设置针脚为输入,状态:{},并监测事件,持续20-40us,持续时间{}", led.state().name(), end - inputStart);

    //等待监听事件结束
    try {
        Thread.sleep(60000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    led.shutdown(pi4j);


    String humidity_str = Arrays.toString(humidity_bit).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
    String humidity_point_str = Arrays.toString(humidity_bit).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
    String temperature_str = Arrays.toString(humidity_bit).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
    String temperature_point_str = Arrays.toString(humidity_bit).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
    String check_str = Arrays.toString(check_bit).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");

    logger.info("humidity_str" + humidity_str);
    Integer humidity_bit_data = Integer.valueOf(humidity_str, 2);
    Integer humidity_point_bit_data = Integer.valueOf(humidity_point_str, 2);
    Integer temperature_bit_data = Integer.valueOf(temperature_str, 2);
    Integer temperature_point_bit_data = Integer.valueOf(temperature_point_str, 2);

    //校验
    Integer sum = humidity_bit_data + humidity_point_bit_data + temperature_bit_data + temperature_point_bit_data;

    if (sum.equals(check_str)) {
        //结果转换
        result.append("h:").append(humidity_bit).append(humidity_point_bit).append(";")
                .append("t:").append(temperature_bit).append(temperature_point_bit);

// //开启事件监听温度过高告警信息
// thListener.actionPerformed(new HumidityEvent(100.01f));
// thListener.actionPerformed(new TemplateEvent(100.01f));

    } else {
        return "-1";//错误读取,需要重新调起
    }

    return result.toString();
}

麻烦问下,为何读取数据的事件并未触发?

@BierDav
Copy link

BierDav commented Jul 23, 2023

If you would be so kind as to appropriately format your code and translate the content of your issue into English, there might be someone who could assist you with resolving your problem.

@taartspi
Copy link
Collaborator

'Please ask, why the event of reading data is not triggered' seems to be your question: As asked we need a translation and single formatted code to help with your implementation. But, I have a DHT22 implementation working, this should be compatible with the DHT11 operation.. Events are not usable with this device. In my readme /* If the commented use of the listener DataInGpioListener, this would
be a more normal implementation. However, the time to idle the gpio from output operation and
re-init the gpio as an input with a listener takes too long to complete
and DHT22 signals are lost and the device attempt to send data fails.
So, for the time being a simple polling implementation is used. */ see https://github.com/Pi4J/pi4j-example-devices/tree/master/src/main/java/com/pi4j/devices/dht22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants