Thermal Printer from Adafruit

I’m trying to get the Adafruit thermal printer working, I’ve had success with printing text, but want the ability to print images.

This is the printer

I found some arduino code online, and was trying to migrate some of that code, but without any luck.

https://github.com/lazyatom/Thermal-Printer-Library/blob/82dacbdd6c3ea6a32fe123cb1ff17b6810a4c330/Thermal.cpp

I’m stuck on the part of the code that actually writes the pixels to the printer. The line that starts “PRINTER_PRINT” is the arduino line I’m trying to convert, and the line below is my version of it, but instead of outputting a byte, it outputs ALL my data, with a incrementally increasing byte added to the end.

I know this part is wrong, but I don’t know how to solve it.

`
// Mini Printer
server.log(“Printer Started”);

// Hardware Configuration
local serial = hardware.uart57;

serial.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS);

local imageArray = “0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F,0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F,0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F,0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F,0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F,0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F,0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F,0x1F, 0x3D, 0xA8, 0x7E, 0x7E, 0xA8, 0x3D, 0x1F”;

local function writeImage(w,h,imageArray){
local rowStart;
local i;
local chunkHeight;

if (w > 384) {return;} // maximum width of the printer

for (rowStart=0; rowStart < h; rowStart += 256) {
chunkHeight = ((h - rowStart) > 255) ? 255 : (h - rowStart);
serial.write(18); //used to tell the printer what to expect (I think)
serial.write(42); //same here
serial.write(chunkHeight); //passing height
serial.write(w/8); // passing width
for (i=0; i<((w/8)chunkHeight); i++) {
//PRINTER_PRINT(pgm_read_byte(bitmap + (rowStart
(w/8)) + i)); // <-- taken from the arduino code
//serial.write(imageArray + (rowStart*(w/8)) + i); // <-- this isn’t right
}
}
}

writeImage(16,16,imageArray);

imp.configure(“Printer”, [], []);
`

I can print a 16 x 16 block of pixels by sequentially writing the bytes in my “image”, but they are random, and have no resemblance to what I’m trying to print.

Thanks for the help!

Maybe take a look at the example code we published for this printer in our examples github?

@hugo
Can it print barcodes ?

The code in the GitHub repo is not setup to print barcodes - but it is absolutely possible. The thermal printer has a couple of barcode fonts built in, so all you need to do is send some commands to switch fonts and then you’re off to the races!

Hi all, any advice on how to print the images? I never managed to get the github source to print images such as the logo (IDK why), but perhaps stupidly, I decided to plough on. So I’m trying to do it with this python code: (apologies if its wrong/bad/insane, still very much learning Python)

`def printimage(imageURL):
### retrieve an image from a supplied URL, load it, resize it, and print it.
size = 384, 384 # magic numbers - 384 is max width the printer supports, so using it for height is a good idea too to avoid pushing the Imp over memory.

file = io.BytesIO(urllib.request.urlopen(imageURL).read())
img = Image.open(file)
img=img.convert(mode="1") # change to a bitmap
img.thumbnail(size, Image.ANTIALIAS) # resize
img.save("temp.bmp") # save it for debugging purposes. `

This part above all seems to work

`
with open(“temp.bmp”, “rb”) as image_file:
imagebytes = base64.b64encode(image_file.read())
print(timestamp()+" image converted, sending to print, size: “+str(len(imagebytes)))
## potentially broken?
##response=requests.post(AGENT_URL+”/image",files={‘data’: imagebytes})
##print(response.content)
data={“justify”: “left”, “bold”:False, “underline”:False, “deleteLine”:False, “reverse”:False, “updown”:False, “data”:str(imagebytes)}
headers={“Content-type”:“application/json”,“Accept”:“text/plain”}

   req = urllib.request.Request(AGENT_URL+"/image",json.dumps(data, separators=(',',':')).encode("ASCII"))
   f=urllib.request.urlopen(req)`

This could work (?), as the printer receives data, but then spews out garbage onto the page, and never comes out of bitmap mode again until I reset it/the imp.

I’ve been banging my head against this problem off and on for a couple months now when I get spare time, and I’ve got nowhere. Any suggestions would be really appreciated!