Amazon ALEXA Lists Exercise – Put’em on Paper…
4.6 Alternative: use your Fritz!Box as print server
In case you don’t want a separate Pi, but do have a router, e.g. a Fritz!Box, you can most likely use that one as print server. I own a 7490 and simply by plugging in the printer, it would be available under the Fritz!Box‘ ip address, e.g. 192.168.175.1 , or its host name, fritz.box , and the RAW port number 9100 .
The ESC/POS code supports this RAW network interface through a separate printer interface, here’s the code.
<?php // small obscurity secret checking... $localsecret = "1234567890__REPLACE__WITH__YOUR__SECRET__1234567890"; if ($localsecret != $_REQUEST["secret"]) { header("HTTP/1.1 403 Unauthorized"); exit(1); } // small obscurity secret checking... $listName = urlencode($_REQUEST["listName"]); $listItems = json_decode($_REQUEST["listItems"]); if (!is_array($listItems) || count($listItems)<1 || count($listItems)>50) { header("HTTP/1.1 400 Bad Request"); $error = array( "statusCode"=>400, "statusText"=>"Please provide at least 1 and at best 50 list item(s) in request parameter named 'listItems' in form of a JSON UTF8 list.", "listName"=>$_REQUEST["listName"], "listItems"=>$_REQUEST["listItems"] ); echo json_encode($error); exit(2); } // print the crap... require __DIR__ . '/escpos/autoload.php'; use Mike42\Escpos\PrintConnectors\NetworkPrintConnector; use Mike42\Escpos\Printer; // ***** USE THE NETWORK PRINTER $connector = new NetworkPrintConnector("fritz.box", 9100); $printer = new Printer($connector); if ($printer==null) { header("HTTP/1.1 503 Service Unavailable"); $error = array( "statusCode"=>500, "statusText"=>"No thermal printer connected." ); echo json_encode($error); exit(1); } // ***** INPUT SANITY CHECKING $listItems = json_decode($_REQUEST["listItems"]); if (!is_array($listItems) || count($listItems)<1 || count($listItems)>50) { header("HTTP/1.1 400 Bad Request"); $error = array( "statusCode"=>400, "statusText"=>"Please provide at least 1 and at best 50 list item(s) in request parameter named 'listItems' in form of a JSON UTF8 list." ); echo json_encode($error); die(1); } $listName = mb_strtoupper($_REQUEST["listName"]); // make a default if ($listName=="") { $listName = "UNBENANNTE LISTE"; } // truncate to 32 chars if (mb_strlen($listName)>32) { $listName = mb_substr($listName, 0, 16); } // Set print mode to double height only if (mb_strlen($listName)<=16) { $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT|Printer::MODE_DOUBLE_WIDTH); } else { $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT); } // **** PRINT THE HEADER $printer -> setJustification(Printer::JUSTIFY_CENTER); $printer -> setEmphasis(true); $printer -> text($listName."\n"); $printer -> selectPrintMode(); // **** PRINT THE DIME (10.10.2017 16:43) $printer -> setReverseColors(true); $printer -> text(date("---- d.m.Y ----- H:i ----\n")); $printer -> setReverseColors(false); $printer -> setEmphasis(false); $printer -> setJustification(Printer::JUSTIFY_LEFT); $printer -> feed(1); // **** PRINT THE LIST foreach($listItems as $item) { $item = iconv("UTF-8", "CP437", $item); //required since now UTF8 characters are not printed correctly $item = "* ". ucwords(strtolower(mb_substr($item, 0, 30))) ."\n"; $printer -> text($item); } $printer -> feed(5); $printer -> cut(); $printer -> close(); $error = array( "statusCode"=>200, "statusText"=>"List '$listName' was printed successfully." ); echo json_encode($error); ?>
Note that you need to change the secret and the IP / host name for your print server.
Small caveat: In my case, the UTF-8 characters that were printing flawlessly on the Raspberry Pi print server, come out as two ASCII characters. A German ‚ü‘ for example would become two of the frame characters. I don’t know if it’s the printer connector’s fault or if there’s something wrong with the fritzbox code, but a work-around is using iConv to convert UTF8 to CP437. It has most of the German diacritics, so good enough for me.
5 Kommentare zu “Amazon ALEXA Lists Exercise – Put’em on Paper…”
Hi,
great post.
Could please tell which thermal printer you have used?
The amazon link isnt‘ working anymore.
Thanks
hi, Thanks. The one I used isn’t available on Amazon anymore, but any USB receipt printer should work… Just look for what mike24’s ESC/POS library is supporting and buy one of those models… Best, Christoph
Thanks for the reply. This product list on github helps a lot.
One further question: Could you really delete the original shopping list with the skill? In your video you use the original „Einkaufsliste“ so you could also delete this one per API? In your code for the lambda it is „shopping list“ and not the original code snipped?
Danke und Gruß
Tom
hi, well, the Einkaufsliste is used by the ALEXA language model. it translates into „shopping list“ in the skill. Therefore the difference… Cheers.
Hello!
Nice work ! I try to integrate it to home assistant .
Your video link is dead could you update it please?
regards
Ben
Einen Kommentar hinterlassen