BELAJAR RASPBERRY PI
Kamis, 29 November 2018
Judul Skripsi/TA/Project Menggunakn Raspberry PI
1.RF-ID Pengamanan Pintu Rumah
2. RF-ID Pengamanan Produk
3. RF-ID Identifikasi Nomer Keanggotaan
4. Opencv Template Matching
5. Pengendali Alat Listrik
6. Penjadwalaan Lampu dan Alat Listrik
7. Network Monitoring
8. Small IDS Control
9. Home Internet Access Control
10. Kontrol & Monitoring Level Air Menggungkan Bot Telegram pada Rumah Berbasis Raspberry PI
11. Sistem Pendeteksi Slot Parkir Berbasis Raspberry Dengan Notifikasi Telegram
Sabtu, 10 November 2018
Code sensor Ultrasonic
import RPi.GPIO
as GPIO #Import GPIO
library
import time #Import time library
GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering
GPIO.setwarnings(False)
TRIG = 23 #Associate pin 23 to TRIG
ECHO = 24 #Associate pin 24 to ECHO
print "Distance measurement in progress"
GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in
while True:
GPIO.output(TRIG, False) #Set TRIG as LOW
#print "Waitng For Sensor To Settle"
time.sleep(2) #Delay of 2 seconds
GPIO.output(TRIG, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG, False) #Set TRIG as LOW
while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH
pulse_end = time.time() #Saves the last known time of HIGH pulse
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round to two decimal points
if distance > 2 and distance < 400: #Check whether the distance is within range
print "Distance:",distance - 0.5,"cm" #Print distance with 0.5 cm calibration
else:
print "Out Of Range" #display out of range
import time #Import time library
GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering
GPIO.setwarnings(False)
TRIG = 23 #Associate pin 23 to TRIG
ECHO = 24 #Associate pin 24 to ECHO
print "Distance measurement in progress"
GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in
while True:
GPIO.output(TRIG, False) #Set TRIG as LOW
#print "Waitng For Sensor To Settle"
time.sleep(2) #Delay of 2 seconds
GPIO.output(TRIG, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG, False) #Set TRIG as LOW
while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH
pulse_end = time.time() #Saves the last known time of HIGH pulse
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round to two decimal points
if distance > 2 and distance < 400: #Check whether the distance is within range
print "Distance:",distance - 0.5,"cm" #Print distance with 0.5 cm calibration
else:
print "Out Of Range" #display out of range
MENGGUNAKAN 2 ULTRASONIC
import
serial
from time import gmtime, strftime
from random import randint
import RPi.GPIO as GPIO #Import GPIO library
import time #Import time library
GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering
import MySQLdb
GPIO.setwarnings(False)
TRIG = 23 #Associate pin 23 to TRIG
ECHO = 24 #Associate pin 24 to ECHO
TRIG1 = 18 #Associate pin 23 to TRIG
ECHO2 = 27
db = MySQLdb.connect("localhost","root","12345","db_pemesanan_parkir")
cursor = db.cursor()
st=0
st2=0
print "Distance measurement in progress"
GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in
GPIO.setup(TRIG1,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO2,GPIO.IN) #Set pin as GPIO in
while True:
GPIO.output(TRIG, False) #Set TRIG as LOW
#print "Waitng For Sensor To Settle"
time.sleep(1) #Delay of 2 seconds
GPIO.output(TRIG, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG, False) #Set TRIG as LOW
while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH
pulse_end = time.time() #Saves the last known time of HIGH pulse
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round to two decimal points
R=distance - 0.5
if R > 2 and R < 5 and st<1: #Check whether the distance is within range
st=1
print "TERISI:",R,"cm" #Print distance with 0.5 cm calibration
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="IA"
status="TERISI"
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707001'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
if R> 5 and st>0:
print "KOSONG:" ,R,"cm" #display out of range
st=0
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="IA"
status="KOSONG"
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707001'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
GPIO.output(TRIG1, False) #Set TRIG as LOW
#print "Waitng For Sensor To Settle"
time.sleep(1) #Delay of 2 seconds
GPIO.output(TRIG1, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG1, False) #Set TRIG as LOW
while GPIO.input(ECHO2)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO2)==1: #Check whether the ECHO is HIGH
pulse_end = time.time() #Saves the last known time of HIGH pulse
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round to two decimal points
R=distance - 0.5
if R > 2 and R < 5 and st2<1: #Check whether the distance is within range
st2=1
print "ADA:",R,"cm" #Print distance with 0.5 cm calibration
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="BI"
status="ADA"
#sql ="INSERT INTO `tb_parkir` (`tanggal`, `jam`, `area`, `status`, `keterangan`) VALUES ('%s', '%s', '%s', '%s', '%s') " % (tgl,jam,area,status,ket)
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707002'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
if R> 5 and st2>0:
print "TIDAK ADA:" ,R,"cm" #display out of range
st2=0
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="IB"
status="TIDAK ADA"
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707002'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
from time import gmtime, strftime
from random import randint
import RPi.GPIO as GPIO #Import GPIO library
import time #Import time library
GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering
import MySQLdb
GPIO.setwarnings(False)
TRIG = 23 #Associate pin 23 to TRIG
ECHO = 24 #Associate pin 24 to ECHO
TRIG1 = 18 #Associate pin 23 to TRIG
ECHO2 = 27
db = MySQLdb.connect("localhost","root","12345","db_pemesanan_parkir")
cursor = db.cursor()
st=0
st2=0
print "Distance measurement in progress"
GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in
GPIO.setup(TRIG1,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO2,GPIO.IN) #Set pin as GPIO in
while True:
GPIO.output(TRIG, False) #Set TRIG as LOW
#print "Waitng For Sensor To Settle"
time.sleep(1) #Delay of 2 seconds
GPIO.output(TRIG, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG, False) #Set TRIG as LOW
while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH
pulse_end = time.time() #Saves the last known time of HIGH pulse
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round to two decimal points
R=distance - 0.5
if R > 2 and R < 5 and st<1: #Check whether the distance is within range
st=1
print "TERISI:",R,"cm" #Print distance with 0.5 cm calibration
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="IA"
status="TERISI"
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707001'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
if R> 5 and st>0:
print "KOSONG:" ,R,"cm" #display out of range
st=0
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="IA"
status="KOSONG"
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707001'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
GPIO.output(TRIG1, False) #Set TRIG as LOW
#print "Waitng For Sensor To Settle"
time.sleep(1) #Delay of 2 seconds
GPIO.output(TRIG1, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG1, False) #Set TRIG as LOW
while GPIO.input(ECHO2)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO2)==1: #Check whether the ECHO is HIGH
pulse_end = time.time() #Saves the last known time of HIGH pulse
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round to two decimal points
R=distance - 0.5
if R > 2 and R < 5 and st2<1: #Check whether the distance is within range
st2=1
print "ADA:",R,"cm" #Print distance with 0.5 cm calibration
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="BI"
status="ADA"
#sql ="INSERT INTO `tb_parkir` (`tanggal`, `jam`, `area`, `status`, `keterangan`) VALUES ('%s', '%s', '%s', '%s', '%s') " % (tgl,jam,area,status,ket)
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707002'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
if R> 5 and st2>0:
print "TIDAK ADA:" ,R,"cm" #display out of range
st2=0
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
ket=str(R)
area="IB"
status="TIDAK ADA"
sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707002'" % (tgl,jam,area,status,ket)
#print (sql)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
Jumat, 02 Maret 2018
Install VNC Server in Raspberry PI
Setting up your Raspberry Pi
VNC Connect is included with Raspbian but you still have to enable it.
First, run the following commands to make sure you have the latest version of VNC Server and VNC Viewer:
sudo apt-get update
sudo apt-get install realvnc-vnc-server
sudo apt-get install realvnc-vnc-viewer
If you’re already using an older version of VNC Server, restart it. If not, and you’re already booted into the PIXEL desktop, select Menu > Preferences > Raspberry Pi Configuration > Interfaces and make sure VNC is set to Enabled.
Alternatively, run the command
sudo raspi-config
, navigate to Advanced Options > VNC and select Yes.Jumat, 12 Januari 2018
Konfigurasi Sensor Vibration on Rapberry PI
SCHEMATIC:
Python Code:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
def callback(channel):
if GPIO.input(channel):
print "Gerakan Terdeteksi!"
else:
print "Gerakan Terdeteksi!"
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)
reff : http://www.piddlerintheroot.com/vibration-sensor/
Python Code:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
def callback(channel):
if GPIO.input(channel):
print "Gerakan Terdeteksi!"
else:
print "Gerakan Terdeteksi!"
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)
reff : http://www.piddlerintheroot.com/vibration-sensor/
Konfigurasi Sensor PIR on Rapberry PI
How it Works
PIR (passive infrared) motion sensor detects any movement of objects, human or animals. Mostly they are used in automatically activated lighting and burglar alarm systems.
Every object with temperature above absolute zero emit heat in the form of infrared radiation. PIR motion sensor detects change in the infrared radiation impinging on it. When any object or human passes in the front of the PIR sensor, the temperature in sensor’s field of view will rise from ambient temperature to object temperature and then back again to ambient temperature. PIR sensor converts this temperature change into the change in output voltage and that change in voltage is considered as motion detected.
PIR Sensor
Sensor
Sensor has three pins. Power (VCC), Ground (GND) and output (OUT) pin which gives logic high if motion is detected. It has two potentiometers. One for sensitivity adjustment and second for adjusting the time the output signal stays high when motion is detected.
Python Code :
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN) #PIR
GPIO.setup(24, GPIO.OUT) #BUzzer
try:
time.sleep(2) # to stabilize sensor
while True:
if GPIO.input(23):
GPIO.output(24, True)
time.sleep(0.5) #Buzzer turns on for 0.5 sec
GPIO.output(24, False)
print("Motion Detected...")
time.sleep(5) #to avoid multiple detection
time.sleep(0.1) #loop delay, should be less than detection delay
except:
GPIO.cleanup()
Selasa, 19 Desember 2017
Setting DHT22 & LCD 16x2 di Raspberry PI
Raspberry PI DHT22 humidity sensor with a LCD 16×2 display – Weather station
For a workshop I’m going to give about Raspberry PI and how to interact with the outside world, I thought for a start, why not something simple like some kind of a Weather Station.
Materials
1x Raspberry PI (I’m using b+)
1x LCD 16×2
1x DHT22 (or DHT11 or am2303) sensor
1x 10K potentiometer
1x 10K resistor
1x breadboard
Here’s the schematic in Fritzing
Connections
Sensor | Arduino PINs | ||
LCD display | Board | BCM | |
1 | GND | ||
2 | 5v | ||
3 | Potentiometer | ||
4 | 7 | GPIO27 | |
5 | GND | ||
6 | 9 | GPIO22 | |
7 | |||
8 | |||
9 | |||
10 | |||
11 | 22 | GPIO25 | |
12 | 18 | GPIO24 | |
13 | 16 | GPIO23 | |
14 | 12 | GPIO18 | |
15 | 5v | ||
16 | 7 | GPIO4 | |
DHT22 | |||
8 | GPIO14 | ||
GND | |||
5v |
After the wiring, time to connect the PI
Note: Using this wiring and the Adafruit’s LCD libraries, the LCD display will not light up when connecting the PI. After testing the example and running the python script, the LCD will light up and start displaying information
Software
DHT
For the sensor to work, we need the DHT libraries developed by Adafruit and setup the environment.
NOTE: As stated by Adafruit, this driver is work in progress
First, set up your development environment
1
|
sudo apt-get install build-essential python-dev python-openssl
|
Now, clone the Adafruit libraries
1
|
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
|
After cloned, enter the directory
1
|
cd Adafruit_Python_DHT
|
Install the libraries
1
|
sudo python setup.py install
|
Everything now should be installed. Let’s try the sensor
1
|
cd examples
|
Now, execute the test file without arguments to see the options available
1
2
3
|
pi@raspberrypi ~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py
usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#
example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4
|
We’re using DHT22 and connected to the GPIO PIN 4, so, our command line will be:
1
|
sudo ./AdafruitDHT.py 22 14
|
And the result:
1
|
Temp=27.0* Humidity=50.8%
|
LCD 16×2
With this method, we’re not going to use i2c or UART, but Adafruit’s libraries for LCD. They are easy to install and easy to work with.
Note: If you just want to connect the LCD, refere to this post.
Install the necessary packages
1
|
sudo apt-get install python-setuptools
|
Upgrade distribute
1
|
sudo easy_install -U distribute
|
1
|
sudo apt-get install python-pip python-smbus
|
Use PIP to install the GPIO packages
1
|
sudo pip install rpi.gpio
|
Install the necessary software modules
1
|
git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git
|
1
|
cd Adafruit_Python_CharLCD
|
1
|
sudo python setup.py install
|
Try with an example
1
2
|
cd examples
python ./char_lcd.py
|
Now, you can see information on the LCD display
If you want to check the API of the LCD Adafruit’s Python Library:
1
2
3
4
5
6
|
python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Adafruit_CharLCD as LCD
>>> help(LCD.Adafruit_CharLCD)
|
The Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#import libraries
import RPi.GPIO as GPIO
import time
import sys
# DHT sensor
import Adafruit_DHT
# LCD
import Adafruit_CharLCD as LCD
# Define sensor
sensor = Adafruit_DHT.AM2302
pin = 14
# Raspberry PI connections
lcd_rs = 27 # Older versions if not working change to 21
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_backlight = 4
# Define some device constants
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows, lcd_backlight)
while True:
try:
# Get values
humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
temp = "{:0.1f}*C".format(temperature)
hum = "{:0.1f}%".format(humidity)
# clear lcd
lcd.clear()
# display temperature
lcd.message ("Temperatura:\n")
# Mostrar a temperatura quase no final
toMove = lcd_columns - len(temp)
lcd.set_cursor(toMove,1)
lcd.message(temp)
time.sleep(5) # 5 second delay
lcd.clear()
# display humidity
lcd.message ("Humidade:\n")
toMove = lcd_columns - len(hum)
lcd.set_cursor(toMove,1)
lcd.message (hum)
time.sleep(5)
except KeyboardInterrupt:
lcd.clear()
lcd.message("Adeus!")
time.sleep(3)
sys.exit()
|
The final product
Langganan:
Postingan (Atom)