Emulating a Raspberry Pi on Virtualbox
So I’m working on a lot of posts for Enterprise Manager, but I can’t post them just yet, so here’s some more Raspberry Pi love to keep you busy until then… 🙂
Why VBox Raspberry Pi?
Many times I just want to write and test some code, but I want to do it while I’m taking a break from my day to day work- waiting for a download, in a meeting, a script is running in another window, etc., so it’s convenient to have a Virtualbox image to run Python scripts with all the libraries and modules installed. I can’t simulate my Pibrella card or other add-ons, but I can install the libraries and verify that all my code tests out syntax wise before I run it on the actual Raspi.
Now the “noobs” install of Raspian, (Debian) won’t work for a direct Vbox image. It’s made for ARM processors and Virtualbox is expecting an OS to support an X86 processor. This just means we need to be more selective in the OS version that we use, so we’re going to use Ubuntu or other supported platforms for our image, then add the Raspian on top of it. You could use an emulator like Qemu, that emulates the hardware, too, but I just want a simple Vbox image and be done with it.
OVA File Already Built
If you just want to use an Ubuntu image, already built, you can locate that here and that’s what we’ll do for this example- use the pre-built image and then add what we need to it!
The OVA fill need to be extracted from the bit torrent file using 7Zip or another file management program after it’s been downloaded.
Once this is done, open up Oracle Virtualbox and choose to “import appliance” option with the file, keeping all the defaults.
The Username/Password is rpi/password and once you login, open a folder and right click, choose “Open in Terminal”. You can then change the directory from the default to debian-
cd debian ./launchDebian
Expect some odd errors to come up- this is normal for this image and can be disregarded.
Once the errors complete and the prompt stops at the “Same machine: preferring raw encoding” statement, then go back to your folder and right click again, opening a second terminal.
Type in the following:
xtightvncviewer localhost
Username/password is root/password and you’ll find that you are now working from the Raspberry Pi command line, just as if you were on the machine!
Pibrella Module Installation
Installing the Pibrella Libraries are pretty easy. Just do the installations and you are set. Start with running the update:
sudo apt-get update
Then run the upgrade:
sudo apt-get upgrade
Now, let’s install the Pibrella modules:
sudo apt-get install python-pip sudo pip install pibrella
Once completed, now I have all the pibrella module installed and can run code. We can run a quick test, too.
vi python_test.py
import pibrella pibrella.buzzer.fail() pibrella.light.pulse(0.2) from sys import exit exit()
Save the file and then execute it:
sudo python -i pibrella_test.py
No, the lights and buzzer, or button commands won’t work on the simulator, but I can test my code to verify that syntax is correct and get it ready before moving it over to the actual Raspberry Pi!
Python 3 Addition
As most folks know, I’m a Python addict, so I also want to update to Python 3 on this image.
sudo apt-get install python3
Now I have everything I need to simulate my Raspberry Pi to run scripts from my PC!
Have a great day and Pi on!
I found this article thoroughly enjoying. However, there is one more piece that would make it complete..how do I make an image for the Raspberry Pi from this emulated environment ?
As the ARM processor is different than the VMbox image we’re emulating on top of the Linux environment, the easiest way is to simply download Win32 Disk Imager. You can use this to make backup images and to “expand” images back onto a micro SD Card. The downloads of different OS images can be first received from the following site, (as well as others!) https://www.raspberrypi.org/downloads/
can this be used to test some code that works with GPIO?
Unfortunately, there’s no way to attach the GPIO to a VM, so no. The GPIO and other component libraries fail upon testing.
is there any way at all to simulate the GPIO?
or at least, write some code in python and compile it, to know that there are no syntax errors? (i’m kinda new to python)
Python has an interpretive compiler, meaning that it compiles at the time of execution. If you use idle or another coding app that can assist you with syntax errors, that can help, but this wasn’t made to simulate GPIO. It was created to get folks prepared to invest in a Raspberry Pi. If this is a serious impact to your projects, you may want to look into an adruino, which is designed for GPIO and not a single board computer.
Hello,
in order to be able to do apt-get update, I changed the content of /etc/apt/source.list to below:
deb http://archive.debian.org/debian-archive/debian/ squeeze main contrib non-free
deb-src http://archive.debian.org/debian-archive/debian/ squeeze main contrib non-free
ok, then i guess i’ll just work when i have the pi available. Thanks for your quick reply
Thank you!
When I start the emulation the window size is just a small horizontal bar. How do I fix this?
Pingback: How to Create a Raspberry Pi Virtual Machine (VM) in VirtualBox – Grant Winney
Hi, after launching xtightvncviewer it loads, but it does not accept any keyboard input. I.E., I can’t login at the login prompt.