This shows you the differences between two versions of the page.
|
software:xbee [2010/07/29 14:07] edwin |
software:xbee [2010/07/29 14:08] (current) edwin |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== XBee in Processing+arduino ====== | ||
| + | ===== Introduction ===== | ||
| + | This page describes simple hardware and software for a wireless sensor using Xbee modules. | ||
| + | |||
| + | {{:software:sensorsystem1.jpg|}} | ||
| + | |||
| + | ===== Electronics ===== | ||
| + | * PC side: Xbee on USB adapter by [[http://www.parallax.com/Store/Accessories/CommunicationRF/tabid/161/CategoryID/36/List/0/SortField/0/catpageindex/3/Level/a/ProductID/643/Default.aspx|parallax]] | ||
| + | * Arduino side: XBee on adapter by [[http://www.sparkfun.com/commerce/product_info.php?products_id=9132|sparkfun]] | ||
| + | {{:software:arduino_xbee.jpg?200|}} {{:software:sany0079.jpg?200|}} {{:software:sany0081.jpg?200|}} | ||
| + | |||
| + | The PC-side XBee has been programmed using the **ZNET 2.5 coordinator** firmware (using [[http://www.digi.com/support/kbase/kbaseresultdetl.jsp?kb=125|X-CTU]], [[http://www.digi.com|digi]]'s configuration tool under Win XP). The Arduino side XBee has been programmed using the **ZNET 2.5 Router / end device** firmware. | ||
| + | |||
| + | The XBee's have been configured to use the same network ID. | ||
| + | |||
| + | Instead of the more common [[http://www.arduino.cc/en/Main/ArduinoXbeeShield|arduino XBee shield]], a simple adapter board has been used which can be connected to an arduino-breadboard shield. Since the normal UART on the Arduino is used for programming and communication to the host PC, the XBee adapter has been connected to a Soft-serial port on pin 2 and 3. | ||
| + | |||
| + | ^Xbee adapter ^^ arduino^^ | ||
| + | | 1 | GND | GND || | ||
| + | | 2 | 5V | 5V || | ||
| + | | 3 | DOUT | 2 | RxD | | ||
| + | | 4 | DIN | 3 | TxD | | ||
| + | |||
| + | ===== Code ===== | ||
| + | |||
| + | ==== Arduino Sketch ==== | ||
| + | First a transparent serial to serial sketch for configuring the XBee module on softserial pin 2 and 3. | ||
| + | |||
| + | This sketch uses the NewSoftSerial library from [[http://arduiniana.org/libraries/newsoftserial/|here]] or local: {{:software:newsoftserial10c.zip|}} | ||
| + | |||
| + | <code c> | ||
| + | #include <NewSoftSerial.h> | ||
| + | |||
| + | NewSoftSerial mySerial(2, 3); | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(9600) | ||
| + | // set the data rate for the NewSoftSerial port | ||
| + | mySerial.begin(9600); | ||
| + | } | ||
| + | |||
| + | void loop() // run over and over again | ||
| + | { | ||
| + | |||
| + | if (mySerial.available()) { | ||
| + | Serial.print((char)mySerial.read()); | ||
| + | } | ||
| + | if (Serial.available()) { | ||
| + | mySerial.print((char)Serial.read()); | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | Now a sketch to send 6 sensor values @ 50 Hz. As sensors, an analog joystick has been added on analog inputs 0,1 and 2. An accelerometer has been connected to inputs 3,4 and 5. | ||
| + | <code c> | ||
| + | // Analog signal graph writer over XBee | ||
| + | // write six analog signal values at 20Hz to softserial port at 9600Bps | ||
| + | // every value preceeded by A, B, C, X, Y or Z | ||
| + | // connect DIN of module to 3(TxD) and DOUT of module to 2(RxD) | ||
| + | // | ||
| + | #include <NewSoftSerial.h> | ||
| + | NewSoftSerial mySerial(2, 3); | ||
| + | int pins[] = {0,1,2,3,4,5}; // fill in the number of pins to send | ||
| + | char headers[] = {'A','B','C','X','Y','Z'}; | ||
| + | unsigned long time; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | mySerial.begin(9600); | ||
| + | digitalWrite(16,HIGH); //pullup for switch on analog 2 | ||
| + | } | ||
| + | void loop() | ||
| + | { | ||
| + | if (millis()>time+49){ // more acurate than delay(50); | ||
| + | for(int z=0;z<sizeof(pins);z++) | ||
| + | { | ||
| + | mySerial.print(headers[z]); | ||
| + | mySerial.println(analogRead(pins[z])); | ||
| + | } | ||
| + | time=millis(); | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ==== Processing Sketch ==== | ||
| + | |||
| + | |||
| + | {{:software:graph.png|}} | ||
| + | |||
| + | <code java> | ||
| + | // Analog signal graph writer | ||
| + | // plots 6 analog signals, received at 9600Bps | ||
| + | // every value preceeded by A, B, C, X, Y or Z | ||
| + | // | ||
| + | import processing.serial.*; | ||
| + | |||
| + | String buff = ""; | ||
| + | char header[] = {'A','B','C','X','Y','Z'}; | ||
| + | int linecolor[] = {20,50,100,150,200,240}; //HSB color mode | ||
| + | int value[] = new int[6]; | ||
| + | int diffValue[] = new int[6]; | ||
| + | int NEWLINE = 10; | ||
| + | int n; | ||
| + | Serial port; | ||
| + | void setup() | ||
| + | { | ||
| + | size(512,256); | ||
| + | println("Available serial ports:"); | ||
| + | println(Serial.list()); | ||
| + | port = new Serial(this, Serial.list()[1], 9600); | ||
| + | frameRate(20); // delay of 50 ms, 20Hz update | ||
| + | colorMode(HSB); | ||
| + | background(255); | ||
| + | } | ||
| + | |||
| + | void draw() | ||
| + | { | ||
| + | while (port.available() > 0) { | ||
| + | serialEvent(port.read()); // read data | ||
| + | } | ||
| + | for(int z=0;z<6;z++) { | ||
| + | stroke(linecolor[z],255,255); | ||
| + | line(n-1,diffValue[z],n,value[z]); //draw line | ||
| + | } | ||
| + | n++; | ||
| + | if (n>width){ | ||
| + | n=0; background(255); // clear screen | ||
| + | } | ||
| + | for (int z=0;z<6;z++){ | ||
| + | diffValue[z]=value[z]; // store previous values | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void serialEvent(int serial) | ||
| + | { | ||
| + | try { // try-catch because of transmission errors | ||
| + | if(serial != NEWLINE) { | ||
| + | buff += char(serial); | ||
| + | } else { | ||
| + | // The first character tells us which axis this value is for | ||
| + | char c = buff.charAt(0); | ||
| + | // Remove it from the string | ||
| + | buff = buff.substring(1); | ||
| + | // Discard the carriage return at the end of the buffer | ||
| + | buff = buff.substring(0, buff.length()-1); | ||
| + | // Parse the String into an integer | ||
| + | for(int z=0;z<6;z++) { | ||
| + | if(c == header[z]) { | ||
| + | value[z] = Integer.parseInt(buff)/4; | ||
| + | } | ||
| + | } | ||
| + | buff = ""; // Clear the value of "buff" | ||
| + | } | ||
| + | } | ||
| + | catch(Exception e) { | ||
| + | println("no valid data"); | ||
| + | } | ||
| + | } | ||
| + | </code> | ||