Monday, July 18, 2016

Minecraft Pi backup Worlds on exit

My kids love building cities on the Raspberry Pi version of Minecraft.  They're not programming them yet, but they spend hours placing blocks and designing houses, hotels, swimming pools, and other building and gardens.
All of which is great.
Except I also use the Raspberry Pi for programming and other things where due to my fumbling around I end up borking the SD card and have to start again.

That's OK. If you backup all the files in ~/.minecraft/games/com.mojang/ and then put them on the new SD card all is good.  Really simple backup.
Only problem is I keep forgetting this step and delete weeks or months or effort.

Rather than expecting somehow that I will remember in the future I thought it would be a good idea to solve the problem with a bit of code and a little modification to the menu.

The goal is to replace the command that runs minecraft-pi with a command that runs minecraft-pi and then on exit archives the worlds and sends them to an ftp server.
In this way the backup is off the Pi and even if I do reformat it I can get the backup minecraft worlds and just reinstate them.

All this is being done with Raspbian as the OS.

For the ftp server I have OSMC set up on a Raspberry Pi (1) B+ that I use mainly as a media server for a couple of Blu-Ray player and tablets in the house so it is on 24x7.

The code is quite short made up of a python script that does the archiving and ftping of the file and a bash script that runs minecraft-pi and then runs the backup script.
The code is available at https://github.com/winkleink/MinecraftPi_backup

Just copy it into pi user home folder '/home/pi'
If you copy the code somewhere else then you will need to modify the bash script and the files to take it in to account.
Once copied change the file preferences to permit execution. Since you're in the GUI the easiest way to do this is right click on the file and select [Properties].
The go to Permissions and set Execute.  I chose Anyone, but if you're only going to run as user pi then you can select 'Only owner'

Change Execute permissions so this is a program that can be run

You need to do this for the 2 files (minec.sh and ftpzipminecraft.py)

minec.sh is the bash script to run the 2 commands. With the semicolon at the end of the first command it knows to run them in sequence (one after each other) rather than potentially at the same time. No modification is needed unless you save the ftpzipminecraft.py program somewhere other than the pi home folder

minec.sh

#!/bin/bash
minecraft-pi; 
/home/pi/ftpzipminecraft.py

ftpzipminecraft.py runs after minecraft-pi closes and this does the archiving to local file in the pi home folder and then connects to the ftp server and transfers the archive to the server.
You have to change 3 items in the file for it to work.  I've highlighted them in red below.

FTP SERVER NAME/IP ADDRESS
USERNAME
PASSWORD

ftpzipminecraft.py

#!/usr/bin/python3
# Script to backup the MinecraftPi worlds and then upload them to an FTP server
# By Winkleink (July 2016)
# https://github.com/winkleink/MinecraftPi_backup
# MIT Licence
# This is a quick fix so use at your peril
import os
import zipfile
import datetime

import ftplib

now = datetime.datetime.now()
year= str(now.year)
month = str(now.month)
day = str(now.day)
hour = str(now.hour)
minute= str(now.minute)

uploadfile = "minecraft"+year+"-"+month+"-"+day+"-_-"+hour+"-"+minute+".tar.gz"

print(uploadfile)
os.system ("tar zcvf " + uploadfile + " /home/pi/.minecraft/games/com.mojang")

# Enter your FTP Server Name or IP address, Username and Password in the following line
session = ftplib.FTP("FTP SERVER NAME/IP ADDRESS","USERNAME","PASSWORD")
file = open("/home/pi/"+uploadfile, "rb")
session.storbinary("STOR " + uploadfile, file)
file.close()
session.quit()
print("Done")





Once both files are in place and are set to executable we need to modify the menu entry for Minecraft so it runs our new script minec.sh

From the main menu select [Preferences] [Main Menu Editor]


Then on the left select [Games] and pick [Minecraft] and then click [Properties] on the right.


Finally, replace the 'minecraft-pi' command with '/home/pi/minec.sh'


Its not shown here, but so I know it's the one I've changed I modified the Comment to 'Minecraft + Backup' so when I mouse over the menu entry I can see it the one with the backup.

