51 microcontroller connects to ESP8266 serial port WiFi module

Publisher:dadigtLatest update time:2023-06-15 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  51 microcontroller is the collective name for all microcontrollers compatible with the Intel 8031 ​​instruction system. The ancestor of this series of microcontrollers is Intel's 8004 microcontroller. Later, with the development of Flash rom technology, the 8004 microcontroller made great progress and became one of the most widely used 8-bit microcontrollers. Its representative model is the AT89 series of ATMEL Company. It is widely used in industrial measurement and control systems. Many companies have launched compatible models of the 51 series, which will occupy a large market for a long time in the future. The 51 microcontroller is a basic entry-level microcontroller, and it is also the most widely used one. It should be noted that 51 series microcontrollers generally do not have self-programming capabilities.


  51 microcontroller difference

  The same program will produce the same result when running on the hardware of various microcontroller manufacturers, such as ATMEL's 89C51 (discontinued), 89S51, PHILIPS, and WINBOND, etc. The 89C51 that we often say has been discontinued refers to ATMEL. The AT89C51 microcontroller has enhanced many features on the original basis, such as clock. What's even better is that the original ROM (one-time write) is replaced by Flash (the contents of the program memory can be rewritten at least 1000 times). The performance of AT89C51 It is very superior to 8051.


  However, in terms of marketization, 89C51 has been challenged by the PIC microcontroller camp. The most fatal flaw of 89C51 is that it does not support ISP (online update program) function. New functions such as ISP function must be added to better continue the legend of MCS-51.

  It is against this background that 89S51 replaced 89C51. 89S51 has become the new darling in the practical application market. Atmel, which has the largest market share, has discontinued AT89C51 and will replace it with AT89S51. 89S51 has been improved in the process. 89S51 adopts 0.35 new process, which reduces the cost and improves the functions to increase its competitiveness. 89SXX is backward compatible with 51 series chips such as 89CXX. At the same time, Atmel no longer accepts orders for 89CXX. The 89C51 you see on the market is actually a huge inventory of Atmel's early production. If the market demands it, Atmel can of course resume production of AT89C51.


  51 microcontroller connects to ESP8266 serial port WiFi module

  Pin connection

  51 microcontroller connects to ESP8266 serial port WiFi module

  Burn firmware

  Open the burning software, select the configuration tab, and click the small gear icon in the second column - load the firmware address

  51 microcontroller connects to ESP8266 serial port WiFi module

  Click the selection bar for other options, uncheck it, and select only the second option (firmware address)

  51 microcontroller connects to ESP8266 serial port WiFi module

  Return to the operation tab, select the correct COM port, and click one-click programming

  The module is powered on again, the download starts, and waits for the download to complete.

  

  Serial port assistant tests ESP8266 serial port WiFi module

  Pin connection

  51 microcontroller connects to ESP8266 serial port WiFi module

  Note: At this time, the connection status is WiFi module - "51 microcontroller -" serial port assistant (PC). Since the TXD and RXD of the WiFi module and the microcontroller are in a positive connection state, the microcontroller and the WiFi module do not communicate, which is equivalent to the WiFi module being directly connected to the serial port assistant.

  AT common commands

  The ESP8266 serial WiFi module is divided into three working modes: Station mode (similar to a wireless terminal), AP mode (providing wireless access services), and AP mode and Station mode.

  When testing the WiFi module through the serial port assistant,

  test

  Send command: AT

  Response: OK

  Restart module

  Send command: AT+RST

  Response: OK

  Setup module

  Send command: AT+CWMODE = "mode"

  Note: It takes effect after restarting (AT+RST)

  "mode": 1-Station mode, 2-AP mode, 3-AP and Station mode.

  Response: OK

  Configure AP parameters

  send command:

  Command: AT+ CWSAP= "ssid", "pwd", "chl", "ecn"

  Note: The command is only valid after the AP mode is turned on.

  "ssid": string parameter, access point name

  "pwd": string parameter, password up to 64 bytes, ASCII

  "chl": channel number

  "ecn": encryption mode, 0-OPEN, 1-WEP, 2-WPA_PSK, 3-WPA2_PSK, 4-WPA_WPA2_PSK

  Example: AT+CWSAP="TEST","123456123456",1,3

  Response: OK

  Turn on multi-connection mode

  Send command: AT+CIPMUX=《mode》

  Description: "mode": 0-single connection mode, 1-multiple connection mode

  Response: OK

  Create server

  Send command: AT+CIPSERVER=《mode》,《port》

  Note: The server can be turned on only when AT+ CIPMUX=1; turning off the server mode requires a restart. After opening the server, server monitoring is automatically established. When a client accesses, a connection will be automatically occupied in sequence.

  "mode": 0-turn off server mode, 1-turn on server mode

  "port": port number, the default value is 333

  Response: OK

  Initialize WiFi module through 51 microcontroller

  By testing AT commands through the serial port assistant, you can find that some AT commands will not be saved when the power is turned off, so initialization settings need to be made in the code.

  Pin connection

