Last active
December 15, 2016 16:51
-
-
Save koma5/a10a85c1d619dfbe8467e0d5dd18fbbb to your computer and use it in GitHub Desktop.
mqtt color temp scale from blue for -15 C° to red for 40 C° via green
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import mosquitto | |
def on_connect(client, userdata, rc): | |
client.subscribe("vw/temp/1") | |
def on_message(client, userdata, msg): | |
rgb = colorWheel(scaleTempToColor(float(msg.payload))) | |
client.publish("vw/neo/color", ','.join(str(x) for x in rgb)) | |
def scaleTempToColor(temp): | |
# (-15|170) und (40|0) | |
return temp * -34 / 11 + 1360 / 11 | |
def colorWheel(wheelPos): | |
wheelPos = 255 - int(round(wheelPos)); | |
if(wheelPos < 85): | |
return [255 - wheelPos * 3, 0, wheelPos *3] | |
if(wheelPos < 170): | |
wheelPos -= 85 | |
return [0, wheelPos * 3, 255 - wheelPos * 3] | |
wheelPos -= 170 | |
return [wheelPos * 3, 255 - wheelPos * 3, 0] | |
client = mosquitto.Mosquitto() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect("mqtt", 1883, 60) | |
client.loop_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment