1. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
1
MODULE WORKSHOP “ Raspberry Pi3 With Python”
2018
2. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
2
Secript Python
1. turn-off-led.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16,GPIO.OUT)
GPIO.output(16,GPIO.HIGH)
time.sleep(2)
GPIO.output(16,GPIO.LOW)
GPIO.cleanup()
Ket:
Import RPi.GPIO as GPIO Merupakan modul atau library yang digunakan untuk
mengakses GPIO pin pada raspberry pi.
Import Rpi.GPIO as GPIO Is a module or library that is used to access GPIO pins on
raspberry pi.
Import time juga merupakan library atau modul yang digunakan untuk pengoperasian
terkait dengan waktu.
Import time Is a module or library that is used to access about the time related.
GPIO setmode(GPIO.BCM) untuk mengaktifkan sistem penomoran pin GPIO.
GPIO setmode(GPIO.BCM) is command to activating numbering system of GPIO pins.
GPIO.setwarnings(False) memberitahu python untuk mengabaikan perintah jika pin
GPIO sudah digunakan.
GPIO.setwarnings(False) is command to show the python to ignor notice if GPIO pin has
been used.
GPIO.setup(16,GPIO.OUT) memberitahu python apabila pin GPIO 16 yang akan
digunakan.
GPIO.setup(16,GPIO.OUT) is command to show the pyhton if GPIO pin 16 has been
used.
GPIO.output(16,GPIO.HIGH) perintah menyalakan lampu LED
3. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
3
GPIO.output(16,GPIO.HIGH) is command to turn on of the LED lights.
Time.sleep(2) Perintah untuk menjeda nyala lampu LED
Time.sleep(2) is command to interrupt turn on of the LED lights.
GPIO.output(16,GPIO.LOW) perintah untuk mematikan lampu LED
GPIO.output(16,GPIO.LOW) is command to turn off the LED lights.
GPIO.cleanup() perintah untuk memastikan bahwa tidak ada arus listrik di pin GPIO
GPIO.cleanup() is command to makesure if nothing electric current on GPIO pins.
2. blink.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16,GPIO.OUT)
for i in range(3):
GPIO.output(16, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(16, GPIO.LOW)
time.sleep(0.5)
GPIO.cleanup()
Ket:
For i in range(3) ini adalah perintah perulangan(for loop) yang nantinya lampu LED
akan berkedip sebanyak 3 kali.
For i in range(3) is command to repeat (for loop) which the LED lights will blink 3 times
Time.sleep(0.5) waktu jeda lampu
Time.sleep(0.5) is break time of the lights
4. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
4
3. traffic.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
pinList =[16,20]
SleepTime = 1
for i in pinList:
GPIO.setup(i, GPIO.OUT)
for i in pinList:
GPIO.output(i, GPIO.HIGH)
time.sleep(SleepTime)
GPIO.output(i, GPIO.LOW)
time.sleep(SleepTime)
GPIO.cleanup()
Ket:
PinList=[16,20] ini adalah perintah List yang akan memberitahu kepada python bahwa pin
GPIO tersebut akan digunakan
PinList=[16,20] this is command to list which give the notice to python if the GPIO pins
has been used.
SleepTime=1 adalah perintah jeda waktu lampu LED.
SleepTime=1 is command the break time of LED lights.
4. flip_flop.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
5. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
5
pinList =[16,20]
SleepTime = 0.2
for i in pinList:
GPIO.setup(i, GPIO.OUT)
try:
while True:
for i in pinList:
GPIO.output(i, GPIO.HIGH)
time.sleep(SleepTime)
GPIO.output(i, GPIO.LOW)
time.sleep(SleepTime)
except KeyboardInterrupt:
pass
GPIO.cleanup()
Ket:
flip-flop tidak jauh berbeda dengan traffic hanya saja di flip-flop loop-nya terus
menerus(infinite)
Flip-flop not really different from traffic only in the flip-flop looping continuously
(infinite)
SleepTiem= 0.2 adalah jeda waktu lampu ketika sedang dioprasikan.
SleepTiem=0.2 is the break time of the lights while being operated.
Except KeyboardInterrup() mengalihkan loop dan akan diterusakn ke GPIO.cleanup
jika ada intrupsi dari keyboard dengan menekan (CRL+C).
Except KeyboardInterrupt is command to switch the loop and will be continue to GPIO.cleanup if
there is an interruption from the keyboard by pressing (CRL+C)
5. piemail.py
import RPi.GPIO as GPIO
import time, math
import smtplib, time
7. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
7
message_c = "light 2 telah menyala "
message_d = "light 2 telah padam "
message_e = "light 3 telah menyala "
message_f = "light 3 telah padam "
message_g = "lights 1,2 dan 3 menyala "
message_h = "lights 1,2 dan 3 telah padam "
while (jawab == 'y'):
perintah = input('masukan perintah : ')
if perintah == 'ON led1':
GPIO.output(16,GPIO.HIGH)
send_email(username, password, recipient, subject, message_a)
status = 'led 1 menyala'
elif perintah == 'OFF led1':
GPIO.output(16,GPIO.LOW)
send_email(username, password, recipient, subject, message_b)
status = 'led 1 padam'
elif perintah == 'ON led2':
GPIO.output(20,GPIO.HIGH)
send_email(username, password, recipient, subject, message_c)
status = 'led 2 menyala'
elif perintah == 'OFF led2':
GPIO.output(20,GPIO.LOW)
send_email(username, password, recipient, subject, message_d)
status = 'led 2 padam'
elif perintah == 'ON semua':
GPIO.output(16,GPIO.HIGH)
GPIO.output(20,GPIO.HIGH)
send_email(username, password, recipient, subject, message_g)
status = 'semua led menyala'
elif perintah == 'OFF semua':
8. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
8
GPIO.output(16,GPIO.LOW)
GPIO.output(20,GPIO.LOW)
send_email(username, password, recipient, subject, message_h)
status = 'semua led padam'
else :
status = 'no perintah'
print ('status : ' + status)
jawab = input("tambah perintah ?")
print ('terimakasih')
Ket:
import time, math merupakan library atau modul yang digunakan untuk pengoperasian
terkait dengan waktu
Import time, math is library or module to use for related operation with the time.
import smtplib,time modul yang digunakan untuk mengirim email
Import smtlib,time is module which use for sending email.
SMTP_SERVER= 'smtp.gmail.com' Protokol yang digunakn untuk mengirim pesan e-
mail antar server.
SMTP SERVER = ‘smtp.gmail com’ is the protocol to used for sending email massage
between server.
SMTP_PORT = 587 Protokol koneksi yang menghubungkan program
SMTP PORT = 587 is the connection protocol to connect the program
def sendiri adalah Identitas
Def is identification.
send-email disebut fungsi
Send-email is funtion
(username, password, recipient, subject, text) ini di sebut parameter
(Username, password, recepient, subject, text) is parameters.
Print digunakan untuk mencetak nilai di dalam parameter ke-layar
Print is used for print the value to display in the parameter.
9. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
9
smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) Untuk mengkoneksikan
dan mengirim email
smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) For connecting and
sending email.
smtpserver.ehlo() memberitahu dan akan mengalihkan printah ketika server tidak
mendukung
smtserver.ehlo() for notice and switch off command when the server not support.
smtpserver.starttls() mengirim perintah
Smtpserver.starttls() to send the command.
smtpserver.login(username, password) berfungsi untuk masuk ke akun email.
smtpserver.login(username, password) Function to login email account
header disini untuk membuat form
Header is use for make a form
smtpserver.sendmail(username, recipient, msg) perintah yang akan mengirim email
smtpserver.sendmail(username, recipient, msg Command for sending email
if , elif, else adalah statement di dalam python yang berguna untuk membangun alur
logika pada program
If, elif, else is a statement in python that is useful for building logic paths in the program.
if adalah suatu kondisi dalam pemrograman yang berarti(jika)
If is condition of the meaning (if) in the program
elif, juga suatu kondisi dalam pemrograman yang berarti (dan jika)
Elif is condition of the meaning (and if) in the program.
else juga termasuk dalam kategori kondisi pemrograman yang berarti(selain)
Else is also included in the category of programming conditions which the meaning
(other than)
6. Percabangan.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
10. Ngoding Study Club 2018
Workshop Raspberry Pi3 With Python
10
GPIO.setup(16,GPIO.OUT)
GPIO.setup(20,GPIO.OUT)
jawab = "y"
while (jawab == 'y') :
perintah = input ('masukan perintah : ')
if perintah == 'ON led1':
GPIO.output(16,GPIO.HIGH)
status = 'led 1 menyala'
elif perintah == 'OFF led1':
GPIO.output(16,GPIO.LOW)
status = 'led 1 padam'
elif perintah == 'ON led2':
GPIO.output(20,GPIO.HIGH)
status = 'led 2 menyala'
elif perintah == 'OFF led2':
GPIO.output(20,GPIO.LOW)
status = 'led 2 padam'
elif perintah == 'ON semua':
GPIO.output(16,GPIO.HIGH)
GPIO.output(20,GPIO.HIGH)
status = 'semua led menyala'
elif perintah == 'OFF semua':
GPIO.output(16,GPIO.LOW)
GPIO.output(20,GPIO.LOW)
status = 'semua led padam'
else :
status = 'no perintah'
print ('status : ' + status)
jawab = input("tambah perintah ? ")
print ('terimakasih')