Chapter 6 serial port

catalogue

1, WIFI module (ESP-01S)

1. Basic information about the module

2. AT instruction

1) Understanding concepts

2) Common AT commands of ESP-01S

3. Programming

1) Configure ESP-01S as Client

📓 How to debug the program? - White box test

2) Configure ESP-01S as Server

2, 4G wireless communication module (EC03-DNC)

1. Basic information about the module

2. Common AT instructions of EC01-DNC

3. Peanut shell inner net penetration

4. Programming implementation

3, Summary

1, WIFI module (ESP-01S)

1. Basic information about the module

The core processor of ESP-01s is ESP8266. This series is a series of low-power UART-WIFI chip modules based on Lexin ESP8266 developed by Anxin Technology Co., Ltd., which can easily carry out secondary development, access cloud services, realize the control of mobile phone 3/4G at any time and anywhere in the world, and accelerate the product prototype design.

✒️ AT firmware used by ESP8266 series module factory. The default baud rate is 115200

✒️ In ESP-01s working AP (routing) mode, the default port is 333

✒️ The MAC address of the module can be queried, and the authenticity can be queried on the official website of Anxin.

2. AT instruction

1) Understanding concepts

✒️ The terminal device (or data terminal device) sends to the terminal adapter (or data circuit terminal device)

✒️ Up to 1056 characters (including the last empty character) can be received except AT two characters

✒️ Each AT command line can only have one AT instruction, and only one (one-to-one) is allowed for the responding terminal

✒️ The instruction ends with a carriage return and the response ends with a carriage return line feed (case sensitive)

2) Common AT commands of ESP-01S

InstructioneffectESP-01S response
AT+RSTRestart moduleready
AT+UART=9600,8,1,0,0Configure serial portOK/ERROR
AT+CWMODE=1. station modeOK/ERROR
=2 routing (AP) modeOK/ERROR
=3 dual modeOK/ERROR
AT+CWJAP = "wifi name", "password"Connect WiFi in device mode

WIFI CONNECTED/FAIL

WIFI GOT IP

AT+CIFSRQuery the obtained IP informationIP information
AT+CIPSTART="TCP","client ip",portConnect serverOK

AT+CIPMODE=1

AT+CIPSEND

Enable transparent mode data transmission

(exit sending + + +)

OK/ERROR
AT+CIPMUX=1Multiple connections (in AP mode)OK/ERROR
At + cipend = channel, bytessend dataSEND OK
AT+CIPSERVER=1TCP connection, default port 333OK/ERROR

3. Programming

1) Configure ESP-01S as Client

#include "reg52.h"
#include <intrins.h>
#include <string.h>

#define MAXSIZE 12

sfr AUXR = 0x8E;
sbit led1 = P3^6;
sbit led2 = P3^7;
char cmd[MAXSIZE];

void uartInit(void){
	AUXR = 0x01;
	SCON |= 0x01<<6;
	SCON &= ~(0x01<<7);
	TMOD |= 0x01<<5;
	TMOD &= ~(0x01<<4);
	TR1 = 1;
	TH1 = 0xFD;
	TL1 = 0xFD;
	REN = 1;
	EA = 1;
	ES = 1;
}

/*********AT Instruction********/
code char WIFI_Connect[] = "AT+CWJAP=\"wifi_name\",\"wifi_password\"\r\n";		//Connect WIFI
char Server_Connect[] = "AT+CIPSTART=\"TCP\",\"172.17.52.195\",8888\r\n";		//Connect server
char Tou_Chuang[] = "AT+CIPMODE=1\r\n";			//Transparent transmission mode
char Data_Transmit[] = "AT+CIPSEND\r\n";	//Data start transmission

/*****Command response flag bit********/
int ready_flag = 0;				//Module power on start completion flag bit
int respond_ok_flag = 0;	
int wifi_connect_flag = 0;

/*******Send instruction function*******/
void send_ATCMD(char *cmd){
	while(*cmd != '\0'){
		SBUF = *cmd;
		while(!TI);
		TI = 0;
		cmd++;
	}
}

/*****Software delay 1s (for heartbeat package)*********/
void Delay1000ms(){
	unsigned char i, j, k;
	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do{
		do{
			while (--k);
		} while (--j);
	} while (--i);
}

/*****Software delay 150ms (for flashing light)*******/
void Delay150ms(){
	unsigned char i, j, k;
	_nop_();
	i = 2;
	j = 13;
	k = 237;
	do{
		do{
			while (--k);
		} while (--j);
	} while (--i);
}

/**********Indicator light**********/
void ledBlink(int num,int cnt){	//cnt: number of flashes
	int i;												//num: 1 or 2
	led1 = 1;
	led2 = 1;
	for(i=0;i<=cnt;i++){
		if(num == 1){
			led2 = 1;
			led1 = ~led1;
		}else{
			led1 = 1;
			led2 = ~led2;
		}
		Delay150ms();
	}
}
void main(void){
	uartInit();			//Serial port initialization
	
	/*****After the module is powered on and initialized, connect WIFI and respond ready****/
	while(!ready_flag);
	send_ATCMD("AT\r\n");  //Send AT confirmation that the module has started, and then send the connection wifi command
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	send_ATCMD(WIFI_Connect);
	ledBlink(1,8);
	
	/*****After the WIFI connection is completed, connect to the server and respond to WIFI GOT IP and OK***/
	while(!wifi_connect_flag);
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	send_ATCMD(Server_Connect);
	ledBlink(2,8);
	
	/****After successfully connecting to the server, turn on the transparent transmission mode and respond with OK*****/
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	send_ATCMD(Tou_Chuang);
	ledBlink(1,8);
	
	/****After the transparent transmission mode is turned on, send the transmission data command and respond to OK****/
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	send_ATCMD(Data_Transmit);
	ledBlink(2,8);

	/****Send heartbeat packets to the server every 1s*****/
	while(1){
		send_ATCMD("I am alive!\r\n");
		Delay1000ms();
		led2 = 1;
	}
}

/*******Serial port interrupt processing**************/
/*******Processing module response**************/
void interruptHandle() interrupt 4{
	static char tmp;		//Static variables, only 1 time
	static char i;
	if(RI){
		RI = 0;		//Receive interrupt bit software clear 0
		tmp = SBUF;
	/********Filter out the first key character of the response and place it at the beginning of the array**********/
		if(tmp == 'r'||tmp == 'W' || tmp == 'O'){
			i = 0;
		}
		cmd[i++] = tmp;		//Keywords for placing response: ready, WiFi, got IP, OK
		
		/***********Capture response ready******************/
		if(cmd[0] == 'r' && cmd[4] == 'y'){
			ready_flag = 1;
			memset(cmd,'\0',sizeof(cmd));
		}
		
		/***********Capture response WIFI GOT IP**************/
		if(cmd[0] == 'W' && cmd[5] == 'G'){
			wifi_connect_flag = 1;
			memset(cmd,'\0',sizeof(cmd));
		}

		/***********Capture response OK******************/
		if(cmd[0] == 'O' && cmd[1] == 'K'){
			respond_ok_flag = 1;
			memset(cmd,'\0',sizeof(cmd));
		}

		/*********Judge array out of bounds********/
		if(i == MAXSIZE) i = 0;
	}
}

📓 How to debug the program? - White box test

If the MCU and ESP-01S directly interact with each other, we can't clearly see and judge the running status of the program, so it's difficult to debug. Therefore, we need to transmit the instructions sent by the single chip microcomputer to the computer first, and then we manually send them to ESP-01S through the computer. Similarly, the data responded by ESP-01S is transmitted to the computer first, so that we can observe the debugging code. The following wiring is required:

2) Configure ESP-01S as Server

#include "reg52.h"
#include <intrins.h>
#include <string.h>

#define MAXSIZE 12

sfr AUXR = 0x8E;
sbit led1 = P3^6;
sbit led2 = P3^7;
char cmd[MAXSIZE];

/*******Serial port initialization******/
void uartInit(void){
	AUXR = 0x01;
	SCON |= 0x01<<6;
	SCON &= ~(0x01<<7);
	TMOD |= 0x01<<5;
	TMOD &= ~(0x01<<4);
	TR1 = 1;
	TH1 = 0xFD;
	TL1 = 0xFD;
	REN = 1;
	EA = 1;
	ES = 1;
}

/*********AT Instruction********/
char AP_MODE[] = "AT+CWMODE=2\r\n";			//Routing mode
char MUL_CONNECT[] = "AT+CIPMUX=1\r\n";		//Multi connection mode
char TCP_SERVER[]  = "AT+CIPSERVER=1\r\n";		//TCP server
char DATA_TRANSMIT[] = "AT+CIPSEND=0,12\r\n";		//

/*****Command response flag bit********/
int ready_flag = 0;						//Module power on start completion flag bit
int respond_ok_flag = 0;			//Module response OK flag bit
int client_connect_flag = 0;	//Customer service terminal access flag bit


/*******Send instruction function*******/
void sendCommand(char *cmd){
	while(*cmd != '\0'){
		SBUF = *cmd;
		while(!TI);
		TI = 0;
		cmd++;
	}
}

/*****Software delay 1s (for heartbeat package)*********/
void Delay1000ms(){
	unsigned char i, j, k;
	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do{
		do{
			while (--k);
		} while (--j);
	} while (--i);
}


/*****Software delay 150ms (for flashing light)*******/
void Delay150ms(){
	unsigned char i, j, k;
	_nop_();
	i = 2;
	j = 13;
	k = 237;
	do{
		do{
			while (--k);
		} while (--j);
	} while (--i);
}

/**********LED Flicker**********/
void ledBlink(int num,int cnt){	//cnt: number of flashes
	int i;												//num: 1 or 2
	led1 = 1;
	led2 = 1;
	for(i=0;i<=cnt;i++){
		if(num == 1){
			led2 = 1;
			led1 = ~led1;
		}
		if(num == 2){
			led1 = 1;
			led2 = ~led2;
		}
		Delay150ms();
	}
}
void main(void){
	uartInit();			//Serial port initialization
	/********Configure to AP mode*********/
	while(!ready_flag);
	sendCommand("AT\r\n");
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	sendCommand(AP_MODE);
	
	/********Enable multiple connections***********/
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	sendCommand(MUL_CONNECT);
	
	/******** TCP Server***********/
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	sendCommand(TCP_SERVER);
	
	/****ESP every 1 second_ 01s send heartbeat packet to client*****/
	while(!respond_ok_flag);
	respond_ok_flag = 0;
	while(!client_connect_flag);
	client_connect_flag = 0;
	while(1){
		sendCommand(DATA_TRANSMIT);
		Delay1000ms();				
		sendCommand("I am alive\r\n");
		Delay1000ms();
	}
}

/*******Serial port interrupt processing**************/
/*******Processing module response**************/
void interruptHandle() interrupt 4{
	static char tmp;		//Static variable, only initialized once
	static char i;
	if(RI){
		RI = 0;		//Receive interrupt bit software clear 0
		tmp = SBUF;
		
		/********Filter out the first key character of the response and place it at the beginning of the array**********/
		if(tmp == 'r'||tmp == 'O' || tmp == ','  || tmp == ':'){
			i = 0;
		}
		cmd[i++] = tmp;		//Keywords for placing response: ready, OK
		
		/***********Capture response ready******************/
		if(cmd[0] == 'r' && cmd[4] == 'y'){
			ready_flag = 1;
			memset(cmd,'\0',sizeof(cmd));
		}
		
		/***********Capture OK response******************/
		if(cmd[0] == 'O' && cmd[1] == 'K'){
			respond_ok_flag = 1;
			memset(cmd,'\0',sizeof(cmd));
		}
		
		/****Capture client access response: 0, CONNECT***/
		if(cmd[0] == ',' && cmd[1] == 'C'){
			client_connect_flag = 1;
			if(client_connect_flag){		//Each time a client accesses, led2 flashes 8 times and then goes out
				ledBlink(2,8);
				led2 = 1;
		}
			memset(cmd,'\0',sizeof(cmd));
}
		/****Process the data sent by the customer service terminal*******/
		if(cmd[0] == ':' && cmd[1] == 'o' && cmd[2] == 'p'){
			ledBlink(1,8);
			memset(cmd,'\0',sizeof(cmd));
		}
		if(cmd[0] == ':' && cmd[1] == 'c' && cmd[2] == 'l'){
			led1 = 1;
			memset(cmd,'\0',sizeof(cmd));
		}
		/*********Judge array out of bounds********/
		if(i > MAXSIZE){
			i = 0;
		}
	}
}

2, 4G wireless communication module (EC03-DNC)

1. Basic information about the module

EC03-DNC is an LTE CAT1 data transmission module product launched by yibaite company. The software function of the product is perfect and covers most conventional application scenarios. EC03-DNC is a product developed to realize serial port equipment and network services and transmit data to each other through the network. The product is an LTE-FDD/LTE-TDD wireless communication data transmission module with diversity reception function, which supports LTE-FDD and LTE-TDD network data connection, Users can realize the two-way transparent data transmission from serial port to network server through simple settings.

✒️ Development mode based on serial port AT instruction

✒️ There are two working modes. The default mode is transparent transmission mode, and the AT command mode is entered through other methods

✒️ When the module is in normal working state, the red light is always on and the yellow light flashes (SIM card)

✒️ Default factory baud rate 115200

✒️ Enter AT command mode: send any AT command (line feed) within 3 seconds after sending + + + (no line feed)

2. Common AT instructions of EC01-DNC

Configuration steps

instructions

effect

Success - module response

(1) View before use

AT+CREG

Query whether to register to the network

+OK=1

AT+CPIN

Query SIM status

+OK=1

AT+ICCID

Query SIM information

+OK=SIM card number

AT+CSQ

Query signal value

+OK = signal value

(2) Basic module configuration

AT+UART=9600,NONE

Query / set serial port parameters

