Python Gateway for Openelec on Raspberry Pi

I believe Python is not currently supported for Blynk but most of the files with the distribution appear to be working.

My project is Arduino connected via USB to Raspberry Pi running Openelec media player. Openelec is a locked down distribution without many of the required features of a full Linux system. So binaries and java are not available. Python is available and obviously runs on many platforms including Android, iOS, Windows, Linux etc.

running python gateway.py on the Pi does connect to the Blynk server (tried SSL and TCP).

then python blynk_ctrl.py --token=xxxxxxxxxxxxxxxxxxx -dw 2 1 will send digital pin 2 on the Arduino high.

For part of yesterday I was able to set the pins high and low as required but today after the first pin setting the gateway needs to be reset before the pin can be changed again. Each pin change requires a gateway reset.

My code works fine with the Arduino using a Windows gateway (Python or Executable). Sometimes the python gateway on the Raspberry Pi gives [Errno 32] Broken pipe but most of the time it just sits at Press Ctrl+C to quit.

Does anyone know why the Raspberry Pi and the Python gateway is dropping the connection after each Python command whereas the same system on a Windows machine is fine?

[Errno 32] Broken pipe is defined as occurring when one end of the connection tries sending data while the other end has already closed the connection. I’m guessing Blynk server is closing the connection.

I have noticed on the Pi the broken pipe message appears without actually sending a Python command to the Blynk server. It appears to simply be a timeout. When I was sending pin high it worked but by the time I send a pin low the connection has dropped. It isn’t the act of sending the pin high that drops the connection it is around 20 seconds after opening the gateway. So if I send a Python command within the 20 seconds the Arduino will respond but not after the first 20 seconds.

Think I might have blocked access to the Blynk server altogether now as the Pi is failing to connect at all. Maybe too many failed login attempts.

Any ideas?

I’d first try a local server on a regular computer, so as to make sure the Java software has enough resources. Broken Pipe usually indicates something with the network. I’m guessing in this case it’s the heartbeat timeout.

When you run the local server you can adapt the time-out values and set them to your liking. For my Lego trains project I set the timeout way higher than usual (like 1 hour or something). This way you also have a good indication of the quality of your network.

OK I think I can see what is breaking the connection. I have a 20 second timer to read a temperature sensor and when the reading is sent to Blynk it appears the connection is lost. This only happens with the Raspberry Pi though. I will turn off the temperature sensor and see what happens.

Might try a local server as I should get more logs etc. When you say a regular machine you mean Pi with Raspbian rather than Openelec, right?

No, when I say regular machine I mean a real computer, LOL.

It depends a bit on what kind of Pi I think. The latest models should suffice for everything seeing as Blynk has a pretty small footprint. I only have an old Pi, so it gets cranky when I put too much stress on it :wink:

Further testing confirmed it is not the temperature sensor that is breaking the connection to the Blynk server from the Raspberry Pi. It disconnects after maybe 30 seconds so in this time I can send pins high and low on the connected Arduino. After the initial 30 seconds it ALWAYS disconnects.

It is as if the connection was never really made but as the socket is open the python code can be sent from anywhere on the internet to control the Arduino via the Pi.

manusovich is also having problems with Python using iOS and a Pi as described in post 62 at Howto for Raspberry Pi

Just to add the problem is not Openelec related as I am currently using Raspbian and will move on to Openelec if I can get it running on Raspbian.

With Kodi loaded on a Pi running Jessie (not Openelec) our Arduino Nano project has now been running for over 24 hours. During this time Kodi has been streaming live TV and recorded movies and it coped well even during intensive search routines for online movies. I had a few problems running Kodi on the Pi and accessing the Blynk server over WiFi but ethernet is fine. Not sure if the WiFi problem is due to poor WiFi adapters, Kodi on Jessie or if the Pi needs more power to process WiFi than it does for ethernet. Using the latest model B Pi (with 4 USB ports) and I’ll try to get hold of a model 2 Pi (with the extra 512MB RAM) to see if the system runs better on WiFi.
Just need to be able to get the system running with Openelec now as it is our preferred Kodi distribution.

As per the Github Blynk don’t have any immediate plans to look at the Python Gateway for the Raspberry Pi.

We have now managed to get Blynk server access to our Arduino via the USB port connected to a Raspberry Pi running Openelec so no immediate need for the Python Gateway at this stage. I think development of Python will be useful for Blynk users in the long run though.

If anyone needs the set up details for Blynk via Openelec on a Raspberry Pi please let me know.

2 Likes

I’d love to see some detailed explanation so I can try too!

OK a ‘quick’ run down of how to access an Arduino connected via a USB port to a Raspberry Pi, running Openelec media player via the Blynk servers. Built for the Pi but we also added Windows compatibility into the addon. You might need a special app to modify it to work on Android devices. We had an app built some time ago to access Arduino clones with the CH340 USB adapters. No idea if it will work with Apple devices.

We are using Openelec 6.0 on model B+ Pi’s and connected to /dev/ttyUSB0 (but you can change this is the addon settings to COM1 for Windows etc).

  1. Amazingly there is already a ‘socat’ addon available for Kodi. It is available from the SuperRepo repositories. We use the Community Portal / Total Installer to install addons and we just did a search for socat. There are no settings for the socat addon but you should reboot the Pi with the Arduino attached to the Pi before you proceed. socat may show as version 4.3.5 but Openelec should update this when you reboot the Pi to the latest version of 6.0.0. If it doesn’t update automatically update try Available updates in addons menu.

  2. In short you could just issue the following command over SSH:
    socat -d -d FILE:/dev/ttyUSB0,raw,echo=0,clocal=1,cs8,nonblock=1,b9600 TCP:cloud.blynk.cc:8442,nodelay

This is a SINGLE line with a space between 9600 and TCP.

  1. We have created a basic script (not service) addon for Kodi that will appear in Programs, rather than write out all the details of how to set it up. Add our source of http://wanttowatchtv.com/blynk to your file manager and then install the addon from the script.blynk.zip file.

  2. If you check the settings page for the (Programs) addon it should already show /dev/ttyUSB0, 9600 baud and set to run on a Pi. Ignore the Windows Path for installations on a Pi. Click the addon and it should connect you to the Blynk server.

  3. If you want Blynk access each time Openelec starts create or edit an autostart.sh file with nano ~/.config/autostart.sh then paste in the following:

    '#!/bin/sh

    remove initial ’ at the start of this script

    (
    sleep 20;
    kodi-send -a “RunAddon(script.blynk)”
    )&

I wouldn’t reduce the time much below the 20 seconds as you need to be sure your Openelec is connected to the internet etc before the script runs.

  1. If you want to use the addon with Windows change the addon settings, create a directory on your C: drive as C:\blynk and copy com2tcp.exe into that directory. We have included the com2tcp.exe executable with the addon files.

With Openelec running on the ‘$5’ (plus extras) Pi Zero we think it gives a cheap and simple way to access Blynk and you get all that wonderful TV and movies thrown in.

Don’t hesitate to ask if you have any questions.
(off to try and set up the ESP’s that arrived earlier today)

Nice flash forum style but I have noticed several very annoying ‘bugs’.

The guide in my last post is actually numbered 1 to 6 but the forum ‘style’ decides it is 1, 2, 1, 2, 3 and 1.
Also when pasting code if the code starts with #, which all scripts do, it displays incorrectly. Hence my script is shown as starting '# rather than just #.

Do you have anyone ‘fixing the forum’ or is it left as is for now?

To be honest in the current form I would prefer the old style ‘bug free’ forum format.