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):
import paho.mqtt.publish as publish
color = 'red'
load = os.getloadavg()
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
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:
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.
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.
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(
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(
'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/tasks'
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.
Other alternative is to use 7-zip or other extractor which does not keep this flag.
Note: SimulANT+ has Python API, hooray! 🙂
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