+OK

AT+HEARTMOD

AT+HEARTMOD=NET/UART

Query / set heartbeat packet mode

+OK = net (network heartbeat)

+OK = UART (serial port heartbeat)

AT+HEARTINFO

At + heartinfo = < content >

Query / set heartbeat package content

+OK = < content >

AT+HEARTM

AT+HEARTM=<time>

Query / set heartbeat packet time

+OK=<time>

(3) Exit AT mode

Enter transparent mode

AT+EXAT

Exit AT mode

+OK

(4) Connect server

AT+SOCK=TCPC,IP,PORT

Connect to socket server

+OK

AT+LINKSTA

Query connection status

+OK=Connect

Note: only the configuration instructions required by the following demo are listed here

3. Peanut shell inner net penetration

In the front, the client access is realized through the ESP-01S module, and the LED light is lit, which is mainly realized in the same local area network (intranet). The 4G module can access the public network (external network) server. Here, the internal TCP server address (e.g. 192.168.136.1:8888) is mapped to the public network (e.g. 103.46.128.53:50014) through the peanut shell software, and then the public network address mapped by the peanut shell (103.46.128.53:50014) is accessed through the 4G module to access the TCP server of the internal network.

(use intranet to set up TCP server)

(peanut shell software intranet penetration)

(after successful intranet penetration, obtain the corresponding public IP and port number)

(authentication server)

  ✒️ The next thing to do is to let the EC03-DNC module access the public network address mapped out by the peanut shell (for example: 103.46.54.106:21910), so as to connect to the TCP server and control the LED of the single chip microcomputer through the instructions transmitted from the server. First, configure EC03-DNC (modify baud rate, heartbeat packet, connect TCP server - through the public network, and the module will automatically connect after restarting after configuration), and finally realize the control of MCU led.

(4G module access server)

4. Programming implementation

#include "reg52.h"
#include <intrins.h>
#include <string.h>

#define MAXSIZE 12

sfr AUXR = 0x8E;
sbit led1 = P3^6;
sbit led2 = P3^7;
char cmd[MAXSIZE];

void uartInit(void){
	AUXR = 0x01;
	SCON |= 0x01<<6;
	SCON &= ~(0x01<<7);
	TMOD |= 0x01<<5;
	TMOD &= ~(0x01<<4);
	TR1 = 1;
	TH1 = 0xFD;
	TL1 = 0xFD;
	REN = 1;
	EA = 1;
	ES = 1;
}


/*****Software delay 150ms (for flashing light)*******/
void Delay150ms(){
	unsigned char i, j, k;
	_nop_();
	i = 2;
	j = 13;
	k = 237;
	do{
		do{
			while (--k);
		} while (--j);
	} while (--i);
}

/**********Indicator light**********/
void ledBlink(int num,int cnt){	//cnt: number of flashes
	int i;												//num: 1 or 2
	led1 = 1;
	led2 = 1;
	for(i=0;i<=cnt;i++){
		if(num == 1){
			led2 = 1;
			led1 = ~led1;
		}else{
			led1 = 1;
			led2 = ~led2;
		}
		Delay150ms();
	}
}
void main(void){
	uartInit();			//Serial port initialization
	while(1);
}
/**Led1 blink**/
/**Led2 blink**/
/*******Serial port interrupt processing**************/

void interruptHandle() interrupt 4{
	static char tmp;		//Static variables, only 1 time
	static char i;
	if(RI){
		RI = 0;		//Receive interrupt bit software clear 0
		tmp = SBUF;
	/********Filter out the first key character of the response and place it at the beginning of the array**********/
		if(tmp == 'L'){
			i = 0;
		}
		cmd[i++] = tmp;		
		if(cmd[0] == 'L' && cmd[3] == '1'){
			ledBlink(1,8);
			led1 = 1;
			memset(cmd,'\0',sizeof(cmd));
		}
		if(cmd[0] == 'L' && cmd[3] == '2'){
			ledBlink(2,8);
			led2 = 1;
			memset(cmd,'\0',sizeof(cmd));
		}
		/*********Judge array out of bounds********/
		if(i > MAXSIZE){
			i = 0;
		}
	}
}

(after configuring the module, the network lighting can be realized by modifying the wifi module code in the previous chapter)

3, Summary

For 51, ESP-01S (wifi module) and EC01-DNC (4G module) are based on MCU serial communication. They are products developed by serial port equipment and network server to realize mutual data transmission. However, the application scenarios of wifi module and 4G module are different. wifi module mainly communicates with servers or clients connected to the same LAN, while 4G module can access public network servers.

Tags: C Embedded system IoT 8051 microcontroller

Posted by kml2katz on Tue, 24 May 2022 12:23:44 +0300