基于STM32F103的智能家居项目设计

发布时间:2024-12-06 05:51

节能改造项目往往基于审计发现的节能项目优先级来实施。 #生活常识# #环保节能技巧# #能源审计#

前言

该项目是嵌入式课程学习最后的一个项目设计,做的不是很好(大佬勿喷),特别是STM32数据处理部分,因为刚学的STM32,并且对C语言的指针等的使用也有些生疏(虽然学过,但大部分都忘了),不敢随便用,所以有些代码设计不好,只能完成一些简单功能。ESP8266使用的是NodeMCU开发板,用ArduinoIDE开发(因为有很多现成的库,资料也多)。APP制作用的是Android Studio开发,从网上参考了很多人的代码,最后修改成自己的。前后花了差不多2周时间(主要是中间还有课要上,一些知识也得现学),不过最后功能基本算实现了,也学到了不少东西。

具体实现的功能包括以下三个方面:

TFTLCD可以显示STM32通过DHT11模块和光敏电阻模块采集到的温度(精度±2°C)、湿度(±5%RH)、光照(0~100,最暗为0,最亮为100)信息;以及显示设置的报警温度、最低光照;还有ESP8266WIFI模块通过网络获取到的时间(年-月-日 时:分:秒)、当地城市的天气信息(气温、湿度、天气、风力)。

STM32可以在室内温度过高(超过报警温度)时驱动蜂鸣器发出声音,在室内亮度较低(低于设定的最低光照)时驱动LED点亮。

在任意可以连接网络的地方,自己设计的手机APP可以显示室内环境信息(温度、湿度、光照强度),并且可以设置报警温度(0~100°C)和最低光照(0~100)。

STM32端程序设计

功能介绍

STM32单片机通过DHT11温湿度传感器采集温湿度

通过模数转换器ADC采集光敏电阻分压后的电压,然后转成光照强度

串口接收ESP8266的信息,提取出时间、天气信息,以及报警温度、最低光照信息(来自APP,用于控制蜂鸣器、led的开闭)

当室内温度超过报警温度时,蜂鸣器启动;当室内光照较暗(低于设定的最低光照)时,led点亮

总体框图如下:

具体实现

下面将主要介绍一些功能函数,完整代码请看:

完整代码开源地址-Gitee

完整代码开源地址-Github

温湿度采集

初始化DHT11模块

c

u8 DHT11_Init() {GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);GPIO_InitStructure.GPIO_Pin=DHT11;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIO_DHT11,&GPIO_InitStructure);GPIO_SetBits(GPIO_DHT11,DHT11);DHT11_Rst();return DHT11_Check(); } void DHT11_Rst() {DHT11_IO_OUT(); DHT11_DQ_OUT=0; delay_ms(20); DHT11_DQ_OUT=1;delay_us(30); } u8 DHT11_Check() {u8 retry=0;DHT11_IO_IN(); while (DHT11_DQ_IN&&retry<100){retry++;delay_us(1);};if(retry>=100)return 1;else retry=0; while (!DHT11_DQ_IN&&retry<100){retry++;delay_us(1);};if(retry>=100)return 1;return 0; }

读取数据

c

u8 DHT11_Read_Bit(void) { u8 retry=0; while(DHT11_DQ_IN&&retry<100) { retry++; delay_us(1); } retry=0; while(!DHT11_DQ_IN&&retry<100) { retry++; delay_us(1); } delay_us(40); if(DHT11_DQ_IN)return 1; else return 0; } u8 DHT11_Read_Byte(void) { u8 i,dat; dat=0; for (i=0;i<8;i++) { dat<<=1; dat|=DHT11_Read_Bit(); } return dat; }          u8 DHT11_Read_Data(u8 *temp,u8 *humi)  {   u8 buf[5];   u8 i;   DHT11_Rst();   if(DHT11_Check()==0)   {   for(i=0;i<5;i++)   {   buf[i]=DHT11_Read_Byte();   }   if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])   {   *humi=buf[0];   *temp=buf[2];   }     }else return 1;   return 0;  }

光照采集

使用ADC3模数转换器采集光敏电阻的分压,然后转换为光照强度(转换过程把最亮的当作100,最暗当作0来作为最终结果)

首先初始化配置ADC3

c

void Lsens_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE ); RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, DISABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOF, &GPIO_InitStructure); ADC_DeInit(ADC3); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC3, &ADC_InitStructure); ADC_Cmd(ADC3, ENABLE); ADC_ResetCalibration(ADC3); while(ADC_GetResetCalibrationStatus(ADC3)); ADC_StartCalibration(ADC3); while(ADC_GetCalibrationStatus(ADC3)); }

然后采集数据

c

u16 Get_ADC3(u8 ch) { ADC_RegularChannelConfig(ADC3, ch, 1, ADC_SampleTime_239Cycles5 ); ADC_SoftwareStartConvCmd(ADC3, ENABLE); while(!ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC )); return ADC_GetConversionValue(ADC3); } u8 Lsens_Get_Val(void) { u32 temp_val=0; u8 t; for(t=0;t<LSENS_READ_TIMES;t++) { temp_val+=Get_ADC3(ADC_Channel_6); delay_ms(5); } temp_val/=LSENS_READ_TIMES; if(temp_val>4000)temp_val=4000; return (u8)(100-(temp_val/40)); }

接收并处理串口USART2数据

初始化配置USART2

c

void usart2_init( void ) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); USART_ClearFlag(USART2, USART_FLAG_TC); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }

接收USART2数据

c

char USART2_RX_BUF[RX_BUF_MAX_LEN]; u16 USART2_RX_STA = 0; void USART2_IRQHandler(void) { u8 r; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { r = USART_ReceiveData(USART2); if((USART2_RX_STA&0x8000)==0) { if(USART2_RX_STA&0x4000) { if(r!=0x0a)USART2_RX_STA=0; else { USART2_RX_STA = 0; } } else { if(r==0x0d)USART2_RX_STA|=0x4000; else { USART2_RX_BUF[USART2_RX_STA&0X3FFF]=r; USART2_RX_STA++; if(USART2_RX_STA>(RX_BUF_MAX_LEN-1))USART2_RX_STA=0; } } } } }

处理USART2接收到的数据

提取时间、天气信息;为了方便,ESP8266发给STM32的数据都是用逗号隔开的,如 时间,天气,气温,湿度,……

天气数据保存在结构体中

c

struct WeatherData{ char datetime[20]; char city[10]; char weather[10]; char temp[10]; char humi[10]; char windpower[10]; };

对USART2接收的数据进行处理

c

struct WeatherData processWdata(char data[]){ u8 i=0, j=0, i0=0, k=0;u8 ind=0, jnd=0; int slen = strlen(data); struct WeatherData weather; for(ind=0; ind<8; ind++){ switch(ind){ case 0: { for(jnd=0; jnd<20; jnd++){ weather.datetime[jnd]='\0'; } };break; case 1: { for(jnd=0; jnd<10; jnd++){ weather.city[jnd]='\0'; } };break; case 2: { for(jnd=0; jnd<10; jnd++){ weather.humi[jnd]='\0'; } };break; case 3: { for(jnd=0; jnd<10; jnd++){ weather.temp[jnd]='\0'; } };break; case 4: { for(jnd=0; jnd<10; jnd++){ weather.weather[jnd]='\0'; } };break; case 5: { for(jnd=0; jnd<10; jnd++){ weather.windpower[jnd]='\0'; } };break; case 6: { for(jnd=0; jnd<10; jnd++){ minLsens_str[jnd]='\0'; } };break; case 7: { for(jnd=0; jnd<10; jnd++){ alarmTemp_str[jnd]='\0'; } };break; } } strcpy(weather.city, "西安"); for(i=0; i<slen; i++){ if(data[i]==',') { i0++; for(j=k; j<i; j++){ if(i0==1) weather.datetime[j-k]=data[j]; else if(i0==2) weather.weather[j-k]=data[j]; else if(i0==3) weather.temp[j-k]=data[j]; else if(i0==4) weather.humi[j-k]=data[j]; else if(i0==5) weather.windpower[j-k]=data[j]; else if(i0==6) alarmTemp_str[j-k]=data[j]; else if(i0==7) minLsens_str[j-k]=data[j]; } k=i+1; } } return weather; }

