George and Us

Oh, don't look so shocked.

Grim Fandango and Homeworld – Remastered!

leave a comment »

I am pretty old now, compared to most gamers. Two classic games from my ahem, second youth, are being re-issued as remastered versions so we can play them again with way better graphics. They’re probably both available for download somewhere on the net already but these releases will be both legal and completely awesome looking, compared to the originals. They were both always awesome games, amazing to play so adding better graphics should just make them that much better. Woot!

Grim-Fandango-Gameplay-1

Grim Fandango Remastered – http://www.gog.com/game/grim_fandango_remastered

Homeworld MohtershipHomeworld Remastered – http://store.steampowered.com/app/244160/

:)

Written by Peter

January 26, 2015 at 8:27 am

Posted in All Posts, Games, Software

Python + Bottle + PythonAnywhere = easy online web app creation

leave a comment »

What is it about weekends that they just really lend themselves to experimenting? I don’t know either. This weekend, I decided I’d spend some time with web development, so I assigned myself a task: create a simple web application which would have a single big text box at the top of the page which would allow the visitor to type whatever they want. Add a Submit button, allowing the user to save that text to a file on the server and then below that form, display all the text that has been entered so far. This is about as simple as a web form can get, I figured.

Capture

I wanted to use my slowly growing python skills for this little dynamic web page, so I looked around for a framework to use, since Python doesn’t create web page source natively. I looked at Tornado, Flask and a few others, but they all seemed like overkill for my very simple purposes. I  liked the way the Bottle home page made it sound very easy to create dynamic content so I decided to use it.

I knew my one page site / app was gong to need just one HTML page, and the only part of it that will be dynamic is the part which lists the previous entries, so I started by making a web page template. I used the very friendly, easy to learn Initializr to save time with this. Just visit the site, make a few choices on what type of page you’re looking for and then download the source files. You get an HTML 5 file, CSS and JavaScript all in one zip file. I spent about 30 more minutes removing the parts I didn’t want (including all of the JavaScript), adding the form, updating the colors in the CSS to liking and voila! Index.tpl (for a Bottle template) and supporting static files all set.

At this point, the site consists of these files:

index.tpl

css/main.css

css/normalize.min.css

Next, I created an empty file which would hold the text: ‘allthetext’. I created the app’s code file next too: ‘bottle_app.py’. It doesn’t matter what you name that file, for this little app, since it will be the only one.

With Bottle then, the first thing you do is map out the “routes” or URLs you want the app to know how to handle. My app is the only thing this web site will be for, so my default route is just “/”. In the code, I knew I would have to handle both a GET of the page, which would be called when you first visit that path, and also a POST, which will be called when you submit some text in the form.

The GET just needs to display the web page, so that was easy:

from bottle import Bottle, debug, get, request, route, run, static_file, template

app = Bottle()
@app.route('/', method='GET')
def index():
    return template('index')

 

The POST though has to get the value of the form field named ‘textarea’ and save that into the ‘allthetext’ file. Python makes this really simple but I have one complication. I want the newest entry to be at the top of the file, and then include the rest of the entries from there on down. Here’s the code that does this:

@app.route('/s', method='POST')
def submit_form():
    try:
        bigtext = request.forms.get('bigtext')
        if (bigtext):
            new_text = ''
            current_text = ''
            current_file = open('allthetext', 'r')
            current_text = current_file.read()
            current_file.close()

            new_file = open('allthetext', 'w')
            new_text = bigtext + '\n------------------------------------------------------------------------------------------------\n'
            new_text = new_text + current_text

            new_file.write(new_text)
            new_file.close()

            return template("index")
        else:
            return template('index')

 

“S” is the Action of the form on the web page (index.tpl) so that’s the URL the code receives when you submit it. The logic here then just validates that we got some text from the form, and if so, opens the text file, writes the new text in there first, then writes all the older text back in as well. Close the file and re-load the page, and that’s about it. The HTML has python included which opens the file for reading, gets each line and adds that to the output, then closes the file.

I tested this locally and it works like a charm. One of the great things about Bottle is that it includes a development “server” out of the box, so you can test your work at your workstation even if you don’t have a web server running locally.

One thing that was not quite right at this pont was the static CSS files. The were not loading in the browser, as their paths were wrong. I added these Routes to the bottle_app.py script to solve that:

# Static Routes
@app.get('/<filename:re:.*\.js>')
def javascripts(filename):
    return static_file(filename, root='js/')

@app.get('/<filename:re:.*\.css>')
def stylesheets(filename):
    return static_file(filename, root='css/')

@app.route('/', method='GET')
def index():
    return template('index')

 

