极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15099|回复: 2

基于Microduino平台的Libview串口蓝牙、手机蓝牙和红外遥控的寝室电器控制

[复制链接]
发表于 2014-1-2 23:43:04 | 显示全部楼层 |阅读模式
本帖最后由 wasdylb 于 2014-1-3 11:51 编辑

目的:
通过电脑蓝牙或者手机蓝牙或者红外遥控器配合Microduino平台控制寝室电器。
Microduino平台:
Microduino测试底板、Microduino core+、Microduino BT4.0、Microduino Ft232R、Microduino nrf、Microduino OLED。
各个模块作用:

    Microduino测试底板:将所有模块级联在一起,方便调试;
    Microduino core+:系统核心板,使整个系统正常运行;
    Microduino Ft232R:下载程序模块;
    Microduino BT4.0:蓝牙通信模块,与电脑蓝牙和手机蓝牙进行数据通信;
    Microduino nrf:2.4G无线通讯模块,主机与副机无线数据传输;
    Microduino OLED:用来显示被控制对象的状态,“1”表示开,“0”表示关;
    蓝牙适配器:不自带蓝牙设备电脑上虚拟一个蓝牙,可用串口通信;
    副机:采用老潘2.4G无线插座,接收主机数据,并对日光灯进行控制。

模块详细参考:http://wiki.microduino.net/index.php?title=Main_Page
方案一:
Libview实现结合电脑蓝牙控制:

Libview分两块,一是:设置串口通讯基本参数;二是:按钮控制界面。
串口通讯基本参数:

1.串口:电脑蓝牙和Microduino BT之间连接的桥梁,两者配上对后电脑会在系统加增加两个串口驱动,一个是传出一个是传入,选择传出串口;
2.波特率:串口数据通讯速率,与core+程序设置的串口服务波特率一致,否则会乱码;
3.数据比特:输入数据的位数。 数据位的值介于5和8之间。默认值为8。
4.奇偶位:奇偶指定要传输或接收的每一帧使用的奇偶校验。 该输入支持下列值:
0        no parity(默认)
1        odd parity
2        even parity
3        mark parity
4        space parity

5.停止位:停止位指定用于表示帧结束的停止位的数量。 该输入支持下列值:
10        1停止位
15        1.5停止位
20        2停止位

6.超时:指定读/写操作的时间,以毫秒为单位。 默认值为10000。
7.缓冲区大小:读取数据的缓冲区间。
控制界面:

四路Boolean控制按钮:控制四路电器;
四路电器状态:显示四路电器状态,”1“表示开,”0“表示关;
停止:停止串口数据传输
测试步骤:
1.插上蓝牙适配器,一般会自动安装驱动,如果不行使用驱动精灵安装,安装完毕重启电脑就行;
2.驱动安装成功插上适配器,会在任务栏上出现一个蓝牙图标,如果没有可能被隐藏到通知区域里,右键图标选择添加设备,如果发现已经有了Microduino BT设备最好删除再重新添加。配上对后电脑会在系统加增加两个串口驱动,一个是传出一个是传入,选择传出串口,右键图标选择打开设置,可以看到两个com端口号;
3.打开Libview,选择传出串口,设置好波特率,其他都默认,设置成功之后,Microduino BT上闪烁灯常量表示匹配成功;
4.打开控制界面就可以实现控制。
方案二:
手机蓝牙实现控制:

测试步骤:
1.测试之前要匹配Microduino BT的MAC地址,BT第一次连接将会显示MAC地址,把地址记下,在Android程序里加入MAC地址,编译通过,把程序安装到手机即可;
2.打开蓝牙,打开刚刚安装好的Android程序,弹出蓝牙匹配请求,输入PIN(1234),匹配成功Microduino BT上闪烁灯常亮表示匹配并且手机进入控制界面;
3.进入控制界面就可以实现控制了。

方案三:
红外遥控实现控制:

红外接法:

