วันพฤหัสบดีที่ 19 มีนาคม พ.ศ. 2563

check program running, crontab shell script

หากเราต้องการตรวจสอบว่า โปรแกรมที่เราเขียนนั้น ยังทำงานอยู่หรือไม่ ให้ทำดังนี้

วิธีที่1 Terminal

เปิด terminal แล้วพิมพ์
pgrep ตามด้วยชื่อโปรแกรม เช่น pgrep TagAgent
ก็จะได้เลข PID ออกมา ซึ่งเป็นเลข process ของโปรแกรม


วิธีที่2 Shell script (ไม่แนะนำ เนื่องจากพลาดได้บางครั้ง)

ในการเขียน shell script การใช้เครื่องหมายต่างๆ, การเว้นวรรค มีผลต่อการเขียนโปรแกรม
จำเป็นต้องศึกษาการใช้งานสักเล็น้อย ก่อนใช้งาน

สร้างไฟล์ขึ้นมาโดยพิมพ์ nano check_process.sh

#!/bin/bash
sudo rm /tmp/TagAgent.pid
pidof TagAgent >> /tmp/TagAgent.pid
while IFS= read -r line; do
    echo "Text read from file: $line"
done < /tmp/TagAgent.pid

Shell script ตรวจสอบโปรแกรม

ถ้าเราต้องการตรวจสอบว่าโปรแกรมรันอยู่หรือไม่ แล้วถ้าไม่รัน ก็สั่งให้เปิดโปรแกรม ให้ทำดังนี้

nano monitor-process.sh

#!/bin/bash
sudo rm /tmp/TagAgent.pid
sudo pidof TagAgent >> /tmp/TagAgent.pid
declare -i x=0
while IFS= read -r line; do
    if [ $line != "" ]
    then
        echo "Text: $line"
        x=$((x+1))
    else
        echo "No text"
    fi
done < /tmp/TagAgent.pid

if [ $x == 0 ]
then
  cd /home/pi/TagAgent
  sudo ./TagAgent &
else
  echo "run"
fi



เมื่อเราสร้างไฟล์ Script แล้ว ต้องทำให้ไฟล์นี้รันได้
sudo chmod +x monitor-process.sh

และก็ต้องตั้งเวลาให้ไฟล์ตัวนี้รันทุกๆ 1 นาที
เราสามารถทำได้โดยใช้ Crontab ดังนี้

พิมพ์ crontab -e
บรรทัดล่างสุด ให้เพิ่ม
* * * * * /home/pi/monitor-process.sh
กด control + o, control+x
sudo /etc/init.d/cron restart


ในรูปนี้พลาดไปอย่างนึง คือมันจะ reboot ทุกๆนาทีตอนตี 2 หมายความว่า 2.00 น., 2.01 น., 2.02 น. ... จะ reboot ตลอดทุกนาที จนกว่าจะถึงเวลาตี 3
ดังนั้นถ้าจะแก้ให้ถูกต้อง คือ reboot แค่ครั้งเดียวตอนตี 2 จะต้องแก้เป็น
0 2 * * * sudo reboot

และจากการทดสอบใช้จริง การใช้ pidof จาก shell script (วิธีที่2) มันจะมีการเช็คพลาดบางครั้ง
ดังนั้น จึงไม่แนะนำให้ใช้ วิธีการนี้ครับ

จบ

ที่มา
https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable
https://linuxize.com/post/how-to-remove-files-and-directories-using-linux-command-line/
https://bash.cyberciti.biz/guide/Create_an_integer_variable
https://askubuntu.com/questions/682913/how-to-write-shell-script-to-start-some-programs

วันศุกร์ที่ 7 กุมภาพันธ์ พ.ศ. 2563

RaspberryPi Connect bluetooth printer ทำยังไง ตอนที่2

จะเป็นการลง Driver ที่ตัว CUPS


