<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> STM32 >> 瀏覽文章

        紅外遙控:利用NRF24L01實現紅外遙控“穿墻”控制家電

        作者:Rainbow開源硬件   來源:Rainbow開源硬件   點擊數:  更新時間:2014年06月21日   【字體:

           家居智能系統中通常要使用紅外遙控去控制各種家電,如:電視、機頂盒、空調、電風扇等,家電往往分布在家庭的各個不同房間,而紅外信號是無法穿墻去控制家電,怎么樣讓紅外信號“穿墻”,這是所有智能家居系統必須解決的問題。

         

         

            我的解決方案是使用NRF24L01無線通信模塊來進行紅外信號“穿墻”,基本思路是:在家居智能主機上插上一片NRF24L01,而紅外穿墻控制分機同樣裝有NRF24L01,分機的功能是用于接收主機主機發來的紅外遙控信號,并將這個信號通過分機上的紅外發光二極管發送出去。

         

            我們采集到的紅外遙控信號是一個時間序列,保存在一個uint16_t的數組中,這個數組根據遙控信號不同,從長度為幾十到幾百不等,比如美的遙控信號的時間序列通常在200個左右。而NRF24L01每次能夠可靠傳輸的數據包最大是32個字節,要通過它傳輸幾百個字節的數據才能解決紅外信號穿墻的問題,為此我設計了一個簡單的流式傳輸協議來通過NRF24L01進行可靠數據的傳輸,相應的函數原型:

         

        uint8_t NRF24L01_Send(uint8_t *buf, uint16_t len);

         

        uint16_t NRF24L01_Recv(uint8_t *buf, uint16_t len);

         

            由于這兩個函數傳輸的是uint8_t的數組,而紅外信號的時間序列是uint16_t的數組,為了將信號通過無線模塊進行傳輸,我還特地設計了兩個轉換函數:

         

        void word2ByteArray(uint16_t *buf, uint16_t len, uint8_t *bBuf);

         

        void byte2WordArray(uint8_t *bBuf, uint16_t bLen, uint16_t *buf);

         

            有了上述一些基礎準備,接下來實現紅外穿墻控制就水到渠成了。

           

            為了驗證我的這個方案,特地設計了一個模擬的家居智能“主機”,這個主機功能非常簡單:接收來自串口的輸入,如果串口輸入1,則向分機發送開空調的紅外信號,讓隔壁的空調打開;如果輸入0,則向分機發送關空調的紅外信號,讓隔壁的空調關機。在軟件包的“Projects\NRF24L01-SendLong”文件夾包含了這個程序的完整工程,可以直接編譯、燒寫和調試。程序代碼如下:

        #include "WProgram.h"

        #include "24l01.h"

        #include "stdlib_ex.h"

         

        //美的空調:開

        uint16_t rawData_1[] = 

        {4486,4428,590,1604,590,534,563,1605,591,1603,592,533,563,535,563,1604,591,535,563,534,564,1606,589,

        534,563,535,563,1604,590,1603,592,535,563,1604,591,1607,588,535,563,1605,590,1607,588,1604,591,1632,

        564,1602,593,1605,591,534,563,1602,593,534,563,534,563,534,563,535,563,534,563,534,563,1604,591,534,

        563,1632,563,534,564,1604,590,1605,590,534,563,534,564,534,563,1605,591,533,563,1604,591,535,563,534,

        563,1605,590,1604,592,5263,4512,4427,592,1602,593,534,563,1602,593,1632,563,535,563,534,563,1605,

        590,535,564,533,563,1607,589,534,563,534,563,1604,592,1602,593,534,563,1604,591,1630,564,534,563,

        1631,564,1604,591,1604,592,1603,592,1604,591,1633,563,534,563,1604,592,534,563,535,563,510,587,534,

        563,534,564,510,587,1630,565,510,587,1631,564,511,586,1603,592,1604,591,511,587,511,587,509,588,1605,

        590,510,587,1603,593,510,587,511,587,1603,592,1604,591};

        //美的空調:關

        uint16_t rawData_0[] = 

        {4464,4451,567,1628,567,532,565,1628,567,1629,567,531,565,532,567,1626,568,531,566,532,565,1628,568,

        532,565,532,566,1627,568,1630,565,532,566,1627,568,532,566,1629,566,1628,567,1629,567,1628,567,532,

        565,1629,567,1627,568,1628,567,531,567,531,565,532,566,532,565,1627,568,531,566,532,566,1628,567,

        1629,566,1627,568,533,565,532,565,532,566,531,566,532,566,531,565,533,565,532,565,1627,568,1629,567,

        1629,567,1628,567,1627,568,5286,4489,4452,567,1629,566,532,566,1628,568,1627,568,531,566,531,566,

        1628,568,530,566,531,565,1629,567,532,565,532,565,1630,566,1629,567,532,565,1628,567,533,565,1629,

        567,1628,567,1628,592,1604,590,508,577,1616,577,1620,590,1603,567,533,589,508,589,509,589,508,589,

        1603,592,508,590,507,590,1603,591,1633,562,1543,652,509,589,509,588,509,589,508,590,508,589,508,590,

        507,589,508,589,1605,590,1604,591,1604,591,1604,592,1604,592};

        uint16_t len1 = sizeof(rawData_1)/sizeof(uint16_t);

        uint16_t len0 = sizeof(rawData_0)/sizeof(uint16_t);

         

        void setup()

        {

          //初始化Rainbow

          boardInit();

          //初始化默認串口

          Serial.begin();

          //初始化無線通信模塊

          NRF24L01_Init();

          Serial.println("NRF24L01-SendLong start...");

        }

         

        void loop()

        {

          uint8_t *bRawData;

          if(Serial.available())

          {

            switch(Serial.read())

            {

              case 0x30:

                bRawData = new uint8_t[2*len0];

                word2ByteArray(rawData_0, len0, bRawData);

                //關空調

                if(NRF24L01_Send(bRawData, 2*len0))

                Serial.println("Close conditioner...");

                delete []bRawData;

                break;

         

              case 0x31:

                bRawData = new uint8_t[2*len1];

                word2ByteArray(rawData_1, len1, bRawData);

                //開空調

                if(NRF24L01_Send(bRawData, 2*len1))

                  Serial.println("Open conditioner...");

                delete []bRawData;

                break;

            }

          }

        }

         

        int main()

        {

          setup();

          while(1) loop();

        }

         

            紅外信號穿墻控制分機的功能則非常簡單,僅僅就是接收NRF24L01發來的紅外遙控時間序列,然后通過紅外發光二極管發送出去。在軟件包的“Projects\NRF24L01-RecvLong”文件夾包含了紅外穿墻分機的完整工程,可以直接編譯、燒寫和調試。程序代碼如下:

        #include "WProgram.h"

        #include "24l01.h"

        #include "stdlib_ex.h"

        #include "IRRemote.h"

         

        //接收緩沖的大小

        #define RECV_BUF_LEN 800

        uint8_t buf[RECV_BUF_LEN];

         

        //定義紅外發射對象,紅外發光二極管接到TIM2的CH1,即:PB8

        IRSend irSend;

         

        void setup()

        {

          //初始化Rainbow

          boardInit();

          //初始化默認串口

          Serial.begin();

          //初始化無線通信模塊

          NRF24L01_Init();

          //用38K的載波進行調制

          irSend.enableIROut(38);

          Serial.println("NRF24L01-RecvLong start...");

        }

         

        void loop()

        {

          //接收到對方發來的數據

          uint16_t wBuf[RECV_BUF_LEN/2];

          uint16_t len = NRF24L01_Recv(buf, RECV_BUF_LEN);

         

          if(len > 0)

          {

            //收到的byte數組,轉化成紅外遙控的uint16_t數組

            byte2WordArray(buf, len, wBuf);

            irSend.sendRaw(wBuf, len/2);

          }

        }

         

        int main()

        {

          setup();

          while(1) loop();

        }

         

         
        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