That just tells Bottle where to look for any file ending with “.css” or (in case I add this in later) “.js”.

So, just to summarize, this is what happens when you first visit the site:

  • the URL requested is handled by the Bottle server
  • it reads my .py code to see what is needed.
  • my .py file says that for the “/” route, just send the template file (index.tpl) back as the response
  • inside that index file, I have Python code reading the contents of the allthetext text file and adding that to the HTML of the page before the browser sees it.

When the user fills in the big text box on that page and clicks the Submit button, this is what happens:

  • Bottle handles the request again, processes the POST request for the “/s” URL
  • Opens the allthetext file, adds the new text from the form field to the top of the file, appends the old text and saves the file again
  • send the index.tpl template again as the response
  • and so the user’s page refreshes with their new text content added to the page beneath the form field and button.

Another really useful feature of Bottle is that when it gets a Form in the request, it knows how to collect up all that data and will also scrub it for things like cross-site-scripting attempts and clickjacking. As the developer of a very simple web app like mine, knowing that I don’t have worry about this is great. Of course, if I were creating an application for production use, I would still very carefully parse the data myself to make sure no bad requests are ever processed.

The last step in my little experiement then was just finding a way to run this online, so I could access the page from anywhere at any time. I found PythonAnywhere, a cloud service which makes it easy to host Python-based web apps like mine. They can support a lot of frameworks too including Flask and Bottle and many others, right out of the box. They very kindly offer a free service for small simple apps and also offer paid services at various levels for folks who need more performance or more CPU / Database time.

I created an account at PythonAnywhere, created my web app, uploaded my files, tweaked them a little for things like paths used and bang! It works. I would recommend this service to anyone else trying out any of the pieces of software I’ve described here. Their documentation is great and gets you up and running very quickly.

I am thinking now about what to do to my little web app next, like maybe adding date and time stamps to each entry and so on, but it was fun creating a really nice looking, HTML 5 compliant, web-based program that I can now run from anywhere, and it only took a few hours. Thanks for all the free stuff that made this all possible: Initializr, Python, Bottle and PythonAnywhere!

:)

 

Written by Peter

January 19, 2015 at 4:31 pm

Posted in All Posts, Software

Arduino + 1602 LCD stopwatch project!

leave a comment »

I recently bought an Arduino Mega2560 beginner’s starter kit from MicroCenter and was looking for an interesting project to use it in. I wanted to use the 1602 LCD from the kit in the project, somehow. It occurred to me that I need a portable stopwatch for my Rubik’s Cube solving so that’s what I made today. It uses the Arduino for counting seconds, minutes and hours and displays the count on a 1602 (16 columns by 2 rows) LCD display. There is a pause button, for when you want to go get some coffee and a reset button for when you lose your focus and need to start over (which happens to me a lot when my kids are around…).

Parts list:

1 x Arduino (Uno or Mega or whatever you have is OK, but there are 8 pins + 5v and GND used here)

1 x1602 LCD display (any as long as it uses Hitachi compatible pins)

1 x potentiometer (I used a 50K Ohm one that came with my kit but usually 10K Ohms is recommended)

1 x breadboard with lots of holes

2 x 4-pin momentary switches (the kind that you click once and then they reset)

2 x 10K Ohm resistors (don’t ask me the colors for the bands; I am partially colorblind. No, really.)

1 x N4001 Diode  (to protect the backlight pin on the LCD)

tons of jumper wires (about 20 actually)

The Arduino sketch:

    #include <LiquidCrystal.h>

    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    // input pins:
    int pausePin = 22;
    int resetPin = 8;
    // output pin:
    int ledPin = 13; 
    // time counting things:
    unsigned long previousMillis = 0;
    long interval = 1000;     // one second
    // pin state trackers:
    int resetButtonState = 0;
    int pauseButtonState = 0;
    int ledState = LOW;
    //de-bounce the switches:
    int lastPauseButtonState = LOW; 
    int lastResetButtonState = LOW; 
    long lastPauseDebounceTime = 0;
    long lastResetDebounceTime = 0;
    long debounceDelay = 50;
    // setup the pause tracker and stopwatch seconds counter
    boolean isPaused = false; 
    unsigned long secs = 0;
    unsigned long mins = 0;
    unsigned long hrs = 0;

    void setup() {
      lcd.begin(16, 2);    // the LCD has 16 columns and 2 rows 
      lcd.setCursor(1,0);    // put the cursor at the first column on the first row
      lcd.print("Stopwatch:");
      pinMode(ledPin, OUTPUT);  
      pinMode(resetPin, INPUT);
      pinMode(pausePin, INPUT);
      Serial.begin(9600);     // for logging on the PC
    }

    void loop() { 
      Serial.print(" - Hrs: "); 
      Serial.print(hrs);
      Serial.print(" - Mins: "); 
      Serial.print(mins);
      Serial.print("Secs: "); 
      Serial.print(secs);
      Serial.print(" --- pause state: ");
      Serial.print(pauseButtonState);
      Serial.print(" --- isPaused: ");
      Serial.print(isPaused);  
      Serial.print(" --- reset state: ");
      Serial.println(resetButtonState);

      pauseButtonState = digitalRead(pausePin);
      if (pauseButtonState != lastPauseButtonState) {
        lastPauseDebounceTime = millis();
      }

      if ((millis() - lastPauseDebounceTime) > debounceDelay) {
        if (pauseButtonState == HIGH) {
          Serial.println("*******************   PAUSE   ***************************");
          if (isPaused == true) {
            // start again
            isPaused = false;
          } else { 
            //pause now
            isPaused = true;     
          }
        }
      } 

      resetButtonState = digitalRead(resetPin);
      if (resetButtonState != lastResetButtonState) {
        lastResetDebounceTime = millis();
      }

      if ((millis() - lastResetDebounceTime) > debounceDelay) {
        if (resetButtonState == HIGH) {
          Serial.println("^^^^^^^^^^^^^^^^^^^^^^^^  RESET  ^^^^^^^^^^^^^^^^^^^^^^^^^^");
          secs = 0; 
          mins = 0;
          hrs = 0;
          isPaused = false;
          lcd.setCursor(1,1);
          lcd.print("               "); 
        }
      }

      if (isPaused == false) {
        unsigned long currentMillis = millis();  
        if(currentMillis - previousMillis >= interval) {
          secs = secs + 1;
          if (secs == 60) { 
            secs = 0;
            mins = mins + 1;
          }
          if (mins == 60) {
            mins = 0;
            hrs = hrs + 1;
          }       
          lcd.setCursor(1,1);
          String theTime = String(hrs);
          if (mins >= 10) {
            theTime = theTime + ":" + String(mins);
          } else {  
            theTime = theTime + ":0" + String(mins);
          }
          if (secs >= 10) {
            theTime = theTime + ":" + String(secs);
          } else {  
            theTime = theTime + ":0" + String(secs);
          }
          lcd.print(theTime);

          // flash the LED for status...
          previousMillis = currentMillis;   
          if (ledState == LOW) {
            ledState = HIGH;
          } else {
            ledState = LOW;
          }
          digitalWrite(ledPin, ledState);
        }
        delay(10);
        lastPauseButtonState = pauseButtonState;
        lastResetButtonState = resetButtonState; 
      }
    }

Initial hardware setup:

  1. Don’t connect the Arduino to power (your PC) until this is all setup.
  2. Drop the LCD onto the breadboard. I put mine over on the right side.
  3. Drop the potentiometer onto the breadboard away from the LCD.
  4. Jump wire from the 5V pin on the Arduino to the “+” rail on the breadboard.
  5. Jump wire from the GND pin on the Arduino to the “-” rail on the breadboard.
  6. Jump wire from the + rail to the left-most pin on the pot.
  7. Jump wire from the – rail to the right-most pin on the pot.
  8. Jump wire from pin “Vss” or #1 on the LCD to the – rail on the breadboard.
  9. Jump wire from pin “Vdd” or #2 on the LCD to the + rail on the breadboard.
  10. Jump wire from pin “V0″ or #3 on the LCD to the middle pin on the pot.
  11. Diode from the + rail to pin “A” or #15 on the LCD.
  12. Jump wire from pin “K” or #16 to the – rail on the breadboard.
  13. Plug in the Arduino to your computer. (That’s right no sketch yet at this point)
  14. The LCD should be backlit if you did the “A” and “K” pins correctly.
  15. Adjust the pot’s knob. You should be able to turn it all the way down and then see a row of boxes on the LCD.
  16. If not, stop here and fix that. GIYF.
  17. Unplug the Arduino.
  18. Jump wire from pin “RW” on the LCD to GND on the breadboard.
  19. Jump wire from pin “RS” on the LCD to pin #12 on the Arduino.
  20. Jump wire from pin “E” on the LCD to pin #11 on the Arduino.
  21. Jump wire from pin #2 to pin “DB7″ on the LCD.
  22. Jump wire from pin #3 to pin “DB6″ on the LCD.
  23. Jump wire from pin #4 to pin “DB5″ on the LCD.
  24. Jump wire from pin #5 to pin “DB4″ on the LCD.
  25. Plug in the Arduino. Publish the sketch to your Arduino and watch it run.
  26. If everything is just right, you’ll see “Stopwatch:” and the time counting up on the LCD display.
  27. If not, pull your hair out and troubleshoot here before moving on. Curse a lot, it helps.

