ESP8266WIFI module AT configuration and experiment on STM32F103

Table of Contents

1. Pin description:

2. Related AT commands

1. Test command: Send: AT

2. Restart the module: send: AT + RST

3. Set the module working mode 1: STA mode 2: AP mode 3: STA and AP mode. Example: Set the module to STA and STA mode. Send: AT + CWMODE=1

4. Let the module list the list of wireless routers in the current environment. Send: AT + CWLAP

?edit

5. Let the module connect to its own router and send: AT + CWJAP=”www.zniot.com”,”littlebee” (where littlebee is the password)

6. Check whether it is really connected. Send: AT + CWJAP?

7. Start module single connection: send: AT + CIPMUX=0

8. Connect to our remote TCP server X1.X2.X3.X4 as the IP address 9999 as the port and send: AT + CIPSTART=”TCP”,”

9. The module sends data to the server. Send: AT + CIPSEND=4,15 (Note: 15 is the length of the sent data set by yourself. Only send if it is >= 15. Otherwise, the total will be 15 before sending. If it is greater than 15, the previous data will be intercepted.)

10. Turn on the local TCP server of the module (1 means turning on, if set to 0, turn off 8888: to listen on the port) ※Send: AT + CIPSERVER=1,8888 Result:

?Edit and check the module IP address: ※ Send: AT + CIFSR=? ※ result

?Try on the edit connection:

3. stm32f103 connection server code


1. Restart the module:
Send: AT + RST “>

1. Pin description:

VCC: positive pole of power supply, connect to 3.3-5V

GND: negative pole of power supply

TXD: data transmission pin

RXD: data reception pin

RST: reset pin (active low level)

I0-0: Firmware programming mode, low level programming, high level operation

1. Test command: Send: AT

2. Restart the module: send: AT + RST

3. Set the module working mode 1: STA mode 2: AP mode 3: STA and AP mode
For example: Set the module to STA and STA mode
Send: AT + CWMODE=1

4. Let the module list the list of wireless routers that exist in the current environment and send: AT + CWLAP

5. Let the module connect to On your own router
Send: AT + CWJAP=”www.zniot.com”,”littlebee” (where littlebee is the password)

6. Check whether the connection is really connected. Send: AT + CWJAP?

7. Start module single connection: send: AT + CIPMUX=0

8. Connect to our remote TCP server. X1.X2.X3.X4 is the IP address and 9999 is the port.
Send: AT + CIPSTART=”TCP”,” X1.X2.X3.X4″,9999

10. Turn on the module Local TCP server
(1 means open. If set to 0, it will close 8888: the port to be monitored)
※Send: AT + CIPSERVER=1,8888
Result:


Check the module IP address:
※ Send: AT + CIFSR=?
※ Result


Try connecting:

3. stm32f103 connection server code

#include "./BSP/ESP8266/esp8266.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/TIME/time.h"
#include "string.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "./BSP/TIME/time.h"

uint8_t AT_CMD[]="AT\r\\
";//Test
uint8_t AT_REST[]="AT + RST\r\\
";//Reset
uint8_t AT_CWAUTOCONN[]="AT + CWAUTOCONN=0\r\\
";//Turn off automatic connection
uint8_t AT_WIFI_CONNECT[]="AT + CWJAP_DEF="TP-LINK_226","226226226"\r\\
";//Connect to wifi
uint8_t AT_CIPMUX[]="AT + CIPMUX=0\r\\
";//Configure a single connection
uint8_t AT_CWMODE[]="AT + CWMODE=1\r\\
";//Set sta mode
//Server iot.100ask.net
uint8_t AT_TCP_CONNECT[]="AT + CIPSTART="TCP","47.114.187.247",1883\r\\
";//TCP connection
uint8_t AT_CIPMODE[]="AT + CIPMODE=1\r\\
";//Turn on transparent transmission
uint8_t AT_CIPSEND[]="AT + CIPSEND\r\\
";//Start transparent transmission
uint8_t AT_EXIT[]=" + + + ";//Disconnect transparent transmission
uint8_t AT_CIPCLOSE[]="AT + CIPCLOSE\r\\
";//Close the tcp connection

uint8_t open_send_flag=0;//Transparent transmission open flag
uint8_t wifi_success=0;//TCP connection flag

