วันอังคารที่ 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

วันศุกร์ที่ 15 พฤศจิกายน พ.ศ. 2562

ใช้ Qt สั่ง print เข้า printer โดยตรง ทั้งแบบ Auto และแบบให้เลือก printer


#include <QApplication>
#include <QPrinter>
#include <QPrintDialog>
#include <QPainter>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString text ="Test print";

    QPrinter printer;

    QPrintDialog *dialog = new QPrintDialog(&printer);
    dialog->setWindowTitle("Print Document");

    if (dialog->exec() != QDialog::Accepted)
        return -1;

    QPainter painter;
    painter.begin(&printer);

    painter.drawText(100, 100, 500, 500, Qt::AlignLeft|Qt::AlignTop, text);

    painter.end();

    return 0;
}

https://stackoverflow.com/questions/11397683/how-to-print-text-file-to-printer-in-qt

===========================================================
ถ้าต้องการให้ print ทันที ก็ให้ตัด code ในส่วนที่เป็น dialog ออกไป
#include <QApplication>
#include <QPrinter>
#include <QPainter>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString text ="Test print";

    QPrinter printer;

    QPainter painter;
    painter.begin(&printer);

    painter.drawText(100, 100, 500, 500, Qt::AlignLeft|Qt::AlignTop, text);

    painter.end();

    return 0;
}

==============================================================
ลง driver printer hp ตามนี้
https://www.youtube.com/watch?v=va6PWYZScZk

วันพุธที่ 13 พฤศจิกายน พ.ศ. 2562

ติดตั้ง node red บน windows

1.เริ่มจาก download จากที่นี่ https://nodejs.org/en/
ให้เลือกตัวที่ลงท้ายด้วย LTS ย่อมาจาก Long Term Support
2.เมื่อลงโปรแกรมเสร็จแล้ว ทดสอบดังนี้
เปิด command prompt แล้วพิมพ์ node --version ควรจะมี version โผล่ขึ้นมา
พิมพ์ npm --version ก็ควรจะมีเลข version โผล่ขึ้นมาเหมือนกัน
3.install Node Red ด้วยการพิมพ์ใน command prompt ดังนี้
npm install -g --unsafe-perm node-red แล้วรอสักพัก
4.สั่งเปิดโปรแกรมด้วยการพิมพ์ node-red ใน command prompt
5.เมื่อ node red จะทำงานอยู่ที่ port 1880 ให้เปิด Browser แล้วใส่หมายเลข IP ของคุณตามด้วยหมายเลข port 1880 ยกตัวอย่าง เช่น 192.168.1.11:1880
แค่นี้ก็ใช้งาน node red ได้แล้ว
ถ้าต้องการให้ auto start ทุกครั้งที่เปิดเครื่อง ให้ทำดังนี้

6. npm install -g pm2
7. เช็คการทำงานด้วย pm2 status
8. สั่ง start node red ด้วยคำสั่ง pm2 start C:\Users\<User>\AppData\Roaming\npm\node_modules\node-red\red.js
9. npm install pm2-windows-startup -g
10. pm2-startup install
11. pm2 save

#Deaware
https://medium.com/@suthipongbubpago/auto-startup-node-red-with-pm2-on-windows-f050f0232d07

วันอังคารที่ 12 พฤศจิกายน พ.ศ. 2562

rpi3b เลิกผลิต จึงต้องทำใหม่ทั้งหมดด้วย rpi4


1.เริ่มต้นด้วย download image lite มาจาก https://www.raspberrypi.org/downloads/raspbian/ โดยเลือกตัว buster lite
2.sudo nano /etc/default/keyboard ให้เปลี่ยนจาก "gb" ไปเป็น "us"  เพื่อทำให้ keyboard เป็น us
2.1 reboot
3.sudo raspi-conig
3.1 change password
3.2 change Time-Zone ให้เป็น Asia, Bangkok
3.3 Interface option
3.3.1 Enable I2C
3.3.2 Enable SSH
4.sudo nano /boot/config.txt
4.1 uncomment hdmi_force_hotplug=1
4.2 uncomment hdmi_group=1, hdmi_mode=16
แล้วแต่กรณีดังตารางต่อไปนี้


hdmi_group hdmi_mode ความละเอียด จอ
1 16 1080p TV
2 16 1024x768 60hz computer
2 82 1080p 60hz computer


5.Static IP
5.1 sudo nano /boot/cmdline.txt เพิ่ม ip=192.168.1.31 เพื่อตั้ง ip
5.2 sudo systemctl disable dhcpcd.service เพื่อไม่ให้รับ ip dhcp
6.sudo apt-get update
7.sudo apt-get upgrade
8.sudo apt-get install wiringpi
9.cd /temp
10.wget https://project-downloads.drogon.net/wiringpi-latest.deb
11.sudo dpkg -i wiringpi-latest.deb
12.check ด้วยคำสั่ง gpio -v และ gpio readall
13.ลง R502-CL
13.1 Open a root terminal window; you need to have administrator rights.
Get and install the libusb library with the apt-get command: apt-get install libusb-dev libusb++-0.1-4c2
Check the installation: You shall find the library libusb.so under the directory /usr/lib/
13.2. Download and install the libccid package: apt-get install libccid
13.3. Download and install the pcsc-lite package: apt-get install pcscd
13.4. Check that the pcscd service is running:
In the terminal console, run the command ps –A.
You should find a "pcscd" process running in the list displayed
Run pcscd in terminal
13.5. Download and install libpcsclite1: apt-get install libpcsclite1
13.6. Download and install libpcsclite-dev: apt-get install libpcsclite-dev
13.7. Check whether the R502 reader is detected: your_system_name:/tmp# lsusb
Bus 001 Device 002: ID 096e:0608
13.8. Use root to run “install script”: $ wget http://download.ftsafe.com/files/reader/product/R502/install.sh $ sh install.sh or $ bash install.sh
13.9. After run “install script”, then insert reader and start “pcscd” service.
Run “pcsctest” command to do test
Like below: pcscd –adf //run pcscd service, for parameter, you can using “man pcscd” to get more helps pcsctest
13.10. To develop application based on FEITIAN R502, check below:
To develop your own application, just follow PCSC Lite APIs
http://pcsclite.alioth.debian.org/api/group__API.html
For the test demo application, you can found in source code, which named "pcsctest"
https://alioth.debian.org/frs/?group_id=30105


http://wiringpi.com/download-and-install/
https://www.raspberrypi.org/forums/viewtopic.php?t=251297
http://wiringpi.com/wiringpi-updated-to-2-52-for-the-raspberry-pi-4b/
http://androidraspberryarduino.blogspot.com/2015/12/install-acr-pcsc-smart-card-raspberry.html
https://hosxp.net/smf2/index.php?topic=35175.0
https://raspberrypith.blogspot.com/2014/09/install-card-reader-rfid-r502-cl.html
https://github.com/FeitianSmartcardReader/R502_SDK_Latest/blob/master/Docs/EN_Build_CCID_Driver_On_Linux_Latest.pdf