Add the switches to the board:

  1. Drop the two 4-pin switches onto the breadboard, spanning the middle valley.
  2. Name one of them reset and one pause in your mind. Keep them separate.
  3. Jump wire from the reset switch on the same side as the other components to pin #8 (or whatever you like) on the breadboard.
  4. Jump wire from the pause switch on the same side as the other components to pin #22 (or whatever you like) on the breadboard.
  5. Jump wire from the reset switch on the opposite side of the breadboard from the other components to the + rail on the breadboard.
  6. Jump wire from the pause switch on the opposite side of the breadboard from the other components to the + rail on the breadboard.
  7. 10K Ohm resistor from the other corner of the reset switch to another line on the opposite side of the breadboard from the other components.
  8. 10K Ohm resistor from the other corner of the pause switch to another line on the opposite side of the breadboard from the other components.
  9. Jump wire from from the reset switch’s resistor’s second line to GND on the breadboard.
  10. Jump wire from from the pause switch’s resistor’s second line to GND on the breadboard.
  11. Whew, that’s it!

LCD Stopwatch - Fritzing diagram

Close-up on the Arduino

Close-up on the pot and switchesClose-up on the LCD and wires

Run it!

With all of that wiring, connecting the LCD to the pot and he Arduino, and the switches wired to their own digital pins on the Arduino and to power and ground, you’re all set.

When you plug the Arduino back in, load the program if it’s not already there and voila! (Hopefully) The screen will show the stopwatch and start counting up. When you press the Reset button, the time should reset to zero. When you press the Pause button, the time should freeze, until you press Pause again.

Troubleshooting

I had trouble with the lCD not displaying the text I sent it at first. I found it was very lossly connecting to the breadboard, so I had to jiggle it and remove/replace it a few times until I could feel it going into the holes on the board properly and then it worked from there. If you have trouble, try this and then check online for other solutions. I rely on the kind folks at http://reddit.com/r/arduino for lots of help and you can too.

Conclusion

If you try this out, let me know how it goes in the comments. Let me know too if you see any really significant improvements I could make. Thanks for reading this far.

:)

Written by Peter

December 6, 2014 at 11:55 am

Posted in All Posts, Arduino, Software

Playing around with Jabber on the Rasberry Pi

leave a comment »

Hi all, this is just a post about my experience today trying something new on my Pi. I have been looking for something my son could do with his Pi, as it’s been sitting on a shelf gathering dust for some time. I thought maybe it would be easy to set it up as a chat / Jabber / XMPP server so he and his buddies could chat whenever they like and so he would learn some about computer networking and Linux services and so on. The Pi is already running Raspbian as the OS so this seemed straight forward.

Here are the steps we followed:

sudo apt-get update 
sudo apt-get upgrade

(Just to get everything up to date in Raspbian land)

sudo apt-get install prosody

Prosody is the XMPP / Jabber server I chose to use, after reading about it and ejabberd online. They both offer lots of options but Prosody won out for its seemingly simple installation. I made that choice pretty quickly though, so I really don’t know ejabberd much.

cd /etc/prosody
sudo cp prosody.cfg.lua ./prosody.cfg.BAK
sudo nano prosody.cfg.lua

Now I needed to make a backup of the config file (LUA really) for Prosodyand then edit it. The server’s web site is very clear about most of this, so reference that if you need help:

https://prosody.im/doc/configure

First, you need to set up a virtual host name. This concerned me at first, as all of the examples given looked like real domain names, like example.com, and I didn’t want to pay for any service like that (and I don’t own one already). I ended up just using my Pi’s name on the network, “firstpi” and that works fine as long as the clients are all inside the house.

Next, I enabled registration as its called, as that seemed the easiest way to get “accounts” setup on my new Chat server. Note to self — don’t forget to disable registration before opening the server to the full internet, or any oddball out there could also register an account for themselves!

sudo service prosody restart

… to make those config changes take effect.

Sure enough, now I have the Pi and Prosody listening on my LAN. I needed a Jabber client to connect to it with. I ended up using Gajim, since my laptops all run Windows and that worked out OK. It has a nice installer and runs OK for the most part. I have seen about six “An error just occurred…” messages in just a few minutes of playing with it, but it’s an Open Source project, so that’s OK.

