赞
踩
一、AT指令
1.AT指令,控制类都要加回车,数据传输时不加回车!
上电后,通过串口输出一串系统开机信息,购买的部分模块可能电压不稳,导致乱码,以 ready 为准。
配置成9600波特率:
AT+UART=9600,8,1,0,0//指令
2.入网设置
(1)设置工作模式
AT+CWMODE=3 //指令:1. 是station(设备)模式 2.是AP(路由)模式 3.是双模
OK
(2)以设备模式接入家中路由器配置
AT+CWJAP="TP-LINK_3E30","18650711783" //指令
WIFI CONNECTED //结果
WIFI GOT IP //结果
(3)查询IP地址
AT+CIFSR //指令
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"4e:75:25:0d:ae:2f"
+CIFSR:STAIP,"192.168.0.148"
+CIFSR:STAMAC,"4c:75:25:0d:ae:2f"
OK
3.连接到 TCP server
(1)开关网络助手,设立TCP服务器
(2)连接服务器
AT+CIPSTART="TCP","192.168.0.113",8888 //指令,注意双引号逗号都要半角(英文)输入
CONNECT //结果:成功
OK //结果:成功
(3)发送数据
AT+CIPSEND=4 // 设置即将发送数据的长度 (这里是4个字节)
>CLCA // 看到大于号后,输入消息,CLCA,不要带回车
Response :SEND OK //结果:成功
//注意,这种情况下,每次发送前都要先发送AT+CIPSEND=长度 的指令,再发数据!
(4)透传模式
AT+CIPMODE=1 //开启透传模式
Response :OKAT+CIPSEND //带回车
Response: > //这个时候随意发送接收数据咯//在透传发送数据过程中,若识别到单独的⼀包数据 “+++”,则退出透传发送
4.ESP-01s当服务器
//1 配置成双模
AT+CWMODE=2
Response :OK
//2 使能多链接
AT+CIPMUX=1
Response :OK
//3 建立TCPServer
AT+CIPSERVER=1 // default port = 333
Response :OK
//4 发送数据
AT+CIPSEND=0,4 // 发送4个字节在连接0通道上
>abcd //输入数据,不带回车
Response :SEND OK
//5 接收数据
+IPD, 0, n: xxxxxxxxxx //+IPD是固定字符串 0是通道,n是数据长度,xxx是数据
//6 断开连接
AT+CIPCLOSE=0
Response :0, CLOSED OK
二、编程
1.编程实现单片机发送指令eps01s 连接服务器
- #include "reg52.h"
- #include <intrins.h>
- #include <string.h>
-
- #define SIZE 12
-
- sbit D5 = P3^7;
- sbit D6 = P3^6;
- sfr AUXR = 0X8E;
-
- char cmd[SIZE];
- char buffer[SIZE];
- code char LJWL[] = "AT+CWJAP=\"MY_WIFI\",\"gaoyingjie\"\r\n";
- code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.102\",8880\r\n";
- char TCMS[] = "AT+CIPMODE=1\r\n";
- char SJCS[] = "AT+CIPSEND\r\n";
- char RESET[] = "AT+RST\r\n";
- char AT_OK_FLAG = 0;
- char AT_Connect_Net_Flag = 0;
-
- 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 UartInit(void) //9600bps@11.0592MHz
- {
- AUXR = 0x01;
- SCON = 0x50;//配置串口工作方式1,REN使能接收
- TMOD &= 0x0F;
- TMOD |= 0x20;//配置定时器1工作方式为8位自动重装
- TL1 = 0xFD;
- TH1 = 0xFD;//9600波特率初值
- TR1 = 1; //启动定时器1
- EA = 1;//开启总中断
- ES = 1;//开启串口中断
- }
-
- 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;//D5、D6灭
- //配置C51串口通信方式
- UartInit();
- Delay1000ms();//给espwifi模块响应时间
- sendString(LJWL);
- while(!AT_Connect_Net_Flag);
- while(!AT_OK_FLAG);
- AT_OK_FLAG = 0;
-
- sendString(LJFWQ);
- while(!AT_OK_FLAG);
- AT_OK_FLAG = 0;
-
- sendString(TCMS);
- while(!AT_OK_FLAG);
- AT_OK_FLAG = 0;
-
- sendString(SJCS);
- while(!AT_OK_FLAG);
-
- if(AT_Connect_Net_Flag){
- D5 = 0;//点亮D5,代表入网成功
- }
- if(AT_OK_FLAG){
- D6 = 0;//点亮D6,代表连接服务器并打开透传模式成功
- }
- while(1){
- Delay1000ms();
- sendString("hello world\n");
- }
- }
-
- void Uart_Handler() interrupt 4
- {
- static int i = 0;//静态变量,只被初始化一次
- char tmp;
-
- if(RI){//中断处理函数中,对于接收中断的响应
- RI = 0;//清除接收中断标志位
- tmp = SBUF;
- if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == 'F'){
- i = 0;
- }
- buffer[i++] = tmp;
-
- if(buffer[0] == 'W' && buffer[5] == 'G'){
- AT_Connect_Net_Flag = 1;
- memset(buffer,'\0',SIZE);
- }
- if(buffer[0] == 'O' && buffer[1] == 'K'){
- AT_OK_FLAG = 1;
- memset(buffer,'\0',SIZE);
- }
- if(buffer[0] == 'F' && buffer[1] == 'A'){
- for(i=0;i<5;i++){
- D5 = 0;
- Delay1000ms();
- D5 = 1;
- Delay1000ms();
- }
- sendString(RESET);
- memset(buffer,'\0',SIZE);
- }
- if(buffer[0] == 'L' && buffer[2] == '1'){
- D5 = 0;
- memset(buffer,'\0',SIZE);
- }
- if(buffer[0] == 'L' && buffer[2] == '0'){
- D5 = 1;
- memset(buffer,'\0',SIZE);
- }
- if(i == 12){
- i = 0;
- }
- }
- if(TI);
- }
2.编程实现ESP-01s当服务器
- //ESP-01s收到收到数据op/cl给单片机,实现D5led的亮/灭
- #include "reg52.h"
- #include <intrins.h>
- #include <string.h>
-
- #define SIZE 12
-
- sbit D5 = P3^7;
- sbit D6 = P3^6;
- sfr AUXR = 0X8E;
-
- char buffer[SIZE];
- //1.工作在路由模式
- char LYMO[] = "AT+CWMODE=2\r\n";
- //2 使能多链接
- char DLJ[] = "AT+CIPMUX=1\r\n";
- //3 建立TCPServer
- char JLFW[] = "AT+CIPSERVER=1\r\n"; // default port = 333
- //4.发送数据
- char FSSJ[] = "AT+CIPSEND=0,5\r\n";
-
- char AT_OK_FLAG = 0;
- char AT_Connect_Net_Flag = 0;
- char Client_Connect_Flag = 0;
-
- 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 UartInit(void) //9600bps@11.0592MHz
- {
- AUXR = 0x01;
- SCON = 0x50;//配置串口工作方式1,REN使能接收
- TMOD &= 0x0F;
- TMOD |= 0x20;//配置定时器1工作方式为8位自动重装
- TL1 = 0xFD;
- TH1 = 0xFD;//9600波特率初值
- TR1 = 1; //启动定时器1
- EA = 1;//开启总中断
- ES = 1;//开启串口中断
- }
-
- 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;
-
- //配置C51串口通信方式
- UartInit();
- Delay1000ms();//给espwifi模块响应时间
-
- sendString(LYMO);
- while(!AT_OK_FLAG);
- AT_OK_FLAG = 0;
- sendString(DLJ);
- while(!AT_OK_FLAG);
- AT_OK_FLAG = 0;
- sendString(JLFW);
- while(!Client_Connect_Flag);
- AT_OK_FLAG = 0;
- if(Client_Connect_Flag){
- D5 = 0;//点亮D5,代表有客户端接入
- D6 = 0;
- }
-
- while(1){
- sendString(FSSJ);
- Delay1000ms();
- Delay1000ms();
- sendString("Hello");
- Delay1000ms();
- Delay1000ms();
- }
- }
-
- void Uart_Handler() interrupt 4
- {
- static int i = 0;//静态变量,只被初始化一次
- char tmp;
-
- if(RI){//中断处理函数中,对于接收中断的响应
- RI = 0;//清除接收中断标志位
- tmp = SBUF;
- if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == '0' || tmp == ':'){
- i = 0;
- }
- buffer[i++] = tmp;
- //入网成功的判断依据WIFI GOT IP
- if(buffer[0] == 'W' && buffer[5] == 'G'){
- AT_Connect_Net_Flag = 1;
- memset(buffer,'\0',SIZE);
- }
- //连接服务器等OK返回值指令的判断
- if(buffer[0] == 'O' && buffer[1] == 'K'){
- AT_OK_FLAG = 1;
- memset(buffer,'\0',SIZE);
- }
- if(buffer[0] == '0' && buffer[2] == 'C'){
- Client_Connect_Flag = 1;
- memset(buffer,'\0',SIZE);
- }
- //灯控指令
- if(buffer[0] == ':' && buffer[1] == 'o' && buffer[2] == 'p'){
- D5 = 0;
- memset(buffer,'\0',SIZE);
- }
- if(buffer[0] == ':' && buffer[1] == 'c' && buffer[2] == 'l'){
- D5 = 1;
- memset(buffer,'\0',SIZE);
- }
- if(i == 12){
- i = 0;
- }
- }
- if(TI);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。