0

This is my first trial run using a Raspberry Pi Zero W to run RGB LEDs following this tutorial: https://dordnung.de/raspberrypi-ledstrip/

I've completed the tutorial, have everything wired up, and running the following python3 code from the command line gives expected results and the LEDs color is consistent.

from gpiozero import PWMLED
from time import sleep
import sys

ri = int(sys.argv[1])
gi = int(sys.argv[2])
bi = int(sys.argv[3])

rl = PWMLED(17)
gl = PWMLED(27)
bl = PWMLED(22)

def rgb_toPWM (r,g,b):
    rl.value = r / 255
    gl.value = g / 255
    bl.value = b / 255


rgb_toPWM(ri,gi,bi)
sleep(3)

When I wrap it in flask the LEDs flicker and the colors don't match what I give in the code above.

from flask import Flask, render_template, request
from gpiozero import PWMLED
from time import sleep
import json

rl = PWMLED(17)
gl = PWMLED(27)
bl = PWMLED(22)

def rgb_toPWM (r,g,b):
   rl.value = 0
   gl.value = 0
   bl.value = 0
   rl.value = r / 255
   gl.value = g / 255
   bl.value = b / 255

app = Flask(__name__)

@app.route('/')
def index():
   redValue = 20
   greenValue = 100
   blueValue = 200
   templateData = {
      'title' : 'Pi Zero 00',
      'red_v': redValue,
      'green_v': greenValue,
      'blue_v': blueValue
      }
   return render_template('index.html', **templateData)

@app.route('/changecolor', methods=['POST'])
def changeColor():
   red = int(request.form['red'])
   green = int(request.form['green'])
   blue = int(request.form['blue'])
   rgb_toPWM (red, green, blue)
   data = {}
   data['red_3'] = str(red)
   data['green_3'] = str(green)
   data['blue_3'] = str(blue)
   data['red_digit'] = str(red / 255)
   data['green_digit'] = str(green / 255)
   data['blue_digit'] = str(blue / 255)
   json_data = json.dumps(data)
   sleep(3)
   return json_data

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)

I assume this has something to do with flask, but I really have no clue. Here's the output of the above

blue_3: "114" blue_digit: "0.4470588235294118"
green_3: "128" green_digit: "0.5019607843137255"  
red_3: "250" red_digit: "0.9803921568627451"

I put those same values in the top code and it looks like the color I'd expect, but from the flask code it flickers and "spurts"

OS VERSION="9 (stretch)"

Kernal Linux 4.9.59+ #1047 Sun Oct 29 11:47:10 GMT 2017 armv6l GNU/Linux

Addition: I've started just moving one pin at a time and the PWM just doesn't seem consistent, regardless of the value I give it.

0

I was able to get it working using PiGPIOFactory pointing to localhost.

changing

rl00 = PWMLED(17)
gl00 = PWMLED(27)
bl00 = PWMLED(22)

to and adding

fac_loc = PiGPIOFactory(host='127.0.0.1')

rl00 = PWMLED(17, pin_factory=fac_loc)
gl00 = PWMLED(27, pin_factory=fac_loc)
bl00 = PWMLED(22, pin_factory=fac_loc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.