This is the 7th day of my participation in Gwen Challenge

When I bought the development board of Zhengspot Atom, I gave a ESP8266wifi module as a gift, but I haven’t used it yet. In recent days, I just took it out to play when I had time. Now I share the implementation process.

Modules used:

1.STM32F103C8T6 minimum system board

2. Spot atom ESP8266wifi module

3. Usb-to-serial port module

Integral connection diagram

Connection relation between ESP8266 module and MCU pin:

STM32F103C8T6 Minimum system

ESP8266 module

5V

VCC

GND

GND

PB11 (USART3_RX)

TXD

PB10 (USATR3_TX)

RXD

The RST and ID_0 pins of the ESP8266 module are suspended and can be ignored.

Connection relation between serial port module and MCU pin:

STM32F103C8T6 Minimum system

USB to serial port TTL module

PA9 (USART1_TX)

RXD

(PA10 USART1_RX)

TXD

GND

GND

The USB-to-serial module is connected to serial port 1 of the single-chip microcomputer, which is used as a debugging port to monitor the execution process of programs. The ESP8266 module is connected to serial port 3 of the MCU, which is used to communicate with the wifi module.

The specific functions are as follows: single chip connects to atomic cloud through wifi module and sends data to cloud in real time. Through the cloud, it can send “LED on”, “LED off” and “LED Toggle” to the single chip microcomputer to realize the lighting, turning off and turning of THE LED lamp on the development board.

Serial port 1 Results of monitoring data

Monitoring data on the atomic cloud

Test software monitoring by atomic cloud host computer

Monitoring through the original cloud client on the mobile phone:

Through web page, PC software and mobile phone client, the command of “LED on”, “LED off” and “LED Toggle” can be sent to the microcontroller to realize the lighting, turning off and turning of LED lights on the development board. The function of basic realization is these, say realization idea below.

The driver code of the ESP8266 module is modified in the WiFi_ESP8266 module data.

The process of connecting ESP8266 to atomic cloud through SCM is similar to that of debugging ESP8266 directly through serial port. The process is as follows:

1. Send test instruction to the module; send instruction to the module: AT module reply instruction: OK 2. Set the module mode to STA mode and send the module instruction: AT+CWMODE=1 to reply the module instruction: OK 3. Set the ID and password of the hotspot to be connected to the module instruction: AT+CWJAP= "enbiens", "EB88858804" module reply instruction: OK 4. Connect atomic CLOUD sends device number and password to module: AT+ATKCLDSTA="61212332528032817648","12345678" module replies command: CLOUD CONNECTEDCopy the code

It only takes 4 steps to connect to the atomic cloud, the ESP8266 module must brush the atomic cloud firmware to connect to the atomic cloud, otherwise the command in the last step will not recognize the atomic cloud.

First send the test command, judge whether the module and the microcontroller connection state is normal, after the module connection is normal, you can send the setting command. The second step is to set the module to station mode, that is, to set the module to slave. The third step is to connect the router and send the wifi name and password. After the wifi is connected, the module can connect to the Internet through the router. Finally connect atomic cloud, directly send atomic cloud on the device number and password can be connected. The device number of atomic Cloud is assigned by default when registering on cloud.alientek.com/. The password can be set on atomic Cloud.

The key to using the MCU to control the ESP8266 module is to use the code to send instructions to the ESP8266 module, in the example provided by wildfire has realized this function package.

