Created
May 8, 2020 13:17
-
-
Save Garfonso/221a42fc0c0558da15db5e09b473d6b7 to your computer and use it in GitHub Desktop.
Control AtmoLight with ioBroker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I had an old AtmoLight ( http://www.mediacenter-pc.com/atmolight/ ) idling around. | |
// It is connected via USB (and creates a serial port) and controls four LED stripes | |
// So I wanted to control it with ioBroker. Here is the script for that. | |
// You should set the right serial port below: | |
//configure right serial port here. I recommend looking up the ID and replacing the scrint below by the id string. | |
//looks like: /dev/serial/by-id/usb-c@rstenpresser.de_AtmoLight_SMD_v04_23U9NVYR-if00-port0" | |
const portPath = "/dev/ttyUSB"; | |
//also you can configure the name of a folder in javascript.* here. There will be two states, brighness and color in there. | |
const baseName = "AtmoLight"; | |
createState(baseName + ".brightness", {type: "number", min: 0, max: 100, role: "level.brightness", unit: "%"}); | |
const brightnessId = "javascript." + instance + "." + baseName + ".brightness"; | |
createState(baseName + ".color", {type: "string", role: "level.color.rgb"}); | |
const colorId = "javascript." + instance + "." + baseName + ".color"; | |
//green: write: ff 00 00 0f 00 00 00 00 ff 00 00 ff 00 00 ff 00 00 ff 00 | |
//red: write: ff 00 00 0f 00 00 00 ff 00 00 ff 00 00 ff 00 00 ff 00 00 | |
//blue: write: ff 00 00 0f 00 00 00 00 00 ff 00 00 ff 00 00 ff 00 00 ff | |
const debug = true; | |
const prefix = "ff 00 00 0f 00 00 00"; | |
const prefixArray = [0xFF, 0, 0, 0x0F, 0, 0, 0]; | |
const SerialPort = require("serialport"); | |
const port = new SerialPort(portPath, { | |
baudRate: 38400, | |
lock: false | |
}, function (err, val) { | |
if (err) { | |
log("Error opening port: " + err); | |
} else { | |
log("Opened port: " + val); | |
} | |
}); | |
function NumToHex(num) { | |
var hex = Number(num).toString(16); | |
if (hex.length < 2) { | |
hex = "0" + hex; | |
} | |
return hex; | |
}; | |
let commandArray = []; | |
let brightness = 100; | |
function processRGBString(rgbString) { | |
if (rgbString.length !== 7) { | |
rgbString = "#" + rgbString; | |
} | |
let r = parseInt(rgbString.substr(1, 2), 16) * brightness / 100; | |
let g = parseInt(rgbString.substr(3, 2), 16) * brightness / 100; | |
let b = parseInt(rgbString.substr(5, 2), 16) * brightness / 100; | |
let colorArray = [r,g,b]; | |
commandArray = prefixArray.concat(colorArray, colorArray, colorArray, colorArray); | |
if (debug) { | |
let strArray = []; | |
commandArray.forEach(v => strArray.push(NumToHex(v))); | |
log("Got color " + rgbString + " Writing: " + strArray.join(" ") + " (" + commandArray.length + ")"); | |
} | |
} | |
setInterval(function () { | |
port.write(commandArray); | |
}, 160); | |
on({id: colorId, ack: false}, (ev) => { | |
let val = ev.newState.val; | |
processRGBString(val); | |
setState(colorId, val, true); | |
}); | |
on({id: brightnessId, ack: false}, (ev) => { | |
brightness = ev.newState.val; | |
getState(colorId, (err, state) => processRGBString(state.val)); | |
setState(brightnessId, brightness, true); | |
}) | |
getState(brightnessId, (err, state) => { | |
brightness = state.val; | |
getState(colorId, (err, state) => processRGBString(state.val)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment