วันเสาร์ที่ 20 กรกฎาคม พ.ศ. 2562

Raspberry Pi Zero W กับการทำ Printer Server ด้วย CUPS

อยากได้ Printer ที่สั่งผ่าน WiFi ได้ ซึ่งเคยทำมานานแล้วครั้งหนึ่งแต่ตอนนั้นลง Driver ในตัว Pi เลยและสิ่งที่ไม่มีก็คือ Driver
ผ่านไปหลายปี ตอนนี้มี Pi Zero W อยู่สามตัวไม่ได้ทำอะไร ยังไม่ได้ทำเลยเอามาลองดูใหม่ว่าจะทำได้ไหมครั้งนี้
จากการ Search Google ก็พบกับคนที่ทำไว้แล้วก็เลยลองเข้าไปอ่านแล้วก็ทำตามเขาเลยครับ
ทำ Print Server ด้วย Raspberry Pi Zero W และขอขอบคุณเจ้าของบทความครับ

ปัญหาแรกที่เจอคือพยายาม Fix IP Address ของ WiFi ยังทำไม่ได้ ไม่รู้ว่าติดตรงไหน ขอข้ามไปละกันเดี๋ยวจะไม่ได้เริ่มสักที
***ตอนนี้ใช้ Fix IP ผ่านทาง Router แทน
# Update package และ ทำการ Install CUPS
sudo apt update
sudo apt install cups

# เราสามารถใช้คำสั่ง systemctl กับ cups ได้มี service ให้เหมือนกัน
# เช่น ดูสถานะของ cups ด้วยคำสั่ง sudo systemctl status cups

# แสดงข้อมูลของอุปกรณ์ด้วยคำสั่ง lpinfo
# lpinfo - show available devices or drivers

# sudo lpinfo -v
sudo lpinfo -v | grep usb:

# ข้อมูลอุปกรณ์ที่ต่อกับ Port USB ตามนี้ เราใช้ EPSON L360
direct usb://EPSON/L360%20Series?serial=5647444B3135373352&interface=1

# ทำการตั้งค่าอุปกรณ์โดนกำหนดชื่อ Destination และ URI ของ Printer เอาถึง Serials
# lpadmin - configure cups printers and classes
# sudo lpadmin -p {destination} -v {device-uri}

sudo lpadmin -p EPSONL360 -v usb://EPSON/L360%20Series?serial=5647444B3135373352

# จากนั้นดูข้อมูลสถานะของ Printer
# lpstat - print cups status information
# sudo lpstat -p {destination} -l
sudo lpstat -p EPSONL360 -l

# ปกติถ้าพึ่งใส่ข้อมูลจะเป็น Disable
printer EPSONL360 disable since Thu 18 Jul 2019 17:40:10 BST
        reason unknown

# ทำการ Enable Printer ด้วยคำสั่ง cupsenable
# cupsdisable, cupsenable - stop/start printers and classes
# sudo cupsenable {destination}
sudo cupsenable EPSONL360

# เสร็จแล้วลองดูข้อมูลเครื่อง Printer อีกครั้งด้วยคำสั่ง lpstat
sudo lpstat -p EPSONL360 -l

# ตอนนี้ Printer ของเราเป็น Enable แล้ว
printer EPSONL360 is idle.  Enable since Thu 18 Jul 2019 17:45:10 BST

เสร็จแล้วให้เปิด เว็บเข้าไปที่ IP ของ Raspberry Pi และ Port เป็น 631 ผลที่ได้คือเข้าไม่ได้ งงสิ

พอเจอแบบนี้ก็ลองตรวจสอบดูว่า Port 631 มันเปิดยัง หรือว่าติด Firewall ต้องสักอย่างนี่แหละไปดูก่อน
# Check port
sudo netstat -plnt

# Filter the list
sudo netstat -plnt | grep ':631'

# ผลที่ได้แบบไม่มี Filter
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address    Foreign Address    State     PID/Program name
tcp        0      0 0.0.0.0:22       0.0.0.0:*          LISTEN    516/sshd
tcp        0      0 127.0.0.1:631    0.0.0.0:*          LISTEN    2241/cupsd
tcp        0      0 127.0.0.1:6010   0.0.0.0:*          LISTEN    654/sshd: pi@pts/0
tcp6       0      0 :::22            :::*               LISTEN    516/sshd
tcp6       0      0 ::1:631          :::*               LISTEN    2241/cupsd
tcp6       0      0 ::1:6010         :::*               LISTEN    654/sshd: pi@pts/0

เจอว่า Port 631 เปิดอยู่ แต่... เปิดให้เฉพาะ Local คือ 127.0.0.1:631 เราต้องทำการ Config ให้เครื่องอื่นสามารถเข้าไปได้
โดยการเข้าไป Config ที่ไฟล์ cupsd.conf ใน /etc/cups โดยแก้ไขในส่วนของ Listen แก้เป็น 0.0.0.0:631
และ WebInterface ให้เป็น Yes
# Backup File ไว้เผื่อมีปัญหา
sudo cp /etc/cups/cupsd.conf /etc/cups/cupsd.conf.orginal

