SSブログ

MaruduinoでXBeeを使ってみる。主に受信。 [アプリケーション]

※スケッチを書き込む時はXBeeを外すか、接続を全て外してから行ってください。
※2個のXBeeが必要です。また、Maruduinoと対向する側はPCに接続し、X-CTUでモニターします。
Img_2365.jpg
前回は送信でしたので、今回はさらっと受信してみます。
Xbeeの設定は前回と同様ですが、PC側のXBeeの送り先16bitアドレスにはMaruduinoに搭載されているXbeeの16bitアドレスを設定しておきます。
前回へのリンク
http://maruduino.blog.so-net.ne.jp/2011-01-04
X-CTUのターミナル画面でASSEMBLE PACKETボタンを押して文字単位での送信ではなく、文字列単位の送信を行える様にしています。
MaruduinoのLCD画面には送信元アドレスとRSSI値、それに受信した文字列を表示します。
/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the 
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  The circuit:
 * LCD RS pin to digital DI2
 * LCD Enable pin to digital Di3
 * LCD D4 pin to digital DI4
 * LCD D5 pin to digital DI5
 * LCD D6 pin to digital DI6
 * LCD D7 pin to digital DI7
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 
 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

/**
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *
 * This file is part of XBee-Arduino.
 *
 * XBee-Arduino is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XBee-Arduino is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XBee-Arduino.  If not, see <http://www.gnu.org/licenses/>.
 */


// include the library code:
#include <LiquidCrystal.h>
#include <XBee.h>
#include <stdlib.h>
#include <string.h>
/*************************************************************************/
/* defines                                                               */
/*************************************************************************/
#if 0
#define  DI13     2
#define  DI12     3
#define  DI11     4
#define  DI10     5
#define  DI9      6
#define  DI8      7
#define  DI7      8
#define  DI6      9
#define  DI5      10
#define  DI4      11
#define  DI3      12
#define  DI2      13
#define  DI1      
#define  DI0      
#else
#define  DI13     13
#define  DI12     12
#define  DI11     11
#define  DI10     10
#define  DI9      9
#define  DI8      8
#define  DI7      7
#define  DI6      6
#define  DI5      5
#define  DI4      4
#define  DI3      3
#define  DI2      2
#define  DI1      1
#define  DI0      0
#endif

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(DI2, DI3, DI4, DI5, DI6, DI7);
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();

uint8_t option = 0;
uint8_t data = 0;

void setup()
{
  lcd.begin(16, 2);
  lcd.print("Xbee Test!");
  xbee.begin(9600);
}

void loop()
{
  xbee.readPacket();

  if(xbee.getResponse().isAvailable())
  {
    // got something
    if(xbee.getResponse().getApiId() == RX_16_RESPONSE
     || xbee.getResponse().getApiId() == RX_64_RESPONSE )
    {
      // got a rx packet
      if(xbee.getResponse().getApiId() == RX_16_RESPONSE)
      {
        xbee.getResponse().getRx16Response(rx16);
        option = rx16.getOption();
//        data = rx16.getData(0);
      }
      else
      {
        xbee.getResponse().getRx64Response(rx64);
        option = rx64.getOption();
//        data = rx64.getData(0);
      }

      uint8_t len = xbee.getResponse().getFrameDataLength();
      char *buf = (char *)malloc( (size_t)len );
      memcpy( buf, xbee.getResponse().getFrameData(), len );

      lcd.clear();
      lcd.print("src=");
      int src = (buf[0] << 8) | buf[1];
      lcd.print(src,HEX);
      lcd.print(":rssi=");
      int rssi = (int)buf[2];
      lcd.print(rssi,DEC);

      lcd.setCursor(0, 1);
      for( char *ptr = buf + 4; len > 4; len-- )
      {
        lcd.write( *ptr++ );
      }
      free( buf );
    }
    else
    {
      // not something we were expecting
    }
  }
}


ITRONプログラミング入門―H8マイコンとHOSで始める組み込み開発

ITRONプログラミング入門―H8マイコンとHOSで始める組み込み開発

  • 作者: 濱原 和明
  • 出版社/メーカー: オーム社
  • 発売日: 2005/04/25
  • メディア: 単行本



nice!(0)  コメント(0)  トラックバック(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

※ブログオーナーが承認したコメントのみ表示されます。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。