找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6345|回復(fù): 7
收起左側(cè)

單片機(jī)+ESP8266實(shí)現(xiàn)WIFI無(wú)線插座 程序+原理圖

  [復(fù)制鏈接]
ID:454295 發(fā)表于 2021-5-6 14:56 | 顯示全部樓層 |閱讀模式
電路原理圖如下:
image002.gif

單片機(jī)源程序如下:
  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. /*****************相關(guān)變量**************/
  20. uchar Receive,i,qj,yz,zz,ht;                              
  21. uint n;  
  22. uchar Recive_table[40];                  //用于接收wifi模塊反饋到MCU上的數(shù)據(jù)  


  23. /*******************************************************************
  24. 名稱:延時(shí)函數(shù)  作用:毫秒級(jí)延時(shí),微妙級(jí)延時(shí)函數(shù),為數(shù)據(jù)收發(fā)完成作等待.......  
  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()                                //使用定時(shí)器1作為波特率發(fā)生器(STC89C52、STC89C51、AT89C51等均可)
  37. {  
  38.     TMOD = 0x20;
  39.     SCON = 0x50;                                 //設(shè)置串行方式
  40.     TH1 = 0xFD;                                         //波特率9600
  41.     TL1 = TH1;
  42.     PCON = 0x00;
  43.     EA = 1;                                                //總中斷打開
  44.     ES = 1;                                                //開串口中斷  
  45.     TR1 = 1;                                        //啟動(dòng)定時(shí)器1
  46. }

  47. /********************************************************************
  48. 名稱:串口發(fā)送函數(shù)  功能:MCU向無(wú)線WIFI模塊ESP8266發(fā)送數(shù)據(jù)  
  49. ********************************************************************/
  50. void Send_Uart(uchar value)
  51. {  
  52.         ES=0;                                                  //關(guān)閉串口中斷  
  53.         TI=0;                                           //清發(fā)送完畢中斷請(qǐng)求標(biāo)志位   
  54.         SBUF=value;                                 //發(fā)送  
  55.         while(TI==0);                                 //等待發(fā)送完畢   
  56.         TI=0;                                           //清發(fā)送完畢中斷請(qǐng)求標(biāo)志位   
  57.         ES=1;                                                  //允許串口中斷  
  58. }  

  59. /********************************************************************
  60. 名稱:WIFI模塊設(shè)置函數(shù)  作用: 啟動(dòng)模塊,以便可以實(shí)現(xiàn)無(wú)線接入和控制  
  61. ********************************************************************/

  62. void ESP8266_Set(uchar *puf)         // 數(shù)組指針*puf指向字符串?dāng)?shù)組               
  63. {   
  64.         while(*puf!='\0')                    //遇到空格跳出循環(huán)  
  65.         {   
  66.                 Send_Uart(*puf);                  //向WIFI模塊發(fā)送控制指令。   
  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. 名稱:主函數(shù)  作用:程序的執(zhí)行入口  
  77. ********************************************************************/

  78. void main()
  79. {         
  80.         Uart_Init();                                                                                        //波特率發(fā)生器
  81.         ms_delay(2000);
  82.         ESP8266_Set("AT+CWMODE=2");                                                         //設(shè)置路由器模式1 station,模式2 AP,模式3 station+AP混合模式   
  83.         ms_delay(2000);
  84. //        ESP8266_Set("AT+RST        ");                                                                         //重新啟動(dòng)wifi模塊            
  85. //  ms_delay(2000);
  86.         ESP8266_Set("AT+CWSAP=\"wifi_yuan\",\"123456789\",11,4");          //AT+CWSAP="wifi_yuan","123456789",11,4  設(shè)置模塊SSID:WIFI, PWD:密碼 及安全類型加密模式(WPA2-PSK)
  87.         ms_delay(2000);
  88.         ESP8266_Set("AT+CIPMUX=1");                                                                //開啟多連接模式,允許多個(gè)各客戶端接入
  89.         ms_delay(2000);
  90.         ESP8266_Set("AT+CIPSERVER=1,5000");                                          //啟動(dòng)TCP/IP 實(shí)現(xiàn)基于網(wǎng)絡(luò)//控制         ESP8266_Set("AT+CIPSERVER=1,5000");  
  91.         ms_delay(2000);
  92.         ESP8266_Set("AT+CIPSTO=0");                                                         //永遠(yuǎn)不超時(shí)
  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接收到的數(shù)據(jù)為+IPD時(shí)進(jìn)入判斷控制0\1來(lái)使小燈亮與滅   
  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. 名稱:串行通訊中斷  作用:發(fā)送或接收結(jié)束后進(jìn)入該函數(shù),對(duì)相應(yīng)的標(biāo)志位軟件清0,實(shí)現(xiàn)模塊對(duì)數(shù)據(jù)正常的收發(fā)。  
  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模塊反饋回來(lái)的數(shù)據(jù)      
  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. }  
復(fù)制代碼

word版的原理圖如下(上面已經(jīng)有一份了,這個(gè)不必下載,內(nèi)容一樣的):
原理圖.docx (49.15 KB, 下載次數(shù): 82)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:924117 發(fā)表于 2021-5-20 22:05 | 顯示全部樓層
這個(gè)確實(shí)好東西,最近也在從事無(wú)線wifi相關(guān)的編程,學(xué)習(xí)了
回復(fù)

使用道具 舉報(bào)

ID:28992 發(fā)表于 2021-5-21 09:52 | 顯示全部樓層
程序在哪下載?
回復(fù)

使用道具 舉報(bào)

ID:507540 發(fā)表于 2021-12-27 13:31 | 顯示全部樓層
非常給力,電路圖很贊
回復(fù)

使用道具 舉報(bào)

ID:220661 發(fā)表于 2022-3-17 08:57 | 顯示全部樓層
樓主三極管驅(qū)動(dòng)繼電器部分三極管用法是錯(cuò)的,應(yīng)該接上臂而不是下臂。
回復(fù)

使用道具 舉報(bào)

ID:941205 發(fā)表于 2022-3-25 17:04 | 顯示全部樓層
確實(shí)是 好東西,下來(lái)好好學(xué)習(xí)下。
回復(fù)

使用道具 舉報(bào)

ID:240034 發(fā)表于 2022-3-25 18:33 | 顯示全部樓層
PNP管5V下臂用沒問(wèn)題,12V用上臂
回復(fù)

使用道具 舉報(bào)

ID:21740 發(fā)表于 2022-3-26 17:02 | 顯示全部樓層
請(qǐng)問(wèn)這個(gè)怎樣控制呢?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表