Introduction
The need is to create a reliable and fast, simply to modify command set (as a matter of fact should become a Python library) to control the printer features. The small thermal ESC/POS printer, supports a well known protocol able to generate all the needed printout we need.
Adopting a Bluetooth enabled printer has the great advantage that the same peripheral can be used to print from inside the Meditech device and from the Mobile Display Unit without changing almost nothing.
Searching on the available documentation in Internet it seems that the most reliable way to manage the bluetooth hardware layers with python is using the lightblue cross-platform. I have tried it and it really support any kind of Bluetooth layer. There are some issues that can be solved but as the final result I have excluded it. The reason is that the architecture should be as simple as possible. In our case we don't need the full availability of the Bluetooth features but only a serial printer enabled and ready to work wireless, available at short and medium range.
The most simple choice revealed to be setup the serial layer over the Bluetooth interface. With the advantage that by the point of view of the entire system the printer remain a serial device as it should be.
On the RPI side I have used a common bluetooth dongle. As for what I see, the same procedure will work with almost any other USB Bluetooth dongle.
The setup procedure
The entire setup procedure is almost simple and can be replicated in minutes on any Raspberry PI running raspian. I am working with python 3.2 so should be took in account that some instructions in the standard libraries - i.e. the serial library - manages data in a slight different way than the previous python versions. I suggest anyway to adopt this version because the printing data are sent through the serial as bytes(...), including the encoding format. Specifying UTF-8 we are sure that every character is sent as an 8 bit packet to the peripheral. This aspect is important because of the printing protocol to avoid unexpected results when sending data including control characters.
Installing the components
If not yet present it is necessary to install the components as shown below:
sudo apt-get install bluetooth bluez-utils blueman sudo apt-get install python-serial
After this operation the linux Bluetooth components and the serial library for python are installed on the system and we can proceed.
Pairing and stable connection with the printer
The next steps are shown as single command lines, but it is not complex to create a bash script to make all together in a single shot.
hciconfig
Use this command to see how the Bluetooth device is recognised by the system. In my case it is handled as hci0
Now the printer should be powered and visible by the RPI Bluetooth then launch the command
hcitool scan
After waiting a while also the printer will appear in the identified devices. The result is something like
xx:xx:xx:xx:xx:xx PRINTER_NAME
that are the device Bluetooth address and the Bluetooth printer name. At this point we should pair the printer with the RPI using the command
sudo bluez-simple-agent hci0 xx:xx:xx:xx:xx:xx
After few seconds the command ask to introduce the printer Bluetooth pin then the device is paired with the RPI.
At this point we should make the binding between the two bluetooth stable also after the reboot. We can do it editing the rfcomm.conf file
sudo leafpad /etc/bluetooth/rfcomm.conf
When the file opens in the editor (you can use the vi editor is there is not any graphic desktop running) add - or uncomment and change if already present - the following lines:
# Change rfcomm0 accordingly with your Bluetooth dongle setting
rfcomm0 {
bind yes;
# Replace xx:xx:xx ... with the printer Bluetooth address
device xx:xx:xx:xx:xx:xx;
channel 1;
comment "Serial Bluetooth printer";
}
Save the file and exit. Now the last command is to enable the binding with the peripheral immediately with the command
sudo rfcomm bind all
Now the printer is connected and ready to receive data from the applications.
Python printing example
Open the Python 3 Ide and from the editor insert the following test script
#! /usr/bin/python
import serial
from time import sleep
bluetoothSerial = serial.Serial("/dev/rfcomm0", baudrate=9600)
testS = "Raspberry-PI Project \nMeditech Printer Test"
testV = input()
bluetoothSerial.write(bytes(testS, 'UTF-8'))
bluetoothSerial.write(bytes(testV, 'UTF-8'))
Run the script and it should print a test.