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