ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

raspberrypi python module

Fri Sep 14, 2012 9:19 pm

Hi,
This is a pre-release of my raspberrypi python module that allows easy access to some information regarding your RPi. When desining it I was thinking about AmigaOS module available on AmigaOS 4.x platform and decided that RPi needs one dedicated module too.

Currently it targets Raspbian wheezy with 2.x Python installed.

Unpack the attached archive:

Code: Select all

 bzip2 -cd ./raspberrypi-python.tar.bz2 | tar xvf -
and start testing it. Inside the directory there are two example files demonstrating how to use this module.

Here is a peek into one of samples:

Code: Select all

import sys

try:
	import raspberrypi
except ImportError:
	print 'You need to install raspberrypi module first to run this sample code.'
	sys.exit(1)

print 'CPU      :', raspberrypi.cpu
print 'Hardawre :', raspberrypi.hardware
print 'Revision :', raspberrypi.hw_rev
print 'Serial   :', raspberrypi.hw_serial
print 'MAC addr :', raspberrypi.MAC
print 'GPIO     :', raspberrypi.isgpio
print 'WiringPi :', raspberrypi.iswiringpi
As you see you can get information about your RPi hardware and also about two extremely useful modules for any RPi die hard fan: gpio and wiringpi.

Additionally the archive contains one C source file that can help get you started with setting up your Python environment if you are new to it. Compile it like this:

Code: Select all

gcc -o rpi-install-python.exe ./rpi-install-python.c
I know than shellscript would be better but it's been ages since I've written anything in C and I wanted to try out gcc directly on RPi ;)

Please let me know if anyone finds it useful or buggy.

rickseiden
Posts: 420
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: raspberrypi python module

Sat Sep 15, 2012 4:20 pm

Looks interesting, and probably should be added to the standard Raspbian build. But I'm curious what kind of information is returned. Can you give us some sample output?

Let's be honest, I'm asking this because I'm too lazy to do it myself. We all know that. I'm just saying it.
There are 10 types of people in this world. Those that understand binary, and those that don't.

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sat Sep 15, 2012 6:08 pm

Thanks!
Here is a sample output from test-rpi example:

Code: Select all

pi@raspberrypi ~/src/python/raspberrypi $ python ./test-rpi.py
CPU      : ARMv6-compatible processor rev 7 (v6l)
Hardawre : BCM2708
Revision : 0002
Serial   : 00000000d4cc9433
MAC addr : b8:27:eb:cc:94:33
GPIO     : True
WiringPi : True

 -- raspberrypi module information --
Module version : 0.1
Errors         : 1
Last error code: 5
5: GPIO check may require root privileges.
Any ideas what else should I include and what API should be made available are more than welcome.

rickseiden
Posts: 420
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: raspberrypi python module

Sat Sep 15, 2012 7:54 pm

How about IP address? I see you have MAC in there. What about processor usage? I'm thinking of displays that might come in handy.
There are 10 types of people in this world. Those that understand binary, and those that don't.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am

Re: raspberrypi python module

Sat Sep 15, 2012 8:06 pm

IMHO having MAC and IP address like there is *the* MAC and *the* IP address is a bit odd. My Raspi has four IP addresses and two MACs at the moment — which would/should the module return and why?

Code: Select all

while not self.asleep():
    sheep += 1

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sat Sep 15, 2012 9:47 pm

About the net interfaces and their addressing: the example above with multiple IP addresses and fewer MACs is a good one.

I'll add IP address information and it will be returned in a tuple: IP, MAC to solve partially the above problem but the user will be required to provide interface name (like is is currently implemented in raspberrypi.get_eth_mac_addr function).

CPU usage? Yup, this might be handy too but since under Linux you get more generic ways of getting it I wounder how useful this feature would really be. Anyway thanks for the idea - this is something I will add eventually. My goal is to have most of RPi dedicated stuff available through that module. For example right now I'm thinking about adding API to detect memory split between ARM and GPU.

Any further ideas are more than welcome. I'd also like to hear from other users if they were able to run the module on their RPis.

eNORm
Posts: 21
Joined: Tue May 29, 2012 3:04 pm
Location: Norway, Oslo

Re: raspberrypi python module

Sun Sep 16, 2012 12:20 am

From the overclocking thread, I've read that it's possible to read out temperature and voltage. That could be handy.
Expology.no

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sun Sep 16, 2012 10:11 am

Hi,
This is another great idea for further development. Thanks :)

Boezelman
Posts: 40
Joined: Sun Aug 05, 2012 3:25 pm

Re: raspberrypi python module

Sun Sep 16, 2012 1:15 pm

Nice!

Will try to make mij LCD show this info :-)

Thank you!

To add IP adres add this to the raspberypi.py or test_rpi.pi

Code: Select all

from subprocess import * 

cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

ipaddr = run_cmd(cmd)

print 'IP       :',  ipaddr

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sun Sep 16, 2012 5:23 pm

Boezelman wrote:Nice!

Will try to make mij LCD show this info :-)

Thank you!

To add IP adres add this to the raspberypi.py or test_rpi.pi

Code: Select all

from subprocess import * 

cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

ipaddr = run_cmd(cmd)

print 'IP       :',  ipaddr
You're welcome!

Let us know how it works with LCD.

As for IP address please stay tuned: I should be releasing upgraded version soon (possibly even today) but I use quite different approach based on socket API. Obviously there are more than one way to achieve this goal but I like sockets more since it does not require awk and calling external programs.

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sun Sep 16, 2012 10:05 pm

Ok, for all the supporters here is a new version providing IP address information and some additional information about RPi settings based on your config.txt. Keep in mind that if you edit config.txt before the reboot, currently raspberrypi module will provide you with incorrect details regarding your ARM and GPU settings.

Additionally you still will not get tuple: IP,MAC instead you will get IP and MAC addresses for the single interface in separate variables for now.

Also there is a new nice utility whichpi:

Code: Select all

pi@raspberrypi ~/src/python/raspberrypi $ ./whichpi.py
CPU: ARMv6-compatible processor rev 7 (v6l) BCM2708 @ 800 Mhz
GPU @ 250 Mhz SDRAM @ 400 Mhz
Rev: 0002 Serial: 00000000d4cc9433
Here is the output from test-rpi.py to demonstrate some changes:

Code: Select all

CPU      : ARMv6-compatible processor rev 7 (v6l)
Hardawre : BCM2708
Revision : 0002
Serial   : 00000000d4cc9433
MAC addr : b8:27:eb:cc:94:33 IP addr : 10.1.1.74
GPIO     : True
WiringPi : True

 -- raspberrypi module information --
Module version : 0.1
Errors         : 1
Last error code: 5
5: GPIO check may require root privileges.
test-eth.py has been updated too to reflect changes.

Have fun!
Attachments
raspberrypi-python.tar.bz2
(5.55 KiB) Downloaded 179 times

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK

Re: raspberrypi python module

Sun Sep 23, 2012 1:04 pm

Hi - great stuff but what are isGPIO and isWiringPi testing for?

I could guess wiringpi but don't all RPi have a GPIO??? :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK

Re: raspberrypi python module

Sun Sep 23, 2012 1:07 pm

Also - and just my opinion but maybe module name "raspberrypi" should be one for the foundation to use :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sun Sep 23, 2012 1:27 pm

simplesi wrote:Hi - great stuff but what are isGPIO and isWiringPi testing for?

I could guess wiringpi but don't all RPi have a GPIO??? :)

Simon
The port itself is so far on all REVs so you are right - but the python module for GPIO access is not. The isGPIO is returning the success (or failure) of import RPi.GPIO. Please note that since RPi module might require root privileges not always the import succeeds but my module handles this situation correctly.

The isWiringPi is basically the same expect it tries to import WirtinPi python module.

Hope this clarifies all.

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sun Sep 23, 2012 1:30 pm

simplesi wrote:Also - and just my opinion but maybe module name "raspberrypi" should be one for the foundation to use :)

Simon
Don't know but I'll be more than happy to donate the code to the foundation and this is an opensource project. I just need to decide which license scheme use but I figured that I'll present my code as prerelease for testing, before turning it into first true release.

As I've written in my first post on this topic: the name was based on other modules provided strictly for certain platforms.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK

Re: raspberrypi python module

Sun Sep 23, 2012 7:45 pm

Don't know but I'll be more than happy to donate the code to the foundation
No - I mean we should leave the name "raspberrypi" alone and not use it in our projects :)

e.g Raspbian don't called their OS RaspberryPI OS even though its the default image presented to everyone to use :)

RaspberryPi meetups are called RaspberryJams etc :)

It just keeps the name "RaspberryPi" special :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Sun Sep 23, 2012 10:25 pm

simplesi wrote: No - I mean we should leave the name "raspberrypi" alone and not use it in our projects :)
Can you point me towards any rule or guideline that has clear statement on this? Obviously I don't want to interfer with a rule or standard but on the other hand there should be a logic behind defining names. It would be hard to call another Linux distro Raspberry PI OS IMHO however since my module is targeting Raspberry Pi strictly I don't see anything wrong with current name.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK

Re: raspberrypi python module

Sun Sep 23, 2012 10:40 pm

There's no rules :)

Apart from obviously any trademarks the foundation owns :)

Most people call their stuff "raspberrypi widget" or "raspwidget" or "Rapsbian" or Pi-Face, RaspPiBot etc etc :)

Just so that if the phrase "raspberrypi" is used on its own - it means a Raspberry Pi :)

e.g The 2 python GPIO libs are called RPi.GPIO and WiringPi :)

Its entirely up to you what you call your module but maybe better to give it a more descriptive (and fun maybe?) name than just raspberrypi :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Mon Sep 24, 2012 11:19 am

simplesi wrote:There's no rules :)

Apart from obviously any trademarks the foundation owns :)

Most people call their stuff "raspberrypi widget" or "raspwidget" or "Rapsbian" or Pi-Face, RaspPiBot etc etc :)
Simon,
Thanks for clarification. Going back to code since I'd love to release new version today.

Boezelman
Posts: 40
Joined: Sun Aug 05, 2012 3:25 pm

Re: raspberrypi python module

Tue Sep 25, 2012 6:28 pm

So... any updates? :-)
It's easy to show the code on my lcd, just need a bigger one now :P

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Tue Sep 25, 2012 9:07 pm

Just give me a while ;)

I'm fixing overlooked overclocking bit blow off case and I want my module to operate on older (read all) Raspbian and firmwares where temperature measurement with vcgencmd for example is not available. Thankfully vcgencmd provides commands command to easily solve this issue.

Boezelman
Posts: 40
Joined: Sun Aug 05, 2012 3:25 pm

Re: raspberrypi python module

Wed Oct 10, 2012 6:15 pm

So how is it going? :D

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: raspberrypi python module

Wed Oct 10, 2012 6:35 pm

I'd like a function to return details on what the current screen/framebuffer resolution/framerate is, and what memory split. I know I could do it myself by reading config.txt, just thought it'd be a good fit for this module...
note: I may or may not know what I'm talking about...

ACP_
Posts: 33
Joined: Sat Aug 18, 2012 10:40 pm

Re: raspberrypi python module

Thu Oct 11, 2012 8:46 am

Guys,
Thanks for the feedback. I've been extremly busy last 2 weeks and didn't have time to code on RPi unfortunately. I should be releasing new version during this weekend hopefully.
All the best

Return to “Python”