/* * Function name: ESP8266_Cmd * Description: Send AT command to WF-ESP8266 * input: Reply1, reply2, response to be expected, NULL; no response is required; 1, instruction sending success * 0, instruction sending failure * call: ESP8266_Cmd (char * CMD, char * reply1, char * reply2, u32 waittime ) { strEsp8266_Fram_Record .InfBit .FramLength = 0; MacESP8266_Usart ("%s\r\n", CMD); If ((reply1 == 0) && (reply2 == 0)) return true; Delay_ms ( waittime ); Stresp8266_fram_record.data_rx_buf [stRESP8266_fram_record.infbit.framLength] = '\0'; // Add terminator macPC_Usart ("%s", stRESP8266_fram_record.data_rx_buf) to the end of the received string; // The debug interface prints the received module instruction if ((reply1! = 0 ) && ( reply2 ! = 0 ) ) return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) || ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) ); else if ( reply1 ! = 0 ) return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) ); else return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) ); }Copy the code

This function is used to send instructions to the ESP82676 module and determine whether the instructions returned by the module are correct. The first argument, CMD, holds the instruction to send, the second argument holds the string the module needs to return, the third argument also holds the string the module needs to return, and the last argument sets the time to wait after sending instructions to the module. There is a delay between sending instructions to the module and returning instructions to the module. After sending the command to the module, you need to wait for a period of time to determine whether the data received by serial port 3 is correct.

The first command to be sent is sent through the macESP8266_Usart() function.

 macESP8266_Usart ( "%s\r\n", cmd );	
Copy the code

This function simply sends the string through serial port 3. After the command is sent to the ESP8266 through serial port 3, the serial port waits for a period of time to receive data.

 Delay_ms ( waittime ); 
Copy the code

This line of code is delayed because it can’t jump out because it has to wait for the ESP8266 module to return.

strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ] = '\0'; // Add terminator macPC_Usart ("%s", stRESP8266_fram_record.data_rx_buf) to the end of the received string; // The debug interface prints the commands received from the moduleCopy the code

After a period of delay, the data returned by the ESP8266 module is received in the interrupt function of serial port 3. Add the end of the string flag to the last bit of the received data, and then print the data returned by the module through the debugging port serial port 1.

Finally, check whether the string returned by the module is the same as the string provided in the parameter. If the string is the same, the module is set correctly; otherwise, the module fails to be set.

To set different commands, call this command setup function directly, such as sending test instructions to the module in the first step

void ESP8266_AT_Test ( void ) { char count = 0; Delay_ms ( 1000 ); while ( count < 10 ) { if( ESP8266_Cmd ( "AT", "OK", NULL, 500 ) ) return; // ESP8266_Rst(); // ESP8266_Rst(); // Otherwise reset the module, re-send AT test instruction ++ count; }}Copy the code

Send the “AT” string to the module, exit if the module returns “OK”, otherwise loop 10 times.

Step 2 Set the module to Station mode

ESP8266_Net_Mode_Choose ( STA ); /* * Function name: ESP8266_Net_Mode_Choose * Description: Select the working mode of the WF-ESP8266 module * Enter: enumMode, working mode * Return: 1, select successful * 0, select failed * call: */ bool ESP8266_Net_Mode_Choose (ENUM_Net_ModeTypeDef enumMode) {switch (enumMode) {case STA: return ESP8266_Cmd ( "AT+CWMODE=1", "OK", "no change", 2500 ); case AP: return ESP8266_Cmd ( "AT+CWMODE=2", "OK", "no change", 2500 ); case STA_AP: return ESP8266_Cmd ( "AT+CWMODE=3", "OK", "no change", 2500 ); default: return false; }}Copy the code

Step 3 Connect the router

ESP8266_JoinAP ( macUser_ESP8266_ApSsid, macUser_ESP8266_ApPwd ); /* * Function name: ESP8266_JoinAP * Description: WF-ESP8266 module connecting to external WiFi * Enter: pSSID, WiFi name string * : pPassWord, WiFi password string * Return: 1, connect successfully * 0, connect failed * call: external call */ bool ESP8266_JoinAP (char * pSSID, char * pPassWord) {char cCmd [120]; sprintf ( cCmd, "AT+CWJAP=\"%s\",\"%s\"", pSSID, pPassWord ); return ESP8266_Cmd ( cCmd, "OK", NULL, 5000 ); }Copy the code

Step 4: Connect the atomic cloud