With this little change I will hopefully never lose my kids amazing creations in Minecraft on the Raspberry Pi.


Wednesday, June 22, 2016

Spider Chart maker using Python and Pygame

16th May 2017 - Updated to make the output an A4 landscape page.
Also, provided Inkscape created SVG file so you can prepare you can add your own School or Club name.  Make sure the final PNG resolution for __spider.png is 3508 x 2481 as the dot locations are hand coded for the spider to be in the location it is on this size of image.

Example of improved chart.



A few days ago a call went on out on Twitter for how to make lots of Spider Charts, also known as Radar charts in a programmatic way.  I decided to take up the challenge.


The code and associated files are available to download from GitHub

Below is a sample completed chart

To do this I started with a blank version of the chart and added the person name top left and the red dots and lines for the individual scores.

The program is written using Python and Pygame and was developed on the Raspberry Pi.
Since it doesn't use any other external libraries I'd expect it to work on other systems with Pygame installed.

To achieve this 2 external CSV files and a large (3508 x 2481) blank chart image are used.

The image file is: __spider.png

spider_data_points.csv is a comma separated variable list of the co-ordinates on the 3508 x 2481 image for the individual points.  By using a separate lookup if this was to be adapted to a different chart then you can just use a new master image and a different set of co-ordinates.

Student_Scores.csv is the file with the individual student (in this case) names and scores for the different skills.  There is no limit to the number of students that can have charts created as the code just goes through the csv line by line and creates a chart for each one.

Rather than creating the csv files manually it is so much easier to use a spreadsheet for data entry and then [Save As] a .csv
To facilitate this I've include .xlsx versions of the files.
These can be open and saved on the Raspberry Pi using OpenOffice.

Meaning if you needed to make 100 of these charts it would be a simple matter of:
Place all the code in a single directory
Enter the data in the spreadsheet.
Save as a .csv
execute spiderchart.py
Watch as all the charts are created automatically with the student name as the filename

Don't worry if the image is mainly off screen it is still being done correctly.

In the code there is a 1 second sleep after each chart is created. If you are doing lots then it might be worth commenting out the sleep.  It's useful for seeing what's going on.

As a complete side note: I was one of the backers of the Indiegogo pi-topCEED an All-in-One form factor for a Raspberry Pi desktop and it was delivered and set up on Monday.  It came with a Raspberry Pi 3.  This was the set up I used to develop and run the code.  I was very impressed by the quality of the 14" screen on the CEED.  This is now set up as the permanent home computer that my kids can use for playing Minecraft and exploring the CEEDuniverse, a game that teaches you about computers, coding and electronics.

Tuesday, May 3, 2016

Minecraft on Raspberry Pi Zero using OTG USB Ethernet connection with RealVNC server and client


In my previous post I connected to a Raspberry Pi 3 using RealVNC to connected over Ethernet.
To achieve this you would need:
Laptop
Raspberry Pi
PSU for Pi
Ethernet cable

What if you could replace the PSU and Ethernet with just a microUSB cable and replace the Pi3 by a PiZero.
Well, this is absolutely possible and quite simple due to the hard work of a number of people including Andrew Mulholland who created a guide on setting up Ethernet over USB using OTG that I hope to make even more simple here. Then it's a matter of installing RealVNC to get the Raspbian desktop on your laptop.

OTG stands for On-The-Go or it's full name USB On-The-Go
Put simply OTG allows a USB device to act like a host and have peripherals  such as keyboard and external storage attached to the device or act like a slave so it can be a network device or a mass storage device.
This is most used on phones an tablets where it's great to be able to plug in a USB stick and access the files (host) or plug the phone into your computer and download the files (slave)

For OTG is enabled by the ID-pin which is available on the PiZero as the USB port is directly connected to SOC and it uses a microUSB connector which has the extra pin for the ID.

Back in December Andrew posted a guide on setting up OTG.
It includes a lot extra information that is worth reading and understanding, but here I want to just do the facts and steps needed to enable Ethernet over USB.

I'm assuming you have already installed Raspbian on your PiZero 

