#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include <string.h>
#include "timer.h"
#include "esp8266.h"
#include "motor.h"
#include "oled.h"
#include "dht11.h"
#include "adc.h"
#include <string.h>
#include <stdlib.h>
#include "font.h"
#include "mqtt.h"
#include "iic.h"
#include "bh1750.h"
/*
(1)ESP8266 串口WIFI,用来与华为云IOT进行远程通信。
(2)DHT11 温度传感器,检测本地环境的温度湿度信息。
(3)继电器,来模拟控制家里的电器。
(4)BH1750 光敏传感器,检测环境光强度
(5)MQ135 空气质量传感器。可以检测可燃气体、烟雾浓度,实现火警预警。
(6)OLED显示屏显示本地的传感器的参数情况,实时展示
*/
//华为物联网服务器的设备信息
#define MQTT_ClientID "61b9ba3a2b2aa20288c1e7f1_QQ1126626497_0_0_2021121510"
#define MQTT_UserName "61b9ba3a2b2aa20288c1e7f1_QQ1126626497"
#define MQTT_PassWord "385ce91dfe7da5b7431868d5d87e7998163c493344040935d5a00024d6324242"
//订阅与发布的主题
#define SET_TOPIC "$oc/devices/61b9ba3a2b2aa20288c1e7f1_QQ1126626497_0_0_2021121510/sys/messages/down" //订阅
#define POST_TOPIC "$oc/devices/61b9ba3a2b2aa20288c1e7f1_QQ1126626497_0_0_2021121510/sys/properties/report" //发布
//设置连接的路由器信息
#define CONNECT_WIFI "Xiaomi_meizi6" //将要连接的路由器名称 --不要出现中文、空格等特殊字符
#define CONNECT_PASS "12170307yu" //将要连接的路由器密码
#define CONNECT_SERVER_IP "121.36.42.100" //服务器IP地址
#define CONNECT_SERVER_PORT 1883 //服务器端口号
//JTAG模式设置,用于设置JTAG的模式
//mode:jtag,swd模式设置;00,全使能;01,使能SWD;10,全关闭;
#define JTAG_SWD_DISABLE 0X02
#define SWD_ENABLE 0X01
#define JTAG_SWD_ENABLE 0X00
void JTAG_Set(u8 mode)
{
u32 temp;
temp=mode;
temp<<=25;
RCC->APB2ENR|=1<<0; //开启辅助时钟
AFIO->MAPR&=0XF8FFFFFF; //清除MAPR的[26:24]
AFIO->MAPR|=temp; //设置jtag模式
}
u8 motor_flag=0; //窗帘的状态 0表示关闭 1表示打开
u8 led_flag=0; //房间照明灯状态 0表示关闭 1表示打开
u16 MQ135_data=0; //MQ135烟雾传感器的值
u8 DHT11_T=0; //环境温度
u8 DHT11_H=0; //环境湿度
u8 BH1750_data=0; //环境光照强度
char request_id[100];
char mqtt_message[100];
//WIFI发送数据存储区域
char data_buff[300];
int main()
{
u8 esp8266_state=0;
u8 key;
u8 i;
u32 time_cnt=0;
u32 timer_hour_cnt=0; //记录定时的时间
JTAG_Set(JTAG_SWD_DISABLE); //释放PA15
LED_Init();
KEY_Init();
USART1_Init(115200);
Moto_Init(); //风扇、加热棒初始化
USART2_Init(115200);//串口-WIFI
TIMER2_Init(72,20000); //超时时间20ms
DHT11_Init(); //初始化DHT11
Lcd_Init(); //LCD初始化
Lcd_Clear(0); //清屏为黑色
LCD_LED_SET;//通过IO控制背光亮
AdcInit(); //ADC初始化--检测MQ2烟雾传感器的数据
IIC_Init();
printf("正在初始化WIFI请稍等.\r\n");
for(i=0;i<5;i++)
{
if(ESP8266_Init()==0)
{
esp8266_state=1;
break;
}
else
{
esp8266_state=0;
printf("ESP8266硬件检测错误.\n");
}
}
if(esp8266_state)
{
printf("准备连接服务器....\r\n");
//非加密端口
printf("WIFI:%d\n",ESP8266_STA_TCP_Client_Mode(CONNECT_WIFI,CONNECT_PASS,CONNECT_SERVER_IP,CONNECT_SERVER_PORT,1));
//2. MQTT协议初始化
MQTT_Init();
//3. 连接服务器
for(i=0;i<5;i++)
{
if(MQTT_Connect(MQTT_ClientID,MQTT_UserName,MQTT_PassWord)==0)
{
esp8266_state=1;
break;
}
esp8266_state=0;
printf("服务器连接失败,正在重试...\n");
delay_ms(500);
}
printf("服务器连接成功.\n");
//3. 订阅主题
if(MQTT_SubscribeTopic(SET_TOPIC,0,1))
{
printf("主题订阅失败.\n");
}
else
{
printf("主题订阅成功.\n");
}
}
//环境温度
LCD_ShowChineseFont(0,0,16,HZ_FONT_16[0],RED,0);
LCD_ShowChineseFont(16*1,0,16,HZ_FONT_16[1],RED,0);
LCD_ShowChineseFont(16*2,0,16,HZ_FONT_16[2],RED,0);
LCD_ShowChineseFont(16*3,0,16,HZ_FONT_16[3],RED,0);
//环境湿度
LCD_ShowChineseFont(0,16*1,16,HZ_FONT_16[4],RED,0);
LCD_ShowChineseFont(16*1,16*1,16,HZ_FONT_16[5],RED,0);
LCD_ShowChineseFont(16*2,16*1,16,HZ_FONT_16[6],RED,0);
LCD_ShowChineseFont(16*3,16*1,16,HZ_FONT_16[7],RED,0);
//光照强度
LCD_ShowChineseFont(0,16*2,16,HZ_FONT_16[8],RED,0);
LCD_ShowChineseFont(16,16*2,16,HZ_FONT_16[9],RED,0);
LCD_ShowChineseFont(16*2,16*2,16,HZ_FONT_16[10],RED,0);
LCD_ShowChineseFont(16*3,16*2,16,HZ_FONT_16[11],RED,0);
//空气质量
LCD_ShowChineseFont(0,16*3,16,HZ_FONT_16[12],RED,0);
LCD_ShowChineseFont(16,16*3,16,HZ_FONT_16[13],RED,0);
LCD_ShowChineseFont(16*2,16*3,16,HZ_FONT_16[14],RED,0);
LCD_ShowChineseFont(16*3,16*3,16,HZ_FONT_16[15],RED,0);
//智能照明
LCD_ShowChineseFont(0,16*4,16,HZ_FONT_16[16],RED,0);
LCD_ShowChineseFont(16,16*4,16,HZ_FONT_16[17],RED,0);
LCD_ShowChineseFont(16*2,16*4,16,HZ_FONT_16[18],RED,0);
LCD_ShowChineseFont(16*3,16*4,16,HZ_FONT_16[19],RED,0);
//智能窗帘
LCD_ShowChineseFont(0,16*5,16,HZ_FONT_16[20],RED,0);
LCD_ShowChineseFont(16,16*5,16,HZ_FONT_16[21],RED,0);
LCD_ShowChineseFont(16*2,16*5,16,HZ_FONT_16[22],RED,0);
LCD_ShowChineseFont(16*3,16*5,16,HZ_FONT_16[23],RED,0);
while(1)
{
//按键可以手动控制开启窗帘和LED灯
key=KEY_Scan();
if(key)printf("key=%d\r\n",key);
//控制LED灯
if(key==2)
{
led_flag=!led_flag;
if(led_flag)
{
LED_OUT=1;
}
else
{
LED_OUT=0;
}
}
//控制继电器
if(key==1)
{
motor_flag=!motor_flag;
if(motor_flag)
{
MOTOR_OUT=1;
}
else
{
MOTOR_OUT=0;
}
}
//轮询时间到达
if(time_cnt>=100)
{
time_cnt=0;
LED1=!LED1;
//读取光强度
BH1750_data=Read_BH1750_Data();
printf("环境光强度:%d\r\n",BH1750_data);
//获取环境温湿度
if(DHT11_Read_Data(&DHT11_T,&DHT11_H))
{
printf("温度读取失败.\r\n");
}
printf("环境温度:%d\r\n环境湿度:%d\r\n",DHT11_T,DHT11_H);
//获取空气质量
MQ135_data=GetAdcCHxDATA(1);
printf("空气质量:%d\r\n",MQ135_data);
//LCD屏实时显示温湿度、空气质量,系统工作状态等等。
//环境温度
sprintf(data_buff,"%d",DHT11_T);
Gui_DrawFont_GBK16(72,16*0,RED,0,(u8*)data_buff);
//环境湿度
sprintf(data_buff,"%d",DHT11_H);
Gui_DrawFont_GBK16(72,16*1,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*1,RED,0,(u8*)data_buff);
//环境光强度
sprintf(data_buff,"%d",BH1750_data);
Gui_DrawFont_GBK16(72,16*2,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*2,RED,0,(u8*)data_buff);
//空气质量
sprintf(data_buff,"%d",MQ135_data);
Gui_DrawFont_GBK16(72,16*3,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*3,RED,0,(u8*)data_buff);
//智能照明
sprintf(data_buff,"%d",led_flag);
Gui_DrawFont_GBK16(72,16*4,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*4,RED,0,(u8*)data_buff);
//智能窗帘
sprintf(data_buff,"%d",motor_flag);
Gui_DrawFont_GBK16(72,16*5,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*5,RED,0,(u8*)data_buff);
//上报数据
sprintf(data_buff,"{\"services\":{\"BH1750\":[{\"v\":%d}],\"DHT11_H\":[{\"v\":%d}],\"DHT11_T\":[{\"v\":%d}],\"MQ135\":[{\"v\":%d}],\"motor\":[{\"v\":%d}],\"LED\":[{\"v\":%d}]}}",
BH1750_data,DHT11_H,DHT11_T,MQ135_data,motor_flag,led_flag);
MQTT_PublishData(POST_TOPIC,data_buff,0);
printf("更新数据.\r\n");
}
// 接收WIFI返回的数据
if(USART2_RX_FLAG)
{
USART2_RX_BUFFER[USART2_RX_CNT]='\0';
printf("WIFI收到数据:\r\n");
//向串口打印服务器返回的数据
for(i=0;i<USART2_RX_CNT;i++)
{
printf("%c",USART2_RX_BUFFER[i]);
}
/*
LED_ON 智能照明灯打开
LED_OFF 智能照明灯关闭
MOTOR_ON 智能窗帘打开
MOTOR_OFF 智能窗帘关闭
*/
//如果是下发了属性
if(USART2_RX_CNT>5)
{
//向服务器应答
if(strstr((char*)&USART2_RX_BUFFER[5],"cmd/request/"))
{
char *p=NULL;
p=strstr((char*)&USART2_RX_BUFFER[5],"cmd/request/");
if(p)
{
//解析数据
strncpy(request_id,p+12,36);
}
//上报给服务器的数据
sprintf(mqtt_message,"success");
//应答主题
sprintf(data_buff,"$sys/501579/dev1/cmd/response/%s",request_id);
MQTT_PublishData(data_buff,mqtt_message,0);
printf("发布主题:%s\r\n",data_buff);
printf("发布数据:%s\r\n",mqtt_message);
}
//LED:0 LED:1 MOTOR:0 MOTOR:1
//开灯
if(strstr((char*)&USART2_RX_BUFFER[5],"LED:0"))
{
LED_OUT=0;
led_flag=0;
}
//关灯
else if(strstr((char*)&USART2_RX_BUFFER[5],"LED:1"))
{
LED_OUT=1;
led_flag=1;
}
//开窗帘
else if(strstr((char*)&USART2_RX_BUFFER[5],"MOTOR:0"))
{
MOTOR_OUT=0;
motor_flag=0;
}
//关窗帘
else if(strstr((char*)&USART2_RX_BUFFER[5],"MOTOR:1"))
{
MOTOR_OUT=1;
motor_flag=1;
}
}
USART2_RX_CNT=0;
USART2_RX_FLAG=0;
}
DelayMs(10);
time_cnt++;
timer_hour_cnt++;
}
}
评论