51 microcontroller connects to ESP8266 serial port WiFi module

  Note: At this time, the connection status is WiFi module - "51 microcontroller -" serial port assistant (PC). Since the TXD and RXD of the WiFi module and the microcontroller are in the positive connection state, the microcontroller and WiFi module can carry out serial communication.

  Implement code

  #include《reg52.h》 //Commonly used header files for 51 microcontrollers

  #define uchar unsigned char //Macro defines an unsigned char type

  #define uint unsigned int //Macro defines an unsigned int type

  //Send one byte

  void sendByte(float b)

  {

  SBUF = b;

  while (!IF);

  TI = 0;

  }

  //Send string

  void sendString(uchar *s)

  {

  while (*s != '') //The default end of the string is '', to determine the end of the string

  {

  sendByte(*s);

  s++;

  }

  }

  //Initialize ESP8266WiFi module

  void initEsp()

  {

  uint a;

  SCON = 0x50; //8-bit data, variable baud rate

  TMOD = 0x20; //Set the timer 1-bit 16-bit automatic reload mode

  TL1 = 0xfd; //Set the initial value of the timer, the baud rate is 9600

  TH1 = 0xfd;

  ET1 = 0; //Disable timer 1 interrupt

  TR1 = 1; //Start timer 1

  EA = 1;

  for (a=0; a<50000; a++); //Delay for a few seconds to allow the module time to start

  sendString(“AT+CWMODE=2”); //Set to softAP and station coexistence mode

  //WiFi hotspot cannot be set through code. You can use the serial port assistant to set it up. The settings will not be lost when the power is turned off.

  /*

  for (a=0; a《20000; a++);

  sendString("AT+CWSAP='TEST','12345678',1,3"); //Establish WiFi hotspot

  */

  for (a=0; a《50000; a++);

  sendString(“AT+CIPMUX=1”); //Start multiple connections

  for (a=0; a《20000; a++);

  sendString("AT+CIPSERVER=1,333");//Establish server, port is 333

  for (a=0; a《20000; a++);

  sendString(“AT+CIPSTO=50”); //Server timeout setting

  RI=0;

  ES=1; //Initialization completed, serial port interrupt is opened

  }

  //Main function

  void main()

  {

  initEsp();

  }

  Note: The baud rate in the code should match the baud rate of the serial port assistant, and some AT commands such as restarting the module and setting up WiFi hotspots cannot be used.

  Get data transmitted by WiFi

  As a TCP server, the ESP8266WiFi module will add +IPD, n, "string.length" by default before accepting information from the client: such characters should be paid attention to when processing.

  Implement code

  //Get data, data format example: +IPD, 0, 14: "time": "11:11"

  void getData()

  {

  uint a;

  if(receiveFlag)

  {

  for(i=0; i《2; i++)

  {

  Hour[i]=Buffer[17+i];

  }

  Hour[2]=‘’;

  for(i=0; i《2; i++)

  {

  Minute[i]=Buffer[20+i];

  }

  Minute[2]=‘’;

  //Send the obtained data to the serial port assistant for display

  for (a=0; a<10000; a++); //Delay is required, which may cause data confusion and packet loss.

  sendString(Hour);

  for (a=0; a《10000; a++);

  sendString(Minute);

  receiveFlag=0;

  count=0;

  for(i=0; i《22; i++)

  {

  Buffer[i]=0;

  }

  }

  }

  //Main function

  void main()

  {

  initEsp(); //Initialize WiFi module

  receiveFlag = 0; //receiveFlag determines the flag for executing getData()

  count = 0; //index of count buffer RXDdata[count]

  while(1)

  {

  getData();

  }

  }

  //Use interrupt to receive information and discard invalid information

  void uart() interrupt 4

  {

  if(RI == 1)

  {

  ES = 0; //Turn off serial interrupt

  RI = 0; //Clear the serial port receiving flag bit

  temp = SBUF; //Get data from the serial port buffer

  if(count<20) //Satisfy the length of information to be received and store the data in the buffer

  {

  Buffer[count]=temp;

  count++;

  if (Buffer[0]=='+') //Determine whether it is invalid data, because the WiFi module will automatically add a string starting with "+PID.."

  {

  receiveFlag = 1;

  }

  else

  {

  receiveFlag = 0;

  count = 0;

  }

  }

  ES = 1;

  }

  }


Reference address:51 microcontroller connects to ESP8266 serial port WiFi module

Previous article:Design and implementation of conductive material temperature measurement system
Next article:Design of temperature acquisition and display system based on AT89S51 microcontroller and LM35 temperature sensor

Recommended ReadingLatest update time:2024-11-15 07:56

STM32 Learning Notes: ESP8266 Module (1)
Since the project required the Internet of Things, the company had to use wifi, so it purchased a serial port wifi module, ESP8266 The first thing you should do after getting it is to test and be familiar with AT commands, because serial port WiFi communication uses AT commands for communication. One thing to note her
[Microcontroller]
STM32 Learning Notes: ESP8266 Module (1)
51 microcontroller connects to ESP8266 serial port WiFi module
  51 microcontroller is the collective name for all microcontrollers compatible with the Intel 8031 ​​instruction system. The ancestor of this series of microcontrollers is Intel's 8004 microcontroller. Later, with the development of Flash rom technology, the 8004 microcontroller made great progress and became one of
[Microcontroller]
51 microcontroller connects to ESP8266 serial port WiFi module
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号