`

树莓派3 +DHT11传感器+Shell读取温度+Java读取温度

 
阅读更多

        经过大量尝试Java直接操作底层存在一些问题,读取温度经常读取不到。本人也不会Python(想学,又不想花时间,听说很容易没有试过)如果使用Shell读取底层速度快效率高。直接上代码。

 

直接上代码:

SHELL部分(文件名dht11_info.sh)

 

#!/bin/bash
# Get information from DHT11 Temperature and Humidity Sensor
# /sys/bus/iio/devices/iio:device0
ok=true
while ok==true
	do
		echo 'run it:'
		TEMP=`cat /sys/bus/iio/devices/iio\:device0/in_temp_input`
#		point='expr index "$TEMP" s'
		echo $TEMP' ->length-> '${#TEMP}
                if [ ${#TEMP} -gt 0 -a ${#TEMP} -le 5 ]
                then
			let TEMP=$TEMP/1000
			HUMIDITY=`cat /sys/bus/iio/devices/iio\:device0/in_humidityrelative_input`
			let sd=$HUMIDITY/1000
                        rm wd.json
                        echo '{"WD":"'$TEMP'","sd":"'$sd'"}'
	                echo '{"wd":"'$TEMP'","sd":"'$sd'"}'&>>wd.json
		       	exit
                else
		        echo ${#TEMP}  >  0
		fi
#		sleep 1
#		clear
	done

 

 

 

 

JAVA部分(DHT11.java)

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DHT11 {
	public static final String WD="./dht11_info.sh";
	public static final String WDJSONFILE="wd.json";
	
	public static String getShell(String command) {
		InputStreamReader stdISR = null;  
        InputStreamReader errISR = null;  
        Process process = null;  
        try {  
        	System.out.println(command);
            process = Runtime.getRuntime().exec(command);  
            int exitValue = process.waitFor();  
  
            String line = null;  
  
            stdISR = new InputStreamReader(process.getInputStream());  
            BufferedReader stdBR = new BufferedReader(stdISR);  
            while ((line = stdBR.readLine()) != null) {
            	System.out.println(line);
               return line;
            }  
  
            errISR = new InputStreamReader(process.getErrorStream());  
            BufferedReader errBR = new BufferedReader(errISR);  
            while ((line = errBR.readLine()) != null) {  
            	System.err.println("err:"+line);
            	continue;
            }  
        } catch (IOException | InterruptedException e) {  
        } finally {  
            try {  
                if (stdISR != null) {  
                    stdISR.close();  
                }  
                if (errISR != null) {  
                    errISR.close();  
                }  
                if (process != null) {  
                    process.destroy();  
                }  
            } catch (IOException e) {  
            }  
        }  
        return getShell(command);
	}
	public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//读取文件 
            String s = null;
            while((s = br.readLine())!=null){
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
	public static void main(String[] args) {
		File file=new File(WDJSONFILE);
		if(file.exists()) {
			file.delete();
		}
		while(!file.exists()) {
			System.err.println("weng du:"+getShell(WD));
			System.err.println("###################################"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"##############################");			
		}
		System.out.println(txt2String(file));
	}

}

 

 

 

 

 运行效果

root@pai:/home/pi/dht11# javac DHT11.java
root@pai:/home/pi/dht11# java DHT11
./dht11_info.sh
run it:
weng du:run it:
###################################2017-11-19 10:34:43##############################

{"wd":"14","sd":"55"}
root@pai:/home/pi/dht11#

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics