标题:
单片机+ESP8266实现WIFI无线插座 程序+原理图
[打印本页]
作者:
山水愚青
时间:
2021-5-6 14:56
标题:
单片机+ESP8266实现WIFI无线插座 程序+原理图
电路原理图如下:
image002.gif
(37.56 KB, 下载次数: 94)
下载附件
2021-5-6 17:46 上传
单片机源程序如下:
#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;
/*****************相关变量**************/
uchar Receive,i,qj,yz,zz,ht;
uint n;
uchar Recive_table[40]; //用于接收wifi模块反馈到MCU上的数据
/*******************************************************************
名称:延时函数 作用:毫秒级延时,微妙级延时函数,为数据收发完成作等待.......
********************************************************************/
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() //使用定时器1作为波特率发生器(STC89C52、STC89C51、AT89C51等均可)
{
TMOD = 0x20;
SCON = 0x50; //设置串行方式
TH1 = 0xFD; //波特率9600
TL1 = TH1;
PCON = 0x00;
EA = 1; //总中断打开
ES = 1; //开串口中断
TR1 = 1; //启动定时器1
}
/********************************************************************
名称:串口发送函数 功能:MCU向无线WIFI模块ESP8266发送数据
********************************************************************/
void Send_Uart(uchar value)
{
ES=0; //关闭串口中断
TI=0; //清发送完毕中断请求标志位
SBUF=value; //发送
while(TI==0); //等待发送完毕
TI=0; //清发送完毕中断请求标志位
ES=1; //允许串口中断
}
/********************************************************************
名称:WIFI模块设置函数 作用: 启动模块,以便可以实现无线接入和控制
********************************************************************/
void ESP8266_Set(uchar *puf) // 数组指针*puf指向字符串数组
{
while(*puf!='\0') //遇到空格跳出循环
{
Send_Uart(*puf); //向WIFI模块发送控制指令。
us_delay(5);
puf++;
}
us_delay(5);
Send_Uart('\r'); //回车
us_delay(5);
Send_Uart('\n'); //换行
}
/********************************************************************
名称:主函数 作用:程序的执行入口
********************************************************************/
void main()
{
Uart_Init(); //波特率发生器
ms_delay(2000);
ESP8266_Set("AT+CWMODE=2"); //设置路由器模式1 station,模式2 AP,模式3 station+AP混合模式
ms_delay(2000);
// ESP8266_Set("AT+RST "); //重新启动wifi模块
// ms_delay(2000);
ESP8266_Set("AT+CWSAP=\"wifi_yuan\",\"123456789\",11,4"); //AT+CWSAP="wifi_yuan","123456789",11,4 设置模块SSID:WIFI, PWD:密码 及安全类型加密模式(WPA2-PSK)
ms_delay(2000);
ESP8266_Set("AT+CIPMUX=1"); //开启多连接模式,允许多个各客户端接入
ms_delay(2000);
ESP8266_Set("AT+CIPSERVER=1,5000"); //启动TCP/IP 实现基于网络//控制 ESP8266_Set("AT+CIPSERVER=1,5000");
ms_delay(2000);
ESP8266_Set("AT+CIPSTO=0"); //永远不超时
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接收到的数据为+IPD时进入判断控制0\1来使小灯亮与灭
{
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; //
}
}
}
}
}
/*********************************************************************
名称:串行通讯中断 作用:发送或接收结束后进入该函数,对相应的标志位软件清0,实现模块对数据正常的收发。
********************************************************************/
void Uart_Interrupt() interrupt 4
{
static uchar i=0;
if(RI==1)
{
RI=0;
Receive=SBUF; //MCU接收wifi模块反馈回来的数据
Recive_table[i]=Receive;
if((Recive_table[i]=='\n'))
{
i=0;
}
else i++; //遇到换行 重新装值
}
else TI=0;
}
复制代码
word版的原理图如下(上面已经有一份了,这个不必下载,内容一样的):
原理图.docx
(49.15 KB, 下载次数: 82)
2021-5-6 14:52 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
sharehope
时间:
2021-5-20 22:05
这个确实好东西,最近也在从事无线wifi相关的编程,学习了
作者:
usaboy
时间:
2021-5-21 09:52
程序在哪下載?
作者:
longqishi57777
时间:
2021-12-27 13:31
非常给力,电路图很赞
作者:
gongnn
时间:
2022-3-17 08:57
楼主三极管驱动继电器部分三极管用法是错的,应该接上臂而不是下臂。
作者:
xiaxiao1127
时间:
2022-3-25 17:04
确实是 好东西,下来好好学习下。
作者:
lvlv99
时间:
2022-3-25 18:33
PNP管5V下臂用没问题,12V用上臂
作者:
zying
时间:
2022-3-26 17:02
请问这个怎样控制呢?
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1