BrokenPipeError using Python on raspberry pi zero

Hello,

I am getting the occasional broken pipe error which is crashing my program.

I searched the community and found one existing topic but it seemed to be caused by widgets accessing data that does not exist. I have confirmed I do not have extra widgets on either my app or desktop gui.

  • Hardware model: Raspberry pi 0w
  • Smartphone: Android
  • Blynk server: Blynk.cloud:443
  • Blynk Library version: 1.0.0

The exact error I get is:

Exception in thread Thread-2:
Sep 20 06:59:00 raspberrypi python3[414]: Traceback (most recent call last):
Sep 20 06:59:00 raspberrypi python3[414]:   File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
Sep 20 06:59:00 raspberrypi python3[414]:     self.run()
Sep 20 06:59:00 raspberrypi python3[414]:   File "/usr/lib/python3.7/threading.py", line 865, in run
Sep 20 06:59:00 raspberrypi python3[414]:     self._target(*self._args, **self._kwargs)
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/gate.py", line 30, in main
Sep 20 06:59:00 raspberrypi python3[414]:     self.process_inputs()
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/gate.py", line 41, in process_inputs
Sep 20 06:59:00 raspberrypi python3[414]:     if self.send_new_position_1hz():
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/gate.py", line 48, in send_new_position_1hz
Sep 20 06:59:00 raspberrypi python3[414]:     self.run_position_fbk_cb()
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/gate.py", line 98, in run_position_fbk_cb
Sep 20 06:59:00 raspberrypi python3[414]:     cb(self.get_position())
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/api.py", line 31, in write_gate_status
Sep 20 06:59:00 raspberrypi python3[414]:     Api.blynk.virtual_write(3, status_in_percent)
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/.env/lib/python3.7/site-packages/BlynkLib.py", line 84, in virtual_write
Sep 20 06:59:00 raspberrypi python3[414]:     self._send(MSG_HW, 'vw', pin, *val)
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/.env/lib/python3.7/site-packages/BlynkLib.py", line 117, in _send
Sep 20 06:59:00 raspberrypi python3[414]:     self._write(msg)
Sep 20 06:59:00 raspberrypi python3[414]:   File "/home/pi/sw/chicken-gate/.env/lib/python3.7/site-packages/BlynkLib.py", line 249, in _write
Sep 20 06:59:00 raspberrypi python3[414]:     self.conn.write(data)
Sep 20 06:59:00 raspberrypi python3[414]:   File "/usr/lib/python3.7/ssl.py", line 930, in write
Sep 20 06:59:00 raspberrypi python3[414]:     return self._sslobj.write(data)
Sep 20 06:59:00 raspberrypi python3[414]: BrokenPipeError: [Errno 32] Broken pipe

The error occurs when I open/close a gate and update the gate position at 1hz. The updates stop once the gate is fully open/closed. This happens twice a day. Other than that, I push an “UpTime” variable so I can see it increment and know that everything is still working. I push this every 5s.

I am particularly confused by the low occurrence rate of this. It will be fine for days, then suddenly crash. As far as I can understand, the rates that I am pushing data are pretty typical. Would this be the expected error if data was pushed too fast? eg if the gate position and uptime were pushed too close together?

I tried catching this error and ignoring it but then my data no longer gets pushed to the cloud. In the below code, api.run() runs the Blynk and BlynkTimer objects.

    while True:
        try:
            api.run()
        except IOError as e:
            if e.errno == errno.EPIPE:
                pass

Has anyone else been through this? Any ideas?

thanks,
Geoff

I’ve never faced this before, but in general, the broken pipe error usually occurs if your request is blocked or takes too long. The connection will be closed, then if the responding side attempts to write to the socket, you will receive a “pipe broken” error.

@John93, thanks for you explanation.
So if I understand this right, the blynk server is most likely blocking my program’s latest data push? Or taking too long to respond? It seems unlikely the server is too slow… so probably somehow an invalid data stream I suppose.

Last night it failed again, after being on only 3 hours and sending “uptime” data after every minute. I don’t know how this simple piece of logic could be failing this way but I’ll try creating something even simpler to see if I can get it to stay alive longer.

thanks,

I’d suggest using the C++ library, and changing the hardware to esp32 or nodemcu instead of the Raspberry Pi Zero.

I’d agree with @John93 that the RPI is a poor choice of Blynk client device. They are expensive, bulky, need a hefty power supply, an SD card (which will fail at some point), an OS on the SD card (which needs frequent updates) and they are slow to boot.

A NodeMCU or ESP32 is a much better choice of client device.

If you’re committed to using the Pi then I’d suggest you install Node-Red and the Blynk plugin for Node-Red. This is an extremely reliable connection method, and allows you to read/write directly to the Pi’s GPIO pins if you install the Pi extensions into Node-Red.

Pete.

1 Like