pi_robot - Playing with Raspberry Pi, iPad and some Code

Raspberry Pi Homeserver

The Raspberry Pi is still one of the most popular mini computers, not only among hobbyists. Cost-effective (about 30 to 40,- EUR depending on the model), flexible in the field of application and at the latest since the Raspberry Pi 3 Raspberry Pi 3 equipped with enough CPU power and memory to solve even more complex tasks.

With the 1.2 GHz quad-core ARM Cortex-A53 CPU, 1 Gig RAM, 40 GPIO pins, Ethernet port, WLan, Bluetooth, 4 USB ports and a Full HDMI port you can have a lot of fun. My operating system is Raspbian, a Debian Linux offshoot.

Table of contents:

Playing with Raspberry Pi, iPad and some Code

I use the GPIO pins among other things for an external reset button, which shuts down the Raspberry Pi cleanly if desired, without scrapping the partition of the MicroSD card MicroSD card, which happens quickly if you simply pull the power plug. For this purpose I programmed a small Java software, which is loaded automatically when starting the Pi, and handles the events on the GPIO ports. To access the GPIO of the Raspberry Pi, I use the open source library of -= The Pi4J Project =-, which greatly simplifies access using the Java programming language.

So the switch for shutting down the Pi is connected with a few lines of code. On the hardware side, a pin (in the example GPIO_00) is connected to a voltage, and it switches the whole thing in such a way, that the voltage is pulled from 1 to 0 by pressing the button of the switch. Then you only need to set a GpioPinListenerDigital to this port and fire it at PULL_DOWN.

GpioController gpio = GpioFactory.getInstance();
final GpioPinListenerDigital shutdownListener = new GpioPinListenerDigital() {
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent arg0) {
if (arg0.getState() == PinState.LOW)
{ commandShutdownPi(); }
final GpioPinDigitalInput pinShutdown = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_DOWN);

The commandShutdownPi() function does nothing but execute the shutdown command. It couldn't be easier.

Runtime.getRuntime().exec("sudo shutdown -h now");

The program (Jar file) must then only be opened with root rights (for full access to the GPIO interface) at system startup of the Pi. This can be easily done by adding the following entry to the crontab (/etc/crontab).

@reboot root java -jar /home/pi/Desktop/Apps/pi.jar &

Raspberry Pi SmarthomeIn the pi.jar together with the different GPIO pins I also realized other gadgets, like for example an IR reader and transmitter (infrared diodes at GPIO_02 and _03) to receive commands from the remote control of the TV and to emulate remote controls (fo example Remote Control of the DVD player).

For this there are already all kinds of libraries and tutorials on the Internet, just like for radio transmitters (RFM12B), which can be used to remote control e.g. radio light switches and sockets. Have a look at the project pages of WiringPi and Pi4J. There are several code examples to realize this. Also check out my other smarthome projects.

HomeServer, MediaCenter and SmartTV

kraken.jpgRelatively simple and fast, the Raspberry Pi can also be transformed into a home server, a media centre or smart TV. There are also innumerable images on the subject, which you only have to install, but making your own is the nicer alternative. To keep the effort as low as possible, I use the free OMX player for Linux or the Raspberry Pi version to play MP3, MP4, AVI and other audio and video files. In combination with the Python program Livestreamer not only local videos and music can be played, but also live streams from the Internet.

In the end my small program pi_robot.jar consists of 2 essential modules. On the one hand there is a server client module. This listens on port 4444 and accepts connections from the LAN of the iPad (see below), receives control commands like playing movies and forwards them to the second module, the actual robot, which executes the commands and delivers a result.

For the communication between iPad and Raspberry Pi I decided for the time being for a plaintext transmission in CSV format (semicolon as limiter). To start a movie from a hard disk, I send a command like "openmedia;/path/to/videodatei.avi;2" from the iPad to the Pi. The first part of the command is the actual control command, the second part is the absolute path to the video file, and the third part is passed with 0, 1 or 2 and specifies the audio port to be used during playback. (0=HDMI, 1=AUX, 2=both). All very simple and simple.