ESP8266_ConnectYuanziyun ( yuanziyun_DeviceID, yuanziyun_DevicePassWord ); /* * ESP8266_ConnectYuanziyun * Description: WF-ESP8266 module connecting atomic cloud * Input: pSSID, device ID on atomic cloud * : pPassWord, device password on atomic cloud * Return: ESP8266_ConnectYuanziyun (char * pSSID, char * pPassWord) {char cCmd [120]; ESP8266_ConnectYuanziyun (char * pSSID, char * pPassWord) {char cCmd [120]; sprintf ( cCmd, "AT+ATKCLDSTA=\"%s\",\"%s\"", pSSID, pPassWord ); return ESP8266_Cmd ( cCmd, "OK", "CONNECTED", 5000 ); }Copy the code

In this way, different commands can be sent to serial port 3 through the command sending function, that is, to the ESP8266 module, so that the ESP8266 module can be connected to the atomic cloud.

When the module is connected to the atomic cloud, it can send data to the cloud through the single chip microcomputer.

void ESP8266_Station_Mode_yuanziyun_Test ( void ) { uint8_t value1 = 0, value2 = 0; uint8_t ucStatus; char cStr [ 100 ] = { 0 }; uint8_t ucId; char cStr1 [ 100 ] = { 0 }; char * pCh; Printf ("\r\n Configuring ESP8266...... \r\n" ); AT ESP8266_AT_Test (); //2, set module mode to STA mode AT+CWMODE=1 ESP8266_Net_Mode_Choose (STA); AT+CWJAP= "enbiens", "EB88858804" while (! ESP8266_JoinAP ( macUser_ESP8266_ApSsid, macUser_ESP8266_ApPwd ) ); AT+ATKCLDSTA="61212332528032817648","12345678" while (! ESP8266_ConnectYuanziyun ( yuanziyun_DeviceID, yuanziyun_DevicePassWord ) ); Printf ("\r\n configure ESP8266 complete \r\n"); while ( 1 ) { value1++; value2++; sprintf ( cStr, "\r\nThe humidity: %d RH ,The temperature: %d C \r\n", value1, value2 ); printf ( "%s", cStr ); // Print data ESP8266_SendString (ENABLE, cStr, 0, Single_ID_0); // Send information to atomic cloud in pass-through mode Delay_ms (1500); if ( strEsp8266_Fram_Record .InfBit .FramFinishFlag ) { USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, DISABLE ); // Disable serial port receive interrupt stRESP8266_fram_record.data_rx_buf [stresp8266_fram_record.infbit.framLength] = '\0'; // Add the terminator printf ("\r\n%s\r\n", stresp8266_fram_record.data_rx_buf) to the end of the received data; STRSTR is a C function that returns the address of the first occurrence of a substring in a string. If ((pCh = STRSTR (stRESP8266_fram_record.data_rx_buf, "LED on"))! = 0 ) { LED1_ON; Else if ((pCh = STRSTR (stRESP8266_fram_record.data_rx_buf, "LED off"))! = 0 ) { LED1_OFF; Else if ((pCh = STRSTR (stresp8266_fram_record.data_rx_buf, pCh = STRSTR (strEsp8266_Fram_Record. "led toggle" ) ) ! = 0 ) { LED1_TOGGLE; } strEsp8266_Fram_Record .InfBit .FramLength = 0; strEsp8266_Fram_Record .InfBit .FramFinishFlag = 0; USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, ENABLE ); // Enable serial port receive interrupt}}}Copy the code

Two variables value1 and Value2 are used to simulate the temperature and humidity values collected by the MCU and send data to the cloud. Add 1 to these two variables every time data is sent. After receiving data from the cloud through serial port 3, print the received content through debugging port serial port 1. If it is “LED on”, it will turn on the LED; if it is” LED OFF “, it will turn off the LED; if it is “LED toggle”, it will flip the LED state. Finally, clear the serial port receive flag.

Through the above steps can realize the communication between the microcontroller and the mobile phone client, also can directly control the microcontroller through the mobile phone. If the microcontroller is connected to the home appliance, it can also directly control the home appliance through the mobile phone.