Establishing a connection (FX) and a local network server based on the unusual WIFI module (ESP8266)

Original Address: https: //blog.csdn.net/ludaoyi88/article/details/62236644

Case Description:

In WIFI module (ESP8266) for the development of the object, in the local area network in the WIFI module, connected to a TCP server to another local area network, forming a communication between the server and the WIFI module. This article first computer-controlled connection WIFI module, using the familiar AT commands, and then by means of programming by the microcontroller to control WIFI module, in-depth study.


A. Debugging computer terminal, connected to the control module WIFI

Description: The WIFI module through the serial port connected to the computer, the computer sends AT commands to the module via serial debugging software, WIFI control module establishes a connection with the other servers in the LAN.


step:

1. Required conditions:

1) two PC, a router, a network computer can ensure, and network routers, and are connected in a different LAN, (Weapon: no WIFI router can start by a computer as a router, but make sure within two LANs. no two LANs, you can use the phone open wifi hotspots to provide computer network, this experiment is not very cost phone traffic, do not worry)

2) networked computer (B computer) installation peanut shells software (note with a penetrating version), stay on the line to sign.

3) opening a network in the computer software debugging assistant peanut shells (B PC), another computer (A PC) Open serial debugging software.

4) computer software debugging serial port (A PC), connected by USB cable to TTL and WIFI module.

Preparatory work in the following figure:
peanut shells software:
Write pictures described here

Network debugging aids:
Write pictures described here
serial debugging assistant:
Write pictures described here

wifi computer serial port module (only TX, RX, VCC, GND pin)
Write pictures described here


2. WIFI module is connected to the router

Assistant wifi module to send AT commands through serial debugging, control module.

1) Reset WIFI module, command: AT + RST ( after sending instructions note that there must wrap, empathy below )

Instructions:
Write pictures described here

Instructions to achieve:
Write pictures described here

1)选择模式,指令:AT+CWMODE=3
指令说明:
Write pictures described here
指令实现:
Write pictures described here
1)连接到路由器,指令:AT+ CWJAP =”ldy”,”99999999”(路由器名称和密码,只能是非中文名称)
指令说明:
Write pictures described here
指令实现:
Write pictures described here


3. 模块与其他局域网服务器建立TCP连接

1)在B电脑上开启花生壳和网络调试助手,其中花生壳被映射的地址应为电脑本机IP地址,端口任意。网络调试助手上的IP和端口应该设置为被花生壳所映射的IP和端口。IP被花生壳映射到外网域名和端口号,是将要被WIFI模块连接的外网地址。如图:
Write pictures described here
2)启动单连接,指令:AT+ CIPMUX =0
指令说明:
Write pictures described here
指令实现:

Write pictures described here

3)连接到TCP服务器,

AT+CIPSTART=”TCP”,”14z95r6389.iask.in”,35447(改指令可以通过域名和端口号去连接,也可以通过IP和端口号连接,由于被穿透后是域名,故采用域名形式连接)
指令描述:
Write pictures described here
指令实现:
Write pictures described here

4)发送消息 AT+CIPSEND=5(5指的要发送消息的长度)
指令说明:
Write pictures described here
指令描述:
WIF模块端发送:
Write pictures described here

服务器端接收:
Write pictures described here

5)接收服务器(接收到消息会有“+IPD”的数据头)
说明:
Write pictures described here
接收实现:(接收到服务器端发的”zzz”字符)
Write pictures described here



二.单片机实现控制WIFI模块与服务器连接

Realized from the above computer terminal, after connecting to the router WIFI module, re-establishing the connection to the server, it is achieved by AT commands, the purpose of debugging and familiar with AT commands. Then it can be relatively easily controlled by the microcontroller WIFI module, the AT command transmitted by writing the program.

Experiment: MCU control WIFI module, transmits to the server 7 a string of length "abcdefg"


1. Hardware conditions

STM32 based microcontroller, is connected via the WiFi module USART3.


2. The program core code

I have written a send AT commands wrapper function provides flexible timing of each instruction and waiting times retransmission function. Can greatly improve the stability of the module, the main code is now attached for reference.

The main function is as follows:

