Skip to content

Instantly share code, notes, and snippets.

@Teebusch
Created March 8, 2019 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Teebusch/06c12744856541dccd6f61226aaa0cbb to your computer and use it in GitHub Desktop.
Save Teebusch/06c12744856541dccd6f61226aaa0cbb to your computer and use it in GitHub Desktop.
quick fix and instructions for the ledfloor mqtt clinet
"""
## Usage
- publish to MQTT topic "ledfloorupdates" to change the color(s) of the LED floor
- send a string of the shape "x y r g b" to change the color of an individual pixel x,y to RGB(r,g,b)
- send a string of the shape "r g b" to change the color of all pixes to RGB(r,g,b)
## Starting the Server
- start the Raspberry Pi
- ssh pi@10.90.154.80
- cd telefloor
- source env/bin/activate (to activate pythin virtual environment)
- sudo killall python (to kill other scripts using the floor)
- sudo python mqttclient.py
## ToDo
- The script on the Pi is actually using GRB, not RGB at the moment.
- the script on the Pi is currently not able to set an individual pixel
I changed this here (in `mqttclient.py`), but You have to update the code on the Pi and test it. I probably made a mistake
"""
import paho.mqtt.client as mqtt
from neopixel import *
import argparse
import time
# LED strip configuration:
LED_COUNT = 122 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP = neo.ws.WS2811_STRIP_GRB
# Define functions which animate LEDs in various ways.
def colorWipe(strip, color, wait_ms=50):
"""Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait_ms/1000.0)
def pos_to_led_nr(x, y):
""" convert [x,y] coordinates into corresponding LED number
"""
width = 11
height = 11
flip_axes = False
flip_x = True
flip_y = False
x = x if not flip_axes else y
y = y if not flip_axes else x
xpos = x if not flip_x else (width) - (x+1)
ypos = y if not flip_y else (height) - (y+1)
assert x <= width and y <= height
led_nr = int(ypos * self.height) + int(xpos)
return led_nr
def parse_updates(msg):
lines = msg.splitlines()
print("--- Start of Update ---")
updates = [line.split(' ') for line in lines]
for u in updates:
if len(u) == 5:
try:
u = [int(c) for c in u]
print("setting ({},{}) to RGB({},{},{})".format(*u))
# TODO: quick fix, test and streamline
x = u[0]
y = u[1]
rgb = Color(u[2], u[3], u[4])
led_nr = pos_to_led_nr(x,y)
strip.setPixelColor(led_nr, rgb)
except:
print("Error.")
elif len(u) == 3:
try:
u = [int(c) for c in u]
print("setting floor to RGB({},{},{})".format(*u))
colorWipe(strip, Color(*u))
except:
print("Error.")
else:
print("(Invalid Command)" + str(u))
print("--- End of Update ---")
strip.show()
def on_connect(client, userdata, flags, rc):
print("Connected With Result Code {}".format(rc))
client.subscribe("ledfloorupdates")
def on_disconnect(client, userdata, rc):
print("Disconnected From Broker")
def on_message(client, userdata, message):
msg = message.payload.decode()
parse_updates(msg)
#print(message.topic)
# Set up LED strip
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT,
LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
strip.begin()
# Set up MQTT client
broker_address = "localhost"
broker_port = 1883
client = mqtt.Client()
#Assigning the object attribute to the Callback Function
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.connect(broker_address, broker_port)
client.loop_forever() # Note: is non-blocking
print ('Press Ctrl-C to quit.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment