51 MCU wifi module communication (esp8266-01s)

Don’t make a fuss, just upload the code directly:

//Control the LED light through the wifi module
#include “reg52.h”
#include “intrins.h”
#include

#define SIZE 12
sfr AUXR = 0x8E;
sbit D5 = P3^7;
sbit D6 = P3^6;

char buffer[SIZE];
code char LJWL[] = “AT + CWJAP=”Redmi_3DB4”, “123456789”\r\\
“; //network access command
code char LJFWQ[] = “AT + CIPSTART=”TCP”, “192.168.31.61”,8880\r\\
“; //Command to connect to the server
char TCMS[] = “AT + CIPMODE=1\r\\
“; //Transparent transmission command
char SJCS[] = “AT + CIPSEND\r\\
“; //Data transmission start command
char RESET[] = “AT + RST\r\\
“; //restart module command
char AT_OK_Flag = 0; //The flag bit of the OK return value
char AT_Connect_Net_Flag = 0; //The flag bit of the return value of WIFI GOT IP

void UartInit(void) //[email protected]
{
AUXR = 0x01;
SCON = 0x50; //Configure serial port working mode 1, REN enables receiving
TMOD &= 0x0F;
TMOD |= 0x20;//Working mode of timer 1 is 8-bit automatic reloading

TH1 = 0xFD;
TL1 = 0xFD;//The initial value of 9600 baud rate
TR1 = 1;//Start the timer

EA = 1;//Enable the total interrupt
ES = 1;//Enable serial port interrupt

}

void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;

_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);

}

void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}

void sendString(char* str)
{
while( *str != \0’){
sendByte(*str);
str++;
}
}

void main()
{
int mark = 0;
D5 = D6 = 1;//Off status light
//Configure the communication mode of the C51 serial port
UartInit();
Delay1000ms();//Time to power on the espwifi module

//Send a networked AT command and wait for success
sendString(LJWL);
while(!AT_OK_Flag);
AT_OK_Flag = 0;
//Send server connection command and wait for success
sendString(LJFWQ);
while(!AT_OK_Flag);
AT_OK_Flag = 0;
//Send transparent transmission mode command and wait for success
sendString(TCMS);
while(!AT_OK_Flag);
AT_OK_Flag = 0;
//Send data transmission command and wait for success
sendString(SJCS);
while(!AT_OK_Flag);

if(AT_Connect_Net_Flag){
D5 = 0;// Light D5, which means the network is successfully connected
}
if(AT_OK_Flag){
D6 = 0;//Light on D6, which means connecting to the server and opening the transparent transmission mode successfully
}

while (1) {
Delay1000ms();
//"heartbeat packet"
sendString("xfj handsome\r\\
");
}

}

void Uart_Handler() interrupt 4
{
static int i = 0;//static variable, initialized once
char tmp;

if(RI)//In the interrupt processing function, the response to the receiving interrupt
{
RI = 0;//clear receive interrupt flag bit
tmp = SBUF;
if(tmp == 'O' || tmp == 'L' || tmp == 'F'){
i = 0;
}
buffer[i + + ] = tmp;
// Judgment of the OK return value command such as connecting to the network server
if(buffer[0] == 'O' & amp; & amp; buffer[1] == 'K'){
AT_OK_Flag = 1;
memset(buffer, '\0', SIZE);
}
//The word FAIL appears when the network fails to capture
if(buffer[0] == 'F' & amp; & amp; buffer[1] == 'A'){
for(i=0;i<5;i ++ ){
D5 = 0;
Delay1000ms();
D5 = 1;
Delay1000ms();
}
sendString(RESET);
memset(buffer, '\0', SIZE);
}
// light control command
if(buffer[0] == 'L' & amp; & amp; buffer[2] == '1'){
D5 = 0;//light up D5
memset(buffer, '\0', SIZE);
}
if(buffer[0] == 'L' & amp; & amp; buffer[2] == '0'){
D5 = 1; // turn off D5
memset(buffer, '\0', SIZE);
}
if(i == 12) i = 0;
}

}

The result picture is not easy to upload, just directly describe the phenomenon:

After turning on the power of the 51 single-chip microcomputer, a series of AT command interactions will be performed between the single-chip computer and the powered-on wifi module. If all the interactions are completed, it will connect to the server. At this time, the server can send commands to control the LED lights. After completing all the AT command interactions, the LED light will flash 5 times, and the microcontroller will send a message to restart the wifi module. Of course, you can not let it restart, you can let the wifi module try to connect to the server again, and you can control it after the connection is successful.

The rendering after successful connection is as follows:

Send the L-1 command to turn on the lights:

Send L-0 light off command:

The wifi module works in routing mode, directly upload the code:

#include “reg52.h”
#include “intrins.h”
#include

#define SIZE 12
sfr AUXR = 0x8E;
sbit D5 = P3^7;
sbit D6 = P3^6;

char buffer[SIZE];
char LYMS[] = “AT + CWMODE=2\r\\
“;//work in routing mode
char DLJ[] = “AT + CIPMUX=1\r\\
“;//Enable multi-connection
char JLFW[] = “AT + CIPSERVER=1\r\\
“;//establish tcp service, default port = 333
char FSSJ[] = “AT + CIPSEND=0,5\r\\
“;// send data
char AT_OK_Flag = 0;
char Clinet_Connect_Flag = 0;

void UartInit(void) //[email protected]
{
AUXR = 0x01;
SCON = 0x50; //Configure serial port working mode 1, REN enables receiving
TMOD &= 0x0F;
TMOD |= 0x20;//Working mode of timer 1 is 8-bit automatic reloading

TH1 = 0xFD;
TL1 = 0xFD;//The initial value of 9600 baud rate
TR1 = 1;//Start the timer

EA = 1;//Enable the total interrupt
ES = 1;//Enable serial port interrupt

}

void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;

_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);

}