ถ้าเราเคยลง driver ไว้ก่อนหน้านี้แล้ว ให้เอาออกก่อน 2 ไฟล์ ดังนี้
/usr/share/cups/model/Custom/
MY3 raster file from /usr/lib/cups/filter/.

0) download driver


เมื่อ download มาแล้ว ให้นำไปวางไว้ใน raspberry pi แล้วแตกไฟล์
tar -xvzf /path/to/yourfile.tgz
เมื่อแตกไฟล์เสร็จแล้ว ให้รัน  MY3_CUPSDrv-200.sh.
sudo ./ MY3_CUPSDrv-200.sh.


1) sudo apt-get install bluez-cups
2) when you add printer from Cups WebPage, add the printer as "AppSocket"

3) in connection box write "bluetooth://BTADDRESSwithoutcolumns/spp"
if the BT Address is 00:80:E1:BA:D4:C3, then you have to write
bluetooth://0080E1BAD4C3/spp
แต่เราต้องรู้เลข Bluetooth ก่อน ตามนี้
 
4)ใส่รายละเอียดต่างๆ
5)เลือกยี่ห้อ printer
6)เลือก Model

7)Set Default
8)Print test page

ตัวอย่าง code python ในการสั่ง print รูป

import cups

conn = cups.Connection()
printers = conn.getPrinters()
for printer in printers:
  print printer,printers[printer]["device-uri"]

printer_name = 'MY3'
file = "/home/pi/DriverPrinterCustom/VKP80III_CUPSDrv-202-PKG/scorpion.png"
conn.printFile(printer_name,file,"PDF Print",{})








ที่มา


 https://unix.stackexchange.com/questions/223397/cups-does-not-recognize-bluetooth-receipt-printer
 https://bbs.archlinux.org/viewtopic.php?id=88250

วันอังคารที่ 21 มกราคม พ.ศ. 2563

RaspberryPi connect bluetooth printer ทำยังไง

ในการใช้ Raspberry Pi ติดต่อกับ Bluetooth printer มีสิ่งที่ต้องทำหลักๆ ดังนี้

1.ติดตั้ง bluetooth
2.software python


1.ติดตั้ง Bluetooth

sudo apt-get update
sudo apt-get install pi-bluetooth
sudo apt-get install bluetooth bluez
sudo apt-get install python-bluez
sudo hciconfig hci0 piscan
sudo hciconfig hci0 name 'Device Name' // change Device Name
sudo python /usr/share/doc/python-bluez/examples/simple/inquiry.py
เพื่อดูว่า ตอนนี้ raspberry pi ของเรา มองเห็น Bluetooth Custom printer รุ่น MY3 หรือยัง ถ้าเห็นแล้ว จะขึ้นแบบนี้



ในที่นี้ Address ของ Bluetooth Custom printer รุ่น MY3 คือ 00:80:E1:BA:D4:C3

2.Software python

เราจะลองส่งคำสั่งให้ปรินท์ Logo หมายเลข 0 ที่มีอยู่ในเครื่องอยู่แล้ว ออกมา

sudo nano test_print.py

import bluetooth
bd_addr = "00:80:E1:BA:D4:C3"
port = 1
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
data = [0x1D,0x70,0x00,0x00,0x0A,0x0A]
for i in range(0,6):
  sock.send(chr(data[i]))
sock.close()

กด Control + "o", enter, Contrl + "x"

สั่งรันโปรแกรม
python test_print.py

ซึ่งได้ผลลัพธ์ แบบนี้
ที่ทำมานี้ คือสั่ง print แบบใช้ command ส่งเข้าไปที่เครื่อง จำเป็นที่จะต้องอ่าน manual ของเครื่องว่าต้องส่งอะไรเข้าไปที่เครื่อง

อธิบายเกี่ยวกับคำสั่ง
0x1D,0x70,0x00,0x00

0x1D,0x70 หมายถึง ให้ปรินท์โลโก้
0x00 หมายถึง ภาพที่0
0x00 หมายถึง ขนาดของรูปภาพ โดยถ้าเปลี่ยนเป็นเลขอื่น มีความหมายดังนี้
0 = normal
1 = double width
2 = double height
3 = double width and double height