In Gajim, I just added a couple accounts for me and my son. Creating the accounts in Gajim was easy (since I had the server setup to allow registration via the client) and before long, I was able to open a group chat room for my multiple accounts and chat. I named the accounts like, “dad@mypi” and “theboy@mypi” and Gajim found the server on its own.

I installed yaxim on my Android tablet next and again, setting up and connecting was easy there. It is limited to only one account in use at a time though, so that might be a concern for some.

https://play.google.com/store/apps/details?id=org.yaxim.androidclient&hl=en

Now we can chat at each other from our Windows laptops and Android tablets with ease. Next step will be setting this up so we can chat from outside the house as well.

The advantages of doing this are: we set it all up ourselves, it’s a private chat channel, we learned a little about networking along the way.

:)

Written by Peter

November 30, 2014 at 1:54 pm

Posted in Raspberry Pi, Software

Tagged with

Cron stopped sending emails; dead.letter file was a clue

with one comment

Quick post:

Today I realized that a few days ago I stopped receiving emails from the Raspberry Pi running the latest version of the Raspbian OS. I have Cron setup to run a couple of scripts on a daily or weekly basis and by default cron emails the results of those scripts to my personal email address. I realized those emails stopped coming a few days ago but couldn’t remember making any changes that should have caused this. Here are the steps I took to troubleshoot this:

1. Check crontab -l to list the jobs associated with my Pi login. They looked the same as always.

2. Run one of the scripts manually from the terminal. Yep, I get the expected result there.

3. Re-configure cron with crontab -l so the job will run in about two minutes. Wait three minutes… No output. Still no email. Hmmmm…

4. Notice there is a file in my Home directory named dead.letter that I never saw there before. Read up on what that is…

5. Look for logs in the /var/log. Notice that mail.log there has these entries:

May  5 13:13:08 firstpi sSMTP[10443]: Creating SSL connection to host
May  5 13:13:08 firstpi sSMTP[10443]: SSL connection using RSA_ARCFOUR_SHA1
May  5 13:13:08 firstpi sSMTP[10443]: Authorization failed (535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 om5sm30207817igb.16 – gsmtp)

6. Remember that I changed the password on this email account a few days ago!

7. Edit the ssmtp.conf file which stores the credentials for this email account and update the password in there. Save the file, exit, breathe a sigh of relief.

Success!

:)

 

Written by Peter

May 5, 2014 at 1:28 pm

Posted in Raspberry Pi, Software

Build a radio station in your home with Raspberry Pi

leave a comment »

Using a Raspberry Pi as a home music server (MPD)

RPi + Music = Awesome

I recently wanted to figure out a way to setup my own local streaming radio station in my house. Internet radio stations are cool, for discovering new music for instance, but I have a really large music collection here at home so most of the time I just want to listen to it. The problem I have though is that my music sits on a USB hard drive and that’s not connected to anything else. With some research, I determined I could setup my Raspberry Pi (which is a tiny computer running Linux) to let me have access to my 30GB + digital music collection from any other computer in my house. A very cool Linux program called MPD (Music Player Daemon) is a good fit for this purpose and it’s free, open source software so that’s a bonus for sure.

There are a few things I learned about all of this along the path of getting it all setup, that I wish I had known earlier on. First, MPD is a great tool for playing music from any Linux computer, but it cannot stream the audio it plays across a network on its own. Instead, MPD’s primary use is for  playing music directly to the audio hardware on the computer it’s running on. But fear not, we’re not defeated yet. The MPD output can be redirected to another application for streaming across the network. I chose to use Icecast2, another free Linux service, which knows how to take the audio signal coming out of MPD and send it across the network. The last thing I needed was a client to receive that data stream and turn it into beautiful music coming out of speakers or headphones. In my case, that’s Mpdroid, on my Android tablet. More on that later.

Note: I am not an audiophile. Most of my music collection is stored in low-quality MP3 files and I listen to it on cruddy headphones most of the time. If you really get into audio quality, you may not love the results here. They’re perfectly awesome as far as I am concerned.

Here’s what I ended up with then:

[My Raspberry Pi Model B] + [A USB hard drive for MP3 storage]
running
[MPD for controlling the playback of my music collection]
and my Android tablet running
[Mpdroid for controlling the music’s playback from my Android tablet]

Setup and configuration

Here are the steps you can follow to create your own totally home network music server

1. The Raspberry Pi requires an OS and the usual setup. I am using Raspbian, which came on the NOOBS SD card I setup. See this page for that — http://www.raspberrypi.org/downloads

2. Connect your Pi to the home network. I placed mine next to my home router and connected it using the built-in Ethernet port, but you could use Wifi instead. I think you’ll see a performance hit on playback if you do that though; try to use a cable instead of wireless if at all possible.