字符串数字转为整型数字

c

u8 str2int(char s[]){ u8 n=0, i=0, len, sum=0; len = strlen(s); for(i=0; i<len; i++){ if(47<s[i]<58){ n = s[i] - 48; sum += (n * pow(10, len-1-i)); } } return sum; }

发送数据

通过定时器TIM2定时1s发送一次数据(json字符串格式)给ESP8266模块

初始化配置TIM2

c

void TIM2_init(u16 arr, u16 psc){ TIM_TimeBaseInitTypeDef TIM_Structure; NVIC_InitTypeDef NVIC_Structure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_Structure.TIM_Period = arr - 1; TIM_Structure.TIM_Prescaler = psc-1; TIM_Structure.TIM_ClockDivision = 0; TIM_Structure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_Structure); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_ClearITPendingBit(TIM2, TIM_IT_Update); NVIC_Structure.NVIC_IRQChannel = TIM2_IRQn; NVIC_Structure.NVIC_IRQChannelPreemptionPriority = 6; NVIC_Structure.NVIC_IRQChannelSubPriority = 4; NVIC_Structure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_Structure); TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_Cmd(TIM2, ENABLE); }

定时时间(项目中使用1s)到达后产生定时器中断,这时候进入中断函数中发送数据

c

void TIM2_IRQHandler(void){ if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET){ ESP8266_Usart("{\"temp\":\"%d\",\"humi\":\"%d\",\"light\":\"%d\",\"ledsta\":\"%d\",\"beepsta\":\"%d\"}\r\n", dhtData.temp, dhtData.humi, lsens, ledSta, beepSta); TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } }

控制LED与蜂鸣器

c

void ledBeepOnOrNot(u8 lsens, u8 mlsens, u8 t, u8 alarmT){ if(lsens < mlsens){ LED1 = 0; ledSta = 1; } else{ LED1 = 1; ledSta = 0; } if(t>alarmT){ BEEP=1; beepSta = 1; } else{ BEEP=0; beepSta = 0; } }

ESP8266端程序设计

ESP8266WIFI模块通过MQTT协议连接服务器,数据发布到服务器后,Android客户端可以向服务器订阅主题来获取到消息,然后进行展示。ESP8266客户端通过订阅也能获取到Android客户端发布的消息。

功能介绍

连接WiFi 启动后led点亮,连接上MQTT服务端后闪烁,未连接常亮 通过网络获取如下信息 时间:使用【苏宁的API】 http://quan.suning.com/getSysTime.do 天气:使用【高德的天气API】http://restapi.amap.com/v3/weather/weatherInfo?key=xxxxx&city=610100)(参数key要自己获取,city是城市编码) 与STM32通过串口进行数据通信 建立与MQTT服务器的连接 向服务器指定的主题发布消息和接收消息 json数据处理

具体实现

ESP8266使用NodeMCU模块,ArduinoIDE开发

一些全局变量

c

const char* ssid = "xxxxxx"; const char* password = "xxxxxxxx"; const char *mqttBroker = "xxxxxx.iotcloud.tencentdevices.com"; const char *mqttUsername = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; const char *mqttPassword = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; char *mqttClientId = "xxxxxxxxxxxesp8266"; const char *pubTopic = "xxxxxx/esp8266/data"; const char *subTopic = "xxxxxx/esp8266/data"; const int mqttPort = 1883; const String timeUrl = "http://quan.suning.com/getSysTime.do"; const String gaoDeUrl = "http://restapi.amap.com/v3/weather/weatherInfo?key=xxxxx&city=610100";

连接WiFi

c

void connectWifi(){ WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } }

发送HTTP请求

c

String callHttp(String httpUrl) { HTTPClient hClient; String message; hClient.begin(httpUrl); int httpCode = hClient.GET(); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { String payload = hClient.getString(); message = payload; } } else { message = "[1-HTTP]failed, error:" + String(hClient.errorToString(httpCode).c_str()); } hClient.end(); return message; }

连接MQTT服务器

c

void connectMqttServer(){ while (!mqttClient.connected()) { if (mqttClient.connect(mqttClientId, mqttUsername, mqttPassword)) { subscribeTopic(subTopic); } else { delay(2000); } } }

发布信息到指定主题

c

void publishTopic(const char* topic, char* jsonMsg){ mqttClient.publish(topic, jsonMsg, false); }

订阅主题

c

void subscribeTopic(const char* topic){ if(mqttClient.subscribe(topic)){ } else { } }

回调函数(当收到服务器的消息后会调用)

c

void receiveCallback(char* topic, byte* payload, unsigned int length) { serverMsg = ""; for (int i = 0; i < length; i++) { serverMsg += (char)payload[i]; } }

完整代码

c

#include <ESP8266WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include <ESP8266HTTPClient.h> #include <WiFiManager.h> const char* ssid = "xxxxxx"; const char* password = "xxxxxxxx"; const char *mqttBroker = "xxxxxx.iotcloud.tencentdevices.com"; const char *mqttUsername = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; const char *mqttPassword = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; char *mqttClientId = "xxxxxxxxxxxesp8266"; const char *pubTopic = "xxxxxx/esp8266/data"; const char *subTopic = "xxxxxx/esp8266/data"; const int mqttPort = 1883; WiFiClient wifiClient; PubSubClient mqttClient(wifiClient); struct Weather{ String wea=""; String temp=""; String windpower=""; String humi=""; }; struct ServerMsg{ String alarmTemp=""; String minLight=""; }; const String timeUrl = "http://quan.suning.com/getSysTime.do"; const String gaoDeUrl = "http://restapi.amap.com/v3/weather/weatherInfo?key=xxxxx&city=610100"; String timeMsg=""; String weatherMsg=""; String timeAfterParse=""; struct Weather weatherAfterParse; String data2stm=""; String data2server=""; String callHttp(String httpUrl); String parseJsonTime(String tjson); struct Weather parseJsonWeather(String wjson); void ledOnOff(); String tqzh2py(String zhtq); String windpowerFormat(String wp); void connectWifi(); void setLedOnOff(); void publishTopic(const char* topic, char* jsonMsg); void subscribeTopic(const char* topic); struct ServerMsg parseServerMsg(String jsonMsg); void getSerialDataAndProcess(); void setup() { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); Serial.begin(115200); connectWifi(); mqttInit(); connectMqttServer(); timeMsg = callHttp(timeUrl); weatherMsg = callHttp(gaoDeUrl); } struct ServerMsg serverMsgAfterParse; String serverMsg=""; String stm32Msg=""; int n = 0; void loop() { delay(500); n++; if(n % 2 == 0){ ledOnOff(); timeMsg = callHttp(timeUrl); if(n >= 20){ weatherMsg = callHttp(gaoDeUrl); n=0; } } getSerialDataAndProcess(); if(timeMsg!=""){ timeAfterParse = parseJsonTime(timeMsg); } if(weatherMsg!=""){ weatherAfterParse = parseJsonWeather(weatherMsg); } if(serverMsg!=""){ serverMsgAfterParse = parseServerMsg(serverMsg); } if(timeMsg!="" && weatherMsg!=""){ data2stm = (timeAfterParse + "," + tqzh2py(weatherAfterParse.wea) + "," + weatherAfterParse.temp + "," +weatherAfterParse.humi +"," + windpowerFormat(weatherAfterParse.windpower) + "," + serverMsgAfterParse.alarmTemp + "," + serverMsgAfterParse.minLight + "," + "\r\n"); Serial.print(data2stm); } if (mqttClient.connected()) { mqttClient.loop(); } else { connectMqttServer(); } } void mqttInit(){ mqttClient.setServer(mqttBroker, mqttPort); mqttClient.setCallback(receiveCallback); } void connectMqttServer(){ while (!mqttClient.connected()) { if (mqttClient.connect(mqttClientId, mqttUsername, mqttPassword)) { subscribeTopic(subTopic); } else { delay(2000); } } } void subscribeTopic(const char* topic){ if(mqttClient.subscribe(topic)){ } else { } } void receiveCallback(char* topic, byte* payload, unsigned int length) { serverMsg = ""; for (int i = 0; i < length; i++) { serverMsg += (char)payload[i]; } } struct ServerMsg parseServerMsg(String jsonMsg){ struct ServerMsg smsg; const size_t capacity = 96; DynamicJsonDocument sdoc(capacity); deserializeJson(sdoc, jsonMsg); smsg.alarmTemp = sdoc["atemp"].as<String>(); smsg.minLight = sdoc["mlight"].as<String>(); return smsg; } void publishTopic(const char* topic, char* jsonMsg){ mqttClient.publish(topic, jsonMsg, false); } void ledOnOff(){ static bool LEDState=0; if(mqttClient.connected()){ LEDState = !LEDState; } else{ LEDState = 0; } digitalWrite(LED_BUILTIN, LEDState); } String callHttp(String httpUrl) { HTTPClient hClient; String message; hClient.begin(httpUrl); int httpCode = hClient.GET(); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { String payload = hClient.getString(); message = payload; } } else { message = "[1-HTTP]failed, error:" + String(hClient.errorToString(httpCode).c_str()); } hClient.end(); return message; } void getTimeMsg(){ timeMsg = callHttp(timeUrl); } void getWeatherMsg(){ weatherMsg = callHttp(gaoDeUrl); } String parseJsonTime(String tjson){ const size_t capacity = 96; DynamicJsonDocument tdoc(capacity); deserializeJson(tdoc, tjson); String datetime = tdoc["sysTime2"].as<String>(); return datetime; } struct Weather parseJsonWeather(String wjson){ struct Weather weather; const size_t capacity = 512; DynamicJsonDocument wdoc(capacity); deserializeJson(wdoc, wjson); JsonObject lives_0 = wdoc["lives"][0]; weather.wea = lives_0["weather"].as<String>(); weather.temp = lives_0["temperature"].as<String>(); weather.humi = lives_0["humidity"].as<String>(); weather.windpower = lives_0["windpower"].as<String>(); return weather; } String tqzh2py(String zhtq){ String zh_cn[68] = {"晴","少云","晴间多云","多云","阴","有风","平静","微风","和风","清风","强风/劲风","疾风","大风","烈风","风暴","狂爆风","飓风","热带风暴","霾","中度霾","重度霾","严重霾","阵雨","雷阵雨","雷阵雨并伴有冰雹","小雨","中雨","大雨","暴雨","大暴雨","特大暴雨","强阵雨","强雷阵雨","极端降雨","毛毛雨/细雨","雨","小雨-中雨","中雨-大雨","大雨-暴雨","暴雨-大暴雨","大暴雨-特大暴雨","雨雪天气","雨夹雪","阵雨夹雪","冻雨","雪","阵雪","小雪","中雪","大雪","暴雪","小雪-中雪","中雪-大雪","大雪-暴雪","浮尘","扬沙","沙尘暴","强沙尘暴","龙卷风","雾","浓雾","强浓雾","轻雾","大雾","特强浓雾","热","冷","未知",}; String py[68] = {"qing","shaoyun","qingjianduoyun","duoyun","yin","youfeng","pingjing","weifeng","hefeng","qingfeng","qiangfeng/jinfeng","jifeng","dafeng","liefeng","fengbao","kuangbaofeng","jufeng","redaifengbao","mai","zhongdumai","zhongdumai","yanzhongmai","zhenyu","leizhenyu","leizhenyubingbanyoubingbao","xiaoyu","zhongyu","dayu","baoyu","dabaoyu","tedabaoyu","qiangzhenyu","qiangleizhenyu","jiduanjiangyu","maomaoyu/xiyu","yu","xiaoyu-zhongyu","zhongyu-dayu","dayu-baoyu","baoyu-dabaoyu","dabaoyu-tedabaoyu","yuxuetianqi","yujiaxue","zhenyujiaxue","dongyu","xue","zhenxue","xiaoxue","zhongxue","daxue","baoxue","xiaoxue-zhongxue","zhongxue-daxue","daxue-baoxue","fuchen","yangsha","shachenbao","qiangshachenbao","longjuanfeng","wu","nongwu","qiangnongwu","qingwu","dawu","teqiangnongwu","re","leng","weizhi"}; for(int i=0; i<68; i++){ if(zh_cn[i] == zhtq) return py[i]; } return zhtq; } String windpowerFormat(String wp){ if(wp=="≤3"){ return "<=3"; } return wp; } void getSerialDataAndProcess(){ if (Serial.available()) { size_t countBytes = Serial.available(); char sbuf[countBytes]; Serial.readBytes(sbuf, countBytes); publishTopic(pubTopic, sbuf); Serial.flush(); } } void connectWifi(){ WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } }

Android端程序设计

功能介绍

主要是通过MQTT服务器,获取ESP8266发布的信息,然后进行处理显示出来;同时APP也可以发布消息,用于修改报警温度、最低光照值。

具体实现

使用Android Studio进行开发(具体安装下载、环境配置请百度)

工程代码已开源,前往GitHub请点击前去Github,前往Gitee请点击前去Gitee

添加MQTT依赖

在自己的[project]下的 build.gradle添加如下代码

gradle

 repositories {   google()   mavenCentral()     maven {   url "https://repo.eclipse.org/content/repositories/paho-releases/"   }   } 在[project]/app下的build.gradle添加如下代码

gradle

 dependencies {   compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'   compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'  }

然后选择顶部弹出的 Sync Now 信息

权限配置

修改AndroidManifest.xml,添加权限

xml

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

如下:

显示界面

修改文件activity_main.xml,实现如下显示效果

具体代码如下:

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_marginBottom="10dp" android:id="@+id/house_env" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="室内环境" android:textStyle="bold" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/show_temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginBottom="10dp" android:text="温度(°C)" android:textSize="20dp" /> <TextView android:id="@+id/temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginBottom="10dp" android:text="0" android:textSize="20dp" /> <TextView android:id="@+id/show_humi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginBottom="10dp" android:text="湿度(%RH)" android:textSize="20dp" /> <TextView android:id="@+id/humi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginBottom="10dp" android:text="0" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/show_light" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="光照强度" android:textSize="20dp" /> <TextView android:id="@+id/light" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="0" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/ledsta1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="LED状态" android:textSize="20dp" /> <TextView android:id="@+id/ledsta2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="关" android:textSize="20dp" /> <TextView android:id="@+id/beepsta1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="蜂鸣器状态" android:textSize="20dp" /> <TextView android:id="@+id/beepsta2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="关" android:textSize="20dp" /> </LinearLayout> <TextView android:id="@+id/sets" android:layout_column="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="设置" android:textStyle="bold" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/set_temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="报警温度(当前温度超过该值蜂鸣器将报警)" android:textSize="15dp" /> <TextView android:id="@+id/seekbarval1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:text="40"/> </LinearLayout> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_gravity="center" android:max="100" android:progress="40"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/set_tlight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="最低光照(当前光照小于该值将点亮led灯)" android:textSize="15dp" /> <TextView android:id="@+id/seekbarval2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:text="12"/> </LinearLayout> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_gravity="center" android:max="100" android:progress="12"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:layout_marginLeft="100dp" android:layout_marginTop="20dp" android:id="@+id/connectbtn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="@color/white" android:text="连接"/> <Button android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:id="@+id/exitbtn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="@color/white" android:text="退出"/> </LinearLayout> <TextView android:layout_margin="5dp" android:id="@+id/msgTxt" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scrollbars="vertical" android:text=""/> </LinearLayout>

界面控制

接下来修改MainActivity.java,添加控制操作,让数据可以通过TextView显示,按下按钮可以作出反应,上面只是完成一个静态界面的创建

java

package com.ajream.mqttdemo4; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.Button; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.json.JSONException; import org.json.JSONObject; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class MainActivity extends AppCompatActivity { private Button connectbtn; private Button exitbtn; private TextView temptv; private TextView humitv; private TextView lighttv; private TextView ledtv; private TextView beeptv; private TextView bar1tv; private TextView bar2tv; private TextView showmsgtv; private SeekBar tempbar; private SeekBar lightbar; private String host = "tcp://xxxxxxx.iotcloud.tencentdevices.com:1883"; private String userName = "xxxxxxxxxx"; private String passWord = "xxxxxxxxxx"; private String mqtt_id = "xxxxxxxxxxxxxxxxxxx"; private String mqtt_sub_topic = "xxxxxxxxxx/AndroidClient/data"; private String mqtt_pub_topic = "xxxxxxxxxx/AndroidClient/data"; private ScheduledExecutorService scheduler; private MqttClient mqttClient; private MqttConnectOptions options; private Handler handler; private String msgToPublish = ""; private String alarmTempMsg = ""; private String minLightMsg = ""; JSONObject msgGet; @SuppressLint("HandlerLeak") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connectbtn = findViewById(R.id.connectbtn); exitbtn = findViewById(R.id.exitbtn); temptv = findViewById(R.id.temp); humitv = findViewById(R.id.humi); lighttv = findViewById(R.id.light); ledtv = findViewById(R.id.ledsta2); beeptv = findViewById(R.id.beepsta2); bar1tv = findViewById(R.id.seekbarval1); bar2tv = findViewById(R.id.seekbarval2); showmsgtv = findViewById(R.id.msgTxt); tempbar = findViewById(R.id.seekBar1); lightbar = findViewById(R.id.seekBar2); alarmTempMsg = String.valueOf(tempbar.getProgress()); minLightMsg = String.valueOf(lightbar.getProgress()); connectbtn.setOnClickListener(view -> { Mqtt_init(); startReconnect(); }); exitbtn.setOnClickListener(view -> { android.os.Process.killProcess(android.os.Process.myPid()); }); tempbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Toast.makeText(MainActivity.this, "设置报警温度为: " + progress, Toast.LENGTH_LONG).show(); bar1tv.setText(check(progress)); alarmTempMsg = check(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { msgToPublish = "{\"atemp\":\"" + alarmTempMsg + "\",\"mlight\":\"" + minLightMsg+"\"}"; publishMsg(mqtt_pub_topic, msgToPublish); } }); lightbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { Toast.makeText(MainActivity.this, "设置最低光照为: " + i, Toast.LENGTH_LONG).show(); bar2tv.setText(check(i)); minLightMsg = check(i); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { msgToPublish = "{\"atemp\":\"" + alarmTempMsg + "\",\"mlight\":\"" + minLightMsg+"\"}"; publishMsg(mqtt_pub_topic, msgToPublish); } }); handler = new Handler() { @SuppressLint("SetTextI18n") public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: break; case 2: break; case 3: showmsgtv.setText(msg.obj.toString()); JSONObject msgGet = null; try { msgGet = new JSONObject(msg.obj.toString()); temptv.setText(msgGet.get("temp").toString()); humitv.setText(msgGet.get("humi").toString()); lighttv.setText(msgGet.get("light").toString()); if(Integer.parseInt(msgGet.get("ledsta").toString())==0) ledtv.setText("关"); else ledtv.setText("开"); if(msgGet.get("beepsta").toString().charAt(0)=='0') beeptv.setText("关"); else beeptv.setText("开"); } catch (JSONException e) { e.printStackTrace(); } break; case 30: Toast.makeText(MainActivity.this,"连接失败" ,Toast.LENGTH_SHORT).show(); break; case 31: Toast.makeText(MainActivity.this,"连接成功" ,Toast.LENGTH_SHORT).show(); try { mqttClient.subscribe(mqtt_sub_topic,1); } catch (MqttException e) { e.printStackTrace(); } break; default: break; } } }; } private String check(int progress) { int curValue = 100 * progress/Math.abs(100); return String.valueOf(curValue); } private void publishMsg(String topic, String message2) { if (mqttClient == null || !mqttClient.isConnected()) { return; } MqttMessage message = new MqttMessage(); message.setPayload(message2.getBytes()); try { mqttClient.publish(topic, message); } catch (MqttException e) { e.printStackTrace(); } } private void Mqtt_init() { try { mqttClient = new MqttClient(host, mqtt_id, new MemoryPersistence()); options = new MqttConnectOptions(); options.setCleanSession(false); options.setUserName(userName); options.setPassword(passWord.toCharArray()); options.setConnectionTimeout(10); options.setKeepAliveInterval(20); mqttClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable cause) { } @Override public void deliveryComplete(IMqttDeliveryToken token) { System.out.println("deliveryComplete---------" + token.isComplete()); } @Override public void messageArrived(String topicName, MqttMessage message) throws Exception { System.out.println("getMsg: "); Message msg = new Message(); msg.what = 3; msg.obj = message.toString(); handler.sendMessage(msg); } }); } catch (Exception e) { e.printStackTrace(); } } private void Mqtt_connect() { new Thread(new Runnable() { @Override public void run() { try { if(!(mqttClient.isConnected())) { mqttClient.connect(options); Message msg = new Message(); msg.what = 31; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); Message msg = new Message(); msg.what = 30; handler.sendMessage(msg); } } }).start(); } private void startReconnect() { scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { if (!mqttClient.isConnected()) { Mqtt_connect(); } } }, 0 * 1000, 10 * 1000, TimeUnit.MILLISECONDS); } }

