Introducing a Bluetooth application solution with rich usage scenarios - a temperature and humidity monitor developed based on the MM32W series.
Figure 1 Solution application diagramTemperature and humidity are closely related to our lives. Scientific research laboratories, agricultural greenhouses, food storage rooms, vaccine storage and distribution, storage rooms, etc. have strict control standards for environmental temperature and humidity. Abnormal changes in temperature and humidity may cause It has serious consequences. Traditional manual inspection and recording of environmental temperature and humidity changes are not easy. With the development of the times, temperature and humidity sensors that can intelligently monitor environmental temperature and humidity have emerged. Nowadays, industrial sectors such as scientific research, agriculture, HVAC, computer rooms, aerospace, and electric power have begun to use intelligent temperature and humidity sensors to monitor the temperature and humidity of the environment. Using temperature and humidity sensors to monitor the temperature and humidity of the environment in real time can not only detect abnormalities in environmental temperature and humidity in a timely manner, and then take countermeasures to avoid or reduce losses, it can also reduce employee workload and labor costs.
Hardware resources are as follows:
This solution is tested and verified based on the MM32 BLE_Test Board . It is equipped with a temperature and humidity sensor DHT11 to collect temperature and humidity data in the environment, and a small OLED screen is used as a local data output display window. In addition, the temperature and humidity changes can be obtained through the mobile APP . . In terms of hardware principles, the single-line data transmission pin of the DHT11 module of this solution is connected to PA7 of the MCU . In order to analyze the data timing of the module, this pin is multiplexed as the TIM3_CH2 input capture function; the hardware IIC interface is used to connect to the OLED screen. The pins are PB6 (SCL) and PB7 (SDA) , which can display temperature and humidity data on the OLED ; the Bluetooth-related function pins are consistent with the solution introduced earlier, so we will not go into too much detail here.
Software resources are as follows:
Combined with the hardware resources used above, below we focus on the software implementation process and related configuration code. Use the MCU pin multiplexing as the TIM3_CH2 input capture function DHT11 module. When turning on the capture, configure PA7 as a floating input mode. Select AF1 as the CH2 input capture channel of TIM3 as the multiplexing function, and turn on TIM3 ; stop capturing. When configured, PA7 is in push-pull output mode and TIM3 is turned off. Due to the limitations of the DHT11 module, the temperature and humidity sampling cycle interval must be greater than 1S . The sampling and display cycle of this solution is 2S .
When using an OLED screen, you need to use the hardware IIC peripheral interface. The corresponding PB6 and PB7 need to be configured in the multiplexed open-drain output mode. During initialization, you also need to modify the slave device address in the function IIC_Init() according to different OLED modules and enable it. After the IIC interface, you can start the transmission work. Due to the needs of data collection and timing display, the low power consumption mode of this solution adopts STOP mode.
The following is the main function to initialize the configuration content. It mainly initializes all peripheral resources and Bluetooth protocol stack, and runs Bluetooth in the way of interrupt service routine. The code is as follows:

The main function implemented in the loop of the main function is to regularly collect and display the temperature and humidity data of the current environment, and this data will also be sent to the APP for display in the Bluetooth service . The code is as follows:
The following is a brief introduction to several functions related to OLED operation:
// Initialize IIC
void IIC_Init(I2C_TypeDef* I2Cx);
// Send command function
static void Write_Command(unsigned char Command);
// Send data to display on the screen
static void Write_DataBuff(unsigned char *Data, unsigned char Len);
//OLED screen initialization
void OLED_Init(void);
// Set coordinates
void OLED_SetPos(unsigned char x, unsigned char y);
// String display
void OLED_DispStr(unsigned char x, unsigned char y, char *ch);
// Display logo
void OLED_DispLogo(void);
// Clear screen operation
void OLED_Clear(void);
The following is a brief introduction to several functions related to the operation of the DHT11 module:
//TIM3_CH2 input capture initialization
void CaptureInit(void); // Use TIM3_CH2 PB5 AF3
// Start capture
void CaptureStart(void);
// Stop capturing
void CaptureStop(void);
The captured data is parsed and processed in TIM3_IRQHandler () .
We send data to the mobile phone in the gatt_user_send_notify_data_callback function. This function is a callback function. The protocol stack will call back this function (asynchronously) when the system allows it. This function can be used for the Bluetooth module to actively send data. Blocking code must not be added inside the function. . In this application, we use this function to transmit the collected and converted temperature and humidity data to the mobile APP . The detailed implementation code is as follows:
// After the Bluetooth connection is successful, the protocol will call this callback function when it is idle.
void gatt_user_send_notify_data_callback(void){
static u8 notiCnt = 0; // Callback times counter
u16 humiBat ,tempBpm = 0;
unsigned char DHTData[3]={0x00,0x00,0x01};
notiCnt++; // The callback count counter +1 for each entry of this function
if(CaptureDataMon(&humiBat, &tempBpm) == 0) return;// If the temperature and humidity data is not successfully collected, return immediately
tempBpm %= 512;// Preliminary judgment of temperature data size
humiBat /= 10; // Preliminary judgment of humidity data size
if (notiCnt >= 20) {// The temperature data will be sent only once every 20 times the callback function is entered.
notiCnt = 0;
cur_notifyhandle = 0x12; // Temperature data reply handle value
if (tempBpm < 0x100){
DHTData[0] = 0; //1Byte
DHTData[1] = tempBpm;
sconn_notifydata(DHTData,2); // Convert and process the temperature data and send it out via Bluetooth
}
else {
DHTData[0] = 1; //2Byte
DHTData[1] = tempBpm;
DHTData[2] = tempBpm>>8;
sconn_notifydata(DHTData,3); // Convert and process the temperature data and send it out via Bluetooth
}
}
else if (10 == notiCnt) {// The humidity data is only sent once every 10 times the callback function is entered.
SimBatt = humiBat; //0~100
cur_notifyhandle = 0x18;// Humidity data reply handle value
sconn_notifydata(&SimBatt,1);// Convert and process the humidity data and send it out via Bluetooth
}
}
In addition to the above-mentioned key Bluetooth data sending functions, the following briefly introduces some Bluetooth-related characteristic value definitions:

The mobile phone operation process is as follows:
Turn on Bluetooth on your phone and open the App , select HRM to enter, and click the Connect button to start searching for temperature and humidity Bluetooth devices.Select the Bluetooth device with the corresponding name ( MM32W0_DHT ) and pair it, waiting for the connection to be successful. After the connection is successful, there will be a corresponding prompt, and the name of the button Connect will change to Disconnect .After successful connection, the battery icon on the App interface will display the humidity information (percentage) obtained from the DHT11 sensor. The finger and chart will display the temperature information obtained from the DHT11 sensor (raw data, temperature value x10 ).