SSブログ

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

※スケッチを書き込む時はXBeeを外すか、接続を全て外してから行ってください。
※2個のXBeeが必要です。また、Maruduinoと対向する側はPCに接続し、X-CTUでモニターします。
Img_2364.jpg
XBeeの使用例です。
これはLiquidCrystalライブラリとxbee-arduinoライブラリを使用しています。
http://code.google.com/p/xbee-arduino/
xbee-arduinoライブラリのインストールは適宜サイトを読んで行って下さい。

Xbeeはシリーズ1を使っています。上記ライブラリを使用するためにはX-CTUでMaruduinoに搭載する側のXBeeにAPI2モードを設定しておきます。端末通信速度はデフォルトの9600bpsに設定しています。使用チャンネルやPAN ID、16bitアドレスなどは適宜設定してください。

相手側はAPIモードでも良いのですが、今回はATモードにしてあります。ターミナル画面でSHOW HEXを設定しておくと、数字がインクリメントされるデータが送られてくるのが判ります。
/*
  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>
/*************************************************************************/
/* 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();
unsigned long start = millis();
uint8_t payload[] = { 0, 0 };
Tx16Request tx = Tx16Request(0, payload, sizeof(payload));
TxStatusResponse txStatus = TxStatusResponse();
int suc_cnt,flt_cnt,loop_cnt;

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

void loop()
{
  // start transmitting after a startup delay.  Note: this will rollover to 0 eventually so not best way to handle
  if (millis() - start > 15000)
  {
    payload[0] = loop_cnt >> 8 & 0xff;
    payload[1] = loop_cnt++ & 0xff;
    xbee.send(tx);
  }

  // after sending a tx request, we expect a status response
  // wait up to 5 seconds for the status response
  if (xbee.readPacket(5000))
  {
    lcd.setCursor(0, 1);
             //0123456789012345
    lcd.print("                ");
    // got a response!
    // should be a znet tx status            	
    if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE)
    {
      xbee.getResponse().getZBTxStatusResponse(txStatus);
      // get the delivery status, the fifth byte
      if (txStatus.getStatus() == SUCCESS)
      {
        // success.  time to celebrate
        lcd.setCursor(0, 1);
        lcd.print("OK:");
        lcd.print(++suc_cnt);lcd.print(":");lcd.print(flt_cnt);
      }
      else
      {
        // the remote XBee did not receive our packet. is it powered on?
        lcd.setCursor(0, 1);
        lcd.print("NG:");
        lcd.print(suc_cnt);lcd.print(":");lcd.print(++flt_cnt);
      }
    }
  }
  else
  {
    // local XBee did not provide a timely TX Status Response -- should not happen
    lcd.setCursor(0, 1);
    lcd.print("TRANSMIT ERR");
  }
  delay(200);
}


Prototyping Lab ―「作りながら考える」ためのArduino実践レシピ (Make:PROJECTS)

Prototyping Lab ―「作りながら考える」ためのArduino実践レシピ (Make:PROJECTS)

  • 作者: 小林 茂
  • 出版社/メーカー: オライリージャパン
  • 発売日: 2010/05/27
  • メディア: 大型本



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

nice! 0

コメント 0

コメントを書く

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

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

トラックバック 0

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