The finished server software pi_robot.jar is also started from the crontab like the GPIO handlers @reboot. However, I have added an export DISPLAY here, so that I can access the keyboard and mouse via the java.awt.robot(), and the corresponding drivers are loaded even if no keyboard or mouse is connected to the Raspberry Pi. Especially if you want to connect the Pi directly to your TV in your living room, this makes sense. Who wants to have keyboard and mouse lying around in the living room?!

Starting pi_robot.jar from /etc/crontab
@reboot pi export DISPLAY=:0.0 && java -jar /home/pi/Desktop/Apps/pi_robot.jar &

iPad-App for controlling the Raspberry Pi

Due to the fact that I don't want to have a keyboard and mouse on the Pi, I decided to bore the software so that it can also emulate keyboard and mouse inputs.

kraken l.jpg

Raspberry Pi iPad KeyboardIn this case, the iPad should take full control of the Pi with all its functions.

The control of the web browser, the volume control of the pi, and all OMX player functions (louder, quieter, forward, back, pause, etc...) have also been integrated.

So that the cigarette box sized computer now serves as a home server, media center, SmartTV and much more.

Mediacenter with Raspberry Pi and iPadAt the moment I'm doing some fine-tuning. In the "Miscellaneous" tab of the iPad app I have integrated a function for shutting down the Pi, as well as some buttons for fast web access to popular websites.

Since there are still some small troubles and a password protection should still be integrated, I will not publish the app and the control software for the Raspberry Pi at this time.

I've also integrated a small messenger module to send messages via telegram, which I'll also introduce if necessary. I'll follow and refine the project as time allows, and then publish it here. Feedback, ideas, questions or remarks to this project you can post in the comments. With you happy coding. 🙂

Author: Sascha from Tinkering-Sascha.com

Author: Sascha

Some words about myself. My name is Sascha - i'm a software developer, trader and martial artist from germany. Besides programming, trading and martial arts some of my interests and hobbies are cooking, fitness and hearing loud heavy metal music. :D

This might also be interesting for you

Meditation Bench - Do it yourself

A pleasant upright posture is enormously important when meditating. As already described in a previous article (Introduction to Meditation), there are a variety of aids such as meditation benches, meditation stools or . These make the meditation phase pleasant and comfortable, whereby a simple woollen blanket or similar is sufficient for some. Especially a has … Continue reading "Meditation Bench - Do it yourself"

3D Printers: Models, Software and Options

Not just since yesterday 3D printers are a great way of printing on the market, which is constantly evolving. Here it is possible to print whole objects using software or scanned originals. This process has proven to be extremely useful both in private use and in numerous professions. In this article I have collected some … Continue reading "3D Printers: Models, Software and Options"

Kids toys and other useful things from the 3D printer

Many projects and ideas have already been presented here in the blog, which can be realised quickly and cost-effectively thanks to 3D printing. It's impossible to capture and collect everything that creative minds around the world come up with. Some funny toys for children and also helpful templates for the 3D printer from the field … Continue reading "Kids toys and other useful things from the 3D printer"

Build your own family bed

When we had prepared the children's room of our daughter including wardrobe and own bed, we assumed at that time that she would also sleep there. After some time, however, we noticed, as many parents before us have, that this was simply not the time yet. Be it through thirst, hunger, toothache or night scare, … Continue reading "Build your own family bed"

3D Printing - 27 Inch iMac Stand Repair

As already known for a long time, the stand of the iMacs (27 inch) from 2012 to mid-2014 is often broken. With these models it happens (not all of them) that the hinge is worn out and the screen position can't be changed anymore or, as in my case, the screen constantly tilts forward. 3D … Continue reading "3D Printing - 27 Inch iMac Stand Repair"

Leave a Reply

Your email address will not be published. Required fields are marked *

I have read and accepted the privacy policy!