Canon PIXMA Ink Level

Hi,

as there is no binding to get the ink level from my printer I have used HTTP Binding to get this information into openHAB. I hope this is helpful to others with a similar device.

Tested with: Canon Pixma TS6350a, Firmware 1.002
Prerequisites:

  1. Canon printer needs to be online reachable from openHAB server
    1.1 This example is based on printer with 5 cartridges; if your printer has a different number of cartridges manually review the JS file from installation step 2.1 and review name & order of the cartridges of your device
    1.2. Name & order of the cartridges of your printer are defined in var inkCOL within the model.js file
  2. HTTP Binding
  3. RegEx - Transformation Services
  4. JavaScript - Transformation Services

Installation & Configuration:

  1. Create JS file within openhab/transformation folder
    1.1 Create a new file with name CanonInkLevel.js within the transformation folder
    1.2 Add the bellow JS transformation code into the file

  2. From the MainUI create a new HTTP:URL thing
    2.1 Enter http://PRINTER_IP/JS_MDL/model.js?ver=1.002-188D as base url
    2.2 Open the newly created thing file and go to Code tab
    2.3 After the existing configuration add the below code for channel definition
    2.4 Optional: Adjust name and order of cartridges if your device is showing different amount / order of ink level:
    image
    2.5 Save the thing file, link some items and have fun

  3. Advanced & Optional: As the HTTP Binding will not work once the printer is turned off / offline, you can use e.g. Network Binding or TR064 binding to monitor online status and only activate HTTP thing if the printer is online
    3.1 Generate an API key from your own openHAB instance, as thing update can only be achieved through rest api
    3.2 Create a switch item that will represent your printers online state and link it to the correct channel (e.g. based on network binding)
    3.3 Create a new rule, that is triggered every time your item from step 3.2 will change
    3.4 Use below rule code (replace your API key & Thing ID) to automatically enable / disable the HTTP thing

JS transformation code
(function(i) {
    return 100-i*10;
})(input)
Thing channel definition
channels:
  - id: Magenta
    channelTypeUID: http:dimmer
    label: Magenta
    description: null
    configuration:
      mode: READONLY
      onValue: "100"
      step: "10"
      offValue: "0"
      stateTransformation: REGEX:.*inktank\[0\]=\[[0-9],([0-9]),[0-9]\].*∩JS:CanonInkLevel.js 
 - id: Black
    channelTypeUID: http:dimmer
    label: Black
    description: ""
    configuration:
      mode: READONLY
      onValue: "100"
      step: "10"
      offValue: "0"
      stateTransformation: REGEX:.*inktank\[1\]=\[[0-9],([0-9]),[0-9]\].*∩JS:CanonInkLevel.js
  - id: Yellow
    channelTypeUID: http:dimmer
    label: Yellow
    description: null
    configuration:
      mode: READONLY
      onValue: "100"
      step: "10"
      offValue: "0"
      stateTransformation: REGEX:.*inktank\[2\]=\[[0-9],([0-9]),[0-9]\].*∩JS:CanonInkLevel.js
  - id: PigmentBlack
    channelTypeUID: http:dimmer
    label: Pigment Black
    description: null
    configuration:
      mode: READONLY
      onValue: "100"
      step: "10"
      offValue: "0"
      stateTransformation: REGEX:.*inktank\[3\]=\[[0-9],([0-9]),[0-9]\].*∩JS:CanonInkLevel.js
  - id: Cyan
    channelTypeUID: http:dimmer
    label: Cyan
    description: null
    configuration:
      mode: READONLY
      onValue: "100"
      step: "10"
      offValue: "0"
      stateTransformation: REGEX:.*inktank\[4\]=\[[0-9],([0-9]),[0-9]\].*∩JS:CanonInkLevel.js
Rule code
  var headers = newHashMap("Authorization" -> "Bearer YOUR_API_KEY", "WWW-Authenticate"-> "Basic")
  var onOff = "";

 switch (newState) {
   case ON:
     {
       onOff = "true";
     }
   case OFF:
     {
       onOff = "false";
     }
 }

sendHttpPutRequest("http://localhost:8080/rest/things/THING_ID/enable", "text/plain", onOff,headers,1000);
4 Likes

Continuing the discussion from Canon PIXMA Ink Level:

Good one, thanks for sharing. Didn’t setup the binding but tested against my Canon MG7160 out of curiosity which worked with a couple of adjustments:

  • URL http://<printer_ip>/rui/JS/model.js?ver=13AU2 to cater for my printer model

  • REGEX .*inktank\[x\]=\[[0-9],([0-9]+),[0-9]\].* to cater for my empty cartridges :roll_eyes:

    inktank[0]=[0,6,0];
    inktank[1]=[5,10,2];
    inktank[2]=[4,7,0];
    inktank[3]=[1,2,0];
    inktank[4]=[2,10,2];
    inktank[5]=[3,9,1];

Cheers :+1:

1 Like