0

I am programming ada fruit ring LED lights in python, I have setup a local network for a midi controller to read data into my Raspberry Pi 3 and send to the 3 Pi Zero W through web sockets. I am running into the issue where some of the functions I have that run on threads will get out of sync with each other. In other words, one of the LED lights will go at a slightly faster or slower rate and eventually look not synced. I was curious if anyone has run into an issue like this or if I am just using the threads and events the incorrect way.

Here is some of my server side code:

import socket
from socket import *
from threading import Thread
import threading
import board
import neopixel
import sys
import time
import random
import os
from adafruit_led_animation.animation.comet import Comet

def handle_client(client):
     p = None
     connected = True
     lights_on_default()
     while True:
          global thread_running
          data=client.recv(1024).decode()
          if not data:
               connected = False
               thread_running = False
               break
          if 'Pad' in data:
               thread_running = False
               thread = threading.Event()
               if data == 'Pad 1':
                    thread_running = False
                    if p:
                         p.join()
                    p = Thread(target=slow_circles, args=(thread,))
                    p.daemon = True
                    p.start()

def slow_circles(event):
     global thread_running
     thread_running = True
     while not event.isSet():
          if not thread_running:
               event.set()
               break
          for i in range(num_of_pixels):
               // goes through the lighting of pixels
               if not thread_running:
                    event.set()
                    break
               pixels.show()

Here is some of my client side code:

import rtmidi.midiutil as midiutil
import time
import socket

buttons = {
     36: 'Pad 1',
}

try: 
     s1 = socket.socket()
     s2 = socket.socket()
except:
     print('Error making socket')
port1 = 12346
port2 = 12347

serverOne = '192.168.1.18' // 1st Pi Zero W
serverTwo = '192.168.1.17' // 2nd Pi Zero W

def start():
     global s1
     global s2
     notConnected = True
     while True:
          while notConnected:
               connected = False
               try:
                    s1.connect((serverOne, port1))
                    s2.connect((serverTwo, port2))
                    connected = True
                    break
               except:
                    s1.close()
                    s2.close()
                    s1.socket.socket()
                    s2.socket.socket()
          if connected:
               midiin, port = midiutil.open_midiinput(1)
               midiin.set_callback(midiCallback) // gets pad from midi controller
               while True:
                    retry = False
                    try:
                         s1.sendall('red connected'.encode('utf8'))
                         s2.sendall('yellow connected'.encode('utf8'))
                    except:
                         retry = True
                    if retry:
                         s1.close()
                         s2.close()
                         midiin.delete()
                         break
                    else:
                         time.sleep(15)

def midiCallback(message, data):
     control = message[0][1]
     try:
          value = message[0][2]
          if (control in button.keys()):
               name = buttons[control]
               if (value > 0):
                    return buttonDown(name)
     except IndexError:
          pass

def buttonDown(button):
     s1.sendall(f'{button}'.encode('utf8'))
     s2.sendall(f'{button}'.encode('utf8'))

start()

And when I hit the pad and do the function I send data to the two PIs and they start at the same time but over time get out of sync. All help would be appreciated thanks.

  • It's really hard to have any ideawhat you are trying to do, or what OS you are using or what the actual problem is... – Mark Setchell Aug 22 '21 at 21:05
  • I am using the raspberrian OS for all the PIs, I have the Pis powering the LED ring lights, one Pi sends data to the others with a Pad number, the 3 other Pis receive the data, decode it, and execute a function based off the decoded value. The problem only exists when the function trying to execute uses threads. For example, on a flashing color function, when the thread gets executed on multiple Pis they seem to execute at the same time across the network but over time the pi and the corresponding LEDs connected seem to start flashing at different intervals. The question is how do I fix that? – justACuriousDJ Aug 23 '21 at 01:20
  • Still no idea what lights you have, nor which RasPis they are connected to, nor how your network is set up, nor what your *"pad"* is that you don't mention in the description, nor why you hit it, nor what you are even hoping to do. Maybe a diagram would help? – Mark Setchell Aug 23 '21 at 07:25
  • Even seeing my own example and I will get the diagram soon, I believe it is due to calling a wait/delay, been reading a lot and seems to have a lot more negative effects than positive. – justACuriousDJ Aug 23 '21 at 15:15

0 Answers0