Sabtu, 28 Januari 2017

Cara mengatur & Menjalankan camera v2 pada Raspberry PI 3

Pertama anda harus menghubungkan modul camera v2 ke port Raspberry PI, dan memastikan perangkat lunak diaktifkan.


1.Cari port kamera dan menghubungkan kamera:

2.Start up Pi tersebut.
3.Buka Configuration Tool Raspberry Pi dari menu utama:

4.Pastikan kamera software diaktifkan:

5. Setelah itu reboot Pi Anda untuk memulai.

Pratinjau Kamera
Sekarang kamera Anda terhubung dan perangkat lunak sudah diaktifkan, Anda bisa memulai dengan mencoba preview kamera.
1.Buka Python 3 dari menu utama:
2.Buka file baru dan simpan sebagai camera.py di direktori /home/pi
3.Masukkan script berikut:
from picamera import PiCamera
from time import sleep

camera = PiCamera()

camera.start_preview()
sleep(10)
camera.stop_preview()


4.Simpan dengan Ctrl + S dan jalankan dengan F5. Kamera akan ditampilkan selama 10 detik, dan menutup otomotis. Atau jalankan kamera anda melalui terminal, buka terminal ctrl+alt+t ketik :
sudo -i
cd /home/pi
python camera.py
Preview kamera hidup harus mengisi layar seperti ini:
NB : preview kamera hanya bekerja ketika monitor dihubungkan ke Pi, sehingga akses remote (seperti SSH dan VNC) tidak akan dapat melihat pratinjau kamera

Jika kamera adalah terbalik, Anda dapat memutar dengan kode berikut:
 camera.rotation = 180
camera.start_preview()
sleep(10)
camera.stop_preview()
Anda dapat memutar gambar sebesar 90, 180, 270 derajat, atau Anda dapat mengaturnya untuk 0 untuk me-reset.

6.Anda dapat mengubah transparansi dari kamera pratinjau dengan menetapkan tingkat alpha:

from picamera import PiCamera
from time import sleep

camera = PiCamera()

camera.start_preview(alpha=200)
sleep(10)
camera.stop_preview()
alpha dapat berupa nilai antara 0 dan 255.

Mengambil Gambar
Penggunaan paling umum kamera untuk mengambil gambar.
1.Edit script python yang anda buat tadi dengan menambahkan camera.capture () pada baris:
camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
Jalankan lagi kamera  anda dengan perintah python camera.py, maka kamera akan mengambil gambar dan akan menyimpan hasinya di /home/pi/Desktop/image.jpg

2.Sekarang coba tambahkan script berikut untuk mengambil 5 gambar berturut-turut :
camera.start_preview()
for i in range(5):
    sleep(5)
    camera.capture('/home/pi/Desktop/image%s.jpg' % i)
camera.stop_preview()
Variabel i berisi nomor iterasi saat ini, dari 0 sampai 4 ,sehingga gambar akan disimpan sebagai image0.jpg, gambar1.jpg dan seterusnya.

Kamis, 26 Januari 2017

Tutorial Backup dan Restore konfigurasi Raspberry PI


Backup
Pertama siapkan aplikasi Win32 Disk Imager


image file (tempat kita akan me-backup/menyimpan konfigurasi raspberry, pilih lokasi nya dimana saja)
device (sd card yang terbaca, yang isinya sudah ada konfigurasi raspberry)
jika suda menentukan lokasinya, maka pilih tombol read untuk memulai penyimpanan (backup)
c

Restore
Pertama siapkan aplikasi Win32 Disk Imager



image file (tempat kita  me-backup/menyimpan konfigurasi raspberry pi )


jika file tida terbaca, maka pda format file name harus dipilih seperti gambar di atas. Agar semua file dapat terbaca.
Lalu klik open


device (sd card yang yang sudah kita format)
Jika suda maka klik tombol write

tunggu sampai selesai..

Codingan python untuk camera Raspberry agar terkoneksi ke web server

Codingan python untuk camera Raspberry agar terkoneksi ke web server
bahan :
1. Siapkan camera raspberry pi (saya menggunakan camera v2 dan raspberry pi 3)
2. Siapkan Hardware arduino

Langkah yang saya buat hanya akan berjalan jika kita sudah update raspberry dan instal web server (lihat di postingan saya sebelumnya)

pertama kita instal mysqldb, agar python dapat terhubung ke database. Ikuti perintah ini :

sudo apt-get install python-mysqldb

Setelah itu kita hubungkan arduino ke raspberry dengan menggunakan kabel serial, lalu jalankan codingan python nya, copy paste script berikut kemudian simpan dengan format .py(lat1.py) :

import serial

import MySQLdb
from time import gmtime, strftime
from random import randint
from picamera import PiCamera
from time import sleep


camera = PiCamera()
camera.rotation = 180

db = MySQLdb.connect("localhost","root","123456","db_project")
cursor = db.cursor()

def mtrim(s):
    if s.endswith(" "): s = s[:-1]
    if s.startswith(" "): s = s[1:]
    return s


read_serial0=""
ser = serial.Serial('/dev/ttyACM1',9600)
#ser = serial.Serial('COM3',9600)

s = [0,1]
while True:
 read_serial=ser.readline()
 read_serial=mtrim(read_serial)
 c=read_serial.split("#")
 print (read_serial)
 print (str(len(c)))
 sleep(5)
 if len(c) >2:
     if read_serial!=read_serial0:
         
         read_serial0=read_serial
         idx=c[0]
         ruang=c[1]
         
         print ("idx=",idx)
         print ("ruang=",ruang)
         
         tgl=strftime("%Y-%m-%d", gmtime())
         jam=strftime("%H:%M:%S", gmtime())

         stgl=strftime("%Y%m%d", gmtime())
         sjam=strftime("%H%M%S", gmtime())

         img=   stgl+sjam+".jpg";

         ###########################################

         try:
             camera.start_preview()

             sleep(3)
             camera.capture('/var/www/html/project/ypathfile/'+img)
             sleep(1)
             camera.stop_preview()
         except:
             print("-")
         ###########################################
         sql='INSERT INTO `tb_history` (`tanggal`, `jam`, `ruang`, `ksj`, `image`, `note`) VALUES ("%s", "%s", "%s", "%s", "%s", "%s")' % (tgl, jam, ruang, idx, img,'-')
         try:
           cursor.execute(sql)
           db.commit()
         except:
           db.rollback()
          
     
         print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")


Setelah selesai di save lalu jalankan pythonnya, disini saya misalkan kita menyimpan lat1.py di /home/pi
Buka Terminal 
masuk direktori /home/pi dengan cara :
cd /home/pi 
/home/pi# python lat1.py

jika ada error seperti di bawah ini 

 File "SJ.py", line 23, in <module>
    ser = serial.Serial('/dev/ttyACM1',9600)
  File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__
    self.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 278, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyACM1: [Errno 2] No such file or directory: '/dev/ttyACM1'

perhatikan pada baris terakhir, disitu ada '/dev/ttyACM1', berarti kabel serial arduino tidak terbaca oleh python. Untuk mengatasinya kita kembali ke terminal & ketikkan perintah berikut :

ls /dev/tty*  (untuk mengetahui serial port yang terbaca oleh python)

/dev/tty    /dev/tty19 /dev/tty3   /dev/tty40 /dev/tty51  /dev/tty62
/dev/tty0   /dev/tty2 /dev/tty30  /dev/tty41 /dev/tty52  /dev/tty63
/dev/tty1   /dev/tty20 /dev/tty31  /dev/tty42 /dev/tty53  /dev/tty7
/dev/tty10  /dev/tty21 /dev/tty32  /dev/tty43 /dev/tty54  /dev/tty8
/dev/tty11  /dev/tty22 /dev/tty33  /dev/tty44 /dev/tty55  /dev/tty9
/dev/tty12  /dev/tty23 /dev/tty34  /dev/tty45 /dev/tty56  /dev/ttyACM0
/dev/tty13  /dev/tty24 /dev/tty35  /dev/tty46 /dev/tty57  /dev/ttyAMA0
/dev/tty14  /dev/tty25 /dev/tty36  /dev/tty47 /dev/tty58  /dev/ttyprintk
/dev/tty15  /dev/tty26 /dev/tty37  /dev/tty48 /dev/tty59
/dev/tty16  /dev/tty27 /dev/tty38  /dev/tty49 /dev/tty6
/dev/tty17  /dev/tty28 /dev/tty39  /dev/tty5   /dev/tty60
/dev/tty18  /dev/tty29 /dev/tty4   /dev/tty50 /dev/tty61

yang terbaca oleh python adalah /dev/ttyACM0, jadi pada script python kita harus menggantinya, 

ser = serial.Serial('/dev/ttyACM1',9600) ganti menjadi ser = serial.Serial('/dev/ttyACM0',9600)

selesai, lalu jalankan kembali pythonnya, maka python akan mulai mengambil gambar dan hasilnya akan langsung dikirim ke web server.
Hasil/gambarnya juga akan tersimpan secara otomatis di /var/www/html/project/ypathfile (lihat script python).

Rabu, 25 Januari 2017

camera configuration v2 on raspberry pi 3

First of all, with the Pi switched off, you'll need to connect the camera module to the Raspberry Pi's camera port, then start up the Pi and ensure the software is enabled.
  1. Locate the camera port and connect the camera:
    Connect the camera
  2. Start up the Pi.
  3. Open the Raspberry Pi Configuration Tool from the main menu:
    Raspberry Pi Configuration Tool
  4. Ensure the camera software is enabled:
    Camera software enabled
    If it's not enabled, enable it and reboot your Pi to begin.

Camera preview

Now your camera is connected and the software is enabled, you can get started by trying out the camera preview.
  1. Open Python 3 from the main menu:
    Open Python 3
  2. Open a new file and save it as camera.py. It's important that you do not save it as picamera.py.
  3. Enter the following code:
    from picamera import PiCamera
    from time import sleep
    
    camera = PiCamera()
    
    camera.start_preview()
    sleep(10)
    camera.stop_preview()
  4. Save with Ctrl + S and run with F5. The camera preview should be shown for 10 seconds, and then close. Move the camera around to preview what the camera sees.
    The live camera preview should fill the screen like so:
    Image preview
    Note that the camera preview only works when a monitor is connected to the Pi, so remote access (such as SSH and VNC) will not allow you to see the camera preview
  5. If your preview was upside-down, you can rotate it with the following code:
    camera.rotation = 180
    camera.start_preview()
    sleep(10)
    camera.stop_preview()
    You can rotate the image by 90180, or 270 degrees, or you can set it to 0 to reset.
  6. You can alter the transparency of the camera preview by setting an alpha level:
    from picamera import PiCamera
    from time import sleep
    
    camera = PiCamera()
    
    camera.start_preview(alpha=200)
    sleep(10)
    camera.stop_preview()
    alpha can be any value between 0 and 255.

Still pictures

The most common use for the camera module is taking still pictures.
  1. Amend your code to reduce the sleep and add a camera.capture() line:
    camera.start_preview()
    sleep(5)
    camera.capture('/home/pi/Desktop/image.jpg')
    camera.stop_preview()
    It's important to sleep for at least 2 seconds before capturing, to give the sensor time to set its light levels.
  2. Run the code and you'll see the camera preview open for 5 seconds before capturing a still picture. You'll see the preview adjust to a different resolution momentarily as the picture is taken.
  3. You'll see your photo on the Desktop. Double-click the file icon to open it:
    Image on Desktop
  4. Now try adding a loop to take five pictures in a row:
    camera.start_preview()
    for i in range(5):
        sleep(5)
        camera.capture('/home/pi/Desktop/image%s.jpg' % i)
    camera.stop_preview()
    The variable i contains the current iteration number, from 0 to 4, so the images will be saved asimage0.jpgimage1.jpg and so on.
  5. Run the code again and hold the camera in position. It will take one picture every five seconds.
  6. Once the fifth picture is taken, the preview will close. Now look at the images on your Desktop and you'll see five new pictures.

Recording video

Now you've used the camera to take still pictures, you can move on to recording video.
  1. Amend your code to replace capture() with start_recording() and stop_recording():
    camera.start_preview()
    camera.start_recording('/home/pi/video.h264')
    sleep(10)
    camera.stop_recording()
    camera.stop_preview()
  2. Run the code; it will record 10 seconds of video and then close the preview.
  3. To play the video, you'll need to open a terminal window by clicking the black monitor icon in the taskbar:
    Open Terminal
  4. Type the following command and press Enter to play the video:
    omxplayer video.h264
    omxplayer
  5. The video should play. It may actually play slightly faster than it was recorded, due to omxplayer's fast frame rate.

Effects

At the beginning, you created a camera object with camera = PiCamera(). You can manipulate this camera object in order to configure its settings. The camera software provides a number of effects and other configurations you can apply. Some only apply to the preview and not the capture, others apply to the capture only, but many affect both.
  1. The resolution of the capture is configurable. By default it's set to the resolution of your monitor, but the maximum resolution is 2592 x 1944 for still photos and 1920 x 1080 for video recording. Try the following example to set the resolution to max. Note that you'll also need to set the frame rate to15 to enable this maximum resolution:
    camera.resolution = (2592, 1944)
    camera.framerate = 15
    camera.start_preview()
    sleep(5)
    camera.capture('/home/pi/Desktop/max.jpg')
    camera.stop_preview()
  2. The minimum resolution allowed is 64 x 64. Try taking one at that resolution.
  3. You can easily add text to your image with annotate_text. Try it:
    camera.start_preview()
    camera.annotate_text = "Hello world!"
    sleep(5)
    camera.capture('/home/pi/Desktop/text.jpg')
    camera.stop_preview()
  4. You can alter the brightness setting, which can be set from 0 to 100. The default is 50. Try setting it to another value:
    camera.start_preview()
    camera.brightness = 70
    sleep(5)
    camera.capture('/home/pi/Desktop/bright.jpg')
    camera.stop_preview()
  5. Try adjusting the brightness in a loop, and annotating the display with the current brightness level:
    camera.start_preview()
    for i in range(100):
        camera.annotate_text = "Brightness: %s" % i
        camera.brightness = i
        sleep(0.1)
    camera.stop_preview()
  6. Similarly, try the same for the contrast:
    camera.start_preview()
    for i in range(100):
        camera.annotate_text = "Contrast: %s" % i
        camera.contrast = i
        sleep(0.1)
    camera.stop_preview()
  7. You can set the annotation text size with the following code:
    camera.annotate_text_size = 50
    Valid sizes are 6 to 160. The default is 32.
  8. You can also alter the annotation colours. First of all, ensure that Color is imported by amending your import line at the top:
    from picamera import PiCamera, Color
    Then amend the rest of your code as follows:
    camera.start_preview()
    camera.annotate_background = Color('blue')
    camera.annotate_foreground = Color('yellow')
    camera.annotate_text = " Hello world "
    sleep(5)
    camera.stop_preview()
  9. You can use camera.image_effect to apply a particular image effect. The options are: none,negativesolarizesketchdenoiseembossoilpainthatchgpenpastel,watercolorfilmblursaturationcolorswapwashedoutposterise,colorpointcolorbalancecartoondeinterlace1, and deinterlace2. The default isnone. Pick one and try it out:
    camera.start_preview()
    camera.image_effect = 'colorswap'
    sleep(5)
    camera.capture('/home/pi/Desktop/colorswap.jpg')
    camera.stop_preview()
  10. Try looping over the various image effects in a preview to test them out:
    camera.start_preview()
    for effect in camera.IMAGE_EFFECTS:
        camera.image_effect = effect
        camera.annotate_text = "Effect: %s" % effect
        sleep(5)
    camera.stop_preview()
    Effects
  11. You can use camera.awb_mode to set the auto white balance to a preset mode to apply a particular effect. The options are: offautosunlightcloudyshadetungsten,fluorescentincandescentflash, and horizon. The default is auto. Pick one and try it out:
    camera.start_preview()
    camera.awb_mode = 'sunlight'
    sleep(5)
    camera.capture('/home/pi/Desktop/sunlight.jpg')
    camera.stop_preview()
    You can loop over the available auto white balance modes with camera.AWB_MODES.
  12. You can use camera.exposure_mode to set the exposure to a preset mode to apply a particular effect. The options are: offautonightnightpreviewbacklightspotlight,sportssnowbeachverylongfixedfpsantishake, and fireworks. The default isauto. Pick one and try it out:
    camera.start_preview()
    camera.exposure_mode = 'beach'
    sleep(5)
    camera.capture('/home/pi/Desktop/beach.jpg')
    camera.stop_preview()
    You can loop over the available exposure modes with camera.EXPOSURE_MODES.