parent
0649f4fd97
commit
66774fd2eb
|
@ -9,9 +9,8 @@ import paho.mqtt.client as mqtt
|
|||
from getmac import get_mac_address
|
||||
import board
|
||||
|
||||
# Replace with your server's IP address and port
|
||||
SERVER_IP = '192.168.0.1' # Change to the actual IP of the PiXelTube Master
|
||||
SERVER_PORT = 5000 # Change to the port your Flask app is running on
|
||||
SERVER_IP = '192.168.0.1'
|
||||
SERVER_PORT = 5000
|
||||
|
||||
# Dynamically obtain the MAC address of the WLAN interface
|
||||
wlan_mac_address = str(get_mac_address(interface="wlan0"))
|
||||
|
@ -22,8 +21,6 @@ global LED_COUNT
|
|||
LED_COUNT = 30
|
||||
global LEDS_PER_PIXEL
|
||||
LEDS_PER_PIXEL = 5
|
||||
global pixel_data
|
||||
pixel_data = None
|
||||
|
||||
# Global variables for LED strip control
|
||||
global strip
|
||||
|
@ -58,37 +55,41 @@ def is_connected_to_wifi():
|
|||
output = subprocess.check_output(['iwgetid'])
|
||||
return output.split('"')[1] is not None
|
||||
|
||||
def update_led_strip(rgb_values, pixel, strip):
|
||||
for i in range(LEDS_PER_PIXEL):
|
||||
strip[int(pixel)] = Color(rgb_values[i])
|
||||
# def update_led_strip(rgb_values, pixel, strip):
|
||||
# for i in range(LEDS_PER_PIXEL):
|
||||
# strip[int(pixel)] = Color(rgb_values[i])
|
||||
|
||||
def mqtt_listner(msg, universe, dmx_address, strip, LEDS_PER_PIXEL):
|
||||
try:
|
||||
# Parse the topic to get universe and channel
|
||||
_, dmx_universe, channel_number = msg.topic.split("/")
|
||||
channel_number = int(channel_number)
|
||||
# def mqtt_listner(msg, universe, dmx_address, strip, LEDS_PER_PIXEL):
|
||||
# try:
|
||||
# # Parse the topic to get universe and channel
|
||||
# _, dmx_universe, channel_number = msg.topic.split("/")
|
||||
# channel_number = int(channel_number)
|
||||
|
||||
# Calculate the pixel index and channel within the pixel
|
||||
pixel_index = (channel_number - dmx_address) // LEDS_PER_PIXEL
|
||||
channel_in_pixel = (channel_number - dmx_address) % LEDS_PER_PIXEL
|
||||
# # Calculate the pixel index and channel within the pixel
|
||||
# pixel_index = (channel_number - dmx_address) // LEDS_PER_PIXEL
|
||||
# channel_in_pixel = (channel_number - dmx_address) % LEDS_PER_PIXEL
|
||||
|
||||
# Initialize a new pixel entry if not present
|
||||
if pixel_index not in pixel_data:
|
||||
pixel_data[pixel_index] = [0] * LEDS_PER_PIXEL
|
||||
# # Initialize a new pixel entry if not present
|
||||
# if pixel_index not in pixel_data:
|
||||
# pixel_data[pixel_index] = [0] * LEDS_PER_PIXEL
|
||||
|
||||
# Update the RGB value for the corresponding channel in the pixel
|
||||
pixel_data[pixel_index][channel_in_pixel] = int(msg.payload)
|
||||
# # Update the RGB value for the corresponding channel in the pixel
|
||||
# pixel_data[pixel_index][channel_in_pixel] = int(msg.payload)
|
||||
|
||||
# Check if all three channels for the pixel are received
|
||||
if len(pixel_data[pixel_index]) == LEDS_PER_PIXEL:
|
||||
# Set the RGB values for the pixel in the LED strip
|
||||
update_led_strip(pixel_index, pixel_data[pixel_index], strip)
|
||||
# # Check if all three channels for the pixel are received
|
||||
# if len(pixel_data[pixel_index]) == LEDS_PER_PIXEL:
|
||||
# # Set the RGB values for the pixel in the LED strip
|
||||
# update_led_strip(pixel_index, pixel_data[pixel_index], strip)
|
||||
|
||||
# Remove the pixel entry from the temporary storage
|
||||
del pixel_data[pixel_index]
|
||||
# # Remove the pixel entry from the temporary storage
|
||||
# del pixel_data[pixel_index]
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
# except Exception as e:
|
||||
# print(f"Error: {e}")
|
||||
|
||||
|
||||
# def on_message(mqttc, obj, msg):
|
||||
# mqtt_listner(msg)
|
||||
|
||||
def loopCheckSettingUpdates():
|
||||
while True:
|
||||
|
@ -100,9 +101,6 @@ def loopCheckSettingUpdates():
|
|||
print(f"Error: {e}")
|
||||
time.sleep(2)
|
||||
|
||||
def on_message(mqttc, obj, msg):
|
||||
mqtt_listner(msg)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Connect to Wi-Fi
|
||||
if is_connected_to_wifi() is not None:
|
||||
|
@ -113,13 +111,13 @@ if __name__ == "__main__":
|
|||
global dmx_address
|
||||
universe, dmx_address = get_assigned_params()
|
||||
|
||||
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||
mqttc.connect("192.168.0.1", 1883, 60)
|
||||
mqttc.on_message = on_message
|
||||
mqttc.subscribe("PiXelTubes/"+str(universe), 0)
|
||||
# mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||
# mqttc.connect("192.168.0.1", 1883, 60)
|
||||
# mqttc.on_message = on_message
|
||||
# mqttc.subscribe("PiXelTubes/"+str(universe), 0)
|
||||
|
||||
settingsUpdateThread = Thread(target=loopCheckSettingUpdates, args=())
|
||||
pixelUpdateThread = Thread(target=mqtt_listner, args=(universe, dmx_address, strip, LEDS_PER_PIXEL))
|
||||
# pixelUpdateThread = Thread(target=mqtt_listner, args=(universe, dmx_address, strip, LEDS_PER_PIXEL))
|
||||
|
||||
settingsUpdateThread.start()
|
||||
pixelUpdateThread.start()
|
||||
# pixelUpdateThread.start()
|
Loading…
Reference in New Issue