2/15/2019

Volumio :: Volumio On Rapberry PI

Volumio :: Volumio On Rapberry PI
1. Download Volumio
https://volumio.org/get-started/
2. ใช้ Program Disk Image Writer ใน Linux Restore ไฟล์ img ที่ ได้จากข้อ 1 ใส่ SD
3. นำ SD ใส่ PI Boot ต่อสาย Lan เพื่อให้ได้ IP User Password Default หน้า Terminal
User : root
Password : volumio
4. ตั้งค่า Wifi Fix IP
Set Time Zone ด้วยคำสั่ง
  1. dpkg-reconfigure tzdata

5. นำเพลงที่ต้องการใส่ SD
6. ตั้ง Playlist นำเพลงที่ต้องการเล่นใส่ PlayList
7. ตั้ง Alarm ตั้งเวลาและเลือก Play List ที่สร้างขึ้นในข้อ 6. (Bug VERSION: 2.526 เล่น Play List ได้อันเดียวได้ตัวล่างสุด)

มีอีกตัว ชื่อ max2play ทำงานเหมือนกัน
https://www.max2play.com/en/max2play-image/

PI :: Pi camera + Motion

PI :: Pi camera + Motion
1. Download OS PI
https://www.raspberrypi.org/downloads/raspbian/

2. เขียน .img ใส่ SD ด้วย Disk Image Writer
3. แก้ config.txt ให้ออกจอได้
disable_overscan=1
hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=35
hdmi_drive=2

4. เปิด ssh และ camera API ตั้ง Time Zone
  1. sudo raspi-config


5. ssh เข้าเครื่อง ติดตั้งโปรแกรม และ Config
  1. sudo apt-get install motion


6. ติดตั้ง bcm2835-v4l2
  1. sudo modprobe bcm2835-v4l2

แก้ไฟล์ /etc/modules
  1. nano /etc/modules

เพิ่ม
# camera with v4l2 driver
bcm2835-v4l2

แล้ว Reboot เครื่อง PI

https://raspberrypi.stackexchange.com/questions/10480/raspi-camera-board-and-motion

7. ทดสอบ
  1. ls /dev/video*

ลองถ่ายรูป
  1. raspistill -o image.jpg

ถ้าได้รูปแสดงว่ากล้องกับเครื่องต่อกันใช้งานได้แล้ว

8. ตั้งค่า Motion
  1. nano /etc/motion/motion.conf

แก้
# Start in daemon (background) mode and release terminal (default: off)
daemon on
...
# Restrict stream connections to localhost only (default: on)
stream_localhost off
...
# Target base directory for pictures and films
# Recommended to use absolute path. (Default: current working directory)
target_dir /home/pi/Monitor

The following lines are optimal and should also be changed (we have read them out previously):

v4l2_palette 15 # Nummer aus der Tabelle davor entnehmen, 15 enstpricht YUYV
...
# Image width (pixels). Valid range: Camera dependent, default: 352
width 640

# Image height (pixels). Valid range: Camera dependent, default: 288
height 480 # Maximum number of frames to be captured per second.

# Valid range: 2-100. Default: 100 (almost no limit).
framerate 10


9. กำหนดให้โปรแกรม Motion ทำงานเป็น Daemon Service
  1. sudo nano /etc/default/motion

start_motion_daemon=yes


10. Start motion
  1. sudo service motion start

ถ้าไม่ได้
  1. sudo service motion restart


11. เข้าดูผ่าน IP
http://192.168.0.181:8081

Config เพิ่มเติม
https://tutorials-raspberrypi.com/raspberry-pi-security-camera-livestream-setup/
https://intranet.sci.com/blog.php?u=281&b=1233

////################## Slack #########################/////
ติดตั้ง โปรแกรมเพิ่มเพื่อให้ Run Python
  1. sudo apt-get install python-picamera python3-picamera python-rpi.gpio


ติดตั้ง Slackclient เพื่อให้ส่งคำสั่งไป Slack ได้
  1. sudo apt-get install python-pip
  1. pip install slackclient


Code python Send Post Text To Slack
  1. nano /home/pi/slack_post.py
from slackclient import SlackClient
sc = SlackClient("xoxb-3713119008-518558293798-egdvc4A1x5M3w8xV1YfFzA5Y")

sc.api_call(
"chat.postMessage",
channel="CF6GEHBDF",
text="Hello from Python! camera :tada:"
)


Send Upload File Command Test.
  1. curl -F file=@03-20190208171217-00.jpg -F "initial_comment=I play the drums." -F channels=CF6GEHBDF -F thread_ts=1532293503.000001 -H "Authorization: Bearer xoxb-3713119008-518558293798-egdvc4A1x5M3w8xV1YfFzA5Y" https://slack.com/api/files.upload


https://api.slack.com/methods/files.upload

ทดสอบ Run คำสั่ง python
  1. python /home/pi/slack_post.py


Python upload
from slackclient import SlackClient
#### Upload IMG #####
sc = SlackClient("xoxb-3713119008-518558293798-egdvc4A1x5M3w8xV1YfFzA5Y")

with open('/home/pi/Monitor/14-20190212104004-08.jpg') as file_content:
sc.api_call(
"files.upload",
channels="CF6GEHBDF",
file=file_content,
title="Find movement in room."
)


https://slackapi.github.io/python-slackclient/basic_usage.html

ทดสอบ Run คำสั่ง python
  1. python /home/pi/slack_post.py


หา Last File .jpg ที่สร้างล่าสุด
import glob
import os

list_of_files = glob.glob('/home/pi/Monitor/*.jpg') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file


https://stackoverflow.com/questions/39327032/how-to-get-the-latest-file-in-a-folder-using-python

แก้ config motion ให้ไปเรียก Python เวลามีการเคลื่อนไหว
  1. nano /etc/motion/motion.conf

แก้
on_motion_detected python /home/pi/slack_post.py

แล้ว restart service
  1. /etc/init.d/motion restart


เมื่อมีการเคลื่อนไหว จะไปเรียก py มาทำงาน และส่งรูปไป Slack


Finish Code
  1.  
  2. import glob
  3. import os
  4.  
  5. list_of_files = glob.glob('/home/pi/Monitor/*.jpg') # * means all if need specific format then *.csv
  6. latest_file = max(list_of_files, key=os.path.getctime)
  7. #print latest_file
  8.  
  9. with open(latest_file) as file_content:
  10.     sc.api_call(
  11.         "files.upload",
  12.         channels="CF6GEHBDF",
  13.         file=file_content,
  14.         title="Find movement in room."
  15.     )


################### Backup To Qnap ################


การ mount Qnap เข้าเครื่อง Pi
สร้าง Folders ในเครื่อง Pi ก่อนที่เราจะ mount เข้ามา ใช้คำสั่ง
  1. sudo mount -t cifs -o username=*******,password=******* //192.168.0.218/backup /ตามด้วย Folders ที่เราสร้างขึ้นในเครื่อง Pi

สร้าง Files ที่ใช้สำหรับเขียนโค๊ตเพื่อสั่งให้เครื่อง Pi ทำไฟล์ให้เป็น .zip แล้ว Backup ไปยังเครื่อง Qnap
  1. ### backup_camera.sh ###
  2. #!/bin/bash
  3. date_2dayago=`date -d "2 day ago 13:00" '+%Y-%m-%d'`
  4. date_yesterday=`date -d "yesterday 13:00" '+%Y-%m-%d'`
  5. date_today=`date '+%Y-%m-%d'`
  6.  
  7. mkdir $date_yesterday
  8.  
  9. find /home/pi/Monitor -type f -name '*.avi' -newermt $date_2dayago ! -newermt $date_today -exec mv -t $date_yesterday {} +
  10.  
  11. tar -czf if_$date_yesterday.tar.gz $date_yesterday
  12.  
  13. mv if_$date_yesterday.tar.gz /BACKUP/BackupToQnap
  14.  
  15. rm -r $date_yesterday

ทดลองใช้งาน File ว่าจะส่งข้อมูลไปยังเครื่อง Qnap ได้ไม่โดยใช้คำสั่ง bash ตามด้วยชื่อไฟล์ ตัวอย่าง
  1. bash backup_camera.sh

พอเสร็จแล้วให้ตั้ง crontab สั่งให้เครื่อง Pi AutoBackup โดยใช้คำสั่ง
  1. crontab -e

แล้วตั้งเป็น
  1. 0 6 * * * bash  /home/pi/backup_camera.sh