3. I plugged my powered USB drive into my Pi’s first USB port. To set this up, you’ll need to do some command-line configuration so your Pi recognizes the drive and “mounts” it (to use a cool Linux term). Those steps are well documented in many places. Search online for ‘Debian mount external usb drive’ to get help.

4. OK, after that last step, you should now be able to access your music files on the external USB drive with a path on the PI something like this: /mnt/myDrive/Music or whatever you setup when you edited the fstab (file system table file which remembers your drive’s path).

5. Next, you need some more software, so enter this command into a terminal to install MPD (server software), mpc (a client), and Icecast (the streaming software):

sudo apt-get update
sudo apt-get install mpd mpc icecast2

6. As that completes, you will most likely be prompted to setup passwords for Icecast. Do that here and now since it’s simpler than editing the configuration file later. Choose passwords as you are prompted and write them down (or pick something secure that you can remember) and choose your Icecast broadcast port. I chose to stick with the default – 8000. If you change that, just make sure you choose a number that’s not already in use on your Pi. Don’t use 6600 since that’s the MPD default port.

7. When the install is done, you’ve got some configuring to do. Enter this command into a terminal to edit the configuration file for the MPD service:

sudo service mpd stop                        <<stop the service before editing its config >>
sudo cp /etc/mpd.conf /etc/mpd.bak           <<always make a backup of a conf file before editing it! >>
sudo nano /etc/mpd.conf

8. Here’s my file, for your reference, with some notes added in so you know what I changed from the default file.

#######################
 music_directory         "/mnt/bigdrive/MEDIA/MP3"     # this is where my music files are stored on my USB drive
playlist_directory        "/var/lib/mpd/playlists"
 db_file                    "/var/lib/mpd/tag_cache"
 log_file                "/var/log/mpd/mpd.log"
 pid_file                "/var/run/mpd/pid"
 state_file                "/var/lib/mpd/state"
 sticker_file            "/var/lib/mpd/sticker.sql"
 #######################
 user                "mpd"
 # I commented the bind_to_address. I don't honestly know why, but it didn't work till I did this!
 # bind_to_address        "localhost"
 port                "6600"
 #######################
 input {
 plugin "curl"
 }
 #######################
 # This is how the MPD service will talk to the Icecast service
 audio_output {
 type        "shout"
 encoding    "mp3"            # optional, can also be "ogg"
 name        "Pi Shout Stream"
 host        "localhost"
 port        "8000"
 mount        "/mpd"
 password    "put your password here - don't copy mine"
 bitrate        "128"
 format        "44100:16:1"
 }
 ########################

9. Save that file in Nano by typing Ctrl-X, Y (for yes) and [Enter] to keep the same file name.

10. Enter some more commands to get MPD and Iceccast2 started:

sudo service mpd start
sudo service icecast2 start

11. Now then you have the MPD server accessing your music files and the Icecast service sending that audio stream out over your network to any client that wants to access it. So, next you need clients.

12. The Icecast broadcast is just like the old Shoutcast service, so there are a lot of client options here. Here are a few I am using, as examples, but do some searching for your OS / preferred platform and you’ll find lots more. Just remember – you may need a MPC-compatible client to control the music’s playback and also an Icecast/Shoutcast compatible player to actually hear the audio.

a. Web browser – that’s right, any web browser should be able to play your music now. What you need to do is get the mpc client on the Pi to play a song / playlist and then you can listen from any LAN connected browser, like this:

1. On the Pi:

mpc add                 << to add all of your music from the music folder in your Config file to the current mpc playlist >>
mpc play {####}         <<i.e., mpc play 1234 will play song 1234 in the playlist>>

2. On any home computer or tablet or smart-phone:

<< open a web browser, and enter this address:  http://{your pi’s IP address}:8000/mpd    or whatever port number you chose for Icecast >>

b. Linux computer:

1. Install an app like GMPC and point it to your Pi’s MPD port number (6600 in my case), which is different from the Icecast port.

2. Use GMPC to control the playlists and songs currently playing from the MPD service. (You won’t hear the sound from here though so keep reading)

3. Open a browser and point it to the Icecast port like in my previous example above, OR

4. Install something like RhythmBox (which comes with Ubuntu) and point it to the Icecast streaming port, just as you would any other Internet radio station.

c. Windows computer:

1. Just do the web browser trick above.

d. Android tablet:

1. Install Mpdroid from the Google play store.

2. Configure it to point to your MPD port number (6600 in my case).

3. Choose songs to play from Mpdroid whenever you like.

4. Check the “Streaming” check box in the settings if you want to listen to the music right here on your Android phone/tablet.

5. Or, open a browser on your PC (or Mac or whatever) and listen to the audio now streaming from there, using the ‘web browser’ instructions in a. above.

That’s about it. If you try this out, please comment here if I can add anything to these notes to improve them. Thanks for reading this far!

:-)

