Homepage Wiki Forum Buy

API Python

From GNUBLIN

Contents


We have now implemented our API in Python. In this article we show how to use the API in Python on the Gnublin and on the Raspberry Pi

The new documentation of the API can be found at http://gnublin.org/gnublin-api/doc/html_en/index.html.


With Gnublin

Preparations

Before you can use the API in Python, we invite us to the following two files:

user@dev-pc:~$ wget https://raw.github.com/embeddedprojects/gnublin-api/master/python-module/gnublin/gnublin.py
user@dev-pc:~$ wget https://github.com/embeddedprojects/gnublin-api/raw/master/python-module/gnublin/_gnublin.so

The two files then we push on our Gnublin.

Use

On the one Gnublin now changes to the folder where the two are attach files and starts the Python console with the call:

root@gnublin:~# python

Next we import the Python module:

>>> import gnublin

Now we want to control our GPIOs as an example in python, we must first create a GPIO object:

>> gpio = gnublin.gnublin_gpio()

If the object is created, how can you access from the C + + API to the usual methods of the class:

LED on / off:

>>> gpio.pinMode(3,'out')
1
>>> gpio.digitalWrite(3,1)
1
>>> gpio.digitalWrite(3,0)
1
>>>

Example blinking LED

This program allows the LED flash on the Gmublin. We create a file called LED blink.py and write the following code to the file:

  1. import gnublin
  2. import time
  3. gpio = gnublin.gnublin_gpio()
  4. gpio.pinMode(3,'out')
  5.  
  6. while True:
  7. 	gpio.digitalWrite(3,1)
  8. 	time.sleep(0.5)
  9. 	gpio.digitalWrite(3,0)
  10. 	time.sleep(0.5)

Execute the program:

root@gnublin:~# python LED-blink.py

To cancel press:

STRG + C

With RaspberryPi

Preperation

The RaspberryPi must be started and have a connection to the Internet. To use the API with Python, we need a few programs. We install it with the following command:

pi@raspberrypi ~ $ sudo apt-get install git swig2.0 python-dev

Get the repositroy:

pi@raspberrypi ~ $ git clone https://github.com/embeddedprojects/gnublin-api.git

Change directory:

pi@raspberrypi ~ $ cd gnublin-api

Before you can compile the API, you must specify that you want the API to run on a RaspbberryPi. These changes are in the file API-config.mk two lines from:

Following line must be uncommented: 2, 12, 17 For this, the # must be removed at the beginning of the following lines: 6, 14, 18 The API-config.mk file looks as follows:

  1. #Crosscompiler for Gnublin
  2. #CXX := arm-linux-gnueabi-g++
  3. #Crosscompiler for Raspberry Pi:
  4. #CXX := arm-linux-gnueabihf-g++
  5. #Compiler for onboard compilation:
  6. CXX := g++
  7.  
  8. #Compilerflags:
  9. CXXFLAGS = -Wall
  10.  
  11. #Architecture for gnublin:
  12. #Architecture = armel
  13. #Architecture for raspberryPi:
  14. Architecture = armhf
  15.  
  16. #Define which Board you want:
  17. #BOARD := GNUBLIN
  18. BOARD := RASPBERRY_PI
  19.  
  20. #DO NOT EDIT BEYOND THIS LINE!
  21. BOARDDEF := -DBOARD=$(BOARD)

Now you can translate the API and create Python modules:

pi@raspberrypi ~ $ make python-module

Ingnore warnings:

Warning 314: 'print' is a python keyword, renaming to '_print'
Warning 314: 'print' is a python keyword, renaming to '_print'
Warning 314: 'print' is a python keyword, renaming to '_print'

Load these drivers before

pi@raspberrypi ~ $ sudo modprobe spi-bcm2708
pi@raspberrypi ~ $ sudo modprobe i2c-bcm2708
pi@raspberrypi ~ $ sudo modprobe i2c-dev

or put them into /etc/modules

spi-bcm2708
i2c-bcm2708
i2c-dev

per row a module

Verwendung

On the RaspberryPi you start the python console with the call:

root@gnublin:~# sudo python

sudo is necessary so that we can access the device files as i2c, gpio, etc.

next step is to import the python modul:

>>> import gnublin

We now want to control the temperature sensor as an example in python, we must first create an object:

>> lm75 = gnublin.gnublin_module_lm75()

If the object is created, how can you access from the C + + API to the usual methods of the class:

Example Temperature:

>>> lm75.getTemp()
23
>>> lm75.getTempFloat()
23.125
>>>

Own Python programs so we can call:

pi@raspberrypi ~ $ python mytool.py
In other languages