Saturday, 6 August 2016

Raspberry PI as Google Cloudprint Connector for Older Printers

Google Clouldprint is a great tool that allows you to print content to directly to your printer from anywhere, even if your printer is at home on the other side of the world!  Also, it will queue up the print jobs until your printer is turned on.   You can also share your printer with others, allowing them to print to it also, from anywhere.

However, only newer printers are Google Cloudprint ready.  This means that older printers need to have some other way to become Google Cloudprint devices... This is where Raspberry PI comes in.  For 40euro you can turn pretty much any printer into a Google Cloudprint device.  Regardless of whether your existing printer is connected via Wifi, LAN or even USB!

This is especially handy if you have just started using a Chromebook and just realised you cannot connect to your existing printer!  So, follow the various guides to get your new Raspberry Pi online, then follow these steps to set it up as a Google Couldprinter Connector for your old printer!  (RPi v3 is the best for this as it has wifi built in!)

FYI, Rasbian is the OS of choice for this howto!

Raspberry Pi setup guides:
https://www.howtoforge.com/tutorial/howto-install-raspbian-on-raspberry-pi/
https://www.raspbian.org/RaspbianStartHere

Here are the steps to turn your Raspberry Pi into a Goole Cloudprint Connector:
From the command line, via either ssh or directly connected, run the following commands to install some required base software:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install cups libcups2 libavahi-client3 avahi-daemon libsnmp30
wget https://github.com/google/cups-connector/releases/download/2016.01.02/gcp-cups-connector-rpi-jessie-2016.01.02.tar.gz
tar xvzf gcp-cups-connector-rpi-jessie-2016.01.02.tar.gz 
sudo mv cups-connector /opt/gcp-cups-connector
sudo chmod 755 /opt/gcp-cups-connector/gcp-cups-connector* 
sudo chown -R gcp:gcp /opt/gcp-cups-connector 
sudo useradd -s /usr/sbin/nologin -r -M gcpcd /opt/gcp-cups-connector
Next step is to run the gcp-cups-connector-util script to initiate the connector on your raspberry pi:
sudo -u gcp /opt/gcp-cups-connector/gcp-cups-connector-util init
Follow prompts, answering y for most. Then, when given the code, goto https://www.google.com/devices and enter it. Edit the json file and change the monitor_socket_filename to this: "monitor_socket_filename": "/tmp/gcp-cups-connector-monitor.sock". Edit the json file and change the log_file_name to this: "log_file_name": "/var/log/gcp/gcp-cups-connector". Prepare the log folder for gcp:
sudo mkdir -p /var/log/gcp
sudo chown -R gcp:gcp /var/log/gcp
Edit /etc/cups/cupsd.conf file replacing all its content with that from here: http://www.storeebud.com/p/cupsdconf.html
sudo vi /etc/cups/cupsd.conf
Now restart cups:
sudo systemctl restart cups.service
Add users to the lpadmin group for printer control:
sudo usermod -a -G lpadmin gcp
sudo usermod -a -G lpadmin pi
Use normal method for connecting/adding your printer to a Linux host (i.e. hplip and/or native CUPS discovery) (sudo apt-get install hplip). Access CUPS GUI via https://:631/ and add the printer. Once the printer is working in CUPS, you may proceed with the gcp final steps.
sudo vi /opt/gcp-cups-connector/gcp.service
[Unit]
Description=Google Cloud Print CUPS Connector
Documentation="https://github.com/google/cups-connector/wiki/Systemd"
#Requires=network-online.target
After=printer.target network.target network-online.target multi-user.target

[Service]
ExecStart=/opt/gcp-cups-connector/gcp-cups-connector -config-filename /etc/gcp-cups-connector/gcp-cups-connector.config.json
User=gcp
Group=gcp
#Restart=on-abort
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target 
Move the service file into positon and enable the service:
sudo chmod 664 /opt/gcp-cups-connector/gcp.service 
sudo mv /opt/gcp-cups-connector/gcp.service /etc/systemd/system/ 
sudo ln -s /etc/systemd/system/gcp.service /opt/gcp-cups-connector/gcp.service 
sudo systemctl enable gcp 
sudo systemctl start gcp 
sudo systemctl status gcp 
Final step is to get CUPS to re-enable to printer automatically.
sudo -u gcp vi /opt/gcp-cups-connector/cups-printer-autoresume.sh 
Add these lines to the file:
 
#!/bin/sh 
# Check if a printer queue is disabled and reenable it.
DISABLED=`lpstat -t | grep disabled | awk '{ print $2;}'`
CUPSENABLE=/usr/sbin/cupsenable
LOGGER=/usr/bin/logger
for PRINTER in $DISABLED
do 
  $LOGGER "gcp: Printer $PRINTER is stopped" 
  $CUPSENABLE -h 127.0.0.1:631 $PRINTER && $LOGGER "gcp: Printer $PRINTER has been enabled."
done 
Enable the script and use cron to make it run regularly:
sudo chmod 755 /opt/gcp-cups-connector/cups-printer-autoresume.sh 
sudo -u gcp crontab -e 
Add this line to the crontab:
*/5 * * * * /opt/gcp-cups-connector/cups-printer-autoresume.sh > /dev/null >&1 

The great thing about this method is that it does not rely on any other computer or node to be active for the cloudprint to work. And as a bonus, if your printer has a USB port for connecting usb/flash drives, you may even be able to power your Raspberry Pi from the printer! Another plus is that it also does not require Google Chrome or Chromium to be installed on the Raspberry Pi... You don't even need to connect it to a screen!

No comments:

Post a Comment