Written by Peter

March 11, 2014 at 6:03 pm

Minecraft + Raspberry Pi + Python = Geeky heaven

leave a comment »

If you know me, you know I am a geeky sort. I like all things to do with computer software and development. I keep up with all the goings on as much as any one person can. When I learned I could write Python scripts to run on my Raspberry Pi, inside of Minecraft, well, I just had to start playing. This is known as Minecraft: Pi Edition (or MCPI for short). First, let me define each of  these terms, in case they’re new to you:

The Raspberry PiRaspberry Pi – A credit card sized computer on a single circuit board which you can pick up for less than $100, including, all the parts that go with that circuit board. This thing is a wonderful example of innovation in the 21st  Century in that it brings software development within reach of a lot of people who might otherwise never have the chance to try it out. The computer is cheap and it is very easy to setup. The company making the Pi has sold more than two million of these little things now and it seems there are nearly as many uses for them. This Ars Technica article lists just ten to give you an idea of the variety of uses out there.

Python – A neat programming language which is currently the default language for beginners to learn. There are TONS of people playing around with Python online and pushing it in wonderful new directions. Python is easy to learn, especially for young people or anyone who has never written a computer program before. It is mostly forgiving of new coder mistakes and so eases the learning curve quite a bit. I’ve been learning it for a few years now, so this was a really cool opportunity for me to extend my learning in a new way.

MinecraftMinecraft – A massively popular video game from a Swedish company called Mojang. It’s a deceptively simple block-building game that’s been around a few years now. Here’s is a link to one of the first videos I watched a few years ago on how to survive your first night in the game. Mojang released a version of Minecraft specifically for the Raspberry Pi earlier this year and that’s what this blog post is all about. MC now has a huge community of very active players and coders, creating all sorts of modifications and creations using the game as a platform. It’s so open-ended in fact, that you’ll see it used in universities and elementary schools alike for learning many different disciplines (from basic math to architecture). I believe this game will be viewed in years to come as the most important thing to come out of the 2010 decade.

When you play Minecraft on a PC or Mac, you run the game and then you are inside the virtual Minecraft world the whole time you’re playing. You can click here and there to create wonderful structures with whatever comes into your imagination. It’s sort of like having an almost infinite selection of Lego blocks to play with. With Minecraft: Pi Edition though, you get that plus the ability to interact with the virtual world via commands you execute in a Python script. This opens the possibility of creating things very quickly via code that would take a lot longer to make by hand and turns out to be a really cool way to learn to code.

For example, say you want to make a building with five stories, glass windows and a solid gold roof. You can do that in the PC-version of Minecraft, but to do so you will be clicking each and every block into place, one at a time. With the Pi edition of MC though, you can write a script with loops for each floor, and commands like setBlock() which will do the clicking for you. The version of Minecraft that Mojang released for the Pi is a limited version of the so-called Pocket Edition though, so it’s not able to do everything the PC versions can, but that’s really OK. The point here isn’t to play Minecraft; it’s to have fun writing scripts in Python and to learn how to program.

There are a lot of good pages online already showing you how to setup MCPI so I’ll just link you to this one from here.

There are some additional notes I wish had been made clearer when I was first starting:

  • You can’t run Minecraft on the Pi via remote access software like RDP or VNC. For some reason, the graphics don’t carry through. You have to connect the Pi to a regular monitor and use it that way.
  • Once you have your Pi setup to run MC, you will end up with a few windows open:
    • Minecraft
    • your text editor, like Leafpad
    • A Terminal window so you can your Python commands
  • Because of this, I found it most useful to have my text  editor in a separate virtual desktop (which is a neat feature in Linux X-Windows desktops)

My workflow then went something like this:

  1. Open a LX Terminal window (like a DOS Command window in Windows)
  2. CD in there to the folder I will store my Python scripts in, which for me was:
    • /home/pi/mcpi/api/python
  3. Run Minecraft from my Desktop shortcut.
  4. Create a new World or open an existing one.
  5. Move to a place with a clear landscape, but not too far from the spawn point.
  6. Alt-Tab away from MC and open Leafpad.
  7. Create a new script, or open an existing file. Save the scripts into the folder above.
  8. Alt-Tab back to the Terminal and run the script with a command like, “python script.py”.
  9. Alt-Tab into the MC window to see whether my script worked or not.

