Sunday 29 June 2014

RFID ARDUINO RASBERRY-PI and GOOGLE DRIVE interface

Description:
Connect Raspberry Pi and Arduino with Serial USB Cable and Back up your Pi to your Google drive ( Application: Attendance System using RFID-ARDUINO-RASBERRY_PI) 

Get wifi running on your Pi


Plug in the USB adapter and boot your Raspberry Pi. There are several ways to check if the adapter has been recognized. The easiest is to type:


ifconfig
You should see a listing for eth0 – the built-in wired Ethernet port; for lo – the loop back device; and wlan0 – the wireless adapter.
Alternatively you can list the current USB devices attached to the Pi using:

sudo lsusb
The list should contain your wireless dongle. On my setup, the list shows a “Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter”.
Raspbian should come with all the correct packages pre-installed but if any of the commands or files mentioned below aren’t available, then run this command to install them:

sudo apt-get install wpasupplicant wireless-tools
The general network settings are configured in “/etc/network/interfaces” while the Wi-Fi details are set in the “/etc/wpa_supplicant/wpa_supplicant.conf” file. First edit the “interfaces” file:

sudo nano /etc/network/interfaces
Ensure that the section about wlan0 (typically found at the end of the file) reads as follows:


allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
If there are difference then change them to accordingly. Don’t alter any of the lines about the loadapter or the eth0 adapter. Press “CTRL + X” to exit nano (press Y and then press ENTER when prompted).
To get a list of the currently available wireless networks, use the iwlist command:


sudo iwlist wlan0 scan
If there is too much information, use grep to find the fields you need. For example to see just the ESSIDs, use:


sudo iwlist wlan0 scan | grep ESSID
Pick a network and add the network authentication information in the “wpa_supplicant.conf” file:


sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
The first two lines should already read:


ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
Now add the following:


network={
        ssid="YourSSID"
        psk="password"
        key_mgmt=WPA-PSK
}
If your router is configured using WEP for encryption then the network information will look like this:

network={
        ssid="YourSSID"
        wep_key0="password12345"
        key_mgmt=NONE
}
For those of you familiar with advanced WiFi configurations, the network information can also include the following fields:
  • proto – Protocol type can be: RSN (for WP2) and WPA (for WPA1).
  • pairwise – CCMP or TKIP (for WPA2 or WPA1).
  • auth_alg – authentication algorithm, can be OPEN for both WPA1/WPA2 and less commonly SHARED or LEAP.
Press “CTRL + X” to exit nano and save the file, press Y and then press ENTER when prompted. Finally reboot your Pi:


sudo reboot
You can check the status of the wireless connection using ifconfig (to see if wlan0 has acquired an IP address) and iwconfig to check which network the wireless adapter is using.
Get py-serial
Use following for py-serial in command-line
sudo apt-get install python-serial

Get Communication between ARDUINO AND PI using USB

Arduino to Raspberry Pi 

Keep usb connected between pi and arduini

We will send ‘Hi’ from the Arduino to the Raspberry Pi every 2 seconds. Here is the Arduino source code.

1void setup(){
2  Serial.begin(9600);
3}
4
5void loop(){
6  Serial.println("Hello Pi");
7  delay(2000);
8}
Run Python 2 on Raspberry Pi. You will find this from the menu under Programming, you should use Python 2 not 3.
Type the following after >>>
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
The first argument – /dev/ttyACM0 is the name for the USB interface used. To find out the port name, we need to run this command in terminal without Arduino plugged in:
ls /dev/tty*
Now plug in your Arduio and run the command again. If a new name appears, then this is the name of your port.
The second argument – 9600 is the baud rate and should match with what you set in the Arduino program.
Now lets start a loop listening for messages from the Arduino.
while 1 :
    ser.readline()
You will need two hit enter twice after you type the second line. Messages ‘Hi’ should now start to appear every 2 seconds. You can press Ctrl + C to stop (interrupt) the Python program.

Raspberry Pi Sending Data To Arduino

In this example, Raspberry Pi will be sending back a single number, and the Arduino will turn on and off the LED on Pin 12 so many times.
1const int ledPin = 12;
2
3void setup(){
4  pinMode(ledPin, OUTPUT);
5  Serial.begin(9600);
6}
7
8void loop(){
9  if (Serial.available())  {
10     light(Serial.read() - '0');
11  }
12  delay(500);
13}
14
15void light(int n){
16  for (int i = 0; i < n; i++)  {
17    digitalWrite(ledPin, HIGH);
18    delay(100);
19    digitalWrite(ledPin, LOW);
20    delay(100);
21  }
22}
On the Raspberry Pi Side, you need to type
ser.write('3')
Now you should see the LED on the Arduino light up 3 times.


