Search code examples
python-3.xraspberry-pibottle

Bottle: BrokenPipeError: [Errno 32] Broken pipe


I've written a little Python that is supposed to react to some webhooks by doing some stuff with a LED-Strip. I'm running this on a Raspberry Pi running Raspbian using a Xterm window that starts at startup. the program starts and runs fine for a few minutes but then it stops working. I've done some debugging and found that sometimes the program would use 100% on one CPU core, sometimes it spits out the 'BrokenPipeError: [Errno 32] Broken pipe' error and sometimes it just doesn't do anything. I've tried running the sending to LED-strip separately and that worked fine. I've also tried using different Bottle servers but also to no avail.

Here is a traceback I found:

*Private IP* - - [25/Mar/2020:17:21:31 +0200] "GET /setcolor/255/0/0 HTTP/1.1" 200 0 "-" "python-requests/2.23.0"
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/bottle.py", line 868, in _handle
    return route.call(**args)
  File "/home/pi/.local/lib/python3.7/site-packages/bottle.py", line 1748, in wrapper
    rv = callback(*a, **ka)
  File "/home/pi/Documents/Python/LED-Webhooks.py", line 59, in index
    light.mode = magichue.NORMAL
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 357, in mode
    self._set_mode(mode)
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 369, in _set_mode
    receive=self.confirm_receive_on_send
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 114, in _send_with_checksum
    self._send(data)
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 105, in _send
    return self._sock.send(data)
BrokenPipeError: [Errno 32] Broken pipe

Here is the whole program:

import time
import datetime
import magichue
from bottle import route, run

light = magichue.Light('192.168.1.36')


def flash(r, g, b):
    light.mode = magichue.NORMAL
    light.rgb = (0, 0, 0)
    time.sleep(0.5)
    light.rgb = (r, g, b)
    time.sleep(0.4)
    light.rgb = (0, 0, 0)
    time.sleep(0.5)
    light.rgb = (r, g, b)
    time.sleep(0.4)


def fadein(r, g, b, tr=1):
    light.mode = magichue.NORMAL
    while tr <= 25:
        r = r + 10
        g = g + 10
        b = b + 10
        light.rgb = (r, g, b)
        time.sleep(0.5)
        tr = tr + 1
    else:
        light.rgb = (255, 255, 255)


@route('/flashgreen')
def index():
    print("Im here")
    x = datetime.datetime.now()
    if 10 <= x.hour <= 22:
        time.sleep(0.1)
        light.update_status()
        if light.on:
            pr = light.rgb
            flash(0, 255, 0)
            light.rgb = pr
        else:
            flash(0, 255, 0)
            light.on = False


@route('/fadein')
def index():
    print("Im here")
    fadein(0, 0, 0)


@route('/setcolor/<r>/<g>/<b>')
def index(r, g, b):
    print("Im here")
    light.mode = magichue.NORMAL
    light.rgb = (int(r), int(g), int(b))


run(host='0.0.0.0', port=4783, server='paste')

Solution

  • Solved after a lot of debugging, apparently the Magichue library forgets my initial declaration of:

    light = magichue.Light('192.168.1.36')
    

    after some time.