# เปิดไฟล์เพื่อแก้ไข
sudo nano /etc/cups/cupsd.conf
#
# Configuration file for the CUPS scheduler.  See "man cupsd.conf" for a
# complete description of this file.
#

# Log general information in error_log - change "warn" to "debug"
# for troubleshooting...
LogLevel warn
PageLogFormat

# Deactivate CUPS' internal logrotating, as we provide a better one, especially
# LogLevel debug2 gets usable now
MaxLogSize 0

# Only listen for connections from the local machine.
Listen 0.0.0.0:631
Listen /run/cups/cups.sock

# Show shared printers on the local network.
Browsing On
BrowseLocalProtocols dnssd

# Default authentication type, when authentication is required...
DefaultAuthType Basic

# Web interface setting...
WebInterface Yes

เสร็จแล้วทำการ Restart CUPS ด้วยคำสั่ง systemctl restart cups แล้วลอง netstat -plnt ดู
เมื่อเปลี่ยนเป็น 0.0.0.0:631 แล้วลองเปิดเว็บดูอีกครั้ง
sudo systemctl restart cups
sudo netstat -plnt

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address    Foreign Address    State     PID/Program name
tcp        0      0 0.0.0.0:22       0.0.0.0:*          LISTEN    516/sshd
tcp        0      0 0.0.0.0:631      0.0.0.0:*          LISTEN    2241/cupsd
tcp        0      0 127.0.0.1:6010   0.0.0.0:*          LISTEN    654/sshd: pi@pts/0
tcp6       0      0 :::22            :::*               LISTEN    516/sshd
tcp6       0      0 ::1:631          :::*               LISTEN    2241/cupsd
tcp6       0      0 ::1:6010         :::*               LISTEN    654/sshd: pi@pts/0

หลังจากเปิดเว็บดูอีกครั้งก็ ยังเข้าไม่ได้ แต่ก็มีอาการที่ดีขึ้น นิดหน่อย คือจากเข้าไม่ได้ เป็นไม่มีสิทธิ์การเข้า

สิ่งที่ต้องทำคือเข้าไปแก้ไฟล์ cups อีกครั้งโดยครั้งนี้เป็นการเข้าไปเพิ่มสิทธิ์ให้สามารถเข้าได้
โดยเพิ่ม Allow all เข้าไปเสร็จแล้ว Save และ Restart CUPS อีกครั้งแล้วลองเข้าดูใหม่
# Restrict access to the server...
<Location />
  Order allow,deny
  Allow all
</Location>

# Restrict access to the admin pages...
<Location /admin>
  Order allow,deny
  Allow all
</Location>

เสร็จแล้วเข้าอีกทีก็เข้าได้ในที่สุด

เมื่อต้องการเพิ่ม Printer ให้เราเปิดที่แถบ [Printer] เสร็จแล้วเลือก Printer ที่เราต้องการ จากนั้นดูที่ URL เราจะใช้ตรงนี้
http://192.168.1.203:631/printers/EPSONL360

สุดท้ายทำการ เพิ่ม Printer ก็ใช้งานได้

เสร็จแล้วลอง Print Test ดู



สั่ง Print Test แล้วไม่ออกทำไงดี

เราก็เจอตอนที่เพิ่มเครื่องพิมพ์ใหม่อีกครั้ง ครั้งแรกทำได้ แต่ครั้งที่สองทำไม่ได้ งงเหมือนกันก็ลองหาข้อมูลดู
เลยได้คำสั่งเพิ่มมา 2 คำสั่ง คือ lpstat -a และ cupsaccept {destination} พอลองแล้วก็สามารถสั่งพิมพ์ได้
ยังไงก็ลองเอาไปใช้ดู ถ้าเจอปัญหาใหม่ๆ และวิธีการแก้จะเอามาเพิ่มอีก
# lpstat -a เพื่อดูสถานะการ accepting
sudo lpstat -a

EPSONL360 not accepting requests since Sun 21 Jul 2019 15:38:01 BST -
          reason unknown

# ตั้งค่าให้ accepting
# cupsaccept/cupsreject - accept/reject jobs sent to a destination
# cupsaccept {destination}
cupsaccept EPSONL360

# ทำการดูสถานะอีกครั้ง
sudo lpstat -a

EPSONL360 accepting requests since Sun 21 Jul 2019 15:38:01 BST

ลองสั่ง Print Test ก็ออกแล้ว



คำสั่งที่ใช้เปิด Firewall
sudo iptables -L
sudo iptables -A INPUT -p tcp --dport 631 -j ACCEPT




บทความที่เกี่ยวข้อง
https://en.wikipedia.org/wiki/CUPS | https://www.cups.org/
การติดตั้งเครื่องพิมพ์บน Raspberry Pi | แปลงเครื่องพิมพ์เป็น Wireless Printer ด้วย Raspberry Pi
การล็อคไอพี (Static IP Address) ให้กับ Raspberry Pi
Check listening ports | Open ports in Ubuntu

ไม่มีความคิดเห็น:

แสดงความคิดเห็น