测试步骤:
1.按规定连接红外接收头;
2.程序下载完毕后,直接遥控即可。
若要更改遥控按键值,分两个步骤:一、解码红外遥控案件值;二:将Core+内匹配的红外编码换成你需要的按键的解码值。
红外解码程序:
  1. #include <IRremote.h>
  2. int RECV_PIN = A0;//匹配红外信号输入引脚
  3. IRrecv irrecv(RECV_PIN);
  4. decode_results results;
  5. void setup()
  6. {
  7.   Serial.begin(9600);//匹配好波特率
  8.   irrecv.enableIRIn(); // Start the receiver
  9. }

  10. void loop() {
  11.   if (irrecv.decode(&results)) {
  12.     Serial.println(results.value, HEX);
  13.     irrecv.resume(); // Receive the next value
  14.   }
  15. }
复制代码

通过串口监视,打印出的解码值是十六进制数,例如:0xFF18E7(0x将不会打印出来),将编码值替换即可。
三个方案可用同一个程序:
  1. #include <IRremote.h>
  2. #include "U8glib.h"
  3. #include <RF24Network.h>
  4. #include <RF24.h>
  5. #include <SPI.h>

  6. int RECV_PIN = A0;
  7. IRrecv irrecv(RECV_PIN);
  8. decode_results results;

  9. U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);        // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)

  10. // nRF24L01(+) radio attached using Getting Started board
  11. RF24 radio(9,10);
  12. // Network uses that radio
  13. RF24Network network(radio);
  14. // Address of our node
  15. const uint16_t this_node = 1;
  16. // Address of the other node
  17. const uint16_t other_node = 0;
  18. // How often to send 'hello world to the other unit
  19. const unsigned long interval = 150; //ms
  20. // When did we last send?
  21. unsigned long last_sent;
  22. // How many have we sent already
  23. //unsigned long packets_sent;

  24. String comdata = "";  //缓存字符串
  25. String onedata = "";  //缓存字符串

  26. int pinRx = 0;
  27. int pinTx = 1;
  28. boolean C[4];

  29. struct payload_t
  30. {
  31.   uint32_t ms;
  32.   uint32_t sensorDataA;
  33.   uint32_t sensorDataB;
  34.   uint32_t sensorDataC;
  35.   uint32_t sensorDataD;
  36. };

  37. static unsigned char u8g_logo_bits0[] U8G_PROGMEM =
  38. {
  39.   0xC0,0x00,0xC0,0x01,0xC0,0x03,0xC0,0x07,0xDC,0x0E,0xF8,0x1C,0xF0,0x0E,0xE0,0x03,
  40.   0xE0,0x01,0xF0,0x07,0xF8,0x0E,0xDC,0x1C,0xC8,0x0E,0xC0,0x07,0xC0,0x01,0xC0,0x00/*"bluetooth",0*/
  41. };
  42. static unsigned char u8g_logo_bits1[] U8G_PROGMEM =
  43. {
  44.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x3F,0xFE,0x7F,0x27,0x69,0x27,0x69,
  45.   0x27,0x69,0x27,0x69,0x27,0x69,0xFE,0x7F,0xFC,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,/*"battary.bmp",0*/
  46. };

  47. void setup()
  48. {
  49.   Serial.begin(115200);
  50.   Serial.println("RF24Network/examples/helloworld_tx/");
  51.   pinMode(pinTx,OUTPUT);
  52.   pinMode(pinRx,INPUT);
  53.   pinMode(A0,INPUT);
  54.   pinMode(3,OUTPUT);
  55.   pinMode(4,OUTPUT);
  56.   pinMode(5,OUTPUT);
  57.   pinMode(6,OUTPUT);
  58.   pinMode(7,INPUT_PULLUP);


  59.   SPI.begin();
  60.   radio.begin();
  61.   radio.setDataRate( RF24_250KBPS ) ;
  62.   network.begin(/*channel*/ 90, /*node address*/ this_node);
  63.   randomSeed(analogRead(0));

  64.   irrecv.enableIRIn();           //红外使能
  65.   while(!Serial)        //串口使能
  66.   {
  67.   }
  68. }
  69. //********************OLED显示*********************//
  70. void draw(void) {
  71.   u8g.setFont(u8g_font_7x14);
  72.   u8g.drawXBMP( 0, 0, 16, 16, u8g_logo_bits0);
  73.   u8g.drawXBMP( 112, 0, 16, 16, u8g_logo_bits1);

  74.   u8g.setPrintPos(32, 16);
  75.   u8g.print("^");
  76.   u8g.print(C[0]);
  77.   u8g.setPrintPos(48, 16);
  78.   u8g.print("^");
  79.   u8g.print(C[1]);
  80.   u8g.setPrintPos(64, 16);
  81.   u8g.print("^");
  82.   u8g.print(C[2]);
  83.   u8g.setPrintPos(80, 16);
  84.   u8g.print("^");
  85.   u8g.print(C[3]);   
  86. }
  87. //********************蓝牙通讯函数*********************//
  88. void libview()
  89. {
  90.   while (Serial.available() > 0)  //判断串口是否有输入
  91.   {
  92.     comdata = "";                     //清空显示字符
  93.     onedata += char(Serial.read()); //读取字符
  94.     comdata += onedata;               //同上
  95.     delay(2);                         //等待串口缓存
  96.     for(int i = 0; i < comdata.length(); i++)
  97.     {
  98.       if(comdata.length()>1)
  99.         C[i]=comdata[i]-48;  
  100.       else
  101.       {
  102.         switch(comdata[0]-'0')
  103.         {
  104.         case 1:
  105.           C[0]=!C[0];
  106.           break;
  107.         case 2:
  108.           C[1]=!C[1];
  109.           break;
  110.         case 3:
  111.           C[2]=!C[2];
  112.           break;
  113.         case 4:
  114.           C[3]=!C[3];
  115.           break;
  116.         }
  117.       }
  118.     }
  119.   }
  120.   onedata = "";  //缓存字符串清空
  121. }
  122. //********************红外函数*********************//
  123. void hongwai()
  124. {
  125.   unsigned long remote = results.value;        //设红外信号为remote

  126.     if(remote == 0xFF30CF)                //判断按键1
  127.   {
  128.     C[0] = !C[0];
  129.     delay(20);
  130.   }

  131.   else if(remote == 0xFF18E7)                //判断按键2
  132.   {
  133.     C[1] = !C[1];
  134.     delay(20);
  135.   }

  136.   else if(remote == 0xFF7A85)                //判断按键3
  137.   {
  138.     C[2] = !C[2];
  139.     delay(20);
  140.   }
  141.   else if(remote == 0xFF10EF)                //判断按键4
  142.   {
  143.     C[3] = !C[3];
  144.     delay(20);
  145.   }
  146. }
  147. //********************2.4G函数*********************//
  148. void nrf()
  149. {
  150.   // Pump the network regularly
  151.   network.update();
  152.   // If it's time to send a message, send it!
  153.   unsigned long now = millis();
  154.   if ( now - last_sent >= interval  )
  155.   {
  156.     last_sent = now;

  157.     payload_t payload = {
  158.       millis(),C[0],C[1],C[2],C[3]                    };
  159.     RF24NetworkHeader header(/*to node*/ other_node);
  160.     bool ok = network.write(header,&payload,sizeof(payload));
  161.   }
  162. }

  163. void loop()
  164. {
  165.   libview();
  166.   nrf();                         //2.4G`
  167.   if(irrecv.decode(&results))     //解码成功,把数据放入results变量中
  168.   {
  169.     hongwai();             //红外控制
  170.     irrecv.resume();       //返回值
  171.   }
  172.   u8g.firstPage();  
  173.   do {
  174.     draw();
  175.   }
  176.   while( u8g.nextPage() );
  177.   digitalWrite(3,C[0]);
  178.   digitalWrite(4,C[1]);
  179.   digitalWrite(5,C[2]);
  180.   digitalWrite(6,C[3]);
  181. }
复制代码

Libview控制视频:

红外和手机蓝牙控制视频:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-1-6 22:38:24 | 显示全部楼层
libview,牛啊
回复 支持 反对

使用道具 举报

发表于 2014-4-16 14:31:57 | 显示全部楼层
{:soso_e113:}
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 08:11 , Processed in 0.037646 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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