各位大神们们,小弟刚刚入手ESP8266WiFi模块,想将ESP8266作为客户端连接服务器,发现用串口调试助手发送命令连接服务器就可以成功,但是用51
单片机给WiFi模块发送数据就一直失败!请求大神们支支招!下面是我的51单片机代码:
#include <reg52.h>
#include <string.h>
typedef unsigned int uint;
typedef unsigned char uchar;
void delay(uint num){
uint i,j;
for( i=num; i>0; i--)
for( j=110; j>0; j--);
}
void send_to_wifi(uchar *dat,uint len){
uint i;
for( i=0; i<len; i++){
SBUF = dat[i];
while(!
ti);
TI = 0;
}
}
void wifi_init(void){
uchar *temp,*read = NULL;
uint flag = 1;
temp = "AT+RST
";
while(flag){
if(RI){
*read = SBUF;
RI = 0;
if(*read == 'k'){
flag = 0;
}
}
}
delay(100);
send_to_wifi(temp,strlen(temp));
delay(5000);
temp = "AT+CWJAP="ashome","bugaosuni"
";
//printf("AT+CWJAP="ashome","bugaosuni"");
send_to_wifi(temp,strlen(temp));
while(flag){
if(RI){
*read = SBUF;
RI = 0;
if(*read == 'K'){
flag = 0;
}
}
}
flag = 1;
delay(2000);
temp = "AT+CIPSTART="TCP","192.168.0.123",8086
";
//printf("AT+CIPSTART="TCP","192.168.0.123",8086");
send_to_wifi(temp,strlen(temp));
while(flag){
if(RI){
*read = SBUF;
RI = 0;
if(*read == 'd'){
flag = 0;
}
}
}
flag = 1;
delay(2000);
temp = "AT+CIPSEND=4
";
//printf("AT+CIPSEND=4");
send_to_wifi(temp,strlen(temp));
delay(2000);
temp = "time
";
//printf("time");
send_to_wifi(temp,strlen(temp));
}
int main(void){
TMOD = 0x20; //设置T1的工作方式为2,自动装载
TH1 = 0xfd; //设置溢出率,使波特率为9600
TL1 = 0xfd;
TR1 = 1;
REN = 1; //开启串口通讯允许
SM0 = 0; //设置串口工作方式为1
SM1 = 1;
delay(2000);
wifi_init();
while(1);
}
已经写完了!已经写完了!已经写完了!已经写完了!已经写完了!已经写完了!
一周热门 更多>