With this being my first time playing around with this, I decided to run a few scripts I found online, to see how they work. I started with this site’s Auto Rainbow script, which creates a huge rainbow across the landscape in your world. It worked just fine after just a little bit of editing (the indents are off a little and Python hates that), so once I fixed the code, I knew this was going to be really cool. Next, I started playing with the variables like the height in that script until  I felt I had the hang of working with the API.

I then decided I would create a house from scratch with all my own Python code. After a lot of trial and error, I got it just right. I used the tip I learned here to get the player’s current position, since it was a bit of a hassle always creating things based on the spawn point. I would move around and then not see whatever the script had built, since MC only shows you a limited view of the landscape whenever you’re walking around your world.

Here’s my first “house” script then, for reference:

import mcpi.minecraft as minecraft
import mcpi.block as block
from math import *
import time

mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
mc.postToChat("pos is " + str(pos.x) + " " + str(pos.y) + " " + str(pos.z))

width = 10    # like x
depth = 10    # like z
maxHeight = 20

# clear as air
for x in range(1, width+5):
    for y in range(0, maxHeight):
        for z in range(1, depth+5):
            mc.setBlock(pos.x+x,pos.y+y,pos.z+z,block.AIR)

mc.postToChat("Cleared with air...")

#floor
for x in range(1, width):
        for z in range(1, depth):
            mc.setBlock(pos.x+x,pos.y-1,pos.z+z,block.WOOD)

#walls
for z in range(1, depth):
    mc.setBlock(pos.x+1,pos.y,pos.z+z,block.STONE)
    mc.setBlock(pos.x+width,pos.y,pos.z+z,block.STONE)

    mc.setBlock(pos.x+1,pos.y+1,pos.z+z,block.GLASS)
    mc.setBlock(pos.x+width,pos.y+1,pos.z+z,block.GLASS)
    mc.setBlock(pos.x+1,pos.y+2,pos.z+z,block.GLASS)
    mc.setBlock(pos.x+width,pos.y+2,pos.z+z,block.GLASS)

    mc.setBlock(pos.x+1,pos.y+3,pos.z+z,block.STONE)
    mc.setBlock(pos.x+width,pos.y+3,pos.z+z,block.STONE)

for x in range(1, width):
    mc.setBlock(pos.x+x,pos.y,pos.z+1,block.STONE)
    mc.setBlock(pos.x+x,pos.y,pos.z+depth,block.STONE)

    mc.setBlock(pos.x+x,pos.y+1,pos.z+1,block.GLASS)
    mc.setBlock(pos.x+x,pos.y+1,pos.z+depth,block.GLASS)
    mc.setBlock(pos.x+x,pos.y+2,pos.z+1,block.GLASS)
    mc.setBlock(pos.x+x,pos.y+2,pos.z+depth,block.GLASS)

    mc.setBlock(pos.x+x,pos.y+3,pos.z+1,block.STONE)
    mc.setBlock(pos.x+x,pos.y+3,pos.z+depth,block.STONE)

# ceiling
for x in range(1, width+1):
    for z in range(1, depth+1):
        mc.setBlock(pos.x+x, pos.y+4, pos.z+z, block.GOLD_ORE)

I believe that will require a little explanation:

  • First, we have to import a few external libraries to make writing our script easier, including the minecraft API and a few others.
  • I use the postToChat() API in a few places to make little debugging messages appear within MC so I know where we are in the script as it runs in the background.
  • Next, I create an instance of the Minecraft class as “mc” and then get the player’s current position in the world.
    • Positions are expressed as three integers: x and y a z coordinates.
  • Next, I “clear” the blocks in the area where I want to draw by setting them to the “air” or empty block type.
  • Then I “draw” the floor by using the setBlock() API for a given range of coordinates to the Wood block type.
  • Then I draw the walls the same way, but using the Stone and Glass blocks.
  • I top it all off with the Gold Ore block type for the roof og my now one story tall house.

Here’s what the House looks like after it’s built by the script (which only takes a few seconds):

My first MCPI house

It was really not hard to learn and I think, with most kids in the 10-12 age range already being Minecraft fanatics, this would make a fantastic way to teach kids about programming in a really fun way. I’ll have to try teaching some of this to my own 12-year-old next.

Many, many thanks to the Raspberry Pi Foundation for their awesome computer and to Mojang for making MCPI available for free.

:)

Written by Peter

December 22, 2013 at 2:24 pm

Posted in Raspberry Pi, Software

Tagged with , ,

Follow

Get every new post delivered to your Inbox.