我是靠谱客的博主 鳗鱼小兔子,最近开发中收集的这篇文章主要介绍ESP8266 AT指令开发篇(三),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

通过51单片机、Arduino等实现AT指令配置8266模块,主要是通过串口通信实现的,博主学校的时候用的是51单片机给8266配置,arduino等原理相同,但是通过这种方法配置8266不是很稳定,会出现莫名重启等错误,也有可能是我外围电路没有去设计的原因。

本次主要涉及到的AT指令如下

//ATE0  关闭回显
uchar code closeBack[6]={0x41,0x54,0x45,0x30,0x0D,0x0A};

//AT+UART=9600,0,0,0  设置UART串口模式
uchar code setUART[20]={0x41,0x54,0x2B,0x55,0x41,0x52,0x54,0x3D,0x39,0x36,0x30,0x30,0x2C,0x30,0x2C,0x30,0x2C,0x30,0x0D,0x0A};

//AT+CWMODE=1  设置成Station模式
uchar code setStationMode[13]={0x41,0x54,0x2B,0x43,0x57,0x4D,0x4F,0x44,0x45,0x3D,0x31,0x0D,0x0A};

//AT+CWJAP="ccc","455455455"  连接WIFI
uchar code linkWifi[28]={0x41,0x54,0x2B,0x43,0x57,0x4A,0x41,0x50,0x3D,0x22,0x63,0x63,0x63,0x22,0x2C,0x22,0x34,0x35,0x35,0x34,0x35,0x35,0x34,0x35,0x35,0x22,0x0D,0x0A};

//AT+CIPMUX=0  设置单路连接模式
uchar code setSingleLinkMode[13]={0x41,0x54,0x2B,0x43,0x49,0x50,0x4D,0x55,0x58,0x3D,0x30,0x0D,0x0A};

//AT+RST  重启
uchar code RST[8]={0x41,0x54,0x2B,0x52,0x53,0x54,0x0D,0x0A};

//AT+CIPSTART="TCP","192.168.199.134",8080  连接TCP服务器
uchar code linkTCPServe[42]={0x41,0x54,0x2B,0x43,0x49,0x50,0x53,0x54,0x41,0x52,0x54,0x3D,0x22,0x54,0x43,0x50,0x22,0x2C,0x22,0x31,0x39,0x32,0x2E,0x31,0x36,0x38,0x2E,0x31,0x39,0x39,0x2E,0x31,0x33,0x34,0x22,0x2C,0x38,0x30,0x38,0x30,0x0D,0x0A};

//AT+CIPSEND=  发送数据
uchar code sendDate[11]={0x41,0x54,0x2B,0x43,0x49,0x50,0x53,0x45,0x4E,0x44,0x3D};


当然你也可以使用如下方式表示变量

//ATE0  关闭回显
uchar code closeBack[]="ATE0rn";

//AT+UART=9600,0,0,0  设置UART串口模式
uchar code setUART[]="AT+UART=9600,0,0,0rn";

//AT+CWMODE=1  设置成Station模式
uchar code setStationMode[]="AT+CWMODE=1rn";

//AT+CWJAP="ccc","455455455"  连接WIFI
uchar code linkWifi[]="AT+CWJAP="ccc","455455455"rn";

//AT+CIPMUX=0  设置单路连接模式
uchar code setSingleLinkMode[]="AT+CIPMUX=0";

//AT+RST  重启
uchar code RST[]="AT+RST";

//AT+CIPSTART="TCP","192.168.199.134",8080  连接TCP服务器
uchar code linkTCPServe[]="AT+CIPSTART="TCP","192.168.199.134",8080";

//AT+CIPSEND=  发送数据
uchar code sendDate[]="AT+CIPSEND=";
以上第一种方法要写个循环一个一个发,而第二种比较方便,可以直接使用printf()函数发送。

所用主要是模块作为station连接tcp服务器的一个过程,下面附上我学习时候写的代码,可能有比较多附加的东西,方便我当时调试执行到哪部了。

#include <reg52.h>
#include <stdio.h>

#define uchar unsigned char
#define uint unsigned int

/******************本地变量声明******************/
uchar Buffer;  //接收串口数据

/******************端口引脚定义******************/
sbit K1=P3^4;  //正转按键
sbit K2=P3^5;  //反转按键
sbit K3=P3^6;  //停止按键
sbit LED1=P1^0;  //正转灯
sbit LED2=P1^1;  //反转灯
sbit LED3=P1^2;  //停止灯
sbit LED4=P1^3;  //连接服务器成功
sbit sig=P2^1;
sbit led=P2^0;