int main(void)
{   //通过域名形式连接到服务器
     char *string 
         ="AT+CIPSTART=\"TCP\",\"14z95r6389.iask.in\",35447"; 
    char *Seddstr = "abcdefg"; //被发送的字符串

    ALL_init();//初始化各种外设

    LED_ALL2_ON;
    delay_ms(1000);
    WIFI_AT_Command( "AT+RST","ready",5,2,ENABLE);
    delay_ms(1000);

    if(Send_wifiAT("AT+CWSAP?")==0)//判断是否连接上路由器
    {
        WIFI_AT_Command("AT+CWMODE=3","OK",3,2,DISABLE); //STATION兼AP模式
              WIFI_AT_Command("AT+CWJAP=\"ldy\",\"99999999\"","CONNECTED",10,3,DISABLE); //连接的到路由器,需要等待较长时间

    }   

    WIFI_AT_Command("AT+CIPMUX=0","OK",3,2,DISABLE);    //单连接

    WIFI_AT_Command((char*)string,"OK",5,3,ENABLE);
    //连接到服务器
    SendDATA_wifi(Seddstr,strlen(Seddstr));
   //发送数据
    while(1)
    {

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

AT commands sent from the write wrapper functions as follows :( wrapper function, for users transplant)

/*
函数:WIFI_AT_Command
功能:(智能化发送指令)在规定时间内,可多次重发某个指令,多次发送失败可复位
参数:b:发送内容,a:等待接收内容,timeout_S:发单次内最多允许等待          的时间,ReSendcount:最多可以重复发几次(失败情况),EN_Reset:如果失败是否复位机器。

返回: 成功回1  失败回0
*/

char WIFI_AT_Command(char *b,char *a,u8 timeout_S , char ReSendcount,FunctionalState EN_Reset)
{
    u16 count =0;
  u8 i = 0;

    WaitACKflag = 1;  //WaitACKflag用于防止重入函数  strstr

    CLR_Buf3();
    ACK_Command = a;

    for(i =1 ; (i<= ReSendcount && WaitACKflag); i++) //若失败 ,重复发送ReSendcount次
    {
        CLR_Buf3(); 
        USART_Puts(USART3 ,b);
        USART_Puts(USART3 ,"\r\n");


        while(WaitACKflag) //判断在中断里,等待变成0
        {
            count ++;
            delay_ms(5);    
            if(count >200*timeout_S) //最多允许等待timeout_S S
            {
                count=0;
                break;
            }

        }
    }

    ACK_Command="";
    WaitACKflag =0;

    if(count == 0 && i == ReSendcount) //重复发送ReSendcount次,还没接收到响应指令(都失败)
    {
        if(EN_Reset == ENABLE)//如果使能了复位模式
        {
            __set_FAULTMASK(1);      // 关闭所有中端
            NVIC_SystemReset();     //软件直接复位(重来)            

        }
        return 0;
    }

    return 1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

Encapsulating transmission data to the server functions:

/**
函数:SendDATA_wifi
功能:给服务器发送数据
参数:a:发送的数据   length:发送的长度
返回: 
**/
void SendDATA_wifi(char *a ,u16 length)
{
    char Buf[30] = "";

    sprintf(Buf,"AT+CIPSEND=%d",length);

    WIFI_AT_Command(Buf,">",3,2,ENABLE);
    WIFI_AT_Command(a,"SEND OK",4,2,ENABLE);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

USART3 serial port receive interrupt function:

/**
 函数名  : USART3_IRQHandler
 描述    : 串口3中断服务程序
 输入    : 无
 返回    : 无 
说明    : 
**/
void USART3_IRQHandler(void)                    
{
    u8  ch=0;

    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
    {       
            USART_ClearITPendingBit(USART3, USART_IT_RXNE);
            ch = USART_ReceiveData(USART3);


            Wifi_RXbuf[Wificount++] =(char)ch;

            if(ch == '\n' || Wificount>=Uart3bufMAX)//防止超限
            {
                Wificount =0;
            }


            if(WaitACKflag ==1) //将判断strstr函数写在中断里,WaitACKflag用于防止重入函数  
            {
                if(strstr(Wifi_RXbuf,ACK_Command)!=NULL)
                {
                    WaitACKflag=0;
                }

            }
            else
            {
                Judge_UART3_box();//对接收服务器接收消息的处理
            }


    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

Receiving a server message function:

/**
函数:Judge_UART3_box
功能:接收服务器消息判断
参数:
返回:
**/
void Judge_UART3_box()
{
    if(strstr(Wifi_RXbuf,"+IPD")!=NULL)
    {
        //用户自行定义

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3. experimental results

Server may receive the data of FIG :( WIFI module server may send data to a WIFI module controls the microcontroller is executing the receive function package also good in the above code, interest related experiments can be done)
Write pictures described here

Guess you like

Origin www.cnblogs.com/pacexdong/p/11478929.html