# Python Output - Blinking Seperate Neopixels

**URL:** <https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477>\
**Category:** General\
**Tags:** lighting, python\
**Created:** [12 March 2023 15:11 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477 "2023-03-12T15:11:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [12 March 2023 15:11 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477/1 "2023-03-12T15:11:09Z")

</div>

problem: how to code Python in Node Red output to blink separate Neopixels

I have the following output to a .py file, which is run by a crontab at certain intervals. Currently, lighting works perfect and all leds are behaving as they should. I would like to set an individual pixel to 'blink' if a condition is met and change the output of the Node Red Python file. I'm thinking I have to 'wrap' each pixel in some kind of loop command. Ideas?

```auto
pixels[25]=(0,255,0)
pixels.show()

pixels[29]=(0,0,255)
pixels.show()

```

I would like pixel[29] to blink, while pixel[25] stays solid.

\<

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 March 2023 20:31 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477/2 "2023-03-12T20:31:10Z")

</div>

Are you blinking the LED with python(3) or Node-Red?

If it is python(3) it shouldn't be hard.

Looking at the lines I can't remember if that is right.  
I have a machine with NeoPixels but it is kind of out of order just now. ☹ I can't power it up to look at my code.

How many LED's on the strip?

I can post a SIMILAR script I have for LEDs. But they are NOT NeoPixels. They are a similar family and are like NeoPixels.

I ca post THAT one if you want.  
It - the script - MAY give you an **idea** of what to do.

---

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [12 March 2023 22:28 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477/3 "2023-03-12T22:28:40Z")

</div>

Thanks for reply. I am using Node Red to build the Python file, which will blink the LEDS. Any sample code may help. Total number of LEDS, about 25. Of which, any number could/may be called to blink, or not.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 March 2023 22:32 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477/4 "2023-03-12T22:32:26Z")

</div>

Well if there are 25 LEDs the command `pixels[29]=(0,0,255)` is going to be problematic.

Hang on I'm knee keep in problems just now.

I'll post the code ASAP.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 March 2023 22:42 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477/5 "2023-03-12T22:42:32Z")

</div>

The code:

(Other people helped me)

```auto
#

import time
import sys
import paho.mqtt.client as mqtt
import blinkt

#blinkt.set_clear_on_exit()

def on_connect(client, userdata, flags, rc):
    print("Connected:", rc)
    client.subscribe("TELEPI_LOCAL/tele_led/#", 0)

def on_subscribe(client, userdata, mid, granted_qos):
    print ('Subscribed:', userdata, mid, granted_qos)
    print ('We are here, waiting for commands...')

def on_message(client, userdata, msg):

    global abort
    global blinkt
    print ("===============================")
    print ("New loop invocation")
    my_string = (msg.payload.decode("utf-8"))
    #my_string = (message.payload.decode("utf-8"))
    print ("my_String")
    print (my_string)
    dummy = my_string.split(",")
    print ("Dummy ",dummy)

# [int(x) & 0xff for x in data]
    command = dummy[0]
    print ("Command ",command)

    if command == 'Heartbeat':
        print ("I am alive")
        client.publish("TELEPI_LOCAL/tele_led/led-response", "Script is alive")

    if command == 'Stop':
        print ("Script terminates..")
        client.publish("TELEPI_LOCAL/tele_led/led-response", "Script is stopping")
        blinkt.set_all(0, 0, 0)
        blinkt.show()
        abort = True

    print (len(dummy))

#
    if command == 'rgb' and len(dummy) == 5:
        pixel = dummy[1]
        red = dummy[2]
        green = dummy[3]
        blue = dummy[4]

        pixel = int(pixel)
        if pixel > 8: pixel = 0
        red = int(red)
        green = int(green)
        blue = int(blue)

        print("--== values ==--")
        print ("Command >",command,"<")
        print ("LED ", pixel)
        print ("Red ", red)
        print ("Green ",green)
        print ("Blue ",blue)

        print ("Data length ",len(dummy))

        if pixel == 8:
            print ("Oops")
            blinkt.set_all(red,green,blue)
            blinkt.show()

# for x in range(blinkt.NUM_PIXELS):
# blinkt.set_pixel(pixel,red,greed,blue)
        else:
            print ("setting pixel")
            print ("Led ",pixel," Red ",red," Green ",green," Blue ",blue)
            blinkt.set_pixel(pixel,red,green,blue)
            print("Done")
            blinkt.show()
        return
#
# if command == "dim":

# inits
#mqtt_broker = "192.168.0.99"
mqtt_broker = "127.0.0.1"
mqtt_port = 1883
abort = False
#client = mqtt.Mosquitto()
client = mqtt.Client("LED blink routine")
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
resp = client.connect(mqtt_broker, mqtt_port, 60)
client.loop_start()

while abort == False:
    time.sleep(1)

client.loop_stop()
client.disconnect()
del client
print ('Script has terminated')
exit(0)

```

Let me know if you need help on it.

# SORRY, I posted the wrong code.

Changed.

# Look again!

---

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [13 March 2023 12:08 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477/6 "2023-03-13T12:08:01Z")

</div>

Thanks, I'll give it a look over.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 May 2023 12:08 UTC](https://discourse.nodered.org/t/python-output-blinking-seperate-neopixels/76477/7 "2023-05-12T12:08:14Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