ที่มา

https://www.cnet.com/how-to/how-to-setup-bluetooth-on-a-raspberry-pi-3/

https://kaebmoo.wordpress.com/2017/06/26/bluetooth-raspberry-pi/
ให้ดูตรงที่ ไม่ต้อง compile เอง

https://www.youtube.com/watch?v=He-3-zLgVMM ประมาณนาทีที่ 7 เป็นต้นไป
https://www.cnet.com/how-to/how-to-setup-bluetooth-on-a-raspberry-pi-3/

https://www.programcreek.com/python/example/19860/bluetooth.RFCOMM // อันนี้ ok

http://pages.iu.edu/~rwisman/c490/html/pythonandbluetooth.htm

https://www.raspberrypi.org/forums/viewtopic.php?t=232626
http://mattrichardson.com/Raspberry-Pi-Wireless-Photo-Printing/

วันอังคารที่ 7 มกราคม พ.ศ. 2563

linux python print picture to Custom Printer VKP80III

วันนี้ผมต้องการ print รูป ออกมาทางเครื่อง Custom Printer VKP80III เป็น printer คุณภาพดีจากประเทศ Italy

ซึ่งต้องลงโปรแกรม CUPS และ Driver เสียก่อน มีขั้นตอนดังนี้

เมื่อลง Driver เสร็จแล้ว ก็เข้าสูกระบวนการทาง software
เราต้อง convert รูป ให้เป็น pdf ก่อน จึงจะสั่งพิมพ์ที่เครื่อง VKP80III ได้

ลงโปรแกรม imagemagick

1.sudo apt-get udpate
2.ลงโปรแกรม impage magick ก่อน โดยพิมพ์ sudo apt-get install imagemagick
3.สั่ง convert รูปให้เป็น pdf โดยใช้คำสั่งดังนี้ convert -density 300 mypic.png -compress JPEG mypic.pdf
เราก็จะได้ mypic.pdf ออกมา


Python Software

จากนั้นเราก็สั่งให้พิพม์ไปที่เครื่อง VKP80III ดังนี้

nano test_print.py

import cups

conn = cups.Connection()
printers = conn.getPrinters()
for printer in printers:
  print printer, printers[printer]["device-uri"]

printer_name = printers.keys()[0]
file = "/home/pi/mypic.pdf"
conn.printFile(printer_name,file,"PDF Print",{})



พอดีได้ทราบมาทีหลังว่า สามารถ print ภาพได้เลย ไม่ต้อง convert เป็น pdf ก่อน แต่ต้อง configure ที่ CUPS ก่อน ดังนี้
1.เปิด Browser เข้าไปที่ 192.168.1.xx:631
2.เข้าเมนู Printer ==> Administration ==> Set Default Options
3.เลือก Halftoning Algolithm ให้เป็น Standard
4.กด Set Default Oprions เป็นอันจบขั้นตอนนี้

ในส่วนของ Software ก็แก้ไขเล็กน้อย แค่เปลี่ยนชื่อ file เฉยๆ ก็ print ได้เลย

file = "/home/pi/mypic.png"



ที่มา
https://www.raspberrypi.org/forums/viewtopic.php?t=180370
https://stackoverflow.com/questions/12723818/print-to-standard-printer-from-python
https://smallbusiness.chron.com/sending-things-printer-python-58655.html

เราสามารถสั่ง print โดยใช้คำสั่งใน command line ดังนี้
lp ชื่อไฟล์ -d ชื่อเครื่อง

https://www.techradar.com/how-to/computing/how-to-turn-the-raspberry-pi-into-a-wireless-printer-server-1312717/2

https://www.raspberrypi.org/forums/viewtopic.php?t=180370

https://www.youtube.com/watch?v=MoCQ-6YTj5o

raspberry pi ติดตั้งเครื่องปรินท์ VKP80III Custom Printer จาก Italy