void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}

void sendString(char* str)
{
while( *str != \0’){
sendByte(*str);
str++;
}
}

void main()
{

D5 = D6 = 1;//Off status light
//Configure the communication mode of the C51 serial port
UartInit();
Delay1000ms();//Time to power on the espwifi module

sendString(LYMS);
while(!AT_OK_Flag);
AT_OK_Flag = 0;
sendString(DLJ);
while(!AT_OK_Flag);
AT_OK_Flag = 0;
sendString(JLFW);
//while(!AT_OK_Flag);
while(!Clinet_Connect_Flag);
AT_OK_Flag = 0;

if(Clinet_Connect_Flag){
D5 = 0;//Indicates that it is accessed by the client
D6 = 0;
}

while (1) {

sendString(FSSJ);
Delay1000ms();
Delay1000ms();
sendString("Hello");
Delay1000ms();
Delay1000ms();
\t
}

}

void Uart_Handler() interrupt 4
{
static int i = 0;//static variable, initialized once
char tmp;

if(RI)//In the interrupt processing function, the response to the receiving interrupt
{
RI = 0;//clear receive interrupt flag bit
tmp = SBUF;
if(tmp == 'O' || tmp == '0' || tmp == ':'){
i = 0;
}
buffer[i + + ] = tmp;
\t\t
if(buffer[0] == 'O' & amp; & amp; buffer[1] == 'K'){
AT_OK_Flag = 1;
memset(buffer, '\0', SIZE);
}
\t\t
if(buffer[0] == '0' & amp; & amp; buffer[2] == 'C'){
Clinet_Connect_Flag = 1;
memset(buffer, '\0', SIZE);
}
\t\t
// light control command
if(buffer[0] == ':' & amp; & amp; buffer[1] == 'o' & amp; & amp; buffer[2] == 'p'){
D5 = 0;//light up D5
memset(buffer, '\0', SIZE);
}
if(buffer[0] == ':' & amp; & amp; buffer[1] == 'c' & amp; & amp; buffer[2] == 'l'){
D5 = 1; // turn off D5
memset(buffer, '\0', SIZE);
}
if(i == 12) i = 0;
}

}

Renderings:
The wifi module acts as a router, when the client computer is connected, D5 and D6 are both on


When the client sends the cl command to close D5: