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() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent arg0) {
if (arg0.getState() == PinState.LOW)
{ commandShutdownPi(); }
}};
final GpioPinDigitalInput pinShutdown = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_DOWN);
pinShutdown.addListener(shutdownListener);

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

Smart Home V1 - Do it yourself

I've been planning my smart home for some time now. Besides the cost factor for all the great things I imagine, I often lack the necessary time for detailed planning and implementation. So I decided to start somewhere and write down this DIY project in parallel...let's see where it leads...I ask for your indulgence if … Continue reading "Smart Home V1 - Do it yourself"


My new 3D printer and housing from Ikea side table

I had the intention to buy myself a 3D printer ages ago. However, this project failed until the end due to bad reviews I had to read about various models and their price-performance ratio. This changed when I discovered the at Amazon. This is rated almost completely positive and was reduced from 499,- to 399,- … Continue reading "My new 3D printer and housing from Ikea side table"


Window child safety locks for a safe home

A very big source of danger for children are windows, because the own child climbs from time to time with much skill on the window sill to look outside. An opened window, or one which can be opened easily, quickly becomes a threatening danger to life and limb if no appropriate precautions are taken. Security … Continue reading "Window child safety locks for a safe home"


Telegram Messenger and the Bot API - Part 2

In the first part - Telegram Messenger and the Bot API - I explained some basics how to set up a telegram bot and how to use it to send messages, states, informations and more to your . Since this tool has proven to be very practical, I decided to take up the previous article … Continue reading "Telegram Messenger and the Bot API - Part 2"


Guitar holder for the wall from the 3D printer

Guitar holders for the wall or guitar stands are a nice thing, especially if the collection of musical instruments grows over the years. Personally, I like wall mounts quite well, because they not only create elegant storage space, but are also a real eye-catcher. Acoustic guitar, electric guitar, bass, banjo or ukulele give the room … Continue reading "Guitar holder for the wall from the 3D printer"


Leave a Reply

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

I have read and accepted the privacy policy!