หากเราต้องการติดตั้งเครื่องปรินท์ ให้กับ RaspberryPi มีสิ่งที่ต้องทำหลักๆอยู่ 3 อย่าง คือ
1.ติดตั้งโปรแกรม CUPS
2.Download และ แตกไฟล์ Driver
3.ติดตั้ง Driver ที่ CUPS


ติดตั้งโปรแกรม CUPS

โปรแกรม CUPS จะเป็นตัวจัดการเครื่องปรินท์ให้เรา มีวิธีติดตั้ง ดังนี้


1.sudo apt install dpkg
2.sudo apt-get install cups -y
3.sudo usermod -a -G lpadmin your_user_name # ให้เปลี่ยน your_user_name เป็นชื่อ user ที่เราใช้ ในที่นี้คือ pi ดังนั้นเราต้องพิมพ์ sudo usermod -a -G lpadmin pi
4.sudo cupsctl --remote-admin --remote-any --share-printers
5.sudo reboot


Download และ แตกไฟล์ Driver

1.Download Driver ของเครื่องพิมพ์ที่ต้องการมาก่อน
https://www.custom.biz/en_GB/product/hardware/printers/self-service-kiosk-printers/vkp80iii
ที่ด้านล่างของเวป จะมี Driver ให้เลือก ในที่นี้ให้เลือก PRINTER VKP80III USB RS232 REAR CONN. เนื่องจากเครื่องที่ผมนำมาใช้ เป็นเครื่องที่มี Port USB อยู่ด้านหลังของเครื่อง และคลิกที่ 4u support


 2.คลิก Download ที่เป็นของ Linux
3.เมื่อเรา Download มาเสร็จแล้ว ก็ให้จับโยนเข้าไปที่เครื่อง RaspberryPi แล้วแตกไฟล์ ด้วยคำสั่ง tar -vxf xxxx.tgz (xxxx คือไฟล์ที่เรา Download มา)
4.เมื่อแตกไฟล์แล้ว จะได้โฟลเดอร์ VKP80III_CUPSDrv-202-PKG มา ก็เข้าไปดู ด้วยคำสั่ง cd VKP80III_CUPSDrv-202-PKG
5.จะมี readme.txt อยู่ ให้เราเปิดอ่านดู เพื่อให้รู้ว่าต้องทำอะไรบ้าง ด้วยคำสั่ง nano readme.txt
6.ให้ออกจาก readme.txt ด้วยการกด control+x
7.พิมพ์คำสั่ง ./VKP80III_CUPSDrv-202.sh ก็จะได้ ไฟล์.pdd ออกมา เป็นอันจบขั้นตอนในส่วนนี้




ติดตั้ง Driver ที่ CUPS

 จากนั้นก็ให้ลง Driver ของเครื่องพิมพ์ โดยผ่านโปรแกรม CUPS ดังนี้
1.เสียบสาย USB เข้ากับ RaspberryPi ของเรา และเปิดเครื่องปรินท์
2.เปิด web browser ขึ้นมา แล้วพิมพ์ IP ของ RaspberryPi ตามด้วย port 631 (192.168.1.27:631)
3.คลิกที่แท็ป Administration

4.คลิกที่ Add Printer
ถ้าขึ้นหน้าที่ให้ใส่ user name และ password ก็ให้เราใส่ user name และ password ที่เราใช้ log in เข้ามา

5.ให้คลิกที่ Local: CUSTOM Engineering VKP80III และคลิก Continue

6.ถ้าหากต้องการ Share printer ให้เครื่องคอมฯเครื่องอื่นๆ สามารถใช้งาน printer เครื่องนี้ได้ด้วย ก็ให้คลิกถูกที่ Share This Printer และคลิก Continue

7.ให้คลิกที่ Custom Engineering VKP80III และคลิก Add Printer

8.ให้ตั้งค่าต่างๆ ตามต้องการ








9.กด Default option เพื่อจบการติดตั้ง

