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:
- 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 - HTTP Binding
- RegEx - Transformation Services
- JavaScript - Transformation Services
Installation & Configuration:
-
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 -
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:
2.5 Save the thing file, link some items and have fun -
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);