找回密码
 注册

QQ登录

只需一步,快速开始

搜索

51单片机+ESP8266实现WIFI无线插座 程序+原理图

[复制链接]
coolfire 发表于 2021-5-8 00:22:32 | 显示全部楼层 |阅读模式

51单片机源程序如下:
  1. #include <REGX52.H>
  2. #define uint unsigned int
  3. #define uchar unsigned char   
  4. sbit LED0=P1^0;         
  5. sbit LED1=P1^4;               
  6. sbit LED2=P3^5;           
  7. sbit LED3=P3^6;            
  8. sbit LED4=P2^4;
  9. sbit LED5=P2^5;
  10. sbit LED6=P2^6;
  11. sbit LED7=P2^7;
  12. sbit LED8=P0^0;
  13. sbit LED9=P0^1;
  14. sbit LED10=P0^2;
  15. sbit LED11=P0^3;
  16. sbit LED12=P0^4;
  17. sbit LED13=P0^5;
  18. sbit LED14=P0^6;

  19. /*****************相关变量**************/
  20. uchar Receive,i,qj,yz,zz,ht;                              
  21. uint n;  
  22. uchar Recive_table[40];                  //用于接收wifi模块反馈到MCU上的数据  


  23. /*******************************************************************
  24. 名称:延时函数  作用:毫秒级延时,微妙级延时函数,为数据收发完成作等待.......  
  25. ********************************************************************/
  26. void ms_delay(uint t)
  27. {  
  28.         uint i,j;  
  29.         for(i=t;i>0;i--)   
  30.         for(j=110;j>0;j--);
  31. }   

  32. void us_delay(uchar t)
  33. {  
  34.         while(t--);
  35. }         

  36. void Uart_Init()                                //使用定时器1作为波特率发生器(STC89C52、STC89C51、AT89C51等均可)
  37. {  
  38.     TMOD = 0x20;
  39.     SCON = 0x50;                                 //设置串行方式
  40.     TH1 = 0xFD;                                         //波特率9600
  41.     TL1 = TH1;
  42.     PCON = 0x00;
  43.     EA = 1;                                                //总中断打开
  44.     ES = 1;                                                //开串口中断  
  45.     TR1 = 1;                                        //启动定时器1
  46. }

  47. /********************************************************************
  48. 名称:串口发送函数  功能:MCU向无线WIFI模块ESP8266发送数据  
  49. ********************************************************************/
  50. void Send_Uart(uchar value)
  51. {  
  52.         ES=0;                                                  //关闭串口中断  
  53.         TI=0;                                           //清发送完毕中断请求标志位   
  54.         SBUF=value;                                 //发送  
  55.         while(TI==0);                                 //等待发送完毕   
  56.         TI=0;                                           //清发送完毕中断请求标志位   
  57.         ES=1;                                                  //允许串口中断  
  58. }  

  59. /********************************************************************
  60. 名称:WIFI模块设置函数  作用: 启动模块,以便可以实现无线接入和控制  
  61. ********************************************************************/

  62. void ESP8266_Set(uchar *puf)         // 数组指针*puf指向字符串数组               
  63. {   
  64.         while(*puf!='\0')                    //遇到空格跳出循环  
  65.         {   
  66.                 Send_Uart(*puf);                  //向WIFI模块发送控制指令。   
  67.                 us_delay(5);   
  68.                 puf++;   
  69.         }  
  70.         us_delay(5);  
  71.         Send_Uart('\r');                         //回车  
  72.         us_delay(5);  
  73.         Send_Uart('\n');                           //换行  
  74. }

  75. /********************************************************************
  76. 名称:主函数  作用:程序的执行入口  
  77. ********************************************************************/

  78. void main()
  79. {         
  80.         Uart_Init();                                                                                        //波特率发生器
  81.         ms_delay(2000);
  82.         ESP8266_Set("AT+CWMODE=2");                                                         //设置路由器模式1 station,模式2 AP,模式3 station+AP混合模式   
  83.         ms_delay(2000);
  84. //        ESP8266_Set("AT+RST        ");                                                                         //重新启动wifi模块            
  85. //  ms_delay(2000);
  86.         ESP8266_Set("AT+CWSAP="wifi_yuan","123456789",11,4");          //AT+CWSAP="wifi_yuan","123456789",11,4  设置模块SSID:WIFI, PWD:密码 及安全类型加密模式(WPA2-PSK)
  87.         ms_delay(2000);
  88.         ESP8266_Set("AT+CIPMUX=1");                                                                //开启多连接模式,允许多个各客户端接入
  89.         ms_delay(2000);
  90.         ESP8266_Set("AT+CIPSERVER=1,5000");                                          //启动TCP/IP 实现基于网络//控制         ESP8266_Set("AT+CIPSERVER=1,5000");  
  91.         ms_delay(2000);
  92.         ESP8266_Set("AT+CIPSTO=0");                                                         //永远不超时
  93.         ES=1;                                                                                           //允许串口中断                 

  94.         qj=1;
  95.         zz=1;
  96.         yz=1;
  97.         ht=1;
  98.         LED0=1;
  99.         LED1=1;
  100.         LED2=1;
  101.         LED3=1;
  102.         LED4=1;
  103.         LED5=1;
  104.         LED6=1;
  105.         LED7=1;
  106.         LED8=1;
  107.         LED9=1;
  108.         LED10=1;
  109.         LED11=1;
  110.         LED12=1;
  111.         LED13=1;
  112.         LED14=1;

  113.         while(1)   
  114.         {   
  115.                 if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//MCU接收到的数据为+IPD时进入判断控制0\1来使小灯亮与灭   
  116.                 {           
  117.                         if((Recive_table[9]=='G')&&(Recive_table[10]=='P'))            
  118.                         {

  119. /**************************一楼控制*************************************/                              
  120.                                 if(Recive_table[15]=='a' )
  121.                                 {      
  122.                                         LED0=0;                         //                  
  123.                                 }                                
  124.                                 else
  125.                                 if (Recive_table[15]=='b' )
  126.                                 {              
  127.                                         LED0=1;                         //
  128.                                 }
  129.                                 else
  130.                                 if (Recive_table[15]=='c' )      
  131.                                 {              
  132.                     LED1=0;            //           
  133.                                 }
  134.                                 else
  135.                                 if (Recive_table[15]=='d' )      
  136.                                 {              
  137.                         LED1=1;                    //
  138.                                 }
  139.                                 else
  140.                                 if (Recive_table[15]=='e' )      
  141.                                 {              
  142.                         LED2=0;                       //
  143.                                 }  
  144.                                 else
  145.                                 if (Recive_table[15]=='f' )      
  146.                                 {              
  147.                         LED2=1;                       //
  148.                                 }
  149.                                 if (Recive_table[15]=='g' )      
  150.                                 {              
  151.                         LED3=0;                       //
  152.                                 }  
  153.                                 else
  154.                                 if (Recive_table[15]=='h' )      
  155.                                 {              
  156.                         LED3=1;                       //
  157.                                 }  
  158.                                 if (Recive_table[15]=='i' )      
  159.                                 {              
  160.                         LED4=0;                       //
  161.                                 }  
  162.                                 else
  163.                                 if (Recive_table[15]=='j' )      
  164.                                 {              
  165.                         LED4=1;                       //
  166.                                 }                                                                  
  167. /**************************二楼控制*************************************/                                                                                                                                                            
  168.                                 if(Recive_table[15]=='0' )
  169.                                 {      
  170.                                         LED5=0;                         //                          
  171.                                 }                                
  172.                                 else
  173.                                 if (Recive_table[15]=='1' )
  174.                                 {              
  175.                                         LED5=1;                         //
  176.                                 }
  177.                                 else
  178.                                 if (Recive_table[15]=='2' )      
  179.                                 {              
  180.                     LED6=0;            //         
  181.                                 }
  182.                                 else
  183.                                 if (Recive_table[15]=='3' )      
  184.                                 {              
  185.                         LED6=1;                    //
  186.                                 }
  187.                                 else
  188.                                 if (Recive_table[15]=='4' )      
  189.                                 {              
  190.                         LED7=0;                       //
  191.                                 }  
  192.                                 else
  193.                                 if (Recive_table[15]=='5' )      
  194.                                 {              
  195.                         LED7=1;                       //
  196.                                 }
  197.                                 if (Recive_table[15]=='6' )      
  198.                                 {              
  199.                         LED8=0;                       //
  200.                                 }  
  201.                                 else
  202.                                 if (Recive_table[15]=='7' )      
  203.                                 {              
  204.                         LED8=1;                       //
  205.                                 }  
  206.                                 if (Recive_table[15]=='8' )      
  207.                                 {              
  208.                         LED9=0;                       //
  209.                                 }  
  210.                                 else
  211.                                 if (Recive_table[15]=='9' )      
  212.                                 {              
  213.                         LED9=1;                       //
  214.                                 }                                                                                                                                                            
  215. /***********************三楼控制*************************************/
  216.                                 if(Recive_table[15]=='Q' )
  217.                                 {      
  218.                                         LED10=0;                         //                  
  219.                                 }                                
  220.                                 else
  221.                                 if (Recive_table[15]=='W' )
  222.                                 {              
  223.                                         LED10=1;                         //
  224.                                 }
  225.                                 else
  226.                                 if (Recive_table[15]=='E' )      
  227.                                 {              
  228.                     LED11=0;            //           
  229.                                 }
  230.                                 else
  231.                                 if (Recive_table[15]=='R' )      
  232.                                 {              
  233.                         LED11=1;                    //
  234.                                 }
  235.                                 else
  236.                                 if (Recive_table[15]=='T' )      
  237.                                 {              
  238.                         LED12=0;                       //
  239.                                 }  
  240.                                 else
  241.                                 if (Recive_table[15]=='Y' )      
  242.                                 {              
  243.                         LED12=1;                       //
  244.                                 }
  245.                                 if (Recive_table[15]=='U' )      
  246.                                 {              
  247.                         LED13=0;                       //
  248.                                 }  
  249.                                 else
  250.                                 if (Recive_table[15]=='I' )      
  251.                                 {              
  252.                         LED13=1;                       //
  253.                                 }  
  254.                                 if (Recive_table[15]=='O' )      
  255.                                 {              
  256.                         LED14=0;                       //
  257.                                 }  
  258.                                 else
  259.                                 if (Recive_table[15]=='P' )      
  260.                                 {              
  261.                         LED14=1;                       //
  262.                                 }
  263.                         }  
  264.                 }
  265.           }   
  266. }   

  267. /*********************************************************************  
  268. 名称:串行通讯中断  作用:发送或接收结束后进入该函数,对相应的标志位软件清0,实现模块对数据正常的收发。  
  269. ********************************************************************/

  270. void Uart_Interrupt() interrupt 4         
  271. {   
  272.         static uchar i=0;  
  273.         if(RI==1)  
  274.         {   
  275.                 RI=0;   
  276.                 Receive=SBUF;                                                        //MCU接收wifi模块反馈回来的数据      
  277.                 Recive_table[i]=Receive;   
  278.                 if((Recive_table[i]=='\n'))
  279.                 {                     
  280.                         i=0;
  281.                 }   
  282.                 else i++;                                                                                  //遇到换行 重新装值  
  283.         }   
  284.         else TI=0;   
  285. }  
复制代码


1.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-20 15:35

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表