10.ทดสอบพิมพ์
เป็นอันเสร็จพิธี









วันเสาร์ที่ 4 มกราคม พ.ศ. 2563

ติดตั้ง printer epson tm-t82 บนเครื่อง PC linux

1. ถอด printer ออกจากเครื่องคอมฯก่อน

2. install CUPS
2.1 sudo apt install dpkg
2.2  sudo apt-get install cups -y
2.3 sudo usermod -a -G lpadmin your_user_name # ถ้าชื่อ user ใช้ชื่อว่า pi ก็ให้ใส่ pi ลงไปแทน your_user_name
2.4 sudo cupsctl --remote-admin --remote-any --share-printers
2.5 sudo reboot

3. download linux driver for tm-t82 โดยการเปิด web browser แล้วใส่ address นี้ลงไป
https://download.epson-biz.com/modules/pos/index.php?page=single_soft&cid=5012&scat=32&pcat=52
tmx-cups-2.0.3.0.tar.gz

4. แตกไฟล์
5. ให้ดูไฟล์ TmxDriverManual.html เพื่อดูว่าต้องทำอะไรบ้าง (ให้ดูข้อ 5.1)
6. ให้ download http://download.opensuse.org/distribution/11.4/repo/oss/suse/i586/libusb-1_0-0-1.0.8-4.1.i586.rpm
7. sudo rpm -i libusb-1_0-0-1.0.8-4.1.i586.rpm
7.1 พอดีว่าเครื่องผมมันลงไม่ผ่าน จึงทำการแก้ไข ดังนี้
7.2 sudo apt --fix-broken install
7.3 sudo apt install rpm
7.4 sudo rpm -i libusb-1_0-0-1.0.8-4.1.i586.rpm
8. ให้เลือก ubuntu 12..4 xx bit
9. sudo ./install.sh (ใน folder ที่เรา download มา)
10. เปิด printer และเสียบสาย usb เข้าเครื่องคอมฯ
11. เปิด Browser และเข้า localhost:631
12. เข้า tab Admin / Add printer
13. เลือก printer TM-T82
14. เลือก driver ที่เขียนว่า ...TM/BA...
เสร็จพิธี

วันเสาร์ที่ 23 พฤศจิกายน พ.ศ. 2562

วัดอุณหภูมิ ในตัว rpi4

 พอดีว่าเพิ่งได้ทดสอบใช้งาน rpi4 และเห็นว่าร้อนมาก
 จึงได้ลองทำการวัดอุณหภูมิภายในของ rpi พบว่ามีมากกว่า 80 องศา
เมื่ออุณภูมิของ rpi มากกว่า 80 องศา ตัว cpu จะลดความเร็วลง เพื่อที่จะลดอุณภูมิ ทำให้ประสิทธิภาพการทำงานลดลงไปด้วย

โดยใช้วิธีวัดอุณภูมิดังนี้
/opt/vc/bin/vcgencmd measure_temp

และถ้าต้องการอ่านอุณหภูมิอย่างต่อเนื่อง ทำดังนี้
nano monitor-temp.py

แล้วเขียน code ลงไปดังนี้

import os
import time
def measure_temp():
   temp = os.popen("vcgencmd measure_temp").readline()
   return (temp.replace("temp=",""))
while True:
    print(measure_temp())
    time.sleep(1)

กด ctrl+x แล้วกด y

จากนั้น
python monitor-temp.py

เราก็จะเห็นอุณหภูมิของ rpi ออกมาเรื่อยๆ
จากการทดสอบ ping และรัน python ทิ้งไว้ พบว่า
rpi จะมีอุณหภูมิอยู่ที่ประมาณ 85-86 องศา
ระยะเวลาในการตอบ ping ประมาณ 3-4 ms บางทีก็หลุดไป 18ms, 59ms และจะมีบางจังหวะที่ Time out ก็มี

