物联网感知课程设计_第1页
物联网感知课程设计_第2页
物联网感知课程设计_第3页
物联网感知课程设计_第4页
物联网感知课程设计_第5页
免费预览已结束,剩余16页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、目录物联网感知课程设计1.1 .功能分析.1.2 .概要设计1.1 .系统组成1.2 .传感器选型1.3 .详细设计1.1 .通信模块程序设计框图1.2 .程序设计2.4 .系统说明8.5 .设计总结9.六附录10物联网感知课程设计环境监测系统程序设计课程设计报告题目:设计一个环境监测系统。一.功能分析环境监测系统主要包括对环境变量的采集和显示。(1)选择温湿度传感器来完成温度、湿度数据的采集。(2)选择协调器串口发送程序。(3)采用java语言制作相应的显示界面,将采集的变量值实时显示并给出相应提示。二.概要设计1 .系统组成系统大体组成包括:传感器模块、数据传送模块和电机控制模块。系统框图

2、如图1所示。由于传感器模块没有外接电路,因此需要通过中间的协调器将数据转为串口形式发送到PC机上。温、湿度传感器Zigbee传输串口传输U协调器<>PC显示电机图1系统框图2 .传感器选型选才iSHT10单片数字温湿度传感器来监测环境。SHT10采用CMOSen变利技术将温度湿度传感器、A/D转换器及数字接口无缝结合,使传感器具有体积小、响应速度快、接口简单、性价比高等特点。电路连接时,接口简单,两线连接,SHT10的两线串行接口(bidirectional2-wire)在传感器信号读取和电源功耗方面都做了优化处理,其总线类似I2C总线但并不兼容I2C总线。三.详细设计1.通信模块

3、程序设计框图6.启动串口后台工作线程2.程序设计packageorg.serial;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.Enumeration;importjava.util.TooManyListenersException;importjava.awt.FlowLayout;importjavax.sw

4、ing.JButton;importjavax.swing.JFrame;importjavax.swing.JTextField;importgnu.io.CommPortIdentifier;importgnu.io.PortInUseException;importgnu.io.SerialPort;importgnu.io.SerialPortEvent;importgnu.io.SerialPortEventListener;/*importgnu.io.UnsupportedCommOperationException;项目名称:illegalsms文件名称:SerialPort.

5、java所在包:org.serial功能描述:串口类创建者:* 创建日期:2016-12-23* 修改记录:* /SuppressWarnings("serial")publicclassDSerialPortextendsJFrameimplementsRunnable,SerialPortEventListenerprivateStringappName=初、调器数据接收"privateinttimeout=2000;/open端口时的等待时间privateintthreadTime=0;privateCommPortIdentifiercommPort;pr

6、ivateSerialPortserialPort;privateInputstreaminputstream;privateOutputstreamoutputstream;/* 方法名称:listPort* 功能描述:列出所有可用的串口* 返回值类型:void* /SuppressWarnings("rawtypes")publicvoidlistPort()CommPortIdentifiercpid;Enumerationen=CommPortIdentifier.getPortIdentifiers();System.out.println("nowto

7、listallPortofthisPC:"+en);while(en.hasMoreElements()cpid=(CommPortIdentifier)en.nextElement();if(cpid.getPortType()=CommPortIdentifier.PORT_SERIAL)System.out.println(cpid.getName()+","+cpid.getCurrentOwner();/*方法名称:selectPort*功能描述:选择一个端口,比如:COM1*返回值类型:void*paramportName*/SuppressWarni

