1、 网络工程〔物联网技术〕 课程设计报告 题 目: 基于温湿度传感器物联网应用实时数据处理系统开发 院〔系〕 别: 数学与信息工程学院 专业: 网络工程〔物联网技术〕 班级 1 班 学 号: 2006099914 姓 名: 小 明
2、 指导教师: 职称 博士 填表日期: 2021 年 5 月 11 日 前 言 一、选题的依据及意义 1. 依据 物联网是一种新概念和新技术,它使新一代IT技术更加充分地应用于各行各业之中。它的问世打破了过去将根底设施与IT设施分开的传统观念,将建筑物、公路、铁路和网站、网络、数据中心合为一体,是信息化和工业化融合的重要切入点。温湿度与人们的生活关系密切,所以物联网在温湿度实时数据处理系统的开发将有很大的前景。 2. 意义 在我
3、们的日常生活中无处不在,控制好温湿度可以使我们生活、生产的更好。温湿度传感器物联网应用实时数据处理系统开发可以帮我们实现对温湿度以实时数据让我们明了的知道。从而更好的控制温湿度、到达我们所需的标准。 二、本课程设计内容简介 1. 通过ubuntu连接传感器实验箱收集由传感器测得的实时数据存入sqlite3数据库。 2. 然后通过ubuntu发送到linux、接收并用动态网页显示代表数据变化的曲线。 三、要到达的目标 1.可以在ubuntu上实现自动接收由传感器取得、传来的实时数据。 2. 并ubuntu上能边接收边连续往linux发送从传感器取得的实时数据。 3.还要确保发送
4、过的数据不会再次发送。 4. Linux能接收到ubuntu发过来的实时数据并通过动态网页曲线图实时显示接收过来的数据。 实 现 方 案 一、开发环境 1.硬件〔详细介绍所涉及硬件的详细内容〕 Pc机、温湿度传感器、传感器实验箱、连接所需的各种线。 2.软件〔详细介绍所涉及软件的详细内容〕 MDK414〔arm平台编译烧录代码软件〕、KeilC51v750a_Full〔C51平台编译软件〕、STC手动下载〔C51烧录代码软件〕、R340〔串口线连接USB驱动〕、ubuntu操作系统、linux操作系统。 二、开发内容 1.工程开发详细内容〔包括传感器的配
5、置、传感器烧录、数据的实时收集、实时数据的存储、实时数据的传输、实时数据在效劳器端的接受及存储-TCPServer及MySql、数据库及Web效劳器安装、利用JSP曲线动态显示实时数据〕 首先烧录整合好的温湿度传感器的代码。接着连接传感器取得数据。然后在ubuntu中编译并运行Com_Sensor程序获取传感器实验箱的数据。 在Ubuntu11编译并运行senddata.c把数据发送到linux.Linux通过TCPServer效劳器接收数据并存入MySQL数据库。 最后将接收到的数据通过Linuxweb效劳器以jsp曲线动态显示实时数据。 2.网络拓扑图〔包括传感器、网关、传输网
6、络、TCPServer效劳器、数据库效劳器、静态及动态Web效劳器、Web效劳器客户端;并详细标注设备名称及IP地址等详细信息;并详细表达网络拓扑图流程〕 三、技术路线 1. 传感器数据处理〔给出详细的传感器烧录代码 〕 代码如下: /*********************************************************/ //中软吉大信息技术 //物联网传感技术教学实验系统 /***********************************************
7、/
#include
8、 // modul-var //---------------------------------------------------------------------------------- enum {TEMP,HUMI}; #define noACK 0 #define ACK 1 //adr command r/w #define STATUS_REG_W 0x06 //000 0011
9、 0 #define STATUS_REG_R 0x07 //000 0011 1 #define MEASURE_TEMP 0x03 //000 0001 1 #define MEASURE_HUMI 0x05 //000 0010 1 #define RESET 0x1e //000 1111 0 sbit DATA=P2^2; sbit SCK=P2^1; sbit POWER=P2^5; sbit flag1=P0^7; sbit flag2=P4^6; sbit fl
10、ag3=P2^7; sbit flag4=P2^6; void delay(unsigned int nTimeDelay) { unsigned int i; while (nTimeDelay--) for (i=0;i<125;i++); } void Serial_Init() { TMOD = 0x01; TR0 = 1; EA = 1; ET0 = 0; TF0 = 0; S2CON = 0x50;
11、 //8位可变波特率 (无校验位) BRT = -(FOSC/32/BAUD); //设置独立波特率发生器的重载初值 AUXR = 0x14; //独立波特率发生器工作在1T模式 //IE2 = 0x01; //使能串口2中断 } void IO_Init(void) { P2M1=P2M1&0xdf; P2M0=P2M0&0xdf; P0M1=P0M1&0x7f; P0M0=P0M0&0x7f; P4M1=P4M1&0xbf; P4M0=P
12、4M0&0xbf; P2M1=P2M1&0x3f; P2M0=P2M0&0x3f; P4SW=P4SW|0x40; } void Power_Identify(void) { while(1) { if(POWER==0) { delay(4000); LcmPrintf("请给传感器模块上电!\n"); } else break; } } void Module_Identify(unsigned int xuhao) { unsigned int abc=0; if(flag4==1)abc=a
13、bc+1; abc=abc<<1; if(flag3==1)abc=abc+1; abc=abc<<1; if(flag2==1)abc=abc+1; abc=abc<<1; if(flag1==1)abc=abc+1; if(abc!=xuhao) { delay(3000); LcmPrintf("提示:您插入的模块不正确!\n"); } while(1) { if(abc!=xuhao); else { delay(2000); LcmPrintf("连接
14、的模块是M%u\n",xuhao); delay(5000); break; } } } //---------------------------------------------------------------------------------- char s_write_byte(unsigned char value) //---------------------------------------------------------------------------------- // writes a byte on
15、the Sensibus and checks the acknowledge { unsigned char i,error=0; for (i=0x80;i>0;i/=2) //shift bit for masking { if (i & value) DATA=1; //masking value with i , write to SENSI-BUS else DATA=0; _nop_();
16、 //observe setup time SCK=1; //clk for SENSI-BUS _nop_();_nop_();_nop_(); //pulswith approx. 5 us SCK=0; _nop_(); //observe hold time } DATA=1; //release DATA-li
17、ne _nop_(); //observe setup time SCK=1; //clk #9 for ack error=DATA; //check ack (DATA will be pulled down by SHT11) SCK=0; return error; //error=1 in case of no acknowledge }
18、 //---------------------------------------------------------------------------------- char s_read_byte(unsigned char ack) //---------------------------------------------------------------------------------- // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" {
19、 unsigned char i,val=0; DATA=1; //release DATA-line for (i=0x80;i>0;i/=2) //shift bit for masking { SCK=1; //clk for SENSI-BUS if (DATA) val=(val | i); //read bit SCK=0;
20、 } DATA=!ack; //in case of "ack==1" pull down DATA-Line _nop_(); //observe setup time SCK=1; //clk #9 for ack _nop_();_nop_();_nop_(); //pulswith approx. 5 us SCK=0; _nop_();
21、 //observe hold time DATA=1; //release DATA-line return val; } //---------------------------------------------------------------------------------- void s_transstart(void) //----------------------------------
22、 // generates a transmission start // _____ ________ // DATA: |_______| // ___ ___ // SCK : ___| |___| |______ { DATA=1; SCK=0; //Initial state _nop_(); SCK=1; _n
23、op_(); DATA=0; _nop_(); SCK=0; _nop_();_nop_();_nop_(); SCK=1; _nop_(); DATA=1; _nop_(); SCK=0; } //---------------------------------------------------------------------------------- void s_connectionreset(void)
24、 //---------------------------------------------------------------------------------- // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart // _____________________________________________________ ________ // DATA:
25、 |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ { unsigned char i; DATA=1; SCK=0; //Initial state for(i=0;i<9;i++)
26、 //9 SCK cycles { SCK=1; SCK=0; } s_transstart(); //transmission start } //---------------------------------------------------------------------------------- char s_softreset(void) //-----------------------------------------------------------------
27、 // resets the sensor by a softreset { unsigned char error=0; s_connectionreset(); //reset communication error+=s_write_byte(RESET); //send RESET-command to sensor return error; //error=1 in case of no response form the
28、sensor } //---------------------------------------------------------------------------------- char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum) //---------------------------------------------------------------------------------- // reads the status register with ch
29、ecksum (8-bit) { unsigned char error=0; s_transstart(); //transmission start error=s_write_byte(STATUS_REG_R); //send command to sensor *p_value=s_read_byte(ACK); //read status register (8-bit) *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
30、 return error; //error=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_write_statusreg(unsigned char *p_value) //----------------------------------------------------------------
31、 // writes the status register with checksum (8-bit) { unsigned char error=0; s_transstart(); //transmission start error+=s_write_byte(STATUS_REG_W);//send command to sensor error+=s_write_byte(*p_value); //send value of status register
32、 return error; //error>=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_measure(unsigned char *p_value, unsigned char *p_checksum, un
33、signed char mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned char error=0; unsigned int i; s_transstart(); //transmission start switch(mode){
34、 //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measuremen
35、t if(DATA) error+=1; // or timeout (~2 sec.) is reached *(p_value) =s_read_byte(ACK); //read the first byte (MSB) *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB) *p_checksum =s_read_byte(noACK); //read checksum return error; } //--------
36、 void calc_sth11(float *p_humidity ,float *p_temperature) //---------------------------------------------------------------------------------- // calculates temperature [°C] and humidity [%RH] // input : humi [Ticks]
37、 (12 bit) // temp [Ticks] (14 bit) // output: humi [%RH] // temp [°C] { const float C1=-2.0468; // for 12 Bit RH const float C2=+0.0367; // for 12 Bit RH const float C3=-0.0000015955; // for 12 Bit RH const float T1=+0.01; /
38、/ for 12 Bit RH const float T2=+0.00008; // for 12 Bit RH float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit float t=*p_temperature; // t: Temperature [Ticks] 14 Bit float rh_lin; // rh_lin: Humidity linear flo
39、at rh_true; // rh_true: Temperature compensated humidity float t_C; // t_C : Temperature [°C] t_C=t*0.01 - 40.1; //calc. temperature[°C]from 14 bit temp.ticks @5V rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to
40、 [%RH] rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH] if(rh_true>100)rh_true=100; //cut if the value is outside of if(rh_true<0.1)rh_true=0.1; //the physical possible range *p_temperature=t_C; //return temperature [°C
41、] *p_humidity=rh_true; //return humidity[%RH] } //-------------------------------------------------------------------- float calc_dewpoint(float h,float t) //-------------------------------------------------------------------- // calculates dew point // input: humid
42、ity [%RH], temperature [°C] // output: dew point [°C] { float k,dew_point ; k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t); dew_point = 243.12*k/(17.62-k); return dew_point; } //---------------------------------------------------------------------------------- void main
43、) //---------------------------------------------------------------------------------- // sample program that shows how to use SHT11 functions // 1. connection reset // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit) // 3. calculate humidity [%RH] and temperature [°C]
44、 // 4. calculate dew point [°C] // 5. print temperature, humidity, dew point { value humi_val,temp_val; float dew_point; unsigned char error,checksum; unsigned int i; Serial_Init(); IO_Init(); Power_Identify(); Module_Identify(3); s_connectionreset(); w
45、hile(1) { error=0; error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature if(error!=0) s_connectionreset(); //in case of an error: connection reset els
46、e { humi_val.f=(float)humi_val.i; //converts integer to float temp_val.f=(float)temp_val.i; //converts integer to float calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature dew_point=calc_dewpoint(humi_val
47、f,temp_val.f); //calculate dew point Power_Identify(); LcmDisplaySHT10(humi_val.f,temp_val.f); //LcmPrint℃ 湿度:%5.1f 露点:%3.1f\n", temp_val.f,humi_val.f,dew_point); //temp :温度 // for (i=0;i<40000;i++); //延迟 // LcmPrintf("", humi_val.f); //humi:湿度 // for (i=0;i<40000;i+
48、); //延迟 // LcmPrintf("", dew_point); //dewpoint:露点〔点在此温度时,空气饱和并产生露珠〕 } //----------wait approx. 0.8s to avoid heating up SHTxx----------------------------- for (i=0;i<50000;i++); //(be sure that the compiler doesn't eliminate this line!) //--------------------------------
49、 } } Lcmdisplay.c: #include "Lcmdisplay.h" typedef enum { LCMPRTF = 0, // 字符串打印 // 1 磁感应及环境光传感器模块 LCMHALL, // 霍尔接近开关 LCMREED, // 干簧管 LCMMETAL, // 金属接
50、近开关 LCMLUX, // 环境光强度 LCMLDR, // 光敏电阻 // 2 震动及mems麦克传感器模块 LCMSHOCKDS, // 双珠单向 LCMSHOCKDD, // 双珠双向 LCMSHOCKSPRING, // 弹簧 LCMSHOCK, // 全向震动 LCMMIC, // MEMS麦克风 // 3 测距测障类及温湿度类传感器模块 LCM