//Determine whether to respond ok 1: OK, 0: Failure
uint8_t esp8266_ack_ok(uint16_t time_out)
{
    uint8_t temp=0;
    uint8_t buf[4];
    uint16_t now_time=time6_over;
    while(1)
    {
        if(!is_rt_buf_empty())
        {
            temp=get_rt_buf();
            if(temp=='O')//Judge whether it is OK
            {
                delay_ms(10);
                temp=get_rt_buf();
                if(temp=='K')
                {
                    printf("OK\r\\
");
                    delay_ms(time_out);
                    r_flag=w_flag;
                    return 0;
                }
            }
            if(temp=='E')
            {
                delay_ms(10);
                for(int i=0;i<4;i + + )
                {
                    buf[i]=get_rt_buf();
                }
                //printf("buf=%s#\r\\
",buf);
                if(buf[0]=='R' & amp; & amp;buf[1]=='R' & amp; & amp;buf[2]=='O' & amp; & amp;buf[3]=='R')
                {
                    //printf("buf=%s\r\\
",buf);
                    printf("ERROR\r\\
");
                    delay_ms(10);
                    r_flag=w_flag;
                    return 1;
                }
            }
            //printf("%c",temp);
        }
        if(time6_over-10>(now_time))
        {
            printf("TIME OVER\r\\
");
            break;
        }
    }
    return 1;
}

void esp01s_read(void)
{
    uint8_t temp=0;
    while(!is_rt_buf_empty())//Determine whether the buffer has data
    {
        temp=get_rt_buf();
        printf("%c",temp);
    }
}

//wifi configuration 1: failure, 0: success
uint8_t wifi_config(void)//mode:0: outside the task, 1: inside the task
{
    //Reset
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_REST,strlen((char *)AT_REST),1000);
    printf("reset:");
    if(esp8266_ack_ok(1000))
        return 1;
    else
    {
        wifi_success=0;
        open_send_flag=0;
    }
    
    //Set sta mode
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_CWMODE,strlen((char *)AT_CWMODE),1000);
    printf("set sta mode:");
    if(esp8266_ack_ok(10))
        return 1;
    
    //Connect to wifi
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_WIFI_CONNECT,strlen((char *)AT_WIFI_CONNECT),1000);
    printf("connect wifi:");
    if(esp8266_ack_ok(10))
        return 1;

    //Set up a single connection
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_CIPMUX,strlen((char *)AT_CIPMUX),1000);
    printf("set Single-way connect:");
    if(esp8266_ack_ok(10))
        return 1;
    
    //Turn on transparent transmission
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_CIPMODE,strlen((char *)AT_CIPMODE),1000);
    printf("open Transparent transmission mode:");
    if(esp8266_ack_ok(10))
        return 1;

    //Connect TCP
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_TCP_CONNECT,strlen((char *)AT_TCP_CONNECT),1000);
    printf("connect TCP:");
    if(esp8266_ack_ok(10))
        return 1;
    else
        wifi_success=1;
    return 0;
}

//Start transparent transmission
uint8_t wifi_open_cipsend(void)
{
    //wifi is nearly connected
    if(wifi_success & amp; & amp; open_send_flag!=1)
    {
        //Start transparent transmission
        HAL_UART_Transmit( & amp;g_uart3_handle,AT_CIPSEND,strlen((char *)AT_CIPSEND),500);
        printf("start Transparent transmission:");
        if(!esp8266_ack_ok(10))
            open_send_flag=1;
        else
            return 1;
        return 0;
    }
    return 1;
}

//Close transparent transmission
void wifi_close_cipsend(void)
{
    if(wifi_success & amp; & amp; open_send_flag==1)
    {
        //Close transparent transmission
        HAL_UART_Transmit( & amp;g_uart3_handle,AT_EXIT,strlen((char *)AT_EXIT),500);
        printf("clsoe Transparent transmission:");
        delay_ms(100);
        HAL_UART_Transmit( & amp;g_uart3_handle,AT_CMD,strlen((char *)AT_CMD),500);
        if(!esp8266_ack_ok(10))
            open_send_flag=0;
    }
    else
        printf("Transparent transmission not open,no need close\r\\
");
}

//Close the connection, return 0 for success, 1 for failure
uint8_t wifi_close_connect(void)
{
    taskENTER_CRITICAL();/* Enter critical section */
    delay_ms(100);
    //Close transparent transmission
    wifi_close_cipsend();
    //Close the connection
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_CIPCLOSE,strlen((char *)AT_CIPCLOSE),1000);
    printf("close TCP connect:");
    if(!esp8266_ack_ok(10))
    {
        open_send_flag=0;
        wifi_success=0;
    }
    else
    {
        taskEXIT_CRITICAL(); /* Exit critical section */
        return 1;
    }
    taskEXIT_CRITICAL(); /* Exit critical section */
    return 0;
}

//Close after reset
//Close the connection, return 0 for success, 1 for failure
uint8_t wifi_reclose_connect(void)
{
    //Close transparent transmission
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_EXIT,strlen((char *)AT_EXIT),500);
    printf("close Transparent transmission,disconnect:");
    delay_ms(100);
    HAL_UART_Transmit( & amp;g_uart3_handle,AT_CIPCLOSE,strlen((char *)AT_CIPCLOSE),1000);
    delay_ms(300);
    open_send_flag=0;
    wifi_success=0;
    esp01s_read();
    return 0;
}
extern SemaphoreHandle_t wifi_xsemaphor_handle;
int wifi_write(uint8_t *buf,int len)
{
    if(wifi_success & amp; & amp; open_send_flag==1)
    {
        xSemaphoreTake(wifi_xsemaphor_handle,portMAX_DELAY);//Add mutex lock, portMAX_DELAY wait
        HAL_UART_Transmit( & amp;g_uart3_handle,buf,len,100);//Send data
        xSemaphoreGive(wifi_xsemaphor_handle);//Unlock the mutex
        return 0;
    }
    else
        printf("send error,no connect or no open Transparent transmission\r\\
");
    return 1;
}

int wifi_read_byte(uint8_t *c,uint16_t timeout)
{
    uint8_t temp;
    uint16_t now_time=time6_over;
    if(wifi_success & amp; & amp; open_send_flag==1)
    {
        while(1)
        {
            if(!is_rt_buf_empty())//The ring buffer is not empty
            {
                temp=get_rt_buf();//Get a byte
                *c=temp;
                return 0;
            }
            if((time6_over-now_time)>timeout)
                return 1;//timeout
        }
    }
    else
        printf("read error,no connect or no open Transparent transmission\r\\
");
    return 1;
}