Using a raspberry pi with pymongo and neopixels together

34 views Asked by At

So, I am beginning a project where I want to use a Raspberry Pi to take input from a MongoDB database from the web and translate it into neopixel colours. I am running a virtual environment.

My problem is that when I run the code normally (python project.py) pymongo works fine, but the neopixels do not, because the neopixels need sudo authorization. When I run (sudo python project.py) neopixels work, but pymongo thows an error of module not found.

Is there a way I can either run neopixels without sudo or a reason that pymongo does not work in sudo?

Here is the code for clarification:


import os
import time
import board
from rainbowio import colorwheel
import neopixel
from pymongo import MongoClient 
from dotenv import load_dotenv

load_dotenv()


client = MongoClient(os.getenv('MONGO_URI'))
db = client["led"]
collection = db["users"]
cursor = collection.find().sort([('_id', -1)]).limit(1)
results = cursor[0] 
message = results['input'].upper()
msg = message
print(results)
print(msg)

letters = []

pixel_pin = board.D18
num_pixels = 150
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)

for letter in msg:
    letters.append(letter)

def colour_chase(color, wait):
    for i in range(num_pixels):
        pixels[i] = color
        time.sleep(wait)
        pixels.show()
    time.sleep(0.5)


def rainbow_cycle(wait):
    for j in range(255):
        for i in range(num_pixels):
            rc_index = (i * 256 // num_pixels) + j
            pixels[i] = colorwheel(rc_index & 255)
        pixels.show()
        time.sleep(wait)
CODE = {
' ': '(0, 0, 0)', 
"'": '(50, 50, 50)', 
'(': '(100, 100, 100)', 
')': '(125, 125, 125)', 
',': '(150, 150, 150)', 
'/': '(175, 175, 175)', 
'-': '(200, 2000, 200)', 
'.': '(255, 255, 255)', 
'0': '(255, 25, 0)', 
'1': '(255, 50, 0)', 
'2': '(255, 75, 0)', 
'3': '(255, 100, 0)', 
'4': '(255, 125, 0)', 
'5': '(255, 255, 0)', 
'6': '(255, 255, 25)', 
'7': '(255, 255, 50)', 
'8': '(255, 255, 75)', 
'9': '(255, 255, 100)', 
':': '(255, 255, 125)', 
';': '(255, 255, 150)', 
'?': '(255, 255, 175)', 
'A': '(255, 255, 200)', 
'B': '(255, 255, 225)', 
'C': '(0, 255, 0)', 
'D': '(0, 255, 25)', 
'E': '(0, 255, 50)', 
'F': '(0, 255, 100)', 
'G': '(0, 255, 125)', 
'H': '(0, 255, 150)', 
'I': '(0, 255, 175)', 
'J': '(0, 255, 200)', 
'K': '(0, 255, 255)', 
'L': '(0, 0, 255)', 
'M': '(0, 50, 255)', 
'N': '(0, 100, 255)', 
'O': '(0, 150, 255)', 
'P': '(0, 200, 255)', 
'Q': '(0, 255, 255)', 
'R': '(255, 0, 25)', 
'S': '(255, 0, 50)', 
'T': '(255, 0, 75)', 
'U': '(255, 0, 100)', 
'V': '(255, 0, 125)', 
'W': '(255, 0, 150)', 
'X': '(255, 0, 200)', 
'Y': '(255, 0, 255)', 
'Z': '(255, 100, 100)', 
'_': '(255, 100, 200)'}
   
while True:
    codeLetter = ""
    for value in letters:
        codeLetter += CODE[value]
        colour_chase(codeLetter, 0.05)
        
    #rainbow_cycle(0)

I tried to run this in different virtual environments and without a virtual environment and I get the same results. I uninstalled pymongo and reinstalled a few times.

0

There are 0 answers