Showing posts with label getting started. Show all posts

Raspberry Pi: Let's light up the (hello) world

There's a light, that shines, special for you, and me..


Soldering complete and Occidentalis installed, it is time to start learning how all this stuff works. Plus check that my soldering isn't so bad that it has destroyed my Cobbler.

From a quick bit of googling I started with the HelloWorld project:

Hello World: Python

So there are a tonne of hello world pythons tutorials and examples, so I won't dwell on this.
  1. from the shell (I setup wifi connection & then ssh on to the Pi so I can work from my normal desktop - I also installed vim - you can either install vim or use your preferred editor) run: 

        vim helloworld.py

    In the editor enter the following:
        print("Hello World")

    Exit and save the file (:wq)
  2. From the command line run
        sudo python helloworld.py
That's it. You will see the Hello World output in the shell. Yay.


Hello World with Lights On

So, the above was trivially easy and kinda pointless - but I did actually go through that, really just to make sure python was behaving and so I had a working baseline to go from.

So next I wanted to do a Hello World equivalent with hardware - the simplest thing to do seemed like a script that instead of printed hello world, just switched on a light briefly.

This assumes you have followed the same path as me and have opted for Occidentalis, and have soldered your Pi Cobbler (the basics are still really the same even if not using a cobbler, and are just breaking out your pi using a GPIO ribbon cable).



  1. If you opted not to install the Occidentalis OS (mod'd Wheezy OS packaged with stuff if you are planning on doing hardware stuff), then you will need to install some extra bits and pieces (you may already have them installed anyway, but running this won't do any harm - will just print the message saying its already there):
        sudo apt-get install python-dev
        sudo apt-get install python-pip
       
    sudo pip install RPi.GPIO

  2. Connect your Pi to the cobbler using the provided ribbon cable - plug the cobbler into your breadboard so it straddles the break down the middle (See photo below of the final setup of my breadboard for this experiment)
  3. Using a standard jumper lead, connect the ground on the cobbler to the -ve power rail on the breadboard (in this case mine is the purple-ish rail as you can see in the photo, the ground on the cobbler will be labelled GND). This is always best practice.
  4. Connect a resistor from the Pi Cobbler plug 23 to one of the free holes not linked to the cobbler (the resistor is needed to reduce the power - without one of these you will just blow your LEDs)
  5. Connect the +ve leg of the LED (the longer of the two legs) to the -ve power rail that we plugged the GND into, and the shorter LED leg into a hole in line with the resistor we plugged in (again, see photo below for final setup - although ignore the surplus resistor - that's not doing anything, I juts forgot to remove for the photo)



Now that the hardware is all connected, we need to update our simple helloworld script to control the light.  So again, run vim hellolightworld.py and enter the following script:


Now save and exit that script, then go to the shell and run python hellolightworld.py  and watch that little sucker flicker on briefly!









Raspberry Pi & Adafruit Cobbler: Getting Started

It's yours, the world in the palm of your hand..


As mentioned last time, I had to get my hands dirty and do some soldering. Having not really done soldering since my early teens, and even then , I can't really remember what or why I was soldering. I just remember having a consciousness of what soldering was and the experience.  Anyway, needless to say, I was pretty useless at it - not helped by the fact that I was using a soldering iron from the 70's that was undoubtedly designed to fix washing machines, and solder itself to match. The pins on the Cobbler were actually closer together than the diameter of the solder i started with, which made things pretty hard for precision soldering.

To give you an idea of the size of the cobbler, here is a photo pre-assembly:


I bought the Adafruit Pi Cobbler to make prototyping easier, and a lot of the tutorials/articles around the web suggested they were a good idea, and being an electronics rookie I dutifully did what I was told.
I also noticed when I was in Maplins recently that there is now a slightly cheaper version of the cobbler available, I don't know about its quality but in shape/size/design it looks pretty similar to the Adafruit cobbler. The advantage of this is you can just stroll into your local maplin and grab one for not much more than a fiver (that is five english pounds). The red one above is the non-branded one, the blue one is the Adafruit version.

After almost doing irreparable damage to the cobbler with my antique soldering iron and solder, I also resorted to a cheap hobbyist soldering iron and solder from Maplin. It cost less than a tenner (£10) and made a huge difference. Not drinking whisky whilst soldering on the second run possibly also made a difference as well, but that theory would need to be further tested.


All in all, it took me about two-three hours, over two evenings to get the thing soldered (there are 52 pins that need soldering), and considering I haven't soldered for probably 15-20 years (and that was as a young boy, so probably just soldering wires ends with my dad or something) it went reasonably well.


Lessons Learned

1. Don't use solder/soldering irons from the 70's. Washing machine repair evidently requires less precision that modern electronics
2. Don't drink whisky whilst soldering (not proven, but I definitely saw a decline in soldering quality on the first night).
3. Soldering irons get hot - touching them to test if they have cooled down/heated up is not advised.

Raspberry Pi: My Shopping List for getting started

As mentioned, I am completely new to electronics stuff really so I have been trying to get everything sorted and ready to get started. Coming from a Java/JVM dev background, I'm going to try and write up my experiences and things I think are different/difficult etc.

One of the first thing I did, having read a fair few tutorials/blogs on the topic, was work out all the components I needed/wanted to buy. Most of the components are really cheap, and I really didn't want to be caught short mid-development finding out I had to go out and buy something (a luxury that never happens with my usual development process - the most I have to do is go on the web and look for something).

Before I get started, here are the things I bought, which might be useful if planning on following the other posts.

They are all amazon affiliates links - which means if you buy via these links amazon gives me some cash, but won't cost you anything.

 

Raspberry Pi - Getting Started (& ready for hardware hacking)

So, it was all plain sailing getting the Pi up and running with NOOBS following the instructions there, and quickly got it all setup with wifi connectivity and ssh access (which is essential for me, as I don't have a HDMI monitor other than my TV in the living room - so until I could get ssh access from my normal workstation it meant having to take over the living room to configure the Pi on the TV, which my wife was not best pleased about! Also a much cheaper option than buying a new HDMI monitor just for the Pi).

I had been looking around for how to get started with some hardware hacking with the Pi, and must admit the last time I did anything vaguely electronics was when I was in school at 14yrs old (actually I did a robotics module in uni, but that was still mostly programming stuff rather than actual chips/resistors/wires etc).  Some of blogs were writing about how great/essential the Adafruit Pi Cobbler was, and again, I had to go and google and read up exactly what a breakout "cobbler" would be useful for etc but decided that enough people were going for it to invest


(So yeah, that's the Amazon affiliates link - it means I get some money from amazon if you buy this thing from here - it doesn't cost you anything, but is nice for me, so by all means click & buy!)

That lead me on to looking more extensivley at the adafruit site, which has some nice tutorials and all recommend using the Occidentalis OS if you are planning some hardware hacking.



Setting up Occidentalis OS

Occidentalis doesn't come in as nice a package as NOOBS - You basically need to get the image, flash it to an SD card, then boot up your Pi.  I'm on Windows 7 setting up the SD card, so here are the steps/software I used:
  • SD Formatter (from the SD association - this is one of the standard tools recommended in normal Pi setup tutorials and is a pretty convenient way to format SD cards) - https://www.sdcard.org/downloads/formatter_4/
  • Fedora ARM installer - This can be used to install OS images on to an SD card - you can't just copy & paste downloaded images on to an SD card and this just makes it pretty easy - https://fedoraproject.org/wiki/Fedora_ARM_Installer
  • Occidentalis distro image - This is Adafruit's mod of the Raspian Wheezy OS and is modified specifically with hardware hacking in mind - The latest version is currently at v0.2 - http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2


Getting the Occidentalis image and flashing it to an SD card is pretty straight forward if you have the above software - the steps are as follows:
  1. Format an SD card - it really needs to be 4gb - I am using an 8gb samsung card that is working fine (disclaimer: as with any formatting or writing images to drives - make sure you are targeting the correct drive for your appropriate SD card - as all data will be wiped!)
  2. Assuming you have downloaded the Occidentalis image from the above link, extract the zip file locally
  3. Right-click and select "Run as administrator" on the Fedora ARM installer executable.  For the source option, select "Browse" and navigate to the .img file you have just extracted from the zip. For the destination triple-check that you have your SD card drive selected (again, this drive will be wiped and all data overwritten when you hit the install button). Then press install. This will correctly setup the SD card with the OS image.
  4. Jam your newly setup SD card in your Raspberry Pi and startup as usual. For most of the boiler plate setup, this will work just like Wheezy/NOOBS setup.

Uh.. Wait.. My Pi isn't Booting..

So yeah. This happened to me. I setup the SD card and jammed it into my Pi and nada. The red power light flickered on, but no signs of activity and no output on the HDMI.  I did some research, and it turns out that there is a problem with some OS booting if they are using the Hyinx/Samsung RAM chip on the newer models of the Pi. However, my Pi used neither of those RAM chips, mine was fitted with the Micron RAM chip, but still suffered the same problem.

Thankfully with a little tinkering, the same solution generally seemed to resolve this problem for me. If you are experiencing this problem, here is what fixed it for me:
  1. Download and flash and standard OS distrubution to the SD card - I just grabbed the standard Wheezy distro from the Pi download site and flashed that to my SD card as per above instructions 
  2. Once on the SD card, I backed up the files written (only ~35mb) locally
  3. I then installed the Occidentalis image to my SD card (the image that wasn't booting previously, again using the steps above)
  4. I then copied the bootloader files from Wheezy backup on to my SD card - this was just overwriting the bootcode.bin and start.elf files from my SD card with those I had backed up from the standard Wheezy distro.
  5. Jam the SD card in and power up..

So.. I'm off. I will now get the normal wifi stuff setup, plus set up the ssh keys for my github account so I can stick all my code on there (I'm not relying on some flaky SD card for my code backups), the next Pi post will likely be about the Cobbler and basic stuff connecting the Pi to a bread board and a few LEDs, maybe a motor or two.. but the first step for that is soldering my Adafruit Cobbler.

Yep. I'm gonna have to use my hands..

Getting Started - A Complete Android App walkthrough for Beginners (Part 1)

About a year ago I released my first app on to the Android market. I had developed the market primarily as a learning tool, and at the end decided that putting it on the market would be a good step to understand the process and get some feedback.
The application was a simple quiz app based on the popular NBC series Chuck, I released it for free (no-ads) and no real advertising other than one or two target tweets. The app currently has about 4,000 – 5,000 downloads with a 4 star rating – both of which it achieved pretty quickly in the first month or two.



DISCLAIMER: I believe this to be purely down to the fact that at the time there weren’t really any other Chuck related apps, and it has a nerd-ish, cult like following, so I think the rating and popularity is more of a reflection on the show’s popularity than the quality of the app!


Anyway, I thought it might be interesting/helpful to release the source code here and walkthrough the different aspects of the code in a tutorial “my first app” kind of way.

I won’t go in to the setting up Eclipse/Android plug-ins stuff here, so will assume familiarity with getting around the IDE.


Android Directory Structure
Creating a new Android project in Eclipse (using the Android plug-ins) creates the required project structure automatically for you. Lets have a look at that now:


Src : as you would expect, this is the main java source directory, so this is where all your source code lives

Gen: this is the generated code that Eclipse handles for you – the files in here will be generated from your config. You needn’t really worry about what happens in here as Eclipse takes care of it for you

Assets: this can be used to contain any resources or libraries you want to bundle with the app – such as third party jars and data sets (you will see later we use this directory to store the data that we will load to populate all the questions in the DB when the app is first launched on a new device)

Res: this is where the configuration for the layout/UI aspects live:
- Drawable – keep you image files in here (.png/jpg/etc)
- Layout – this is where you keep the xml config for UI layout
- Values – this is where you keep the config with application code, such as application name or set text. This means you can easily change text through out the application without having to change hardcoded strings in the java code and re-compile


Getting Started
Ok, lets get started – the first thing we need is a welcome screen that is displayed when a user first launches the app. In Android, a screen is modelled by an “Activity” class, so to create our welcome screen we need to extend Activity.

public class SplashActivity extends Activity implements OnClickListener{

 @Override
 public void onCreate(Bundle savedInstanceState) {

Every class that overrides the Activity class must implement the onCreate() method – this is called when the activity is launched. As our welcome screen will just be a simple screen with a few buttons on it (play, rules, exit) we don’t need to do much at this point other than set up the buttons to have a listener so we know when one of them is pressed. For convenience here, we will set our current activity class to implement “OnClickListener” – this means we can define the onClick() method directly in our activity to handle the button events.

@Override
 public void onClick(View v) {
  Intent i; 
  switch (v.getId()){
  case R.id.playBtn :
   List questions = getQuestionSetFromDb();

   GamePlay c = new GamePlay();
   c.setQuestions(questions);
   c.setNumRounds(getNumQuestions());
   ((ChuckApplication)getApplication()).setCurrentGame(c);  

   //Start Game Now.. //
   i = new Intent(this, QuestionActivity.class);
   startActivityForResult(i, Constants.PLAYBUTTON);
   break;
   
  case R.id.rulesBtn :
   i = new Intent(this, RulesActivity.class);
   startActivityForResult(i, Constants.RULESBUTTON);
   break;
   
  case R.id.settingsBtn :
   i = new Intent(this, SettingsActivity.class);
   startActivityForResult(i, Constants.SETTINGSBUTTON);
   break;
   
  case R.id.exitBtn :
   finish();
   break;
  }
 }

As you can see, we have one single method that handles any button press, and we manage this with a switch statement checking the id of the view (we will go into this later). Mostly, the cases are simple, “exit” finishes the application whilst “rules” and “settings” have code like this:

i = new Intent(this, RulesActivity.class);
startActivityForResult(i, Constants.RULESBUTTON);

Intents are objects in Android that are used to communicate between activities, and as you can probably spot from the above, this simply creates a new Intent and then uses it to start another Activity class, in this case, our RulesActivity class.

The “Play” case is slightly more complicated – as this needs to initialise the game and load the questions to be used from the DB. Once it has done this, it loads the information in to our “Application” class (we will cover this later also) and then just kicks of the QuestionsActivity with a new Intent as per above.

So that is our welcome page pretty much done, we have a single screen that has a few buttons and we have setup listeners to perform appropriate actions on pressing them. The only remaining issue is the layout of the screen.


Activity Design

In the onCreate() method of our Activity you will notice the second line is

setContentView(R.layout.welcome);

This call tells Android which layout xml file should be used to configure the design – to find the relevant file is quite intuitive, its simply going to be located at /layout/welcome.xml



There are several layouts available to use, each of which are applicable in different circumstances, but as we are just going to have a straight list of buttons, we will use the simple “LinearLayout”. You can nest layouts, as you will see in this example, but lets walk through some of the config options we are using for our layout:

android:orientation="vertical" – This defines our layout as vertical, so new elements will be added on a new row below (if it were horizontal it would add as a new column)

android:layout_width="fill_parent" – this tells the activity to fill the whole width of the screen with the contents

android:layout_height="fill_parent" – this tells the activity to fill the whole width of the screen with the contents

android:gravity="center_horizontal" – this centers all elements that are contained in this layout

android:background="@drawable/background – this defines a background image to use for the activity (this image should exist at /drawable/background.png)


Now, if we look inside the layout you will see some simple components. The first one being a simple ImageView, which points to an image that we want to use as a header. The following elements are the buttons – you will notice that the ID of each button corresponds to the onCreate() method of our Activity where we inflate the button to set the onClickListener.
If you press the “Graphical Layout” tab in eclipse, you will see what the welcome screen will look like.




Ok, so we have our welcome Activty, we have designed a nice UI to control the app and have an onClick handler - the next part will cover intergrating with the DB to pull out the questions and then we are all there!


Grab complete source code for the project here

Getting Started - Best Books to learn Android

I wanted to recommend a couple of great books to get people started learning Android (of course, you could just skip the books and just use some of my tutorials including source code ;)



Hello, Android
This is a nice introduction to the Android platform - walking through from simple applications (as you may guess from the title!) through to some more complex topics such as working with Android's built in SQLite DB and OpenGl Graphics.
Note though that the book isnt a Java book (as none of these here are), and whilst its an introduction to Android, its not an introduction to the wider Java language structure/syntax - so will assume an understanding of the background.



Beginning Android Games
A great book to get started with Android game development! One of the nice things about this book is that it a more hollistic walkthrough of mobile game development and covers more general topics and issues for the area, whilst also providing specific Android examples to try out, it should give you a firm grounding in Android Game development.
The one thing to note that whilst this book does give you an excellent base in game development, it doesn't cover any of the great libraries currently existing for game development such as AndEng, libGDX, Rokon etc


Pro Android 2
This book is actually in its third edition now, but I only have the second edition, so I will link that here - although edition 3 is probably also a good shout.
First thing to mention, don't be fooled by the name! Whilst it descibes itself as Pro Android, it is still pretty suitable for a beginner (I found it was anyway, as long as you know Java, XML, etc) - and actually if you are looking for a "Pro" book this may not be the one for you as it doesn't cover all the topics you may expect for a pro book - such as use of the accelerometer and physics engines. That said, it does provide a solid covering of core topics needed for Android development right through from UI/Menu design through to also covering 2+3/D graphics.