Through the ESP8266WIFI module, 51 MCUs can exchange data to the back end

What I did this time was to use the ESP8266WIFI module to allow the 51 microcontroller to interact with the back-end data. Simply sent a string.
Module connection section: VCC power supply connected, GND grounding module TXD connected MCU RXD , module RXD connected MCU TXD .
It should be noted that RXD and TXD cannot be connected before the program is burned into the microcontroller.
It is recommended to use XCOM to send instructions to verify whether the WIFI module can be used normally before burning the code. Otherwise, it is useless to debug the code of the single-chip computer all the time. The following code realizes that the single-chip computer sends data to the back end through the WIFI module.

#include <reg52.h>
#include <string.h>
#include <intrins.h>
typedef unsigned char u8;
typedef unsigned int u16;
unsigned char Usart_Receive[20]={0};
unsigned char Usart_Cnt=0;
bit Usart_AT_flage;
bit flag;
u8 dat;
 
void Init(void)//初始化串口 
{
	TMOD = 0x20;
    SCON = 0x50;
    TH1 = 0xFD;
    TL1 = TH1;
    PCON = 0x00;
    EA = 1;
    ES = 1;
    TR1 = 1;
	REN=1;
}
 
void delay5ms(void)  //延时函数 
{
    unsigned char a,b;
    for(b=15;b>0;b--)
        for(a=152;a>0;a--);
}
 
void delay1s(void)   //延时 
{
    unsigned char a,b,c;
    for(c=13;c>0;c--)
        for(b=247;b>0;b--)
            for(a=142;a>0;a--);
    _nop_();  //if Keil,require use intrins.h
}
 
void Sent_ZF(u8 dat) //串口发送字符 
{
	ES = 0;
	TI=0;
	SBUF = dat;
	while(!TI);
        TI = 0;
        ES = 1;
}
 
void AT_Send_String(u8 *string) //串口发送字符串 
{  while(*string)
  {
    Sent_ZF(*string++);
		delay5ms();
  }
}
 
void ESP8266_Init()//用串口向ESP8266发送命令 
{
	while(1)       
	{
		AT_Send_String("AT\r\n");
		delay1s();
		delay1s();
		if(Usart_AT_flage ==1)
		{
				Usart_AT_flage = 0;
				break;
			}	
		}
	
	while(1)
	{
		AT_Send_String("AT+CWMODE=1\r\n");   //设置为模式1 
		delay1s();
		delay1s();
		if(Usart_AT_flage ==1)
		{
				Usart_AT_flage = 0;
				break;
			}	
		
	}
	while(1)
	{
		AT_Send_String("AT+CWJAP=\"mumn\",\"123456789\"\r\n");	  //连接网络 ,输入自己的网络名的密码
		delay1s();
		delay1s();
		if(Usart_AT_flage ==1)
		{
				Usart_AT_flage = 0;
				break;
			}	
		}
	
	while(1)
	{
		AT_Send_String("AT+CIPSTART=\"TCP\",\"192.168.43.177\",8080\r\n");	//作为客户端	
		delay1s();
		delay1s();
		delay1s();
		if(Usart_AT_flage ==1)
		{
				Usart_AT_flage = 0;
				break;
			}	
		}
	while(1)
	{
		AT_Send_String("AT+CIPMODE=1\r\n");		
		delay1s();
		delay1s();
		delay1s();
		if(Usart_AT_flage ==1)
		{
				Usart_AT_flage = 0;
				break;
			}	
		}	
	
		while(1)
	{
		AT_Send_String("AT+CIPSEND\r\n");		
		delay1s();
		delay1s();
		delay1s();
		if(Usart_AT_flage ==1)
		{
				Usart_AT_flage = 0;
				break;
			}	
		}	
}	

void sendnumber()
{
	while(1)
	{
   AT_Send_String("12345");		
   delay1s();
		delay1s();
		delay1s();
		if(Usart_AT_flage ==1)
		{
				Usart_AT_flage = 0;
				break;
		}	
	}	
}

 
void main()
{
    Init();
    ESP8266_Init();
    sendnumber();
    while(1);
}
 
void InterruptUART(void) interrupt  4  //串口中断 
{
	if(RI)
	{
		RI=0;
		Usart_Receive[Usart_Cnt]=SBUF;
		Usart_Cnt++;
		if(Usart_Receive[Usart_Cnt]=='\0')   
		{
			Usart_Cnt=0;
			Usart_AT_flage=1;			
		}
	}
	else
		TI=0;
}

Write a JAVA service program on the computer, run it and wait for the connection of the client, then burn the above code into the 51 single-chip microcomputer, you can see that the data is successfully sent

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Service {
     public static void main(String[] args) throws Exception {
            ServerSocket server = new ServerSocket(10012);
            byte[] msg = new byte[50];
            System.out.println("服务端ready,,,,");
            Socket client = server.accept();
            InputStream in = new DataInputStream(client.getInputStream());
            boolean accept = true;
            while (accept) {
                in.read(msg);
                System.out.println(new String(msg));
            }
            in.close();
            client.close();
            server.close();
        }
}
Published 9 original articles · won 7 · visited 1767

Guess you like

Origin blog.csdn.net/weixin_44906810/article/details/103570525