注意:MQTT服务端配置这里没有介绍,可以使用一些公用的MQTT服务器(可以去百度搜索),也可以自己搭建一个,我这里使用的是自己通过腾讯云配置的MQTT服务,具体搭建可以到我的博客查看或者看官方文档

效果预览

TFTLCD屏幕显示

Android APP界面

前言STM32端程序设计    功能介绍    具体实现        温湿度采集        光照采集        接收并处理串口USART2数据        控制LED与蜂鸣器ESP8266端程序设计    功能介绍    具体实现    完整代码Android端程序设计    功能介绍    具体实现        添加MQTT依赖        权限配置        显示界面        界面控制效果预览

__EOF__

本文作者: aJream 本文链接: https://www.cnblogs.com/ajream/p/15572376.html 关于博主: 评论和私信会在第一时间回复。或者直接私信我。 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处! 声援博主: 如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。

网址:基于STM32F103的智能家居项目设计 https://www.yuejiaxmz.com/news/view/392520

相关内容

基于STM32F103和OLED的个人健康助手的设计
stm32项目(8)——基于stm32的智能家居设计
STM32项目设计:基于stm32的智能家居系统设计
基于STM32的智能节能风扇设计
基于WIFI的智能家居系统(共60页)
基于物联网的居家环境监测系统设计
基于智能家居环境控制器的设计
基于STM32的智能家居设计(有三款设计)
基于STM32的个人健康助手的设计与实现
基于Java的智能家居设计:设计基于Java的智能照明系统的策略与挑战

随便看看