Sunday, January 17, 2016

Fuel level measurement

                   


We used Ultrasonic Sensor HC-SR04 for the purpose of measuring the level of the Fuel

Hardware requirement
  1. HC-SR04
  2. 1k Ohm Resistor
  3. 1.5k Ohm   Resistor
  4. Jumper wires
  5. BreadBoard


Specification of HC-SR04:

Power Supply :+5V DC
Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: <15°
Ranging Distance : 2cm – 400 cm/1? – 13ft
Resolution : 0.3 cm
Measuring Angle: 30 degree
Trigger Input Pulse wi
dth: 10uS
Dimension: 45mm x 20mm x 15mm

Pins :

  1. VCC: +5VDC
  2. Trig : Trigger (INPUT :Sends pulses to object)
  3. Echo: Echo (OUTPUT :output is of 5v)
  4. GND: GND



Voltage Divider:
The ECHO output is of 5v. The input pin of Raspberry Pi GPIO is rated at 3.3v. So 5v cannot be directly given to the unprotected 3.3v input pin. Therefore we use a voltage divider circuit using appropriate resistors(always R1=R and R2<=2R) to bring down the voltage to 3.3V.




Circuit Diagram:


Screenshot of the Hardware connections:





Output :

Python Code:

We used the Python script to interact with the Hardware as there is a limitation in Java while interacting with these kind of sensors(because of waiting issue).
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM) #GPIO_mode=BCM=refers pin by broadcom SOC channel number

GPIO_TRIGGER=14 #assign TRIGGER output pin num=14
GPIO_ECHO=15#assign ECHO I/P pin num=15

GPIO.setup(GPIO_TRIGGER,GPIO.OUT)
GPIO.setup(GPIO_ECHO,GPIO.IN)

GPIO.output(GPIO_TRIGGER,False)
time.sleep(0.5) # waits for 0.5s

GPIO.output(GPIO_TRIGGER,True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER,False)
start = time.time()

while GPIO.input(GPIO_ECHO)==0:
 start = time.time()
while GPIO.input(GPIO_ECHO)==1:
 stop = time.time()
elapsed=stop-start
distance=elapsed*34000
distance=distance/2
print "Distance: %.1f"%distance

GPIO.cleanup()


Java Code:  
As we are publishing the sensor data to the cloud using Kura, We will use the Java code to execute and get output of the python script.
Also we used ScheduledExecutorService  to schedule the publishing for every 30 seconds interval
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class CallPython {
    static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
    public static void main(final String[] args) {
        try {
            scheduledExecutorService.schedule(new Runnable() {
                @Override
                public void run() {
                    final Runtime r = Runtime.getRuntime();
                    Process p = null;
                    try {
                        p = r.exec("sudo python DistanceMeasure.py");
                    } catch (final IOException e) {
                        e.printStackTrace();
                    }
                    final BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = "";
                    try {
                        while ((line = br.readLine()) != null) {
                            if (line.contains("Distance:")) {
                                System.out.println("Distance: " + line);
                            }
                        }
                    } catch (NumberFormatException | IOException e) {
                        e.printStackTrace();
                    }
                }
            },
            30,
            TimeUnit.SECONDS);
        } catch (final Exception e) {
            final String cause = e.getMessage();
            if (cause.equals("python: not found")) {
                System.out.println("No python interpreter found.");
            }
        }
    }
}













Sunday, January 3, 2016

Setup NOOBS and Installing Pi4J, Kura on Raspberry Pi

NOOBS (New Out Of the Box Software) 

       NOOBS is an easy operating system install manager for the Raspberry Pi.
We have followed steps mentioned  in the below link to Manually install NOOBS on new SD card
Link : https://www.raspberrypi.org/help/noobs-setup

Installing Pi4J on Raspberry Pi

       We have followed below steps to install Pi4J on Rasperrypi manually
1. Downloaded  the latest Pi4J Debian/Raspian installer package (.deb) file to  local computer from below link
Link : http://pi4j.com/download.html

2. Transferred downloaded file from Local system to Raspberry pi using FileZilla Software.
  DownLoaded FileZilla from source: https://filezilla-project.org/

3. Steps to transfer using FileZilla software
     i. Launch FileZilla and go to File > Site manager
    ii. Entered IP address of the Raspberry pi  (using command : sudo hostname -I  )
   iii. Enter usename and password (By Default UserName:pi Password: raspberry)
   iv. Select Protocol as SFTP (else it may throw unable to connect exception)
    v. Click on Connect

4. Once transfer of the  *.deb file completes, execute below command in command line to install the Pi4J on Raspberry pi
 sudo dpkg -i pi4j-1.0.debs

Please note: if Pi4 already installed it is mandatory to uninstall before installing newer version

   Kura Installation on Raspberry pi

We are using latest version of kura V1.3.0
To install Kura with its dependencies on the Raspberry Pi, perform the following steps:

For installing kura, Pi Must contains latest version of java (preferred : JDK8 ).
1. To check whether Java installed or not use below command and you will get which version of JAVA installed
sudo java –version 
If it is not installed use following command to install Latest Java version
sudo apt-get install java

2. Once we have latest java version, download Kura Debian/Raspian installer package (.deb) file from the below link
Link : http://www.eclipse.org/kura/downloads.php

3. To install kura use below command
sudo dpkg -i <name of the *.deb file >  ##dpkg will complain about missing dependencies
sudo apt-get install -f

4. Reebot Raspberyy pi
sudo reboot

5. After reboot enter http://127.0.0.1:8080/kura URL in the Browser of Pi and kura will ask user name and password(by default UserName: admin and password: admin)
 and also we can use command  tail -f /var/log/kura.log to check logs related to  Kura.

6. Once Kura running on local machine we can access the same on Different client by entering the IP address of the Raspberry pi followed by /kura something as below.
Eg: IP address of the 192.1.1.0 then use http://192.1.1.0/kura

7. To stop kura service if required use  command : killall java hostapd named dhcpd