https://github.com/elsehow/sap
super automous printer
https://github.com/elsehow/sap
Last synced: about 1 year ago
JSON representation
super automous printer
- Host: GitHub
- URL: https://github.com/elsehow/sap
- Owner: elsehow
- Created: 2016-02-12T02:34:36.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-20T19:32:38.000Z (over 10 years ago)
- Last Synced: 2024-04-15T07:13:50.748Z (about 2 years ago)
- Language: Python
- Size: 24.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
Awesome Lists containing this project
README
* sap
Super Automous Printer
an api for [[https://learn.adafruit.com/pi-thermal-printer/][adafruit's IoT printer]].
* Setup
** Raspberry Pi setup
Let's assume you've already assembled the printer physically, and configured it to be accesisble wirelessly over SSH.
Now, we'll ssh into our server and follow the setup instructions, heavily inspired by [[https://learn.adafruit.com/pi-thermal-printer/pi-setup-part-2][the adafruit tutorial]], but including a few additions to make the api server run.
#+BEGIN_SRC shell
#!/bin/bash
# install requisite software
curl -sLS https://apt.adafruit.com/add | sudo bash # add adafruit repos
sudo apt-get update
sudo apt-get install node # install node
# install thermal printer code
sudo apt-get install git
git clone https://github.com/elsehow/sap
git checkout stable
cd sap
npm install
#+END_SRC
Now, =sudo nano /boot/cmdline.txt= and change the contents to:
#+BEGIN_SRC bash
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
#+END_SRC
Now, =sudo nano /etc/inittab= and comment out the last line. After you're done, the last line should read:
#+BEGIN_SRC bash
# T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
#+END_SRC
Hooray! Now you can configure and run your print server.
** Server setup
Now, in ~/sap/, write a file called =config.js= to store your secret API key.
#+BEGIN_SRC js
module.exports = 'my-secret-key'
#+END_SRC
Make sure your API key is unguessable. Anyone who has it will be able to print stuff over your printer!
Now, just =npm start= to start the server as a daemon using [[http://npmjs.com/package/forever][forever]].
#+BEGIN_SRC shell
npm start
#+END_SRC
You can disconnect from your ssh session, now.
(If you ever want to stop your server, just ssh in again and do =npm stop= in ~/sap).
* Usage
** Your first print
There is a simple example client, in =example-client/client.js=.
It will take strings to print via the command line
To install it
#+BEGIN_SRC shell
cd example-client
npm install
npm link
#+END_SRC
Now you can use it from anywhere with
#+BEGIN_SRC shell
thermal-print "hello, my friendly, furry
friends"
#+END_SRC
** Printing stuff with POST requests of JSON
We can use our API by sending JSON-formatted POST requests to indra.webfactional.com/
JSON requests should look like this:
#+BEGIN_SRC js
var payload = {
type: 'my-secret-printer-key',
message: 'hello my furry, lovely friends'
}
#+END_SRC
where =type= refers to the secret key of the printer. (you set this key in a file called =config.js=, on your printer).
* Code
=server.js= subscribes to an [[https://github.com/berkeley-biosense/indra-server][indra server]] instance. It receives messages by subscribing to events that have a secret API key in the =type= field of the JSON posted to the indra server.
Here, the server will connect to indra.webfactional.com (but any other hostname running an [[https://github.com/berkeley-biosense/indra-server][indra server]] instance will do).
finally, we have =example-client/client.js=, which shows how to query the api.
to develop, edit the code below, and [[http://orgmode.org/manual/Extracting-source-code.html][use emacs to tangle the file]].
** server.js
*** package.json
First let's setup its =package.json=
#+BEGIN_SRC json :tangle package.json
{
"name": "printer-server",
"version": "2.0.0",
"description": "a websocket server for the adafruit printer",
"main": "server.js",
"scripts": {
"start": "forever start -a -m 1 -l server.log -o out.log -e err.log server.js",
"stop": "forever stopall"
},
"repository": {
"type": "git",
"url": "https://github.com/elsehow/sap/"
},
"keywords": [
"iot"
],
"author": "elsehow",
"license": "BSD-2-Clause",
"dependencies": {
"kefir": "^3.2.0",
"serialport": "^2.0.6",
"socket.io-client": "^1.4.5",
"thermalprinter": "^0.3.8"
},
"devDependencies": {
"forever": "^0.15.1"
}
}
#+END_SRC
*** server.js
#+BEGIN_SRC js :tangle server.js
var socket = require('socket.io-client')('http://indra.webfactional.com')
, spawn = require('child_process').spawn
, key = require('./config.js')
, Kefir = require('kefir')
, SerialPort = require('serialport').SerialPort
, serialPort = new SerialPort('/dev/ttyAMA0', {baudrate: 19200 })
, Printer = require('thermalprinter');
function logError (e) {
console.log('--------err--------')
console.log(e)
}
// a stream of printer objects
// which come through when printer is ready to print
var printerS = Kefir.stream(function (e) {
serialPort.on('open', function () {
var printer = new Printer(serialPort)
printer.on('ready', function () {
e.emit(printer)
})
})
})
// a stream of socket objects
// which come through when socket connects to server
var socketS = Kefir.stream(function (e) {
socket.on('connect', function () {
e.emit(socket)
})
})
// setup listener that prints on event
function printOnEvent (socket, printer) {
socket.on(key, function (msg) {
printer.printLine(msg.message).printLine('\n\n').print()
})
return
}
// log errors
socketS.onError(logError)
printerS.onError(logError)
// set up listener when printer and socket are both ready
socketS.combine(printerS, printOnEvent).log('connected to server + printer - ready to print')
#+END_SRC
** example-client/client.js
#+BEGIN_SRC js :tangle example-client/client.js
#!/usr/bin/env node
var argv = process.argv.slice(2)
, key = require('../config.js')
var payload = {
type: key,
message: argv[0]
//message: 'hello my furry, lovely friends'
}
var request = require('request-json')
var client = request.createClient('http://indra.webfactional.com')
client.post('/', payload, function(err, res, body) {
console.log('posted')
})
#+END_SRC
**** =example-client/package.json=
#+BEGIN_SRC :tangle example-client/package.json
{
"dependencies": {
"request-json": "^0.5.5"
}
}
#+END_SRC
* TODO possible todos / improvements
** TODO configure server to start on boot
Now let's get the server to start up
Edit =/etc/rc.local=, and add these two lines at the end, before the =exit 0= line.
#+BEGIN_SRC shell
cd /home/pi/sap
npm start
#+END_SRC
** TODO richer formatting in the output?
*** bold, underines...headings...subset of markdown?