Namespaces

Variants
Actions

BayleAdvancedProject-p5

From Cycling '74 Wiki
Jump to: navigation, search

XBee modules configurations and Arduino code

Xbee modules can be configured using XBee explorer modules or wirelessly. I chose the first option.

I successively connected my two modules to this XBee explorer. I then used the program Fio Xbee config tool and it works fine.

jb-arduino-XBeeconfig.jpg Configuring my XBee modules

I used the following setup steps:

First, you have to choose the right serial port corresponding to your XBee explorer module.

Then you have to choose the Mode. In that case, I needed to use the Xbee with an Arduino Fio.

After that, you have to choose the Baud rate, Arduino Fio owning an ATmega328, I chose this one.

Last, you have to setup your PAN ID. It is an identifier corresponding to the network you want to create. Basically, everything with the same PAN ID could be able to communicate.

I put the same on my two Xbee.

MY ID can b 0 or 1 depending on the status you want to give to your modules. It is changed automatically by changing the Mode.

0 is for programming device, 1 for end-user. I configured the XBee with the antenna (computer side) as a Programming Radio and the other one as Arduino Fio radio.

The first mode is also known as coordinator mode, and the other one a end-user mode.

Basically, in Zigbee specifications, we have usually one coordinator device and multiple end-user ones. As we aimed at designing 2 devices with Pippo and Céline, I obviously chose one coordinator (computer side) and 2 end-user (device in hands of public/visitors). Then, click to configure.'

jb-arduino-XbeeConfigTOom.PNG Xbee Config Tool

If everything is ok, it says Configured successfully

I did that on both module, then, I put the one without antenna on my Arduino Fio, I connected the Battery on this latter too, and finally I put the Xbee with antenna on the Xbee Explorer which can also be used as the interface between the Xbee module and your computer (Max6 or Openframeworks in my case)

Here was the Arduino firmware at this step:


#include <Wire.h>
#include <ADXL345.h>

ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
int currentState;


void setup(){
 // init the serial communication between the Arduino Fio and XBee module
 Serial.begin(57600);
 // accelerometer setup
 setupAccelerometer();
}

void loop(){
 // measuring Accelerometer on 3-axis
 
 //int x,y,z;  
 //adxl.readAccel(&x, &y, &z); //read accelerometer values store them in x,y,z
 //Serial.print(x);
 //Serial.print(y);
 //Serial.println(z);
 // reading interrupts
 // getInterruptSource clears all triggered actions after a first returning value
 byte interrupts = adxl.getInterruptSource();

 // detecting freefall
 if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
   Serial.println("freefall");
 }

 // detecting inactivity
 if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
   Serial.println("inactivity");
 }

 // detecting activity
 if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
   Serial.println("activity");
 }
 // detecting double tap
 if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){
   Serial.println("double tap");
 }
 // detecting tap
 if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){
   Serial.println("tap");
 }
}

// -------------------------- HELPERS & FUNCTIONS

void setupAccelerometer(){
 adxl.powerOn();

 //set activity/ inactivity thresholds (0-255)
 adxl.setActivityThreshold(75); //62.5mg per increment
 adxl.setInactivityThreshold(75); //62.5mg per increment
 adxl.setTimeInactivity(5); // how many seconds of no activity is inactive?

 //look of activity movement on this axes - 1 == on; 0 == off
 adxl.setActivityX(1);
 adxl.setActivityY(1);
 adxl.setActivityZ(1);

 //look of inactivity movement on this axes - 1 == on; 0 == off
 adxl.setInactivityX(1);
 adxl.setInactivityY(1);
 adxl.setInactivityZ(1);
 //look of tap movement on this axes - 1 == on; 0 == off
 adxl.setTapDetectionOnX(0);
 adxl.setTapDetectionOnY(0);
 adxl.setTapDetectionOnZ(1);

 //set values for what is a tap, and what is a double tap (0-255)
 adxl.setTapThreshold(50); //62.5mg per increment
 adxl.setTapDuration(15); //625μs per increment
 adxl.setDoubleTapLatency(80); //1.25ms per increment
 adxl.setDoubleTapWindow(200); //1.25ms per increment

 //set values for what is considered freefall (0-255)
 adxl.setFreeFallThreshold(7); //(5 - 9) recommended - 62.5mg per increment
 adxl.setFreeFallDuration(20); //(20 - 70) recommended - 5ms per increment

 //setting all interupts to take place on int pin 1
 //I had issues with int pin 2, was unable to reset it

 adxl.setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT,   ADXL345_INT1_PIN );
 adxl.setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT,   ADXL345_INT1_PIN );
 adxl.setInterruptMapping( ADXL345_INT_FREE_FALL_BIT,    ADXL345_INT1_PIN );
 adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT,     ADXL345_INT1_PIN );
 adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT,   ADXL345_INT1_PIN );
 //register interupt actions - 1 == on; 0 == off  
 adxl.setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
 adxl.setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
 adxl.setInterrupt( ADXL345_INT_FREE_FALL_BIT,  1);
 adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   1);
 adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
}


This code is (very) inspired by this one at blidr.org and is nice for testing things. Basically, it uses the ADXL345 accelerometer library, making our life easy for this part. It also uses the Wire library which is the I2C native Arduino library.

First we are instantiating the adxl instance of the ADXL345 library and basically, it brings some easy to use features as we are going to see now.

In setup(), we start the serial communication. You have to know we are in the Arduino Fio (actually the firmware is) and this is all about the serial communication between Arduino Fio & XBee module. As you remember, we set up this one to 57600bauds. This is why you HAVE to use this value here too.

In my book, I talk about Serial communication protocols and the protocol design necessary requirement : everyone in a communication MUST have the knowledge about the same protocol. In this case, Arduino Fio & XBee now knows how to communicate (I mean Serial, 57600 baud, 8 bits with no parity)

You have to understand here that sending (and receiving) message to the serial port directly send your message to the XBee which is able to transmit it to the federated Zigbee network.

Then, the Max6’s patch we already described request the XBee explorer for it to send all data received on the XBee module to Max6 itself.

Previous: More Hardware Design -- Next: Data Management