Bluedot – Bluetooth Raspberry Pi control

This is something I’ve really been waiting for … a link between a Raspberry Pi and a mobile phone app.

Bluedot consists of two parts – an App and some Python code which runs on the Raspberry Pi. I used a Raspberry Pi Zero W, which is a handy combination of small size and built-in WiFi and Bluetooth.

Setting up Bluedot is easy – just follow the instructions and download the App from the Google App store.

IMG_20170630_094649037_HDR[1]

Bluetooth controlled belt

I’ve been using Bluedot to demonstrate some work for e-Textiles at school. It’s my intention to combine a small Raspberry Pi and power pack into something that can provide a range of patterns and colours controlled from a smart phone. Bluedot has enabled this to happen easily.

Now, here’s where I show my ignorance of Python terminology and correct use of syntax.

As I see it, Bluedot’s documentation points to the library receiving the events and then calling functions depending on what’s been pressed on the smart-phone’s screen.

I wanted to see whether it was possible to display patterns in a background loop and then change these. When I’m doing this with a microcontroller such as the Picaxe, I’d have a main loop reading a variable, in which I had stored the current “state”.  The state would be used to trigger different patterns of outputs. Some where in the main loop I would poll the inputs to see if there’s a change – if there is, I’d change the state variable to a new value. I wasn’t sure if this is the right thing to do within Python, but it has worked for me for at least two hours.

PiZeroPixelController

PiZero controller in 3d printed case.

My demonstration “belt”, which is currently adorning a mannekin, has 34 NeoPixel (WS2812) RGB LEDs connected to a long servo cable. The Pi Zero has a matching connector soldered to the relevant connections and is currently running from a PoundLand USB battery. My program allows the belt colour to be changed to Red, Green, Blue, Purple and random colour sparkles. I’ve used the Pimoroni Unicorn Hat python module to drive the LEDs although I did modify it to allow me to access individual LEDs along the line (Set Single Pixels) rather than relying on figuring out an X/Y matrix.

Overall, I’m pleased. I’m looking forward to some feedback regarding my approach to polling Bluedot rather than relying on events triggering functions. I’m sure there’s a better way but this sort-of suits my understanding as I sit on the fence between different programming languages.

Oh, and by the way, the Glowing Pendant is a ring of LEDs but controlled independently from a Redfern Electronics Crumble Controller.


## Bluetooth control of sparkles ##
from bluedot import BlueDot
from signal import pause
from random import randint
import time
import unicornhat as unicorn

ledno = 34
dim = 150
mode = 0
print("Bluedot Sparkles")

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(1)
width,height=unicorn.get_shape()

def scroll():
    for led in range(ledno):
        unicorn.setsingle(led,0,0,255)
        unicorn.show()
        time.sleep(0.05)

def allred():
    led = randint(0,ledno)
    red = 255
    unicorn.setsingle(led,red,0,0)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,dim,0,0)
    unicorn.show()
    time.sleep(0.1)

def allgreen():
    led = randint(0,ledno)
    green = 255
    unicorn.setsingle(led,0,green,0)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,0,dim,0)
    unicorn.show()
    time.sleep(0.1)

def allblue():
    led = randint(0,ledno)
    blue = 255
    unicorn.setsingle(led,0,0,blue)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,0,0,dim)
    unicorn.show()
    time.sleep(0.1)

def allpurple():
    led = randint(0,ledno)
    red = 255
    blue = 255
    unicorn.setsingle(led,red,0,blue)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,dim,0,dim)
    unicorn.show()
    time.sleep(0.1)

def alloff():
    for led in range(ledno):
        unicorn.set_pixel(led,0,0,0,0)
        unicorn.set_pixel(led,1,0,0,0)
    unicorn.show()

def randomsparkle():
    x = randint(0, (width-1))
    y = randint(0, (height-1))
    r = randint(0,2)*127
    g = randint(0,2)*127
    b = randint(0,2)*127
    unicorn.set_pixel(x, y, r, g, b)
    unicorn.show()
    time.sleep(0.1)

def dpad(pos):
    global mode
    if pos.top:
        alloff()
        print("up")
        mode = 1
    elif pos.bottom:
        alloff()
        print("down")
        allgreen()
        mode = 2

    elif pos.left:
        alloff()
        print("left")
        mode = 3

    elif pos.right:
        alloff()
        print("right")
        mode = 4

    elif pos.middle:
        alloff()
        mode = 5
        print("fire")

print("Starting bluedot")

scroll()
#alloff()
#scroll()
#allred()
#time.sleep(1)
#allblue()

bd = BlueDot()

while True:
    bd.when_pressed = dpad

#    print(mode)

    if mode == 5:
        randomsparkle()
    if mode == 1:
        allred()
    if mode == 2:
        allblue()
    if mode == 3:
        allgreen()
    if mode == 4:
        allpurple()

pause()

Advertisement