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 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, 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

AmazonRelatively 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.

Amazon

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 in my mid-30s and a software developer and martial artist from germany. Besides programming and martial arts some of my interests and hobbies are cooking, fitness and hearing loud heavy metal music. :D

Related postings

self-construction - DynDNS-Alternative

If you already have your own webserver/webspace on the Internet and some programming knowledge, you can save yourself a service like DynDNS.org & Co. without much effort if you want to access parts of your home network from the road. Such services provide a host name/URL (e.g. my-home-network.dyndns.org) which is then forwarded to the IP … Continue reading "self-construction - DynDNS-Alternative"


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"


Playstation 4 / PS4 Pro Mods from the 3D Printer

The Playstation 4 was introduced in 2013 and has since been sold by Sony well over 94 million (as of January 2019) times. The slightly more powerful PS4 (Playstation 4 Pro, resolution: 3840 × 2160 and higher graphics and CPU clock rate) was launched at the end of 2016 and is still the flagship of … Continue reading "Playstation 4 / PS4 Pro Mods from the 3D Printer"


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"


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"


Leave a Reply

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

I have read and accepted the privacy policy!