bitrate16 / peripage-python

Python module and documentation for direct printing on Peripage thermal printers via bluetooth
GNU General Public License v3.0
77 stars 10 forks source link

Keeping printer awake? #5

Closed createcandle closed 2 years ago

createcandle commented 2 years ago

I'm trying to integrate your fantastic work into a plugin for a privacy friendly smart home system. The idea is that data logs can be printed to paper every hour, and then deleted, so that only a paper trail exists.

I've managed to get everything working. But even though it's permanently plugged into power, after a while the PeriPage printer sometimes doesn't connect.

[Errno 112] Host is down

It probably went into standby mode?

How would you recommend keeping the printer awake/connected so that I can print once every hour/day?

flatsiedatsie commented 2 years ago

For anyone finding this: polling the printer for its battery level every 30 seconds is keeping it alive so far.

Also, trying the reconnect method before you try the connect method as a backup also seems to work well. After a reboot of the Raspberry Pi it seems to reconnect well.

bitrate16 commented 2 years ago

@createcandle It seems like this printer may go in standby mode after some time, but mostly it can be configured using setTimeout(). In other case you may use the solution suggested by @flatsiedatsie and ping printer every 30 seconds by requesting battery status with getDeviceBattery().

createcandle commented 2 years ago

Do you have an example/suggestion on setTimeout()? Do you know the maximum value the printer will accept? What value do you recommend? Or is this not how I should understand it?

bitrate16 commented 2 years ago

I've mistyped function name.

You can use setPowerTimeout(minutes) to change power timeout on printer. This printer allows setting timeout from 1 to 65520 seconds, which means printer will stay awake for this time. I've set it for 60 minutes when performed tests.

In addition you can set socket connection timeout (time that socket will wait for response until recognizes the printer as unavailable) with setTImeout(seconds), but time limit seems to be implementation dependent in pybluez. I've used 1 second when performed tests.

In case when you need constantly working connection, you can setTimeout to several seconds, setPowerTimeout to 10+ minutes and periodically call getDeviceBattery() to keep socket connected and printer awake.

createcandle commented 2 years ago

Ok, thanks, I'm trying it now.

                self.printer = ppa6.Printer(self.persistent_data['printer_mac'], ppa6.PrinterType.A6p)
                self.printer.setPowerTimeout(300) # minutes
                self.printer.setTimeout(8) # seconds
bitrate16 commented 2 years ago

I would suggest to make a test like this:

import time

printer = ppa6.Printer('mac', ppa6.PrinterType.A6p)
printer.connect()
printer.setPowerTimeout(1)
printer.setTimeout(1)

while True:
    print(printer.getDeviceBattery())
    time.sleep(10)

To check if getDeviceBattery really keeps printer awake. I can not check it right now because my bluetooth support is broken.

Also, remember to call connect() before using setPowerTimeout()

createcandle commented 2 years ago

Thanks! I'm was seeing an error:

error setting up printer: 'Printer' object has no attribute 'sock'

Probably because it wasn't connected yet :-D

I know that getDeviceBattery keeps the device awake, I've tested that.

createcandle commented 2 years ago

I noticed that the printer is not marked as 'trusted' after being connected. Marking it as such might make automatic reconnecting easier?

sudo bluetoothctl trust XX:XX:XX:ETC

bitrate16 commented 2 years ago

I'm not sure because my tests were performed on windows and raspberry pi without making printer trusted and reconnection worked similar on each platform

createcandle commented 2 years ago

Ah, yes, that would matter. In my experiments the printer seemed to reconnect ok as long as the reboot was fast. It generally lasted a few days, but then would eventually lose connection. I'm hoping these experiments improve it.

I should also explore the USB connection option I guess, but my Raspberry Pi doesn't have that much power or ports to spare. Are you still planning to implement USB support?

bitrate16 commented 2 years ago

Yes, I'm planning to implement usb printing, but not sure when

bitrate16 commented 2 years ago

For now you can give a try for print_service.py. It's not fully tested yet, but worked fine for ASCII & Images until RPI zero W SD died