8、ngs("rawtypes")publicvoidselectPort(StringportName)mPort=null;CommPortIdentifiercpid;Enumerationen=CommPortIdentifier.getPortIdentifiers();while(en.hasMoreElements()cpid=(CommPortIdentifier)en.nextElement();if(cpid.getPortType()=CommPortIdentifier.PORT_SERIAL&&cpid.getName().equals

9、(portName)mPort=cpid;break;/*openPort();方法名称:openPort功能描述:打开SerialPort返回值类型:void*/privatevoidopenPort()if(commPort=null)log(String.format("无法找到名字为"%1$s"的串口!"commPort.getName();else10g("端口选择成功,当前端口:"+commPort.getName()+",现在实例化SerialPort:");tryserialPort=(Serial

10、Port)commPort.open(appName,timeout);10g("实例SerialPort成功!)catch(PortInUseExceptione)thrownewRuntimeException(String.format("端口'1$s'正在使用中!”,commPort.getName();tryserialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,/设置用口读写参数一SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);/*catc

11、h(UnsupportedCommOperationExceptione)if(serialPort=null)thrownewRuntimeException("SerialPort对象无效!");)/* 方法名称:write* 功能描述:向端口发送数据,请在调用此方法前先选择端口,并确定SerialPort正常打开!* 返回值类型:void* parammessage* /publicvoidwrite(Stringmessage)checkPort();tryoutputStream=newBufferedOutputStream(serialPort.getOutp

12、utStream();catch(IOExceptione)thrownewRuntimeException("获取端口的OutputStream出错:"+e.getMessage();tryoutputStream.write(message.getBytes();10g("信息发送成功!”);catch(IOExceptione)thrownewRuntimeException("向端口发送信息时出错:"+e.getMessage();finallytryoutputStream.close();catch(Exceptione)/* 方法

13、名称:startRead* 功能描述:开始监听从端口中接收的数据* 返回值类型:void* paramtime监听程序的存活时间,单位为秒,0则是一直监听*/publicvoidstartRead(inttime)checkPort();ints;s=serialPort.getBaudRate();System.out.println("波特率为"+s);tryinputStream=newBufferedInputStream(serialPort.getInputStream();catch(IOExceptione)thrownewRuntimeException(

14、"获取端口的InputStream出错:"+e.getMessage();tryserialPort.addEventListener(this);catch(TooManyListenersExceptione)thrownewRuntimeException(e.getMessage();serialPort.notifyOnDataAvailable(true);10g(String.format("开始监听来自1$s'的数据",commPort.getName();if(time>0)this.threadTime=time*100

15、0;Threadt=newThread(this);t.start();10g(String.format("监听程序将在1$d秒后关闭。",threadTime);/* 方法名称:close* 功能描述:关闭SerialPort* 返回值类型:void* /publicvoidclose()serialPort.close();serialPort=null;commPort=null;publicvoidlog(Stringmsg)System.out.println(appName+"->"+msg);)/* 数据接收的监听处理函数* /Ov

16、erridepublicvoidserialEvent(SerialPortEventarg0)switch(arg0.getEventType()caseSerialPortEvent.BI:/*Breakinterrupt通讯中断*/caseSerialPortEvent.OE:/*Overrunerror溢位错误*/caseSerialPortEvent.FE:/*Framingerrqr传帧错误*/caseSerialPortEvent.PE:/*Parityerror校验错误*/caseSerialPortEvent.CD:/*Carrierdetect载波检测*/caseSeria

17、lPortEvent.CTS:/*Cleartosend消除发送*/caseSerialPortEvent.DSR:/*Datasetready数据设备就绪*/caseSerialPortEvent.RI:/*Ringindicater响铃指示*/caseSerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Outputbufferisempty输出缓冲区清空*/break;caseSerialPortEvent.DATA_AVAILABLE:/*Dataavailableattheserialport端口有可用数据。读到缓冲数组,输/到终端*/bytereadBuffe

18、r=newbyte16;trywhile(inputStream.available()>0)SuppressWarnings("unused")intnumBytes=inputStream.read(readBuffer);)outputStream.write("xiaogangH.getBytes();Stringhex;StringBufferresult=newStringBuffer();for(inti=0;i<readBuffer.length;i+)hex=Integer.toHexString(readBufferi&0x

19、FF);if(hex.length()=1)hex="0'+hex;)result.append(hex.toUpperCase();)Strings=newString(result);intx=newint4;inty=newint4;for(inti=0;i<4;i+)Strings1=s.substring(10+i,10+i+1);if(s1.equals("A")xi=10;elseif(s1.equals("B")xi=11;elseif(s1.equals("C")xi=12;elseif(s1

20、.equals("D")xi=13;elseif(s1.equals("E")xi=14;elseif(s1.equals("F")xi=15;elsexi=Integer.parseInt(s1);Strings2=s.substring(14+i,14+i+1);if(s2.equals("A")yi=10;elseif(s2.equals("B")yi=11;elseif(s2.equals("C")yi=12;elseif(s2.equals("D"

21、;)yi=13;elseif(s2.equals("E")yi=14;elseif(s2.equals("F")yi=15;elseyi=Integer.parseInt(s2);Overridepublicvoidrun()tryThread.sleep(threadTime);serialPort.close();10g(String.format("端口”监听关闭了!",commPort.getName();catch(Exceptione)e.printStackTrace();四.系统说明本系统搭建了一个温湿度的采集显示环境

22、,能够采集周围环境中的温度、湿度值,并且实时在PC机上显示。PC机实时显示界面如图:FileEditSourceRefactorNavigateSearchProjectRumWindowHelpr3HIx,一曰一看:雷尊,尹中-3"皿。之一落日/-蚌ISiProblems日Consclt显|画ICX|当£DSerialPort(2)Jav-aApplicationCsProgramFile£jrebinjavavy.exe场01612月23<曰SnaJoleLibraryNazxveIxbVersionRXTX-2.JavaixioversionRKTX-

23、2.WARNINS:KXTXVeraionJarversionRXTX-2.1nativelibVersion加调器数据接收一协调器勘据接收一物调器数据接收一下端口选择成功.波特率为工L5N8协调器数据接收一下协调器数据接收-安例营噂工iNPnr信息发送成功!开始监听耒自,监听程序将在124rfiOfi25.237.77>24.12°37-24-33型24.省目马37一71%24.11*3V.71%24.9°25.21C25.23°37.74%36,30%36.30%系统采用SHT10单片数字温湿度传感器将温湿度采集集成在一起,数据的读取要采用时序控制来进行

24、。采集变量的实时显示由JAVA编制界面来完成。并且在图形化界面温湿度上下界及理想温湿度处输入数值,温湿度采集过程中能对数据进行判断并给出3glLnhj/rir/ol/OSeriIPortva-Edip-File-EjditGouiruvfi.e-fartorNavigate-SearchProjectR.unWnnidowHe-lipLS_ProblerTrt-0Console券|DSerialPorl!:JavaAppIicationC:ProgramFile-sXjrierXfciinXjavaw.e-xt£511122!3H-F4fcp4二;34二i0】11*2S-IO0J6-

25、1*26,24.65*25.73°25-3*J5-63024-S2025-53*34T后n25_90ejs.es°9TqS5.67*25-5*2S_S-7e1Z-BJ03552°2:5372s.ea0N石口-mm-2鼻口26.e2:515"2后二20N9.二70N方_工石口2-1-930工七W19协调器被据接检-端II监叫关闭了!三与-三口,as.f、36.40*35.50%3石-33+BS.日8事3fi.3335<9=上3.47%*±3%三旧-36:,S11e3石.ea%3.36%.33与36.95,3-7-1VST-3%3729%st

26、*3T.二石mm、3F,9击37.50%37-mm,三百-on,ST,57%36.H曰牝五.设计总结这次选题选择了与日常生活息息相关的基于物联网的温湿度环境检测系统,一开始连这个系统该分为哪三个模块以及各自该做些什么都不清楚,但后来逐渐清楚了可以分为上位机、下位机和通信三个模块,而我此次负责通信模块,即将温湿度传感器的数据通过发送模块传到上位机实现通信。这部分的代码老师给出了其中一部分(其中一部分库文件比较老,因此需要安装64位机的GNLW文件),我补写了剩下的一些,其实这里面最难的就是十六进制转换为十进制的那段代码了,所幸并不是无迹可寻,中途也遇到过很多问题,但通过查询相关书籍、请教同学以及

27、上网搜索都逐一解决了,因此最终还是编写成功了。本来还尝试扩展功能譬如当温度过高启用电机降温,但由于时间较短,又因为本人能力有限,这部分代码就没有编写。这次课设虽然时间短但是学到的东西一点儿也不少,对于java编程和与硬件的连接通信也有了一定的感悟,小组成员也分工有序,配合默契,相信下次我们可以实现扩展功能,使这个系统更加完善。六.附录源程序清单:packageorg.serial;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.IOException;importjava.io

28、.InputStream;importjava.io.OutputStream;importjava.util.Enumeration;importjava.util.TooManyListenersException;importjava.awt.FlowLayout;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JTextField;importgnu.io.CommPortIdentifier;importgnu.io.PortInUseException;importgnu.io.SerialP

29、ort;importgnu.io.SerialPortEvent;importgnu.io.SerialPortEventListener;importgnu.io.UnsupportedCommOperationException;* 项目名称:illegalsms* 文件名称:SerialPort.java* 所在包:org.serial* 功能描述:* 用口类* 创建者:* 创建日期:2016-12-23* 修改记录:* /SuppressWarnings("serial")publicclassDSerialPortextendsJFrameimplementsRu

30、nnable,SerialPortEventListenerprivateStringappName=初、调器数据接收"privateinttimeout=2000;/open端口时的等待时间privateintthreadTime=0;privateCommPortIdentifiercommPort;privateSerialPortserialPort;privateInputStreaminputStream;privateOutputStreamoutputStream;publicstaticintwendu1;publicstaticintwendu2;publicst

31、aticintshidu1;publicstaticintshidu2;publicstaticbooleanopen;publicstaticdoublew1;publicstaticdoublew2;publicstaticdoubles1;publicstaticdoubles2;publicstaticdoublewx;publicstaticdoublesx;JFramef=newJFrame环境温度检测");JButtonbutton1=newJButton('温度:");JButtonbutton2=newJButton("S度:"

32、);JButtonbutton3=newJButton('温度下界:")JButtonbutton4=newJButton('温度上界:")JButtonbutton5=newJButton('理想温度:")JButtonbutton6=newJButton('湿度上界:")JButtonbutton7=newJButton('湿度下界:")JButtonbutton8=newJButton('理想湿度:")JTextFieldtf1;JTextFieldtf2;JTextFieldtf

33、3;JTextFieldtf4;JTextFieldtf5;JTextFieldtf6;JTextFieldtf7;JTextFieldtf8;JTextFieldtf9;JTextFieldtf10;/*方法名称:listPort功能描述:列出所有可用的串口返回值类型:void*/SuppressWarnings("rawtypes")publicvoidlistPort()CommPortIdentifiercpid;Enumerationen=CommPortIdentifier.getPortIdentifiers();System.out.println(&quo

34、t;nowtolistallPortofthisPC:"+en);while(en.hasMoreElements()cpid=(CommPortIdentifier)en.nextElement();if(cpid.getPortType()=CommPortIdentifier.PORT_SERIAL)System.out.println(cpid.getName()+","cpid.getCurrentOwner();/*/*openPort();方法名称:openPort功能描述:打开SerialPort返回值类型:void*/privatevoidope

35、nPort()if(commPort=null)log(String.format("无法找到名字为"%1$s"的串口!"commPort.getName();else10g("端口选择成功,当前端口:"+commPort.getName()+",现在实例化SerialPort:");tryserialPort=(SerialPort)commPort.open(appName,timeout);10g("实例SerialPort成功!)catch(PortInUseExceptione)thrownew

36、RuntimeException(String.format("端口'1$s'正在使用中!”,commPort.getName();tryserialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,/设置用口读写参数一SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);/*catch(UnsupportedCommOperationExceptione)if(serialPort=null)thrownewRuntimeException("SerialPor

37、t对象无效!");)/* 方法名称:write* 功能描述:向端口发送数据,请在调用此方法前先选择端口,并确定SerialPort正常打开!* 返回值类型:void* parammessage* /publicvoidwrite(Stringmessage)checkPort();tryoutputStream=newBufferedOutputStream(serialPort.getOutputStream();catch(IOExceptione)thrownewRuntimeException("获取端口的OutputStream出错:"+e.getMes

38、sage();tryoutputStream.write(message.getBytes();10g("信息发送成功!”);catch(IOExceptione)thrownewRuntimeException("向端口发送信息时出错:"+e.getMessage();finallytryoutputStream.close();catch(Exceptione)/* 方法名称:startRead* 功能描述:开始监听从端口中接收的数据* 返回值类型:void* paramtime监听程序的存活时间,单位为秒,0则是一直监听*/publicvoidstartRe

39、ad(inttime)checkPort();ints;s=serialPort.getBaudRate();System.out.println("波特率为"+s);tryinputStream=newBufferedInputStream(serialPort.getInputStream();catch(IOExceptione)thrownewRuntimeException("获取端口的InputStream出错:"+e.getMessage();tryserialPort.addEventListener(this);catch(TooMan

40、yListenersExceptione)thrownewRuntimeException(e.getMessage();serialPort.notifyOnDataAvailable(true);10g(String.format("开始监听来自1$s'的数据",commPort.getName();if(time>0)this.threadTime=time*1000;Threadt=newThread(this);t.start();10g(String.format("监听程序将在1$d秒后关闭。",threadTime);/*

41、方法名称:close* 功能描述:关闭SerialPort* 返回值类型:void* /publicvoidclose()serialPort.close();serialPort=null;commPort=null;publicvoidlog(Stringmsg)System.out.println(appName+"->"+msg);)/* 数据接收的监听处理函数* /OverridepublicvoidserialEvent(SerialPortEventarg0)switch(arg0.getEventType()caseSerialPortEvent.BI

42、:/*Breakinterrupt通讯中断*/caseSerialPortEvent.OE:/*Overrunerror溢位错误*/caseSerialPortEvent.FE:/*Framingerrqr传帧错误*/caseSerialPortEvent.PE:/*Parityerror校验错误*/caseSerialPortEvent.CD:/*Carrierdetect载波检测*/caseSerialPortEvent.CTS:/*Cleartosend消除发送*/caseSerialPortEvent.DSR:/*Datasetready数据设备就绪*/caseSerialPortEv

43、ent.RI:/*Ringindicater响铃指示*/caseSerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Outputbufferisempty输出缓冲区清空*/break;caseSerialPortEvent.DATA_AVAILABLE:/*Dataavailableattheserialport端口有可用数据。读到缓冲数组,输/到终端*/bytereadBuffer=newbyte16;trywhile(inputStream.available()>0)SuppressWarnings("unused")intnumByte

44、s=inputStream.read(readBuffer);)outputStream.write("xiaogangH.getBytes();Stringhex;StringBufferresult=newStringBuffer();for(inti=0;i<readBuffer.length;i+)hex=Integer.toHexString(readBufferi&0xFF);if(hex.length()=1)hex="0'+hex;)result.append(hex.toUpperCase();)Strings=newString(r

45、esult);intx=newint4;inty=newint4;for(inti=0;i<4;i+)Strings1=s.substring(10+i,10+i+1);if(s1.equals("A")xi=10;elseif(s1.equals("B")xi=11;elseif(s1.equals("C")xi=12;elseif(s1.equals("D")xi=13;elseif(s1.equals("E")xi=14;elseif(s1.equals("F")

46、xi=15;elsexi=Integer.parseInt(s1);Strings2=s.substring(14+i,14+i+1);if(s2.equals("A")yi=10;elseif(s2.equals("B")yi=11;elseif(s2.equals("C")yi=12;elseif(s2.equals("D")yi=13;elseif(s2.equals("E")yi=14;elseif(s2.equals("F")yi=15;elseyi=Integer

47、.parseInt(s2);wendu1=(4096*x0+256*x1+16*x2+x3)/100;wendu2=(4096*x0+256*x1+16*x2+x3)%100;shidu1=(4096*y0+256*y1+16*y2+y3)/100;shidu2=(4096*y0+256*y1+16*y2+y3)%100;if(wendu1>0&&wendu1<100)System.out.println(wendu1+”."+wendu2+"0"+"+shidu1+"."+shidu2+"%&q

48、uot;);tf1.setText(String.valueOf(wendu1+"."+wendu2+"");tf2.setText(String.valueOf(shidu1+"."+shidu2+'%");if(open=true)w1=Double.parseDouble(tf3.getText();w2=Double.parseDouble(tf4.getText();wx=Double.parseDouble(tf5.getText();s1=Double.parseDouble(tf6.getText()

49、;s2=Double.parseDouble(tf7.getText();sx=Double.parseDouble(tf8.getText();if(wendu1>0&&wendu1<100&&shidu1>0&&shidu1<100)tf1.setText(String.valueOf(wendu1+"."+wendu2+"°");tf2.setText(String.valueOf(shidu1+"."+shidu2+'%");i

50、f(wendu1>=w1&&wendu1<wx)tf9.setText(String.valueOfC当前温度低于舒适温度");if(wendu1>wx&&wendu1<=w2)tf9.setText(String.valueOfC当前温度高于舒适温度");if(wendu1>=wx&&wendu1<=wx)tf9.setText(String.valueOfC当前温度适宜");if(wendu1<w1|wendu1>w2)tf9.setText(String.valueOf(温度超出范围!!");if(shidu1>=s1&&shidu1<sx)tf10.setText(String.valueOfC当前湿度小,干燥");if(shidu1>sx&&shidu1<=s2)tf10.setText(String.valueOf('当前湿度大,潮湿");if(shidu1>=sx&&shidu1<=sx)tf10.setText(String.valueOf('当前湿度适宜");if(shidu1<

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论