Monday, 29 September 2014

Use Raspberry Pi as a Game Server

            The Raspberry pi is a mini computer which is designed in a single board with all the essential components required for running an operating system. The Raspberry pi is a device which uses the Broadcom controller chip which is a SoC (System on Chip). This SoC has the powerful ARM11 processor which runs on 700 MHz at its core. This minicomputer comes without a display unit, but it can be used with HDTV display or normal NTSC or PAL standard TV screen. It has an Ethernet port which allows it to be connected to a network. Operating systems from Mac, Windows and Linux can be loaded in the Raspberry pi. The capability of this inexpensive board to load operating systems from Linux and accessibility of the board through a LAN network makes it a perfect choice for tiny dedicated web servers.
This article focus on how to configure the Raspberry pi board as a web server and the technique to make it serve a graphical game for those who type the IP or address of the Raspberry pi board from the address tab of their web browser. The Raspberry pi web server is available in the same LAN network or it can be made available to anyone who has the internet connectivity using the technique of port forwarding. 

In this particular project Ubuntu is installed on Raspberry pi board and is loaded on the Raspberry pi board and is connected to a router using a cross-over LAN cable. Hence the only additional hardware required with the Raspberry pi for this project is a LAN cable only.
Once connected to the router with Ubuntu OS on it, it can be accessed remotely from other systems connected in same LAN network. The board can be accessed from a Linux PC using the secure shell which is enabled in the Ubuntu of Raspberry pi board.  If the IP of the Raspberry pi board in the network is say, 192.168.1.5 then the following command can be used to login as the user named ‘pi’ in the Raspberry pi;
ssh pi@192.168.0.5
The password for the username ‘pi’ will be prompted and it is ‘raspberry’ by default. The same can be done from the windows system with the help of software like ‘PuTTY’ which can be downloaded for free. One might find difficulty in finding the IP address of the Raspberry pi board in the LAN and it can be easily done with the help of software like ‘Ip Scanner’.
Check the IP range of the router before further editing and it can be simply found by checking the IP of the system which is use to access the Raspberry pi board. For a Linux PC the IP address can be found by using the command;
ifconfig
and for a windows system the IP address can be found using the command:
ipconfig
Now add the following lines below that commented statement;
Make sure that the will set for the Raspberry pi board is not already in use for any other PCs and it can be done by using the ‘ping’ command from both Linux and Windows PC.
If there is no PC having the IP address 192.168.1.68 then the following ‘ping’ command should fail.
ping 192.168.1.68

Once logged into the Raspberry pi, the first think to do is to set a static IP address and it can be done by editing a single file in the Ubuntu. The file named ‘interfaces’ need to be edited and saved which is in the directory /etc/network. Open the file using the VIM editor and disable off the following statement;
iface eth0 inet static
The statement can be disabled by commenting it off like as shown below;
# iface eth0 inet static
If the IP of the PC found to be like 192.168.1.5, then write the following statements only
                                                                                           address 192.168.1.68
netmask 255.255.255.0
gateway 192.168.1.1
Save the file and it is suggested to restart the Raspberry pi board which will set the new IP address as 192.168.1.68.
If the IP of the PC found to be like 192.168.0.5, then write the following statements only
                               address 192.168.0.68
netmask 255.255.255.0
gateway 192.168.0.1


Save the file and it is suggested to restart the Raspberry pi board which will set the new IP address as 192.168.0.68.
Use the following command to reboot the Raspberry pi board;
reboot

Once the Raspberry pi reboots try to login using the new IP address.
The next step is to download and install the Apache server for the Ubuntu. It can be done using the following commands;
sudo apt-get update
sudo apt-get install apache2
Now simply copy the game folder in the directory “/var/www”. Change the permission of files in the game folder named say ‘game’, using the following command so that it can be accessed by others in the network.
chmod 777 game/*
Now open a browser in the PC and type the address of the Raspberry pi board in the address tab and start playing the game.
The Raspberry pi is connected to the port number 80 of the router and it can be forwarded to the internet by logging into the router and do the settings under ‘PORT FORWARDING’, the steps will vary from one router model to another.
Enable the forwarding of the port 80 using the following details;

Port to be forwarded : 80
Starting port number : 80
End port number : 81
Protocol : ALL or TCP/UDP

Find the public IP of the router also and if the public IP is say 123.238.112.227; then one can use the same IP to play the game from the Raspberry pi game server from anywhere around the world using a web enabled device.

Video:





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.