Pi Printer is a minimal thermal printer server for orange-pi or raspberry-pi or any other SCB compatible board. Background story: we have setup a POS software that connects to a thermal printer via bluetooth. once we got to use it in a real location, e.g. in a shopping mall, we found that there are 20 bluetooth printer/devices around us, and we have difficulties on picking one. and once we connected to the correct printer, we have difficulties on maintaining the connection. thus we come up with a reliable old way of "wired" connection.
a serverless backend that stores the print jobs. this serverless is hosted in mongodb-atlas, with mongodb-stitch as the serverless functions. the thermal printer is connected to a rasp-pi/orange-pi via USB. this pi-device is connected to the internet via wi-fi, and then the nodejs codes that are deployed in pi-device is the client, which manages print jobs, e.g. pull the jobs, print it, and "removing" the print job.
on another side of it, there is your web app/app that has something that you want to print.(e.g. POS software, ios/android app, etc). this App will send POST request to the serverless backend. this request,then will be stored in the backend, and the printer server will check the updates every 3 seconds and print it if there's any new jobs that still has not been printed.
- SCB (raspberry compatible) e.g orangepi etc. this codes is tested on orangepi zero, using Armbian
- in the Pi, you have to have
git, node, yarn, npm
- bluetooth thermal printer with mini-usb/usb connection.
- create mongodb atlas account here https://www.mongodb.com/cloud/atlas
- set up a free cluster
- create mongo-stitch app, set up a webhook for your app/web app to send print job to.
- set up functions and webhooks for your thermal printer/raspberry-pi/orange-pi to pull print jobs and for the webapp/print job sender
- set up sentry account for error monitoring
- set up pm2 account for orange pi monitoring
- run the print server
create mongodb atlas account here https://www.mongodb.com/cloud/atlas it has to be mongodb atlas because we are depending on mongo-stitch.
once you created an account there, you are allowed to create 1 free mongo-db cluster. so now, create it. put it in a region close to where your printer is located.
once you created the cluster,
- go to
Services -> stitch
create new application
,- give it app name, link to your created cluster, name the service.
- select
local
deployment model. - select primary region, pick the same/closest to where you setup your cluster.
once the application is created, click on it, and you go to the stitch
dashboard.
- take note your
APP ID
- initialise a mongoDB collection. fill in Database name with
printer
and Collection namejobs
- create rules, pick your database, and collection, make sure that you have
write
access
- you need to create 2 different users, 1 for the webapp/the one who sending in printer jobs. and the 2nd is for the orange-pi.
- go to
providers
tab, click on API Keys. - enable it, if not enabled yet.
- click
create api key
- name the 1st one,
printer
take note the key, prepare it for the orange-pi - click
create api key
again to create the 2nd one. - name the 2nd one, e.g.
web_app
take note the key, prepare it for your web-app/print job sender.
functions
is a serverless API/RPC where you can call from the client with thestitch-sdk
- go to
functions
-> create new function -> name itgetQueues
- authentication
application authentication
- make sure
private
is disabled - go to
function editor
and paste in content from your repostitch/function/getQueues.js
- go to
functions
-> create new function -> name it setIsPrinted - authentication
application authentication
- make sure
private
is disabled - go to
function editor
and paste in content from your repostitch/function/setIsPrinted.js
Webhook is a public facing API or a https based serverless API. your web app/print job sender supposed to be able to send POST request to this webhook as a way to send the print job.
- go to
3rd party service
->add incoming webhook
- name it
add-print-job
- authentication
application authentication
- take note the
Webhook Url
- set HTTP method to
POST
respond with result
toON
request validation
can beNo additional authorization
- go to
function editor
and paste in content fromstitch/webhook/add-print-job.js
once you saved all of those, don't forget to press deploy
on the top of the window.
we are using sentry as one of the transport of our logger. the printer server will be placed remotely thus we need a little bit of visibility in term of when there's error/exception on our printer server.
- please go to https://sentry.io/ and create a free account, and login.
- take note the
sentry dsn
- register an account https://id.keymetrics.io/api/oauth/register
- login, then add new bucket.
- on top right, you see
connect
button, take note thelinking command
to link your printer to this monitoring tool.pm2 link xx5522 xx1234
-
connect the thermal printer via mini-usb to usb part of orange-pi/raspberry-pi. in my case, the usb device assigned is
/dev/usb/lp0
reference: https://mike42.me/blog/2015-03-getting-a-usb-receipt-printer-working-on-linux -
ssh to your orange-pi/raspberry pi,
git clone
this repo from there. -
make sure you have
node, npm, yarn
in your orange-pi/raspberry-pi -
install pm2.
npm install -g pm2
-
paste the
linking command
and execute it. -
yarn install
-
setup
.env
file as your config file.cp .env.dist .env
now copy paste your keys/dsn that you noted above into this.env
file. -
run the printer server with pm2.
pm2 start main.js
-
save the task to pm2 service manager.
pm2 save
-
orange-pi/raspberry-pi are devices that easily restartable, and difficult to connect to, so we can set it up as a startup script.
pm2 startup
, copy the response, and paste and execute in the terminal. when there's error at a later point of the running time, you can just unplug the power, then replug it.
this is a client / software that sends print job to the printer server. thanks to the webhook that we set above, we can just send a POST request to it, and once it received by the mongodb-stitch, your printer server will print it straight away.
example request
POST
https://ap-southeast-2.aws.webhooks.mongodb-stitch.com/api/client/v2.0/app/printer-abcd/service/printer-service/incoming_webhook/add-print-job
body:
{
"api-key": "api-key-secret-akn20380-1--2980020",
"jobContent": "print this"
}