1

I am trying to control some NeoPixel lights, and because the Raspberry Pi can only control one light at any given time, I decided I would just get a microcontroller to do it, specifically the Adafruit ItsyBitsy M4 Express, and just send serial data through the USB connecting the two to control them. I got that working, using PySerial to send data to the microcontroller. However, after the RPi script sends data 8 times, it stops working and I have to reload the script. Additonally, by stop working, I mean that the script gets stuck sending data and never ends. The strange part is that if I open the MU serial after my script has started running, I can A: see the data being sent in, and B: it never stops. It goes past the 8 issue and keeps going. RPi Code:

import serial
import time
from random import randint
a = time.time() #Unimportant, was looking to see how quickly Serial could be re-defined
try:
    ser = serial.Serial(port='/dev/ttyACM1', baudrate=115200, write_timeout=0.05)
except:
    ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200, write_timeout=0.05) 
print(time.time() - a)
EnterKey = "\x1A\r\n"
e = 0
while True:
    e+=1 
    d = []
    for number in range(0, 30):
        d.append([randint(0, 255), randint(0, 255), randint(0, 255)])
    d = ((str(d)) + EnterKey).encode()
    ser.write(d)
    time.sleep(1)
    print(e)

Microcontroller code:

import board
import supervisor
import neopixel
import time
a = neopixel.NeoPixel(board.D5, 30, auto_write=False)
supervisor.diable_autoreload()
while True:
    b = input("automated")
    RST = []
    c = ""
    R3T = []
    started = False
    for value in b:
        if started == True:
            if value == '[':
                started2 = True
                ending = False
            elif value == ']':
                if ending == True:
                    started=False
                else:
                    RST.append(int(c))
                    c = ""
                    ending = True
                    started2 = False
                    R3T.append(RST)
                    RST = []
            elif started2 == True:
                if value.isdigit():
                    c += value
                elif value == ",":
                    RST.append(int(c))
                    c = ""
        elif value == '[':
            started = True
    for value in range(0, 30):
        a[value] = R3T[value]
    a.show()
    print(a)

I tried being lazy and just re-defining the serial object after a write timeout, but that does not work. (Errno 16] Device or resource busy). I then went around looking for Raspberry Pi settings seeing if anything was getting in my way, but found nothing. I ultimately gave up and came here. If you have any ideas, please tell me!

Lazuli
  • 31
  • 5
  • What does this mean? **past the 8 issue and keeps going** There is nothing in your sender code that stops sending after any amount sent. – markus-nm Apr 05 '22 at 14:44
  • That's the issue. My code is never told to stop, but for some reason the serial connection stalls, and it gets stuck sending the data for the 8th loop. The code never actually encounters an error (unless I put a timeout), it just stalls infinitely. What happens when I open the mu serial is that the code never stalls infinitely and it keeps sending data even past the 8th loop. – Lazuli Apr 06 '22 at 07:29
  • What do you mean by 'opening the mu serial'? Can you continuously send data to your µc using a bash script (loop echo "testdata" > /dev/ACM0) or does this stall as well? What happens when you reverse the communication flow (read from RPi, send from µC)? – markus-nm Apr 07 '22 at 09:54
  • Sorry for the delay on the response, I have yet to test the bash script, but I can at least explain the mu serial. MU is a coding environment, or whatever they are called (like Thonny or Jupyter Notebook). It is meant to code adafruit microcontrollers, and they have a built-in serial that automatically connects to the microcontroller so you can see your prints. – Lazuli Apr 15 '22 at 20:38

0 Answers0