When you hear the term ‘Raspberry Pi’, it sounds like some kind of fruit or at least some module of some random programming language. But no, I am here to prove you wrong. So lets see what this Pi is exactly and how is our favorite python involved in all of this.
What is Raspberry Pi?
Raspberry Pi is a Credit card sized single board Microcomputer. This is what a Raspberry Pi looks like:
The Raspberry Pi is based on Broadcom BCM2835 Chip. The original chip is an ARM based Chip of 700 MHz processor and VideoCore IV GPU with a 256 MB of RAM. But this was few years back. Later B and B+ versions were released which were of 512 MB of RAM. And the latest Model Raspberry PI 2 is a 900 Mhz quad-core ARM Cortex-A7 processor, 1 GB of RAM, 4 USB ports, 40 GPIO pins, Full HDMI port, 1 Ethernet port, Combined 3.5mm audio jack and composite video, Camera interface (CSI), Display interface (DSI), Micro SD card slot supporting upto 64 GB, and a VideoCore IV 3D graphics core. Sounds Interesting? Then let me tell you something more.
Raspberry Pi is actually a Pocket sized computer almost equal to the size of a deck of cards; and since it has a ARM Cortex-A7 processor, it can run a full range of ARM GNU/Linux distributions, including Snappy Ubuntu Core, as well as Microsoft Windows 10.
So now enough with the introduction part, let’s see what is the real world application of this Micro Computer.
Real World Usage
Following are some of the most awesome projects I have seen till date. Just before I proceed with how python is useful with Raspberry, lets have a look at these projects (I will only be providing with the project names. Kindly search the whole project on github or Google for more information):
1. Making a Linux Based Tablet:
Yes, you read it right. You can make a tablet out of you r simple RaspPi. All you would need is to solder a simple 10” Touch Screen, A Portable 10000 mAh charger, A Camera module and a Wi-Fi Adapter and an IR Sensor. You probably may want to build a case as well which I suggest you make out of Balsa Wood. Reason being it is strong, lightweight and easily replaceable. There are a lot of people who have already built this amazing machine. Make sure to check those out.
2. Network-Hijacking Pi:
Have you seen the famous T.V. series Mr. Robot? If you haven’t, then you surely must check it out. It is insanely amazing. If you a Computer geek, this series is an exact replica of what Hacktivists do in real life. But I am not here to promote any Series. So, coming back to our topic, if you have seen the series, then you know what I am talking about.
Besides, what this is exactly is the Pi is installed with an ARM version of Kali Linux and an Adapter is attached to it. The one that works perfectly well is TP-LINK wn722n. After installing just install a VNC in the RaspPi and then leave it any secluded area in any company premises. BOOM!! Connect to the VNC using your system at home and now you can easily crack WPA/ WPA2 passwords and get into the network.
3. Building a Rasp-Pi QuadCopter:
Now this something not everyone would want to build. The reason being RaspPi needs a 5V battery. And if you are from a quadcopter background, then you probably know that we need to make the Quad as lightweight as possible. So until unless you are an expert in electronics, probably this is not something you would want to do. But just think of the possibilities of the things you can do, if you could create a Quad with RaspPi.
4. Portable RaspStation Pi:
Wait. Wha??? What does this mean? This should have been Portable Playstation or PSP. Nope, you read it exactly right. People with the help of a bit of coding have learned to make a portable Gameboy out of pi. I read this article somewhere on the github last week. This is something I think you should check it out.
5. Pi-rate Radio:
Pi-rate Radio is not just your everyday radio. You can make you own radio within a few spans of hours and start experimenting with the frequencies that it can change to. This is actually very cool, except that if you get into the wrong frequency, like that of governments, then it could actually land you into trouble.
Now, my dear friends, I think you must have had an Idea of what RaspPi is capable of; Almost everything that your normal Cell phone or Computer can do, and also those things which they cant.
Now lets take a step back and see how python is related to all of this.
A Mix of Python
When you purchase the whole RaspPi Kit, it comes with an 8 gigabyte of Class 10 sdcard. This card comes with a pre-installed operating system called as NOOBS. NOOBS is based on debian, and it already contains Python 2 as well as 3. But due to some reason, if you find that you missing any specific version of python, then you can simply install it by typing the following in the command line:
>>>sudo apt-get install python
But before building your python, you need to make sure you have a few things setup. In order for the HTTPS/SSL functionality of python to work correctly, we need libssl-dev. tk-dev also is required for Tk GUI development, and libsqlite3-dev will add Sqlite3, embedded SQL database, support. So, let configure and install all of these before you get started with python:
>>>sudo apt-get update
>>>sudo apt-get upgrade -y
>>>sudo apt-get install build-essential libncursesw5-dev libgdbm-dev libc6-dev
>>>sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
>>>sudo apt-get install libssl-dev openssl
Optionally, you can overwrite the included Python 3 version (3.2.3 on my distribution) by updating the symlink.
>>> sudo ln -sf /usr/local/bin/python3.4 /usr/local/bin/python3
An alternative to python is ipython. IPython is an interactive Python shell with syntax highlighting, auto completion, pretty printing, built-in documentation and more. IPython is not installed by default. You can simply install it by typing:
>>>sudo apt-get install ipython #or ipython3 if you want
Then running ipython in the terminal will get you started. You can also install python libraries for RaspPi by using the following commands:
>>>sudo apt-get update
>>>sudo apt-get install python-picamera
This is a preferable method of installing apps, libraries and modules as it means that the modules you install will be kept up to date easily with the usual sudo apt-get update and sudo apt-get upgrade commands.
But these are only libraries, which are present in the Raspbian archives. In order to install other archives, which are not present over there, you can use PIP to install them. Just type the following in order to install whichever library you want to install:
>>>sudo apt-get install python-pip
>>>sudo pip install simplejson
And in order to install the pi Umbrella libraries and GPIO software, you can use the following:
>>>sudo apt-get install python3-rpi.gpio
>>>sudo pip install pibrella ##to install pibrella
for python 2
>>>sudo pip3.2 install pibrella ##to install pibrella
for python 3
Using Python on the Raspberry Pi opens up the opportunity to connect to the real world through the Pi’s GPIO pins. This can be done with the RPi GPIO library. It comes preinstalled on recent Raspbian images.
To control the GPIO pins, you would need the root access in Python. So, you would need to run sudo python, ipython, sudo idle &.
In your Python script (or in the REPL), import the GPIO module, set the board mode to that of your preference, set up the pins you want to use and turn them on:
>>>import RPI.GPIO as GPIO
## By doing it this way, you can refer to RPI.GPIO as just GPIO through the rest of your script.
There are two ways of numbering the IO pins on a Raspberry Pi within RPi.GPIO. The first is using the BOARD numbering system. This refers to the pin numbers on the P1 header of the Raspberry Pi board. The advantage of using this numbering system is that your hardware will always work, regardless of the board revision of the RPi. You will not need to rewire your connector or change your code.
The second numbering system is the BCM numbers. This is a lower level way of working – it refers to the channel numbers on the Broadcom SOC. You have to always work with a diagram of which channel number goes to which pin on the RPi board. Your script could break between revisions of Raspberry Pi boards.
>>>GPIO.setmode(GPIO.bcm) ##set board mode to Broadcom
If you don’t know what you are doing, simply type the following to be safe:
>>> mode = GPIO.getmode()
This will detect which pin numbering system has been set (for example, by another Python module)
>>>GPIO.setup(17, GPIO.OUT) ##set up PIN 17
>>>GPIO.setup(18, GPIO.OUT) ##set up PIN 18
>>>GPIO.output(17, 1) ##turn on PIN 17
>>>GPIO.output(18, 1) ##turn on PIN 18
For those people, who do not know what GPIO is, its a module to control Raspberry Pi GPIO channels. This package provides a class to control the GPIO on a Raspberry Pi.
Note that this module is unsuitable for real-time or timing critical applications. This is because you cannot predict when Python will be busy garbage collecting. It also runs under the Linux kernel, which is not suitable for real time applications – it is a multitasking O/S and another process may be given priority over the CPU, causing jitter in your program.
If you are after true real-time performance and predictability, you should consider buying yourself an Arduino. Note that the current release does not support SPI, I2C, hardware PWM or serial functionality on the RPi yet. Although hardware PWM is not available yet, software PWM is available to use on all channels.
So, I think this would be it to get you started, if you have more doubts about Raspberry Pi or Python, you can always refer to the GPIO or Raspberry Pi documentation for further details.
There is so much you can do with Raspberry Pi. This blog alone cannot define wonders done by people who have used it. Make sure, you atleast give this a try, and if you succeed, kindly be humble enough to share your source code on the github. After all, “Sharing is Caring.”
Here are some articles that will help you to get more detail about Raspberry Pi with a Mix of Python so just go through the link.