Created
March 5, 2023 03:27
-
-
Save JoeCortopassi/f496ab4ea7f156a61c2f1833728d84b1 to your computer and use it in GitHub Desktop.
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
import requests | |
import asyncio | |
import websockets | |
import json | |
import time | |
import board | |
import neopixel | |
pixel_pin = board.D18 | |
num_pixels = 30 | |
ORDER = neopixel.GRB | |
pixels = neopixel.NeoPixel( | |
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER | |
) | |
temp = 0; | |
response = requests.get("http://fluiddpi.local/access/oneshot_token") | |
token = response.json()["result"] | |
websocketURL = f'ws://fluiddpi.local/websocket?token={token}' | |
def percentage_of_value(value, lower, upper): | |
return (value - lower) / (upper - lower) | |
def get_RGB_for_temp(temp): | |
if (temp > 100): | |
temp = 0 | |
red = 0 | |
red = 0 | |
green = 0 | |
blue = 0 | |
if (temp < 50): | |
blue = percentage_of_value(temp, 0, 50) * 255; | |
blue = 255 - blue | |
if (temp >= 40 and temp < 50): | |
green = percentage_of_value(temp, 40, 60) * 255; | |
elif (temp >= 50 and temp <= 60): | |
green = percentage_of_value(temp, 50, 60) * 255; | |
green = 255 - green | |
if (temp > 50): | |
red = percentage_of_value(temp, 50, 100) * 255; | |
red = int(red) | |
green = int(green) | |
blue = int(blue) | |
return (red, green, blue) | |
async def get_temperature(): | |
async with websockets.connect(websocketURL) as websocket: | |
# Subscribe to the printer_status topic | |
await websocket.send(json.dumps({"jsonrpc": "2.0", "id": 54321, "method": "printer.objects.subscribe", "params": {"objects": { "extruder": None, "heater_bed": None}}})) | |
# Continuously receive and process messages | |
async for message in websocket: | |
data = json.loads(message) | |
# print(f'{data}') | |
if ('method' in data and data["method"] == "notify_status_update"): | |
if ('params' in data and 'extruder' in data["params"][0]): | |
temp = int(data["params"][0]["extruder"]["temperature"]) | |
RGB = get_RGB_for_temp(temp) | |
print(f'temp: {temp}, colors:{RGB}') | |
pixels.fill(RGB) | |
pixels.show() | |
asyncio.get_event_loop().run_until_complete(get_temperature()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment