经过大量尝试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#
相关推荐
3. **微控制器编程**:熟悉如Arduino、Raspberry Pi或ESP8266等微控制器平台的编程,以及如何编写代码来初始化、读取和解析DHT传感器的数据。 4. **错误处理**:由于DHT通信可能会出现错误,比如信号噪声、超时等...
2. **传感器接口设计**:单片机通过I/O口连接各种类型的传感器,如模拟传感器(如热电偶、热敏电阻)和数字传感器(如温湿度传感器DHT11/DHT22)。需要理解不同传感器的工作原理和接口协议,如UART、I2C、SPI等,...
1. **Arduino编程**:编写Arduino程序,与DHT11传感器通信,读取温度数据,并通过串口发送到主机。 2. **数据处理与报警**:利用`serialCommu.py`解析接收到的串口数据,设置温度阈值(如low: 28°C,high: 32°C)...
Linux-Torque是一个基于Bash shell脚本的 Transmission Daemon 图形用户界面(TUI)客户端,专为那些喜欢在命令行环境中工作但又想享受简单易用的交互式界面的Linux用户设计。Transmission是一个流行的BitTorrent...
DHT11是一种常见的湿度和温度传感器,广泛应用于环境监测。了解如何读取并处理DHT11的数据,对于构建环境监控系统至关重要。另一方面,HC-SR04是一种超声波测距传感器,用于测量物体的距离。掌握其工作原理和数据...