Pages

Showing posts with label XBee. Show all posts
Showing posts with label XBee. Show all posts

Saturday, 14 March 2015

Arduino Wireless SD Shield Tutorial


As the name implies, the Arduino Wireless SD shield serves two functions. Foremost, this shield allows you to easily interface with Xbee transceiver modules to create mesh networks, and other wireless devices. Secondly, the micro SD socket allows you to store and access a large amount of data. Whether using these functions by on their own or together, this chip greatly enhances the capabilities of a standard Arduino. The best part about this shield is how easy it is to use. In no time flat, you can have its various components up and running.

Step 1: Plug in the Xbee

Plug in your Xbee modules in order to use the shield as a wireless transceiver.Make sure the module's pointy end is lined up with the edge of the board. If you are using the shield for wireless data transfer, you will need two or more of them.

Step 2: Plug it in:

Plug your shields into your Arduinos.

Step 3: Features

The wireless SD shield supports Xbee modules. These modules allow for easy wireless serial communication. A standard module has the range of 100 - 300 feet.
It also boasts a micro SD socket. This can easily be interfaced with the Arduino SD library. Unfortunately, this library does not come bundled with the Arduino development environment, so you will have to set it up yourself.
The shield also boasts a perfboard grid for prototyping your own circuit, and a micro switch for toggling between the USB port and micro SD port.
For more technical information visit its official Arduino page.

Step 4: Program the receiver

Plug one of the Arduinos into the computer. Make certain the micro switch is toggled to the "USB" option.

Upload the following code:
//Xbee receiver
//This example code is in the Public Domain

int sentDat;

void setup() {
  Serial.begin(9600);   
  pinMode(2, OUTPUT); 
}

void loop() {
  if (Serial.available() > 0) {
sentDat = Serial.read(); 

if(sentDat == 'h'){
          //activate the pumpkin for one second and then stop
   digitalWrite(2, HIGH);
          delay(1000);
          digitalWrite(2, LOW);
}
  }
}

Step 5: Setup the receiver

Unplug the Arduino from the computer. Toggle the micro switch from "USB" to "MICRO".

Plug the red wire from a 9V battery connector into the Vin pin. Plug the black wire into the GND pin.

Connect the positive leg of an LED to pin D2 and the other leg in series with a 220 ohm resistor to ground.

Plug in your battery.

It is now a standalone receiver.

Step 6: Program the transmitter

Plug in the Arduino for the transmitter. Make certain the micro switch is toggled to the "USB" option.

Before you upload any code to the Arduino, open the serial monitor. Type in "h" and hit the "send" button. The LED on your receiver should light up. You have made a wireless connection!

Fantastic.

Now upload the following code:


/*
  Wireless transmitter demo  
  Based on Button example code
  http://www.arduino.cc/en/Tutorial/Button
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 This code is in the public domain.
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize serial communication:
  Serial.begin(9600); 
     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    //transmit a High command to the pumpkin and delay a second so that it does not receive more than one command
    //per button press
    Serial.println('h');
    delay(1000); 
  } 
}

Step 7: Setup the transmitter

Picture of Setup the transmitter
6B.jpg
Unplug the Arduino from the computer. Toggle the micro switch from "USB" to "MICRO".
Plug the red wire from a 9V battery connector into the Vin pin. Plug the black wire into the GND pin.
Connect a 10K resistor between pin D2 and ground. Also connect a push button switch between pin D2 and 5v.
Plug in your battery.
It is now a standalone transmitter.

Step 8: Prepare the SD card

Before you can use the micro SD card, it needs to be formatted to either FAT16 or FAT32.

On a Mac:
  • Connect your SD card
  • Open Disk Utlity
  • Select the Disk
  • Click "Erase" at the top of the window
  • Select "Volume Format: MS-DOS(FAT)" and hit "erase"
  • It is now FAT32 formatted
On a PC:
  • Open "My Computer"
  • Right-click on the disk and select "Format"
  • Select "FAT" and click "start"
  • It is now formatted to FAT16
Once the disk is formatted, the next thing you have to do is make sure that you have the SD Card Library. For instructions on how to setup the library, check out the bottom of Adafruit's extremely thorough micro SD card tutorial.

Plug the SD card into the socket on the shield.

To test the SD card, plug the Arduino into the computer and upload the following code:

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4


 This example code is in the public domain.
   
 */

#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
// nothing happens after setup
}









Sunday, 28 September 2014

ZigBee/XBee Technology

             The explosion in wireless technology has seen the emergence of many standards, especially in the industrial, scientific and medical (ISM) radio band. There have been a multitude of proprietary protocols for control applications, which bottlenecked interfacing. Need for a widely accepted standard for communication between sensors in low data rate wireless networks was felt. As an answer to this dilemma, many companies forged an alliance to create a standard which would be accepted worldwide. It was this Zigbee Alliance that created Zigbee. 
