Skip to content

Instantly share code, notes, and snippets.

@koma5
Last active December 15, 2016 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koma5/a10a85c1d619dfbe8467e0d5dd18fbbb to your computer and use it in GitHub Desktop.
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
# -*- 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