/******************电机相序表定义******************/
uchar code CW[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};    //电机正时钟旋转相序表
uchar code CCW[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};   //电机逆时钟旋转相序表

/**********************ESP8266指令定义***********************
//ATE0  关闭回显
uchar code closeBack[6]={0x41,0x54,0x45,0x30,0x0D,0x0A};

//AT+UART=9600,0,0,0  设置UART串口模式
uchar code setUART[20]={0x41,0x54,0x2B,0x55,0x41,0x52,0x54,0x3D,0x39,0x36,0x30,0x30,0x2C,0x30,0x2C,0x30,0x2C,0x30,0x0D,0x0A};

//AT+CWMODE=1  设置成Station模式
uchar code setStationMode[13]={0x41,0x54,0x2B,0x43,0x57,0x4D,0x4F,0x44,0x45,0x3D,0x31,0x0D,0x0A};

//AT+CWJAP="ccc","455455455"  连接WIFI
uchar code linkWifi[28]={0x41,0x54,0x2B,0x43,0x57,0x4A,0x41,0x50,0x3D,0x22,0x63,0x63,0x63,0x22,0x2C,0x22,0x34,0x35,0x35,0x34,0x35,0x35,0x34,0x35,0x35,0x22,0x0D,0x0A};

//AT+CIPMUX=0  设置单路连接模式
uchar code setSingleLinkMode[13]={0x41,0x54,0x2B,0x43,0x49,0x50,0x4D,0x55,0x58,0x3D,0x30,0x0D,0x0A};

//AT+RST  重启
uchar code RST[8]={0x41,0x54,0x2B,0x52,0x53,0x54,0x0D,0x0A};

//AT+CIPSTART="TCP","192.168.199.134",8080  连接TCP服务器
uchar code linkTCPServe[42]={0x41,0x54,0x2B,0x43,0x49,0x50,0x53,0x54,0x41,0x52,0x54,0x3D,0x22,0x54,0x43,0x50,0x22,0x2C,0x22,0x31,0x39,0x32,0x2E,0x31,0x36,0x38,0x2E,0x31,0x39,0x39,0x2E,0x31,0x33,0x34,0x22,0x2C,0x38,0x30,0x38,0x30,0x0D,0x0A};

//AT+CIPSEND=  发送数据
uchar code sendDate[11]={0x41,0x54,0x2B,0x43,0x49,0x50,0x53,0x45,0x4E,0x44,0x3D};
********************ESP8266指令定义结束*********************/

/**********************ESP8266 AT指令定义***********************
//ATE0  关闭回显
uchar code closeBack[]="ATE0rn";

//AT+UART=9600,0,0,0  设置UART串口模式
uchar code setUART[]="AT+UART=9600,0,0,0rn";

//AT+CWMODE=1  设置成Station模式
uchar code setStationMode[]="AT+CWMODE=1rn";

//AT+CWJAP="ccc","455455455"  连接WIFI
uchar code linkWifi[]="AT+CWJAP="ccc","455455455"rn";

//AT+CIPMUX=0  设置单路连接模式
uchar code setSingleLinkMode[]="AT+CIPMUX=0";

//AT+RST  重启
uchar code RST[]="AT+RST";

//AT+CIPSTART="TCP","192.168.199.134",8080  连接TCP服务器
uchar code linkTCPServe[]="AT+CIPSTART="TCP","192.168.199.134",8080";

//AT+CIPSEND=  发送数据
uchar code sendDate[]="AT+CIPSEND=";
/********************ESP8266 AT指令定义结束*********************/


/******************延时函数******************/	
void delay(uint x)  	   
{
	uint i,j;
	for(i=0;i<x;i++)
		for(j=0;j<110;j++);
}
/******************延时函数完毕******************/

/******************电机转动函数*******************/
int CW_motor()  //内部齿轮正转一圈
{
	uint i,j,flag=0;  //flag为停止标识  1为停止  初始为0
	for(i=0;i<8;i++)
	{
		for(j=0;j<8;j++)
		{
			if(K3==0||Buffer=='t')
			{
				flag=1;
				break;			
			}
			P1=CW[j];
			delay(10);  //延时
		}
		if(flag==1)
			break;
	}
	if(flag)
		return 0;
	else
		return 1;
}
int CCW_motor()  //内部齿轮反转一圈
{
	uint i,j,flag=0;  //flag为停止标识  1为停止  初始为0
	for(i=0;i<8;i++)
	{		
		for(j=0;j<8;j++)
		{
			if(K3==0||Buffer=='t')
			{
				flag=1;
				break;
			}
			P1=CCW[j];
			delay(10);  //延时
		}
		if(flag==1)
			break;
	}
	if(flag)
		return 0;
	else
		return 1;
}
/******************电机转动函数完毕******************/

/******************串口初始化******************/
void INIT() 
{
	 TMOD = 0x20;  //设置定时器1工作在模式2
	 PCON = 0x00;
	 SCON = 0x50;			
	 TL1 = 0xfd;
	 TH1 = 0xfd;  //设置波特率 9600
	 TR1 = 1;     //启动定时器1
	 ES = 1;      //开串口中断
	 EA = 1;      //开总中断
	 TI=1;        //printf()需TI=1	
}
/******************串口初始化完毕******************/

/******************8266初始化******************/
void send_message(char *x)  //初始化8266 发送AT指令
{
	uint a;
	uint b,c,d,e;
	b=c=d=e=1;	
	do
	{
		for (a = 0; a<5000; a++);	
		printf("%s",x);
		do {
			if (RI) {    //查询是否有数据接收
				Buffer = SBUF;  //数据暂存于变量中
				RI = 0;
				if (Buffer == 'K')
				{
					b = 0;
					d = 0;
				}
				c++;
				if (c == 1000)  //重新发送
				{
					c = 1;
					d = 0;
				}
			}
		} while (d);
	} while (b);
}
void INIT_8266()  //初始化8266
{
	delay(2000);
	send_message("ATE0rn");
	delay(3000);
	send_message("AT+UART=9600,0,0,0rn");
	delay(3000);
	send_message("AT+CWMODE=1rn");	
	delay(3000);
	send_message("AT+CWJAP="ccc","455455455"rn");	
	delay(3000);
	send_message("AT+CIPMUX=0rn");
	delay(3000);	
	send_message("AT+RSTrn");
	delay(5000);		
}
/******************8266初始化完毕******************/

/*
void Com_Int() interrupt 4	
{	
	if(RI == 1)   			//当硬件接收到一个数据时,RI会置位
	{
		RI = 0; 
		Buffer=SBUF;		
	}
}
*/
void main()
{
	uint N=64;  //28BYJ-48 5V DC 4线程步进电机 减速比为1:64
				//及内部齿轮转64圈,最外部齿轮转1圈	
	uint i;
	uint flag_motor=0;  //是否转动
	LED1=LED2=1;
	LED3=0;
	LED4=1;
	INIT();
	//INIT_8266();
	//led=1;
	while(1)
	{
		/*if(K1==0)
		{
			while(K1==0);
			send_message("AT+CIPSTART="TCP","172.28.151.1",8080");
			LED4=0;
		}
		if(K2==0)
		{
			while(K2==0);
			send_message("AT+CIPSEND=15rn");
			while(K3!=0);
			send_message("I can hear you!");
		}*/
		if(K1==0||Buffer=='z'||sig==1)
		{
			//while(K1==0);
			/*send_message("zheng zhuang zhong...n");
			LED1=0;
			LED3=1;*/
			for(i=0;i<N;i++)
			{
				if(!CW_motor())
					break;
			}
			//LED1=1;
			//LED3=0;
		}
		if(K2==0||Buffer=='f')
		{
			/*send_message("fan zhuang zhong...n");
			while(K2==0);
			LED2=0;
			LED3=1;*/
			for(i=0;i<N;i++)
			{
				if(!CCW_motor())
					break;
			}
			//LED2=1;
			//LED3=0;
		}
		Buffer=0;  //清空Buffer的值  至下一次串行中断再获取指令值
	}		
}


最后说几点,我用的是STC89C52RC的51单片机芯片,晶振是12MHz,非常坑...根本配不出9600的波特率,误差太大,所以AT指令发送各种鬼畜,当时也是图便宜买的8266-01的模块,模块的稳定性也可能有比较大的问题,而且刚买的8266模块的波特率不是9600,也忘记是多少了,所以不是十分推荐用这种方式,下次有空会更新固件开发的文章,希望可以帮到刚入的师弟师妹或者开发者。

最后

以上就是鳗鱼小兔子为你收集整理的ESP8266 AT指令开发篇(三)的全部内容,希望文章能够帮你解决ESP8266 AT指令开发篇(三)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部