- #include "DHT11.h"
- #include <stdio.h>
- #include "string.h"
- extern unsigned char F16T,F16RH; //溫濕度數據
- sbit LED1=P2^0;
- sbit LED2=P2^3;
- sbit LED3=P2^6;
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char idata Rxbuff[40],Rxnum,SF16T,SF16RH;
- unsigned char Recwifi_data[5],led[2],n,x;
- char *strx=0;
- char clinetid;//連接ID
- unsigned char getflag;//獲取標志
- void Get_Clinet(void);
- void InitUART(void) //這是串口的基本配置,配置他的波特率是9600.這些參數都是標準的。
- {
- TMOD = 0x20;
- SCON = 0x50;
- TH1 = 0xFD;
- TL1 = TH1;
- PCON = 0x00;
- EA = 1;
- ES = 1;
- TR1 = 1;
- }
- /******************************************************************************/
- void delayms(unsigned int x)
- {
- unsigned int i;
- while(x--)
- for(i=1250;i>0;i--);
- }
- /*
- * UART 發送一字節
- */
- void UART_send_byte(char dat)
- {
- SBUF = dat; //發送
- while (TI == 0); //等待發送完畢
- TI = 0; //清發送完畢中斷請求標志位
- }
- /*
- * UART 發送字符串
- */
- void Send_Str(unsigned char *buf)
- {
- while (*buf != '\0')
- {
- UART_send_byte(*buf++);
- }
- }
- void ESP8266_SERVER(void)//建立服務器
- {
- Send_Str("AT\r\n");
- delayms(500);
- Send_Str("ATE0\r\n");
- delayms(500);
- Send_Str("AT+CWMODE=2\r\n"); //服務器搭建在WIFI模塊上
- delayms(500);
- Send_Str("AT+CWSAP=\"wifi\",\"12345678\",5,3\r\n"); //設置顯示名稱:wifi,密碼:12345678
- delayms(500);
- Send_Str("AT+CIPMUX=1\r\n");//啟動多連接,建立服務器都需要配置
- delayms(500);
- Send_Str("AT+CIPSERVER=1,5000\r\n");//建立服務器
- delayms(500);
- Send_Str("AT+CIPSTO=0\r\n");
- }
- void Send_DATA(uchar *buffer)
- {
- Send_Str("AT+CIPSEND=0,11\r\n");
- delayms(10);
- Send_Str(buffer);//發送數據
- delayms(1000);
- if((Rxbuff[0]=='1')&&(Rxbuff[1]=='1'))//關燈
- {
- LED1=1;
- x=1;
- }
- if((Rxbuff[0]=='1')&&(Rxbuff[1]=='0'))//開燈
- {
- LED1=0;
- x=0;
- }
- if(x==0)
- {
- if(F16T>=SF16T)
- LED2=0;
- else LED2=1;
- if(F16RH>=SF16RH)
- LED3=0;
- else LED3=1;
- }
- else
- {
- if((Rxbuff[0]=='2')&&(Rxbuff[1]=='1')) //關燈
- LED2=1;
- if((Rxbuff[0]=='2')&&(Rxbuff[1]=='0'))//開燈
- LED2=0 ;
- if((Rxbuff[0]=='3')&&(Rxbuff[1]=='1')) //關燈
- LED3=1;
- if((Rxbuff[0]=='3')&&(Rxbuff[1]=='0'))//開燈
- LED3=0 ;
- }
- if((Rxbuff[0]=='5')&&(Rxbuff[1]=='1')) //加溫度定值
- {
- SF16T++;
- if(SF16T>50)
- SF16T=50;
- Rxbuff[1]=9;
- }
- if((Rxbuff[0]=='5')&&(Rxbuff[1]=='0'))//減溫度定值
- {
- SF16T--;
- if(SF16T<1)
- SF16T=1;
- Rxbuff[1]=9;
- }
- if((Rxbuff[0]=='6')&&(Rxbuff[1]=='1')) //加濕度定值
- {
- SF16RH++;
- if(SF16RH>90)
- SF16RH=90;
- Rxbuff[1]=9;
- }
- if((Rxbuff[0]=='6')&&(Rxbuff[1]=='0'))//減濕度定值
- {
- SF16RH--;
- if(SF16RH<20)
- SF16RH=20;
- Rxbuff[1]=9;
- }
- }
- void main()
- {
- unsigned char Tx_Buf[12];
- unsigned char LEDstatus;//燈的狀態
- delayms(500);
- delayms(1000); //延時一段時間,讓WIFI模塊穩定
- InitUART(); //初始化串口
- ESP8266_SERVER(); //初始化ESP8266
- SF16T=25;
- SF16RH=50;
- x=1;
- while(1)
- {
- getDHT11(); //獲取溫濕度值
- Tx_Buf[0]=F16T/10%10+0x30; //將溫濕度數據送往發送數組,送給模塊讓手機APP顯示
- Tx_Buf[1]=F16T%10+0x30;
- Tx_Buf[2]=F16RH/10%10+0x30;
- Tx_Buf[3]=F16RH%10+0x30;
- Tx_Buf[4]=SF16T/10%10+0x30; //將溫濕度數據送往發送數組,送給模塊讓手機APP顯示
- Tx_Buf[5]=SF16T%10+0x30;
- Tx_Buf[6]=SF16RH/10%10+0x30;
- Tx_Buf[7]=SF16RH%10+0x30;
- LEDstatus=LED1;
- Tx_Buf[8]=LEDstatus+0x30;//發送燈的狀態
- LEDstatus=LED2;
- Tx_Buf[9]=LEDstatus+0x30;
- LEDstatus=LED3;
- Tx_Buf[10]=LEDstatus+0x30;
- Send_DATA(Tx_Buf) ;//發送數據
- }
- }
- /*****************串口接收中斷函數,接收數據*********************/
- void UARTInterrupt(void) interrupt 4
- {
- if(RI)
- {
- ES=0;
- RI = 0;
- Rxbuff[Rxnum]=SBUF;
- if(Rxbuff[Rxnum]=='=')
- {
- Rxnum=0;
- }
- else
- {
- Rxnum++;
- if(Rxnum>2)
- {
- Rxnum=2;
- }
- }
- ES=1;
- }
- }
-
復制代碼 這個是DHT11數據發送到手機的,89c52程序在app上可以顯示溫濕度,但是stc12c5a60s2程序在手機上可以控制但是無法顯示溫度
newapp.png (198.09 KB, 下載次數: 81)
下載附件
2018-10-15 21:04 上傳
|