標(biāo)題:
單片機(jī)+ESP8266實(shí)現(xiàn)WIFI無(wú)線插座 程序+原理圖
[打印本頁(yè)]
作者:
山水愚青
時(shí)間:
2021-5-6 14:56
標(biāo)題:
單片機(jī)+ESP8266實(shí)現(xiàn)WIFI無(wú)線插座 程序+原理圖
電路原理圖如下:
image002.gif
(37.56 KB, 下載次數(shù): 94)
下載附件
2021-5-6 17:46 上傳
單片機(jī)源程序如下:
#include <REGX52.H>
#define uint unsigned int
#define uchar unsigned char
sbit LED0=P1^0;
sbit LED1=P1^4;
sbit LED2=P3^5;
sbit LED3=P3^6;
sbit LED4=P2^4;
sbit LED5=P2^5;
sbit LED6=P2^6;
sbit LED7=P2^7;
sbit LED8=P0^0;
sbit LED9=P0^1;
sbit LED10=P0^2;
sbit LED11=P0^3;
sbit LED12=P0^4;
sbit LED13=P0^5;
sbit LED14=P0^6;
/*****************相關(guān)變量**************/
uchar Receive,i,qj,yz,zz,ht;
uint n;
uchar Recive_table[40]; //用于接收wifi模塊反饋到MCU上的數(shù)據(jù)
/*******************************************************************
名稱(chēng):延時(shí)函數(shù) 作用:毫秒級(jí)延時(shí),微妙級(jí)延時(shí)函數(shù),為數(shù)據(jù)收發(fā)完成作等待.......
********************************************************************/
void ms_delay(uint t)
{
uint i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void us_delay(uchar t)
{
while(t--);
}
void Uart_Init() //使用定時(shí)器1作為波特率發(fā)生器(STC89C52、STC89C51、AT89C51等均可)
{
TMOD = 0x20;
SCON = 0x50; //設(shè)置串行方式
TH1 = 0xFD; //波特率9600
TL1 = TH1;
PCON = 0x00;
EA = 1; //總中斷打開(kāi)
ES = 1; //開(kāi)串口中斷
TR1 = 1; //啟動(dòng)定時(shí)器1
}
/********************************************************************
名稱(chēng):串口發(fā)送函數(shù) 功能:MCU向無(wú)線WIFI模塊ESP8266發(fā)送數(shù)據(jù)
********************************************************************/
void Send_Uart(uchar value)
{
ES=0; //關(guān)閉串口中斷
TI=0; //清發(fā)送完畢中斷請(qǐng)求標(biāo)志位
SBUF=value; //發(fā)送
while(TI==0); //等待發(fā)送完畢
TI=0; //清發(fā)送完畢中斷請(qǐng)求標(biāo)志位
ES=1; //允許串口中斷
}
/********************************************************************
名稱(chēng):WIFI模塊設(shè)置函數(shù) 作用: 啟動(dòng)模塊,以便可以實(shí)現(xiàn)無(wú)線接入和控制
********************************************************************/
void ESP8266_Set(uchar *puf) // 數(shù)組指針*puf指向字符串?dāng)?shù)組
{
while(*puf!='\0') //遇到空格跳出循環(huán)
{
Send_Uart(*puf); //向WIFI模塊發(fā)送控制指令。
us_delay(5);
puf++;
}
us_delay(5);
Send_Uart('\r'); //回車(chē)
us_delay(5);
Send_Uart('\n'); //換行
}
/********************************************************************
名稱(chēng):主函數(shù) 作用:程序的執(zhí)行入口
********************************************************************/
void main()
{
Uart_Init(); //波特率發(fā)生器
ms_delay(2000);
ESP8266_Set("AT+CWMODE=2"); //設(shè)置路由器模式1 station,模式2 AP,模式3 station+AP混合模式
ms_delay(2000);
// ESP8266_Set("AT+RST "); //重新啟動(dòng)wifi模塊
// ms_delay(2000);
ESP8266_Set("AT+CWSAP=\"wifi_yuan\",\"123456789\",11,4"); //AT+CWSAP="wifi_yuan","123456789",11,4 設(shè)置模塊SSID:WIFI, PWD:密碼 及安全類(lèi)型加密模式(WPA2-PSK)
ms_delay(2000);
ESP8266_Set("AT+CIPMUX=1"); //開(kāi)啟多連接模式,允許多個(gè)各客戶端接入
ms_delay(2000);
ESP8266_Set("AT+CIPSERVER=1,5000"); //啟動(dòng)TCP/IP 實(shí)現(xiàn)基于網(wǎng)絡(luò)//控制 ESP8266_Set("AT+CIPSERVER=1,5000");
ms_delay(2000);
ESP8266_Set("AT+CIPSTO=0"); //永遠(yuǎn)不超時(shí)
ES=1; //允許串口中斷
qj=1;
zz=1;
yz=1;
ht=1;
LED0=1;
LED1=1;
LED2=1;
LED3=1;
LED4=1;
LED5=1;
LED6=1;
LED7=1;
LED8=1;
LED9=1;
LED10=1;
LED11=1;
LED12=1;
LED13=1;
LED14=1;
while(1)
{
if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//MCU接收到的數(shù)據(jù)為+IPD時(shí)進(jìn)入判斷控制0\1來(lái)使小燈亮與滅
{
if((Recive_table[9]=='G')&&(Recive_table[10]=='P'))
{
/**************************一樓控制*************************************/
if(Recive_table[15]=='a' )
{
LED0=0; //
}
else
if (Recive_table[15]=='b' )
{
LED0=1; //
}
else
if (Recive_table[15]=='c' )
{
LED1=0; //
}
else
if (Recive_table[15]=='d' )
{
LED1=1; //
}
else
if (Recive_table[15]=='e' )
{
LED2=0; //
}
else
if (Recive_table[15]=='f' )
{
LED2=1; //
}
if (Recive_table[15]=='g' )
{
LED3=0; //
}
else
if (Recive_table[15]=='h' )
{
LED3=1; //
}
if (Recive_table[15]=='i' )
{
LED4=0; //
}
else
if (Recive_table[15]=='j' )
{
LED4=1; //
}
/**************************二樓控制*************************************/
if(Recive_table[15]=='0' )
{
LED5=0; //
}
else
if (Recive_table[15]=='1' )
{
LED5=1; //
}
else
if (Recive_table[15]=='2' )
{
LED6=0; //
}
else
if (Recive_table[15]=='3' )
{
LED6=1; //
}
else
if (Recive_table[15]=='4' )
{
LED7=0; //
}
else
if (Recive_table[15]=='5' )
{
LED7=1; //
}
if (Recive_table[15]=='6' )
{
LED8=0; //
}
else
if (Recive_table[15]=='7' )
{
LED8=1; //
}
if (Recive_table[15]=='8' )
{
LED9=0; //
}
else
if (Recive_table[15]=='9' )
{
LED9=1; //
}
/***********************三樓控制*************************************/
if(Recive_table[15]=='Q' )
{
LED10=0; //
}
else
if (Recive_table[15]=='W' )
{
LED10=1; //
}
else
if (Recive_table[15]=='E' )
{
LED11=0; //
}
else
if (Recive_table[15]=='R' )
{
LED11=1; //
}
else
if (Recive_table[15]=='T' )
{
LED12=0; //
}
else
if (Recive_table[15]=='Y' )
{
LED12=1; //
}
if (Recive_table[15]=='U' )
{
LED13=0; //
}
else
if (Recive_table[15]=='I' )
{
LED13=1; //
}
if (Recive_table[15]=='O' )
{
LED14=0; //
}
else
if (Recive_table[15]=='P' )
{
LED14=1; //
}
}
}
}
}
/*********************************************************************
名稱(chēng):串行通訊中斷 作用:發(fā)送或接收結(jié)束后進(jìn)入該函數(shù),對(duì)相應(yīng)的標(biāo)志位軟件清0,實(shí)現(xiàn)模塊對(duì)數(shù)據(jù)正常的收發(fā)。
********************************************************************/
void Uart_Interrupt() interrupt 4
{
static uchar i=0;
if(RI==1)
{
RI=0;
Receive=SBUF; //MCU接收wifi模塊反饋回來(lái)的數(shù)據(jù)
Recive_table[i]=Receive;
if((Recive_table[i]=='\n'))
{
i=0;
}
else i++; //遇到換行 重新裝值
}
else TI=0;
}
復(fù)制代碼
word版的原理圖如下(上面已經(jīng)有一份了,這個(gè)不必下載,內(nèi)容一樣的):
原理圖.docx
(49.15 KB, 下載次數(shù): 82)
2021-5-6 14:52 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
sharehope
時(shí)間:
2021-5-20 22:05
這個(gè)確實(shí)好東西,最近也在從事無(wú)線wifi相關(guān)的編程,學(xué)習(xí)了
作者:
usaboy
時(shí)間:
2021-5-21 09:52
程序在哪下載?
作者:
longqishi57777
時(shí)間:
2021-12-27 13:31
非常給力,電路圖很贊
作者:
gongnn
時(shí)間:
2022-3-17 08:57
樓主三極管驅(qū)動(dòng)繼電器部分三極管用法是錯(cuò)的,應(yīng)該接上臂而不是下臂。
作者:
xiaxiao1127
時(shí)間:
2022-3-25 17:04
確實(shí)是 好東西,下來(lái)好好學(xué)習(xí)下。
作者:
lvlv99
時(shí)間:
2022-3-25 18:33
PNP管5V下臂用沒(méi)問(wèn)題,12V用上臂
作者:
zying
時(shí)間:
2022-3-26 17:02
請(qǐng)問(wèn)這個(gè)怎樣控制呢?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1