20. February 2017

How to send command from Python via MQTT to RGB LED connected to ESP8266

I’ve described how to send server load as a number to MQTT in the previous article. The number could be then translated via Node-RED to command for LampESP with RGB LED. The result is simple. LED indicates server load by displaying different colors.

The other option is to deliver color command directly from the server using Python.

Just install paho-mqtt:

pip install paho-mqtt

Here is small snippet of Python code (publish_server_load.py):

#!/usr/bin/env python3

import paho.mqtt.publish as publish
import os

color = 'red'
load = os.getloadavg()[0]
if load < 0.7:
    color = 'black'
elif load < 1.5:
    color = 'blue'
elif load < 3:
    color = 'green'
elif load < 7:
    color = 'orange'

publish.single('/server/monitoring/command', color, hostname='iot.sinusgear.com')

ESP should listen to /server/monitoring. Code of LampESP 0.3 is available at GitHub.

Put this code into crontab

* * * * * /usr/local/bin/publish_server_load.py

If you’re using virtualenv the command should be:

* * * * * /opt/my-python-env3/bin/python /usr/local/bin/publish_server_load.py

24. May 2015

How to revert no-site-packages for virtualenv on Windows with Python 3.4

Python has support for virtual environment. It’s very handy to set up isolated space where you can store specific packages for the project. You can use command virtualenv to create virtual environment.

It’s very common to add option --no-site-packages which will isolate environment from packages installed in system.

Sometimes it is necessary to revert this option.

Linux and Mac users can control it by mechanism based on one file:

venv/lib/python3.4/no-global-site-packages.txt

When this file exists Python is ignoring packages from system.

In case of Windows there is configuration file stored in venv/pyenv.cfg and you can change configuration of global packages:

home = c:\Python34
include-system-site-packages = true
version = 3.4.3

Change the configuration and call Scripts\Activate.ps1 to load Python virtualenv to PowerShell.

23. May 2015

Flask OAuthlib Multiple Scope Values for calling Google API

Google provides myriad of APIs for invoking operations on Google App Platform. It’s possible to integrate this calls with custom app using OAuth.

One option is to write app based on Flask (Python Microframework) with OAuth support provided by Flask-OAuthlib.

There is simple example of web app in Lepture’s repo.

The key practice in OAuth world is to get user’s consent to access API on her/his behalf. Often implemented by simple consent screen.

google-consent

You need to perform two steps to display consent screen:

  • enable API in Developer Console
  • define scope in your application

The second step is straightdorward:

google = oauth.remote_app(
    'google',
    ...
    request_token_params={
        'scope': 'https://www.googleapis.com/auth/userinfo.email'
    },
    ...
)

It works perfectly. The only problem is that this solution provides access just to one API.

The question is: How to request access to multiple scopes?

You can find many hints about OAtuh for other frameworks, that you should separate scopes by comma. That won’t work.

Correct solution is to use white space as delimiter of scopes (as suggested for HTML forms).

google = oauth.remote_app(
    'google',
    ...
    request_token_params={
        'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/tasks'
    },
    ...
)

30. August 2014

SimulANT+ Scripting Interface CHM – Windows help does not display content

I was testing SimulANT+ software from www.thisisant.com. Application was working without problem, but document with API was not readable. Only index was visible, but no content.

The API documentation is stored in file SimilANT+ Scripting Interface.chm. The problem with CHM content is classical gotcha on Windows.

When you download ZIP with CHM then Windows will automatically mark this file as downloaded from Internet. You have to unblock content of file manually.

simulant-chm-unblock

Other alternative is to use 7-zip or other extractor which does not keep this flag.

Note: SimulANT+ has Python API, hooray! 🙂

6. July 2014

SimpleHTTPServer in Python 3.x

One very cool feature of Python 2.x is instant web server. You can type following command and Python will start simple http server.

python -m SimpleHTTPServer

This command does not work in Python 3.x. The reason is that SimpleHTTPServer was merged to http.server module.

Correct command for Python 3.x is:

python -m http.server

28. May 2014

Book review: Visual Studio 2013 Cookbook

Visual Studio 2013 is a huge technological leap forward. The book Visual Studio 2013 Cookbook helps to get on track with latest features including enhanced support for JavaScript and C++.visual-studio-2013-cookbook