Next sync your data with google drive
Being able to back up data to the cloud is very useful. It means that even if your Raspberry Pi dies or your SD card gets corrupted, your data is still safe. It also means that you can access your data from any where in the world.
If you have a Google drive account, you can use the grive program to sync a folder on your Pi with your Google drive.

Install grive

Start by making sure that your Pi's software is up to date, install some additional packages, and get the code using git:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install git cmake build-essential libgcrypt11-dev libjson0-dev libcurl4-openssl-dev libexpat1-dev libboost-filesystem-dev libboost-program-options-dev binutils-dev libboost-test-dev libqt4-dev libyajl-dev

$ git clone git://github.com/Grive/grive.git
Modify /home/pi/grive/libgrive/src/drive/State.cc to resolve some compiler problems. Lines that use the Add() method need to contain a cast to the correct size of integer. The expression "(boost::uint64_t)" must be added to lines 251, 252 and 256. The updated code should look like this:
void State::Write( const fs::path& filename ) const { Json last_sync ; last_sync.Add( "sec", Json((boost::uint64_t)m_last_sync.Sec() ) ); last_sync.Add( "nsec", Json((boost::uint64_t)m_last_sync.NanoSec() ) ); Json result ; result.Add( "last_sync", last_sync ) ; result.Add( "change_stamp", Json((boost::uint64_t)m_cstamp) ) ; std::ofstream fs( filename.string().c_str() ) ; fs << result ; }
Now you need to configure the make files by running cmake (note the '.' after the cmake command) and compile the source code by running make:
$ cd ./grive $ cmake . $ make
You need to create a directory where you can store files that you want to be syncronized with your Google drive, and copy the grive executable to that directory.
$ mkdir ~/google_drive $ cp ./grive/grive ~/google_drive
The first time you execute grive, you need to use the -a option in order to authenticate with Google. You must be logged into your Google account from your Pi for this to work. If you're logged into your Google account on a PC or laptop, log out, and then log in again using a browser on your Pi. Once you've done this, change to your Google drive directory and run grive.
$ cd ../google_drive/ $ ./grive -a
A link will be printed in the terminal window. Copy the link and paste it in a browser. A page on google.com will appear with a long code that you need to enter in the terminal window. Pasting the code didn't work for me, so you may have to type it in. The authentication process will then take place, and your Pi will sync for the first time. Before you do this, check to see how much space you've used up on your Google drive, and check to see if there's enough space on your Pi's SD card.

Running a backup to the cloud

If you've got this far, you should now be able to sync files and folders in /home/pi/google_drive with your Google drive. The next step is to backup the data on your Pi and upload it to your Google drive.
The following bash script creates a tar archive containing my home directory. I've made sure not to include the Google drive directory in this archive, otherwise previous archives would be included in the backup. The second line compresses the archive into a .tar.gz file. The file is moved to the google drive directory before running grive to upload the new archive to your Google drive on the internet. I've used the date command to embed the current date in the file name.
#!/bin/bash tar -crvf backup_$(date +%y.%m.%d).tar /home/pi --exclude="/home/pi/google_drive" gzip backup_$(date +%y.%m.%d).tar mv backup_$(date +%y.%m.%d).tar.gz ./google_drive cd ./google_drive ./grive cd ..
Save this code as grive_backup.sh, and make it executable using the chmod +x command.
When you run the script, it may appear to hang while gzip is running. This may take a few minutes depending on how much data there is in your home directory.

Running a backup script as cron job

This script can eaily be automated by creating an entry in the crontab file. Linux uses the crontab file to schedule tasks. Entries are made in the format
minute hour day_of_month month day_of_week username command
Use this command to open crontab:
$ sudo leafpad /etc/crontab
...and then add this line:
00 03 * * * pi /home/pi/grive_backup.sh
This specifies that you want to run grive_backup.sh at 3.00am every day, and it should be run as user pi. Linux should automatically detect that your crontab file has been modified, so there's no need to reboot.
Your SD card will fill up after a few days or weeks, so it's best to remove old backups. You can do this by adding the following line to the script above:
rm backup_$(date --date="7 days ago" +%y.%m.%d).tar.gz
This time the date command is being used to create a date from 7 days ago, so a back up file from 7 days ago will be deleted.


ps:I complied it from many other sources and tested all of it to get it running and solving my need...
I hope it helps you'll :)
team:venali sonone.

No comments:

Post a Comment