下位机原理图
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "adc.h"
- #include "timer.h"
- #include "usart1.h"
- #include "LCD1602.h"
- #include "string.h"
- #include "stdio.h"
- #include "dht11.h"
- void Change_data_handle(void);
- void SHOW_AND_BAOJING_Handle(void);
- u8 KEY_SCAN(void); //按键函数
- u8 key,PPM,Temperature,HUM;
- u16 PPM_MAX=60,Temperature_MAX=40,HUM_MAX=70;//烟雾温湿度上限初始值
- unsigned char Alarm_Buf[16]="Warning";
- char buf[30];
- extern unsigned char RxCounter,Usart1RecBuf[64];
- extern char bUsart1RecFlag;
- //ESP8266相关函数
- #define ESP_RECV_PTYPE_NULL 0
- #define ESP_RECV_PTYPE_DATA 1
- unsigned char cur_conn_id;
- char *strx=0;
- void Clear_Buffer(void)//清空缓存
- {
- u8 i;
- for(i=0;i<64;i++)
- Usart1RecBuf[i]=0;//缓存
- RxCounter=0;
- }
- void esp_ap_mode(void)
- {
- Clear_Buffer();
- Uart1_SendStr("AT+CWMODE=2\r\n"); ////<mode>:1-Station模式,2-AP模式,3-AP兼Station模式
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- while(strx == NULL)
- {
- Clear_Buffer();
- Uart1_SendStr("AT+CWMODE=2\r\n");
- delay_ms(500);
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- }
-
- /*
- <ssid>:字符串参数,接入点名称
- <pwd>:字符串参数,密码最长64字节,ASCII
- <chl>:通道号
- < ecn >:0-OPEN,1-WEP,2-WPA_PSK,3-WPA2_PSK,4-WPA_WPA2_PSK
- */
- Clear_Buffer();
- Uart1_SendStr("AT+CWSAP=\"TEST\",\"12345678\",1,3\r\n");
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- while(strx == NULL)
- {
- Clear_Buffer();
- Uart1_SendStr("AT+CWSAP=\"TEST\",\"12345678\",1,3\r\n");
- delay_ms(500);
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- }
-
- /*
- AT+CIPMUX=1
- OK
- */
- Clear_Buffer();
- Uart1_SendStr("AT+CIPMUX=1\r\n");
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- while(strx == NULL)
- {
- Clear_Buffer();
- Uart1_SendStr("AT+CIPMUX=1\r\n");
- delay_ms(500);
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- }
- /*
- AT+CIPMUX=1时才能开启服务器;关闭server模式需要重启?
- AT+CIPSERVER=1,8080
- OK
- */
- Clear_Buffer();
- Uart1_SendStr("AT+CIPSERVER=1,8080\r\n");
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- while(strx == NULL)
- {
- Clear_Buffer();
- Uart1_SendStr("AT+CIPSERVER=1,8080\r\n");
- delay_ms(500);
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- }
- }
- void esp8266_init()
- {
- Usart1_Init(115200);
- Uart1_SendStr("ESP8266 Init\r\n");
- Uart1_SendStr("AT\r\n"); //模块测试
- strx = strstr((const char*)Usart1RecBuf,(const char*)"OK");
- while(strx == NULL)
- {
- Clear_Buffer();
- Uart1_SendStr("AT\r\n");
- delay_ms(500);
- strx=strstr((const char*)Usart1RecBuf,(const char*)"OK");
- }
-
- esp_ap_mode();
- }
- void esp_send_buf(unsigned char conn_id, char * buf, unsigned char len)
- {
-
- printf("AT+CIPSEND=%d,%d\r\n",cur_conn_id,len);
- strx = strstr((const char*)Usart1RecBuf,(const char*)">");
- delay_ms(200);
- if(strx==NULL)
- {
- return;
- }
-
- Uart1_SendStr(buf);
- strx = strstr((const char*)Usart1RecBuf,(const char*)"SEND OK");
- delay_ms(100);
- if(strx==NULL)
- {
- return;
- }
-
- }
- int main(void)
- {
- u16 test,test_adc;
-
- delay_init(); //延时函数初始化
- NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- KEY_IO_Init();
- TIM2_Int_Init(1000-1,72-1); //定时器初始化,定时1MS
- //Tout = ((arr+1)*(psc+1))/Tclk ; 72000/72=1000us
- //Tclk:定时器输入频率(单位MHZ)
- //Tout:定时器溢出时间(单位us)
- delay_ms(300);
- DHT11_Init(); //DHT11初始化
- RELAY_Init();//继电器初始化
- BEEP_DISENABLE(); //蜂鸣器初始化
- LCD_Init(); // 1602初始化
- Adc_Init(); //ADC初始化
- esp8266_init();//ESP8266初始化
- while(1)
- {
- test_adc = Get_Adc_Average(ADC_Channel_9,20);//读取通道9的AD值,读取20次的平均值
- PPM = test_adc*99/4096;//12位AD,把AD值转换成百分比0~99
- PPM = PPM >= 99? 99: PPM;//最大值不能超过99
- DHT11_Read_Data(&Temperature,&HUM);//读取温湿度值
- SHOW_AND_BAOJING_Handle();
- Change_data_handle();//按键调节
- if(test++>20)//ESP8266延时一段时间,发送一次数据
- {
- esp_send_buf(0,buf,27);
- test = 0;
- }
- delay_ms(1);
- }
- }
- extern u8 FLICKER;
- void SHOW_AND_BAOJING_Handle(void)
- {
- unsigned char show_buf[16];//显示缓存区
-
- if(Temperature >= Temperature_MAX)//温度超限
- {
- memcpy(buf,Alarm_Buf,8);//把Alarm_Buf里的字符串拷贝到buf中
- buf[7] = '!';
- if(FLICKER)//超标显示闪烁
- {
- LCD_Write_String(0,0," ");
- }
- else
- {
- sprintf(show_buf,"TEMP:%02dC",(u16)Temperature);//转换显示格式为TEMP:00C
- LCD_Write_String(0,0,show_buf);//显示
- }
- }
- else
- {
- sprintf(show_buf,"TEMP:%02dC",(u16)Temperature);//转换显示格式为TEMP:00C
- LCD_Write_String(0,0,show_buf);//显示
- memcpy(buf,show_buf,8);//将温度显示字符拷贝到buf中
- }
- buf[8] = ',';
- if(HUM >= HUM_MAX)//湿度超限
- {
- memcpy(buf+9,Alarm_Buf,8);//把Alarm_Buf里的字符串拷贝到buf中
- if(FLICKER)//超标显示闪烁
- {
- LCD_Write_String(9,0," ");
- }
- else
- {
- sprintf(show_buf,"HUM:%02d%%",(u16)HUM);//转换显示格式为HUM:00%
- LCD_Write_String(9,0,show_buf);//显示
- }
- }
- else
- {
- sprintf(show_buf,"HUM:%02d%%",(u16)HUM);//转换显示格式为HUM:00%
- LCD_Write_String(9,0,show_buf);
- memcpy(buf+9,show_buf,7);//将湿度显示字符拷贝到buf中
- }
- buf[16] = ',';
- if(PPM >= PPM_MAX)//烟雾超限
- {
- memcpy(buf+17,Alarm_Buf,7);
- buf[24] = '!';
- buf[25] = '!';
- if(FLICKER)//超标显示闪烁
- {
- LCD_Write_String(0,1," ");
- }
- else
- {
- sprintf(show_buf,"SMOKE:%02d%%",PPM);//转换显示格式为SMOKE:00%
- LCD_Write_String(0,1,show_buf);
- }
- }
- else
- {
- sprintf(show_buf,"SMOKE:%02d%%",PPM);//转换显示格式为SMOKE:00%
- LCD_Write_String(0,1,show_buf);
- memcpy(buf+17,show_buf,9);
- }
- buf[26] = '\r';
- buf[26] = '\n';
- if(Temperature >= Temperature_MAX || HUM >= HUM_MAX || PPM >= PPM_MAX)//温度湿度烟雾超限
- {
- RELAY = 1;//继电器开启
- BEEP_ENABLE(); //蜂鸣器报警
- }
- else
- {
- RELAY = 0;
- BEEP_DISENABLE();
- }
- }
- u8 KEY_SCAN(void)
- {
- u8 res = 0;
- if(!KEY1)
- {
- delay_ms(10);
- if(!KEY1)
- {
- while(!KEY1);//卡死
- res = 1;
- }
- }
- if(!KEY2)
- {
- delay_ms(10);
- if(!KEY2)
- {
- while(!KEY2);//卡死
- res = 2;
- }
- }
- if(!KEY3)
- {
- delay_ms(10);
- if(!KEY3)
- {
- while(!KEY3);//卡死
- res = 3;
- }
- }
- return res;
- }
- void Change_data_handle(void)
- {
- u8 key;
- static u8 MODE = 0;
- key = KEY_SCAN();//读取按键值
- if(key == 1)//按键1按下
- {
- MODE++;
- LCD_Clear();//清屏
- delay_ms(100);
- }
- while(MODE)
- {
- key = KEY_SCAN();
- switch(MODE)
- {
- case 1: //设置温度
- //显示
- LCD_Write_String(0,0,"Set the TEMP ");
- LCD_Write_String(0,1,"max:");
- LCD_Write_Char(8,1,0xdf);
- LCD_Write_Char(9,1,'C');
- if(key == 2 && Temperature_MAX < 99) Temperature_MAX++;//按键2按下,值加1
- if(key == 3 && Temperature_MAX > 0) Temperature_MAX--;//按键3按下,值减1
- LCD1602_write_long(6,1,Temperature_MAX,2);//显示值
- delay_ms(10);
- break;
- case 2: //设置湿度
- //显示
- LCD_Write_String(0,0,"Set the HUM ");
- LCD_Write_String(0,1,"max:");
- LCD_Write_String(8,1,"%");
- if(key == 2 && HUM_MAX < 99) HUM_MAX += 1;//按键2按下,值加1
- if(key == 3 && HUM_MAX > 0) HUM_MAX -= 1;//按键3按下,值减1
- LCD1602_write_long(6,1,HUM_MAX,2);
- delay_ms(10);
- break;
- case 3: //设置烟雾
- //显示
- LCD_Write_String(0,0,"Set the SMOKE ");
- LCD_Write_String(0,1,"max:");
- LCD_Write_String(10,1,"% ");
- if(key == 2 && PPM_MAX < 99) PPM_MAX += 1;//按键2按下,值加1
- if(key == 3 && PPM_MAX > 0) PPM_MAX -= 1;//按键3按下,值减1
- LCD1602_write_long(8,1,PPM_MAX,2);
- delay_ms(10);
- break;
- default: break;
- }
- if(key == 1)
- {
- MODE++;
- LCD_Clear();
- if(MODE == 4)//按下次数等于4退出设置
- {
- MODE = 0;
- }
- }
- }
-
- }
复制代码 代码:
程序.7z
(204.99 KB, 下载次数: 506)
|