Next you need your PiZero to be connected to the network to be able to download and install some files. For this I used an Wii USB Ethernet adapter.  This adapter is supported by default. They're about £10 on eBay.  Just search Wii USB Ethernet.

With the PiZero connected to the network I found the Pis IP address using Fing on my phone.
With the IP address known I used putty to ssh in. (Yes, this is the correct link for downloading)

Once logged in do the usual housekeeping.
sudo raspi-config to expand the filesystem and set GPU memory to 128MB
sudo apt-get update
sudo apt-get upgrade

Then it's a matter of running the following commands one after another to get Ethernet over USB set up. Andrew's guide explains what they do and also the other OTG options available.


sudo BRANCH=next rpi-update

echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txt

echo "dwc2" | sudo tee -a /etc/modules

echo "g_ether" | sudo tee -a /etc/modules

echo -e "interface usb0 \nstatic ip_address=169.254.64.64" | sudo tee -a /etc/dhcpcd.conf

Now Ethernet over USB should be set up.

Next RealVNC server needs to be installed.  The guide for installing the latest Alpha that supports Minecraft can be  found here. https://github.com/RealVNC/raspi-preview

With the relevant commands being:
curl -OL https://github.com/RealVNC/raspi-preview/releases/download/5.3.1.18206/VNC-Server-5.3.1-raspi-alpha1.deb

sudo dpkg -i VNC-Server-5.3.1-raspi-alpha1.deb

sudo systemctl enable vncserver-x11-serviced.service

These install RealVNC server and enable it as a service that starts at boot up.

Shutdown your PiZero using
sudo shutdown 0

Unplug the cabled.
Plug single microUSB cable into the USB port on the PiZero and the other end into your laptop USB port. The PiZero uses so little power it can safely be powered from the laptop USB with no issues.
Mine laptop is running Windows 10 and the device is automatically detected and the drivers installed.  I didn't have to do anything.
This creates a new Ethernet device.

Download and install RealVNC Viewer 

Run RealVNC Viewer on your laptop and enter the fixed IP address of 169.254.64.64 used when setting up the PiZero.
It will ask for your login. On mine it defaults to the laptop username. so, just change to pi/raspberry assuming you haven't changed it.

RealVNC should now login to the PiZero.
It's that simple.

RealVNC recommend some optimisation that I definitely found to improve performance.

On the VNC Viewer toolbar (top of the Window), click the Options button.
Click Advanced..., then the Expert tab.
Set PreferredEncoding to JPEG, AutoSelect to False, and ColorLevel to Full

These changes really make RealVNC smoother.

If using Minecraft you have to make one more selection.
Press [F8] to go back to the menu and select Relative Pointer Motion
Then start Minecraft as normal.
All should work perfectly.

You will notice that the RealVNC window is quite small this is because there is no resolution details set by a display so the Raspberry Pi defaults to 640x480 as the safest mode.

To increase the resolution you have to make some minor changes to config.txt
RealVNC give full details on this on the following support page

But to put it simply to set the resolution to 800x600 do the following

Open a terminal window
cd /boot
sudo cp config.txt config.txt.backup
sudo nano config.txt

This makes a backup of config.txt and then opens it with nano

Remove the # in front of hdmi_force_hotplug and set it to 1
hdmi_force_hotplug=1


Enter the following line into config.txt

hdmi_ignore_edid=0xa5000080


No idea what it does and even RealVNC describe it as better safe than sorry.  I've run without it and it worked.

Remove the # in front of hdmi_group and set it to 2.
hdmi_group=2

Remove the # in front of hdmi_mode and set it to 9 which is the value for 800x600.
You can look up all the values and adjust according to your needs on the elinux.org page

Press [CTRL]-[x] to exit.
Select y for saving changes and then press [Enter]

Now your config.txt is up to date.

Restart the Raspberry Pi and it will restart with a resolution of 800x600 rather than 640x480 making everything a little easier.
If you want a higher resolution then hdmi_mode=16 is 1024x768 and hdmi_mode=39 is 1360x768

Finally, here is a (low quality recorded from my phone) video of it all running. You can see the speed is good and even programming in Python works well.