สรุปว่า rpi4 จะมีอุณหภูมิสูงสุดประมาณ 85-86 องศา และประสิทธิภาพในการทำงานจะลดลง ควรจะต้องมี heat sink หรือ พัดลม ช่วยลดอุณหภูมิ



เพิ่มเติม

พอดีว่าผมได้ใส่พัดลมเพิ่ม และ ต้องการไม่ให้พัดลมทำงานตลอด 24 ชม. จึงได้ใช้ GPIO ควบคุมพัดลมอีกที
โดยใช้ Hardware ดังรูปนี้

โดยที่ P8 ใช้ต่อเข้ากับพัดลมขั้ว + และ -
ใช้ GPIO26 (ขา 37) เป็นตัวควบคุมการ เปิด/ปิด พัดลม


ก่อนที่จะเขียน software ควบคุมพัดลม ต้องติดตั้ง python เสียก่อน ตามนี้
และเขียน software ควบคุมพัดลม ดังนี้

nano monitor-temp.py

#! /usr/bin/python

import os
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(37,GPIO.OUT)
GPIO.setwarnings(False)

def measure_temp():
        temp = os.popen("vcgencmd measure_temp").readline()
        return (temp.replace("temp=",""))

while True:
        x = measure_temp().split("'")
        print x[0]
        if float(x[0]) > 50:
          GPIO.output(37,True)
        else:
          GPIO.output(37,False)
        time.sleep(1)

control + o , enter, control +x

sudo chmod +x monitor-temp.py
เพื่อให้โปรแกรมสามารถ execute ได้

หลักการทำงานของโปรแกรมนี้
ถ้าอุณหภูมิมากกว่า 50 องศา พัดลมก็จะทำงาน
ถ้าอุณหภูมิน้อยกว่า 50 องศา พัดลมจะหยุดทำงาน

และได้ทำการให้เปิดโปรแกรมตัวนี้อัตโนมัติ หลังจากการเปิดเครื่อง
โดยเข้าไปแก้ไขที่ /etc/rc.local
sudo nano /etc/rc.local

ที่บรรทัดรองสุดท้าย ก่อน exit 0 ให้เพิ่ม
cd /hom/pi/
./monitor-temp.py &


ต้องใส่ "&" ด้วย เพราะว่าต้องการให้ system สามารถไปทำโปรแกรมอื่นๆได้ด้วย ไม่ต้องมาติดตายอยู่ที่โปรแกรมตัวนี้ตัวเดียว


แถมด้วยการสั่งให้ reboot ทุกๆวันตอนเวลาตี 2
crontab -e
1, enter

ที่บรรทัดล่างสุด ให้ใส่
0 2 * * * sudo reboot

control + o, enter, control + x


ที่มา
https://medium.com/@kevalpatel2106/monitor-the-core-temperature-of-your-raspberry-pi-3ddfdf82989f

วันพฤหัสบดีที่ 21 พฤศจิกายน พ.ศ. 2562

create launcher on desktop

ถ้าเราต้องการให้มี short cut ในการเรียกใช้โปรแกรม อยู่บน Desktop (เรียกว่า Create Launcher) เราสามารถทำได้หลายวิธี
แต่ในที่นี้จะใช้โปรแกรมที่ชื่อว่า Arronax
วิธีทำก็คือ

sudo add-apt-repository ppa:diesch/testing 
sudo apt-get update
sudo apt-get install arronax
nautilus -q

หลังจากทำเสร็จแล้ว ก็ให้เราไปหาไฟล์ที่เราต้องการทำ launcher แล้วคลิกขวาจะเห็นคำว่า
"Create starter for this program"

เราจะเห็นโปรแกรม Arronax ขึ้นมา
ถ้าเราต้องการเปลี่ยนรูป ก็ให้คลิกในวงกลมส้ม และ เลือกรูปที่ต้องการ


แล้วก็คลิกที่ปุ่ม save
เป็นอันเสร็จพิธี




https://askubuntu.com/questions/64222/how-can-i-create-launchers-on-my-desktop