Intelligent desk lamp offline voice recognition solution chip Jie Li Bluetooth IC intelligent AI language remote control solution PCBA

C language implements WiFi communication – STM32 wifi communication

C language implements WiFi communication – STM32 wifi communication

ESP8266 Project – ESP8266 WiFi Module – ESP8266 WiFi Communication Technology

C language implements WiFi communication, and STM32 implements simple WiFi communication

C language implements WiFi communication
Recently, I need to do wifi communication experiments, there are many pits in the experiment, and this article is used to record the problems encountered in the experiment.C language wifi - C language implements WiFi communication - STM32 wifi communication

This article only implements a simple WiFi connection function, and recently encapsulates a more complete WiFi protocol, which can realize WiFi connection, server connection, encapsulation of sending data, parsing and receiving data, server creation and other functions

C language implementation of WiFi communication experimental content:ESP8266 project program project file download

Esp8266 projects – Esp8266 wifi module – ESP8266 WiFi communication

Learn the basic working principle of ESP8266, enabling the program to connect to a given WIFI hotspot and server on its own and achieve two-way communication.wifi bluetooth dual-mode smart home graffiti bluetooth wifi two-in-one module voice control camera module

ESP8266 project program project file download:

The overall idea of the C language WiFi communication experiment:

By directly controlling the wifi module, it can be found that the whole connection process has several instructions that are indispensable:

AT test,

AT+CWMODE=1 to set the working mode,

AT+CWJAP=”Hotspot Name”, “Hotspot Password”

AT+CIPSTART=”TCP”, “Server Address”, Port Number

In addition, for ease of testing, ATE1 also turns on echo.

Write the wifi_init() function according to the above command, first send “AT” to the wifi module, send “AT+CWMODE=1” after receiving OK, and send 3 and 4 commands at a time after receiving OK to detect whether OK is received at the same time.C language wifi module

If no OK is detected after any of the above links exceed the timeout period, the connection fails, the next connection is made, and an error message is returned after three consecutive connection failures.

Code parsing

WiFi module parameter configuration:

#define WIFI_NAME "hello_world" // Name of the hotspot to be connected to

#define WIFI_PWD "87654321" // hotspot password

#define SERVER_ADDR "192.168.43.73" // server IP address

#define SERVER_PORT "5656" // port number

This code snippet is line 4-7 in my_func.h, change the parameters to your own hotspot and server information.

WiFi initializer:

int wifi_init()

{

uint8_t ret=0;

ret = Send_AT_commend("AT", "OK", 100);

if(!ret)

return -1;

#ifdef DEBUG

Send_AT_commend("ATE1", "", 100);

#else

Send_AT_commend("ATE0", "", 100);

#endif

ret = Send_AT_commend("AT+CWMODE=1", "OK", 100);

if(!ret)

return -2;

wifi_str();

ret = Send_AT_commend(temp, "OK", 9000); AT+CWJAP="pxc002","?????"

if(!ret)

return -3;

server_str();

ret = Send_AT_commend(temp, "OK", 3000);//AT+CIPSTART="TCP","192.168.31.32",3456

if(!ret)

return -4;

return 1;

}

The code between #ifdef and #endif is the echo of the switch AT command, which has no effect on the program function after removal.Intelligent desk lamp offline voice recognition solution chip Jie Li Bluetooth IC intelligent AI language remote control solution PCBA

Send AT command function:

uint8_t Send_AT_commend(char *at_commend, char *re_commend, uint16_t time_out)

{

uint8_t i=0;

for(i=0; i<3; i++)

{

clear_buf();// Clears the receive array

HAL_UART_Transmit(&huart1, (uint8_t *)at_commend, strlen(at_commend), 0xFFFF);

HAL_UART_Transmit(&huart1, (uint8_t *)"rn", 2, 0xFFFF);// Send carriage return line feed

HAL_Delay(time_out);

if(find_str(re_commend))

return 1;

i++;

}

return 0;

}

 

This function receives three parameters: the AT command to be sent, the expected return data, and the timeout period.

Before sending the AT command, clear the my_re_buf1 to prevent misjudgment of the previous data, then send the AT command and send a carriage return line feed, delay time_out, through the find_str() function to find whether the development board received the OK sent by the wifi module, and return 1 if detected, otherwise i++ sends it again, and returns 0 if it is not detected three times.

Timer interrupt callback:

while(send_buf[i])send_buf[i++]=0x00;//empty the send_buf array

while(pt_r2

Comment out the program in the demo and change it to your own so that the PC can send data to the server through the board. When pt_r2 send data function:

void wifi_send(uint8_t *buf, int len)

{

char len_str[]="",temp1[256]="";

itoa(len, len_str);

strcat(temp1,"AT+CIPSEND=");

strcat(temp1,len_str);

HAL_UART_Transmit(&huart2, (uint8_t *)"Send datarn", 10, 0xFFFF);

if(Send_AT_commend(temp1, ">", 300))

if(Send_AT_commend((char *)buf, "SEND OK", 300))

HAL_UART_Transmit(&huart2, (uint8_t *)" sent successfully!!! rn", 16, 0xFFFF);

}

This function receives two parameters, an array of strings to be sent, and the number of bytes sent.

Convert the integer len to a string via the tioa() function, and concatenate temp1 to AT+CIPSEND=len via strcat. Send_AT_commend() sends the string temp1, sends the string buf after receiving “>”, and sends “SEND OK” successfully.

strstr() function:

The strstr(s1,s2) function receives two string arguments, the specific function is to detect whether s2 is a subset of s1, if so, return the address of the first occurrence of s2, otherwise return NULL. But there is a hard bug in this function, if s1[]={0x00, ‘a’, ‘b’, ‘c’} s1[]={‘a’}, strstr(s1,s2) returns NULL after execution, that is, the end flag of strstr function execution is the 0x00 of the s1 array. This bug plagued me for more than two hours!!!

Pointer value transfer problem:

In C language programs, you need to calculate the length of the string array s usually use the sizeof or strlen function, but if char *p=s, sizeof§ and strlen§ get data is not the length of s, the specific explanation is that although p points to the first address of the s array, p does not contain the data behind the s array, because the above two functions cannot obtain the length of s through the pointer p.C language implements wifi module communication technology

summary

It is really important to have a solid grasp of the basic knowledge of the C language, otherwise I will encounter many very mentally retarded bugs like me.

Since this project is written using fragmentary idle time and relatively hasty, and there is not enough experimental verification, so the project has some hidden and unsolved bugs, such as always losing the first byte when sending data (but adding a space before sending data can solve it perfectly, as for why I don’t know, I’m too lazy to solve it) (This bug has been solved). Therefore, engineering is only for learning and communication, not for production.

Leave a Reply

Your email address will not be published. Required fields are marked *