Software projects often combine many different technologies like C++, .NET or JavaScript. Professional developer must be aware of all these technologies. The book is a great source of essential information which helps developer to grasp important concepts implemented in Visual Studio.

Cookbook provides short and easy to understand chapters about wide range of features. Each chapter contains some links to web sites or related technologies.

I recommend to read the book one chapter a day. Learn new stuff, experiment with it and next day continue with next chapter.

What you can find inside the book? Here is selection of some interesting topics

Chapter 1: Discovering Visual Studio 2013

  • contains productive tips how to use Visual Studio in effective way (keyboard shortcuts, navigation in project)

Chapter 2: Getting Started with Windows Store Application

  • good starting point for building own application for Windows Store
  • introduction to analysiss of application performance

Chapter 3: Web development

  • learn how to work with Bootstrap
  • minification of JavaScript and CSS

Chapter 4: .NET 4.5.1 Development

  • unit testing of .Net apps
  • managing packages with NuGet

Chapter 5: Debugging .NET Application

  • debugging with IntelliTrace
  • debugging parallel code

Chapter 6: Asynchrony in .NET

  • introduction to popular way of programming applications
  • integration of async concepts with web applications

Chapter 7: Unwrapping C++ Development

  • C++ is still very popular and Visual Studio 2013 introduces several concepts known in .NET or JavaScript
  • Native Unit Test Project for C++
  • graphics programming – 3D, DGSL and diagnostics

Chapter 8: Working with Team Foundation Server

  • getting feedback from users
  • git integration

Chapter 9: Languages

  • highly improved integration with further languages like TypeScript or Python
  • Python support is simply awesome

Visual Studio 2013 Cookbook is available at Packt Publishing store.

22. March 2014

Kivy – buildozer android debug failed with libstdc++.so.6: cannot open shared object file

Kivy is awesome library for developing GUI applications in Python.

It’s possible to build same application for desktop, Android or iOS. It’s something like Cordova/PhoneGap for JavaScript.

I was following Kivy crash course 2: Building an android apk tutorial recorded by Alexander Taylor.

I was trying to build application on Linux Debian for Android:

buildozer android debug

Build failed with quite strange message:

[mergemanifest] Manifest merger disabled. Using project manifest only.
     [echo] Handling aidl files...
     [aidl] Found 1 AIDL files.
     [aidl] Compiling 1 AIDL files.
     [aidl] /home/georgik/.buildozer/android/platform/android-sdk-21/platform-tools/aidl: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

BUILD FAILED

assets/private.mp3: /home/georgik/idea/kivytest/.buildozer/android/app/sitecustomize.pyo
Traceback (most recent call last):
  File "build.py", line 431, in 
    make_package(args)
  File "build.py", line 346, in make_package
    subprocess.check_call([ANT, arg])
  File "/usr/lib/python2.7/subprocess.py", line 540, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['ant', 'debug']' returned non-zero exit status 1

The reason of this problem is not so obvious.

My operating system is 64bit, but Android build system requires 32 bit version of stdc++ library.

Fix is very easy. Just install lib32stdc++6 package 🙂

apt-get install lib32stdc++6

If build is still failing, because of other missing libraries, then you can use online Debian package search to find missing dependencies: https://www.debian.org/distrib/packages

Other missing 32bit libraries are often libz, libncurses5:

apt-get install lib32z1 lib32ncurses5

29. June 2013

True/False gotcha Python vs Ruby vs PowerShell vs NodeJS vs PHP

Evaluation of conditions is very important part programming. True is ture, false is false…

Or not? It might surprise you, but not all languages evaluate numbers, empty strings or empty arrays in the same way.

Let’s examine several technologies. Python, Ruby, PowerShell, NodeJS and PHP.

Here is small code written in Python:

def test(value):
    if value:
        print "True"
    else:
        print "False"

test(0)
test(1)
test("")
test([])
test({})

Here is similar code written in Ruby:

def test(value)
    if value
        print "True"
    else
        print "False"
    end
end

test(0)
test(1)
test("")
test([])
test({})

Let’s compare it to PowerShell:

Function Test($value) {
    if ($value) {
        Write-Host "True"
    } else {
        Write-Host "False"
    }
}

Test(0)
Test(1)
Test("")
Test(@())
Test(@{})

Same logic in JavaScript for NodeJS:

function test(value) {
    if (value) {
        console.log("True");
    } else {
        console.log("False");
    }
}

test(0);
test(1);
test("");
test([]);
test({});

Code in PHP:

<?php 
function test($value) {
    if ($value) {
        echo "True";
    } else {
        echo "False";
    }
}

test(0);
test(1);
test("");
test(array());
test(array());
?>

Results are little bit surprising:

 

Python Ruby PowerShell NodeJS PHP
0 False True False  False False
1 True True True True True
“” False True False False False
[] False True True  True False
{} False True True  True False

You can test code on-line in Cloud9 IDE except PowerShell example 😉

2. June 2013

Configure Nginx with uWSGI to serve Pootle

It’s quite easy to set up Pootle. Just follow the manual – Quickstart.

virtualenv /var/www/pootle/env/
 source /var/www/pootle/env/bin/activate
 (env) $ pip install pootle
 (env) $ pootle init
 (env) $ pootle syncdb --noinput
 (env) $ pootle initdb

You can start Pootle with default CherryPy server. In order to boost performance it is necessary to use more powerfull server.

Let’s take intermediate step and serve Pootle by uWSGI.

First of all you’ll need to create wsgi.py file, because this file is missing in default Pootle installation. File: /var/www/pootle/wsgi.py

import os

# this part is only required when running from checkout instead of an install
try:
    import sys
    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
    sys.path.insert(0, ROOT_DIR) # Top level directory
    import syspath_override
except ImportError:
    # not running from checkout
    pass

# comment the above lines if running from install

os.environ['DJANGO_SETTINGS_MODULE'] = 'pootle.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

You can start uWSGI server by following command:

uwsgi --socket 127.0.0.1:3031 --chdir /var/www/pootle/lib/python2.7/site-packages/pootle --wsgi-file wsgi.py --master --virtualenv /var/www/pootle/ --http ":9090"

This will create uWSGI server and you can test it by connecting to http://localhost:9090

Let’s take further step and configure Nginx to server Pootle. We can modify previous command, because we do not need to server it as http at port 9090:

uwsgi --socket 127.0.0.1:3031 --chdir /var/www/pootle/lib/python2.7/site-packages/pootle --wsgi-file wsgi.py --master --virtualenv /var/www/pootle/

Create webroot directory where you will put static assets, just to avoid information leackage. Store there symlinks to assets and static files.

mkdir /var/www/pootle/webroot
ln -s /var/www/pootle/lib/python2.7/site-packages/pootle/assets /var/www/pootle/webroot/
ln -s /var/www/pootle/lib/python2.7/site-packages/pootle/static /var/www/pootle/webroot/

Store configuration to /etc/nginx/sites-available and create symlink to /etc/nginx/sites-enabled. Configuration of site:

upstream emperor {
  server 127.0.0.1:3031;
}

server {
  listen 80;
  server_name pootle.sinusgear.com;
  root /var/www/pootle/webroot;

  access_log /var/log/nginx/pootle.sinusgear.com_access.log;
  error_log /var/log/nginx/pootle.sinusgear.com_error.log;

  location / {
    location ~* ^.+\.$ {
      root /var/www/pootle/webroot/static/styles;
      expires 30d;
    }
    try_files $uri @django;
  }

  location @django {
    root /var/www/pootle;
    include uwsgi_params;
    uwsgi_param UWSGI_FASTROUTER_KEY $host;
    uwsgi_pass emperor;
  }

  location = /robots.txt {
    root /var/www/pootle/webroot/static/;
    access_log off;
  }

  location ~* ^.+\.py$ {
    return 404;
  }
}

Reload Nginx and Pootle should be much faster 🙂

I recommend book Mastering Nginx. You can learn there much more about Nginx configuration.

26. May 2013

Python virtualenv and PowerShell

It’s quite easy to set up virtual environment with isolated packages for Python in PowerShell.

Creating virtual environment is the same like in any other shell. Let’s call this environment pyenv:

pip install virtualenv
virtualenv pyenv

The last step is to activate this environment in PowerShell (watch dots and spaces):

. .\pyenv\Scripts\activate.ps1

You should see name of your virtual environment in command line. Virtual environment is active.

  • Where’s the fish?

  • Translations

  • Further info

  • Twitter

    Follow @jurajmichalek on twitter.

  • Comments

  • Tags

  • Topics