Bluetooth and Wi-Fi should not be confused with Zibgee. Both Bluetooth and Wi-Fi have been developed for communication of large amount of data with complex structure like the media files, software etc.  Zigbee on the other hand has been developed looking into the needs of communication of data with simple structure like the data from the sensors.


What is Zigbee and who all are involved?

Zigbee is a low power spin off of WiFi. It is a specification for small, low power radios based on IEEE 802.15.4 – 2003 Wireless Personal Area Networks standard. The specification was accepted and ratified by the Zigbee alliance in December 2004. Zigbee Alliance is a group of more than 300 companies including industry majors like Philips, Mitsubishi Electric, Epson, Atmel, Texas Instruments etc. which are committed towards developing and promoting this standard. The alliance is responsible for publishing and maintaining the ZIgbee specification and has updated it time and again after making it public for the first time in 2005. Most of the recent devices conform to the Zigbee 2007 specifications has two feature sets– Zigbee and Zigbee Pro. The manufacturers which are members of the Alliance provide software, hardware and reference designs to anyone who wants to build applications using Zigbee.
Many years ago, when Bluetooth technology was introduced, it was thought that Bluetooth would make WiFi redundant. But the two coexist quite well today, so do many other Wireless standards like WirelessHART and ISA100.11a. Then why would we need another WPAN standard like Zigbee? The answer is, the application focus of Zigbee Alliance - low cost and low power for energy efficient and cost effective intelligent devices. Moreover, Zigbee and Bluetooth have different application focus. Despite of all their similarities, and despite the fact that both are based on the IEEE 802.15 standards, the two are different in technology as well as scope. Bluetooth is made with mobile phones as its centre of universe enabling media transfer at rates in excess of 1 Mbps while Zigbee is built with emphasis on low data rate control system sensors featuring slower data of just 250 kbps.

Zigbee Networks:

Zigbee devices can form networks with Mesh, Star and Generic Mesh topologies among themselves. The network can be expanded as a cluster of smaller networks. A ZigBee network can have three types of nodes: Zigbee Coordinator (ZBC), Zigbee router (ZBR) and Zigbee End Device (ZBE) each having some unique property.
Let us understand Zigbee through a typical usage scenario in a home automation system. There can be only one ZBC in a network, the one that initiates the network in the first place and stores the information about the network. This would be the main control panel or remote control in the living room of each storey.  All the devices in the network communicate with this ZBC. It has routing capabilities and acts as a bridge to other networks on other floors. A ZBR is an optional component used to extend the coverage, say, providing access to the Zigbee receivers controlling the garage lighting and shutter which is in the nearby shed. The router itself may host an application like a CC Camera which is continuously in active monitoring state. It can also handle local address allocation or de-allocation . A ZBE is optimized for low power consumption and is the cheapest among the three node types. It communicates only with the coordinator and is the point where sensors are deployed. Any end device like lighting units, air conditioning elements etc. can be Zigbee End Devices. Unicast Device Discovery is done if Network ID is available; else Broadcast Device Discovery is done. A ZBR or ZBC’s response to Device Discovery query is a payload containing IEEE address, the Network Address and all known network addresses. Device bindings which are logical links between end devices can be created like binding of a Lamp Application Object with a Switch Application Object. The Radio unit and the Processing unit are often built into a single chip to reduce costs. When a car enters the premises, the radio transmitter inside the car broadcasts its presence to the Zigbee Coordinator through routers. The coordinator then binds the garage shutter’s receiver with the Car’s transmitter and all packets from the Car transmitter are routed to the Shutter, which can then open and close without stepping out of the car. The whole transaction can be automated such that by the time the car reaches the garage door, it automatically opens.
In a network, data traffic can be periodic, intermittent or repetitive. When data is periodic, the application determines the rate of transfer. Intermittent data needs optimum power savings and hence the data rate is stimulus dependent. For repetitive type of data, guaranteed time slots are used, for example the air conditioning unit.

Architectural Overview:

Zigbee bases itself on the IEEE 802.15.4-2003 specifications which lay down standards for the Physical and MAC layers. The protocol stack is completed by adding Zigbee’s own Network and Application Layers. Drawing analogies from the OSI protocol stack simplifies the study of Zigbee protocol. In the figure below, the two protocols are stacked up side by side to see the similarity of roles of various layers.
A brief overview of each layer is now presented below:
Physical Layer
Zigbee uses three frequency bands for transmission- 868 MHz band with a single channel has a raw data rate of 20 kb/s. The 915MHz band with 10 channels has each channel’s central frequency separated from the adjacent band by 2 MHz and data rate of 40 kb/s. BPSK modulated symbols are transmitted at 1 bit per symbol using Direct Sequence Spread Spectrum (DSSS) technique with 15 bit chips. The 2.4 GHz ISM band with 16 channels, 5 MHz wide offers 250 kb/s data rate. It employs O-QPSK modulation with 4 bits/symbol transmitted using DSSS with 32 Bit chips. To reduce the transmitted power, the Zigbee transmitters use Energy Detection (ED) and Link Quality Indication (LQI). It is the responsibility of the physical layer to perform channel assessment.
MAC Layer
Channel access is primarily through Carrier Sense Multiple Access- Collision Avoidance (CSMA-CA). On a node hop to hop basis, the MAC layer can take care of transmitting data. Depending on the mode of transmission, i.e. Beacon or Non-Beacon mode, the MAC layer decides whether to use slotted or unslotted CSMA-CA. The MAC layer takes care of scanning the channel, starting PANs, detecting and resolving PAN ID conflicts, sending beacons, performing device discovery, association and disassociation, synchronizing network device and realigning orphaned devices on the network. Along with this, the MAC layer also provides some standard security features like access control, encryption of data, duplicate rejection and frame integrity. Like in the case of the standard OSI MAC Layer, MAC layer in Zigbee also cannot take care of the situation when the nodes have intermediate nodes between them. This functionality of routing the packets to their destinations is provided in the network layer.
Network and Security Layer
The network layer takes care of network startup, device configuration, topology specific routing, and providing security. On each node, the network layer is the part of the stack that does the route calculations, neighbor discovery and reception control. All the nodes are optimized using unique 64 bit addresses as per the IEEE 802.15.4 standard, supporting a maximum of 65536, 16 bit network address devices which can have 256 sub addresses. The network routing table is populated when the devices come alive in the network for the first time by generating Broadcast Routing Request Packets (RREQ). Endpoint routers respond to these packets as Routing Response Packets (RREP).
Application Support Sub-Layer
It interfaces the network layer and application layer providing a general set of services through two entities, the APS Data Entity (APSDE) and APS Management Entity (APSME) accessed through their respective Service Access Points (SAP). These provide services like binding management, making application level PDU, group filtering, and managing Object database called APS Information Base, providing reliability of transaction etc. which are the necessary functions for an application to work properly.

Application Framework
It is the environment to host application objects on a Zigbee device.Up to 240 distinct application objects can be defined uniquely. It consists of the application profiles as the top layer over ZDO which provides the base functionality.
·         Application Profiles define an accepted language for exchanging data and provide interoperable services across different manufacturers. Zigbee Alliance has released several Standard Profiles which contain different device descriptors which have unique identifiers.
·         ZigBee Device Objects (ZDO) provides an interface between the application objects, the device profiles, and the APS layer in Zigbee devices. It is located between the Application Profiles and the application support sub-layer. The ZDO are responsible for initializing the APS, the network layer, and the Security Service Provider, and also forming the configuration information from applications to implement discovery, security, network and binding management.


Data Transfer Modes
This data can be transferred in two modes: Beacon Mode and Non-beacon mode. In beacon mode, the data is sent periodically over the network. In between the time period when the devices are not sending data, they may enter a low power sleep state to minimize power consumption. However, such close timing and network synchronization has precise timing needs as the beacon period is of the order of milliseconds. This can pose design constraints while reducing costs and eventually is a tradeoff between the design constraints and costs involved. In a non-beacon mode, the coordinators and the routers active in the network have to stay awake for most of the time to listen incoming data and hence need robust power supplies. Hence, the end devices can sleep most of the time and wake up solely for sending data, or on receiving a trigger, while the core devices need to be active, creating an asymmetric power distribution in the network. This creates a heterogeneous network.
Having gone through the protocol stack of the Zigbee protocol, let us have a look at its pros and cons. Zigbee has proven to be a good extension of existing standards, which is now backed by many companies worldwide. This is a big leap towards achieving a widely accepted industry standard. On the flipside, ZIgbee networks have a single point of failure especially in the star networks.   
Zigbee devices find use in a multitude of areas like Industrial, commercial, toys, Computer Peripherals, Personal Health Care, Building Automation etc., virtually everything imaginable in the role of Wireless Sensor Nodes or short range communications. . The first thrust of the Alliance seemed to be Smart Energy and Home Automation. There are 5 application profiles that have been released so far involving Home Automation and Smart Energy, and others involving Building Automation and Retail Services are in the pipeline. This has met with phenomenal success and now, Zigbee has started to form strong foothold in Advanced Metering Infrastructures (AMI). Zigbee and RF4CE have made combined efforts to develop the Zigbee RF4CE specifications for consumer devices that could replace multiple remote controls with a single one.
And why is Zigbee considered a possible competitor to Bluetooth technology? The answer would be evident from a comparison between the two. This however should not be the basis of deciding which technology is the best, but to decide, what technology is best suited for the specific task. Eventually, it might coexist with Bluetooth just as Bluetooth has come to live with WiFi.