Saturday, May 25, 2013

raspberry pi and android simple remote control

The goal

Raspberry pi control using android application.

Usability

High. Can be part of intelligent house project.

Code


Hardware


Use case

  • At the end of this page

Media

Client and server running example


Example use: IR emulator control


Example use: IR emulator control

Description

Java client sends an action event (demand button state change) to Perl server script. Perl executes commands which are attached to events (defined in XML) and returns its stdout to socket. Java client receives answer and allow or disallow for button to change its state. Java client also fetch states of buttons and their names, this is separate process not related directly with button change event.

Pseudo communication can look like this:

java > What is the name of button B1 ?
perl > The name is  "On/Off".
java > Ok I'm setting new name.

java > What is the state of button B4 ?
perl > The state is OFF
java > Ok I'm setting button B4 to OFF.

java > I want to change B1 button state to ON
perl > Wait, I'm executing a command. Ok, change it

java > I want to change B1 button state to OFF
perl > Wait, I'm executing a command. Nope.
java > Ok, buttons still is in ON position.

Client


Client was written in Eclipse on android-sdk. Application is very simple it has only one screen (activity), no settings panel, just 4 buttons, 4 buttons descriptions and one additional to display command status.

Java client application view
In early state, there was only one screen (and 4 buttons on it), now by adding gestures, it's possible to change 'virtual screens' from 0 to integer variable range.

When user makes a gesture on screen, for e.g. slide left, screen prefix will change +1, screen prefix is also button and their description prefix. So If, in XML, will be defined 25 buttons, then we have 7 screens. On 6 we will be having 4 buttons and 7 screen will have only one button.

Sliding by +/-1 is slow for more than 25-30 buttons, so upper 1/5 of the screen has different factor, when we slide it there, screen prefix will change by +/-5.

Screen change factor highlighted
When we double tap on the screen, button prefixes, their descriptions and states will be refreshed.

When we are on 134 screen and we want to go back to 0 screen, we only need to press on the screen some longer time.

Every action (button press, slide) triggers new connection to server, so when we don't press or change screens, application is idle. Nothing runs in the background.

Screen lock is disabled when application is running.

Server


Server is written in Perl. Script loads XML into memory and then when command arrives, it searches XML hash for matching button prefix. If it find a match, script executes shell command assigned to this button (in XML) and returns its output (not return code) to client by connected socket.

Client is fully configured from server configuration file 'server.xml'. This file has structure like below:


            
      echo "OFF"   
      
         i2cset -y 1 0x10 3 ; echo "OFF"    
         echo "OFF"                       
      
   

   
      echo "OFF"
      
         i2cset -y 1 0x10 1 ; echo "OFF"
         echo "OFF"
      
   

   
      echo "OFF"
      
         i2cset -y 1 0x10 2 ; echo "OFF"
         echo "OFF"
      
   

         
      echo "OFF"   
   

When button is not defined, the <default> section is used to get name and state.

Above 'server.xml' example is kind of special, because buttons will not act like ON/OFF switch, rather they only fire shell command defined in XML. This configuration file is part of IR remote emulator, described in 'Use case' section of this post.

Normally instead of echoing everywhere "OFF" we would do something like this:
   
      if [ our_custom_script_get_something_state ]; then echo "ON"; else echo "OFF"; fi;
      
         if [ our_custom_script_set_something_on ]; then echo "ON"; else echo "OFF"; fi;
         if [ our_custom_script_set_something_off ]; then echo "OFF"; else echo "ON"; fi;
      
   

our_custom_script_get_something_state: this script returns 1 if 'something' is on and 0 if its off

our_custom_script_set_something_on: this script tries to turn on something, when succeed returns 1 otherwise 0

our_custom_script_set_something_off: this script tries to turn something off, when succeed returns 1 otherwise 0

Example communication


Get button name:
java > GET NAME B4
perl > Mute

Get connection state:
java > GET PING CONN
perl > PONG

Get button state:
java > GET STATE B1
perl > OFF

Set button state. Button was in false (OFF) state, now we change it to true (ON), and command executed on shell returns "ON", button state is changed from OFF to ON.
java > SET STATE B3 true
perl > ON

Set button state. Button was in false (OFF) state, now we change it to true (ON), and command executed on shell returns "OFF", button state is not changed and remains OFF.
java > SET STATE B3 true
perl > OFF


Example use

I use my IR remote emulator with attiny45 connected to Raspberry Pi like on picture below, to control my stereo. Video is on top of this page.

IR remote emulator connected to Raspberry Pi by I2C interface

5 comments:

  1. I just through about controlling yamaha amplifier like that, and yet again , Igor has already done that... :)

    ReplyDelete
    Replies
    1. Our lazyness is similar ;)
      btw. I'm planning to open up my yamaha and try to connect directly to cpu ... we'll see, maybe this emulator won't be needed, and you could connect directly, by wifi :)

      Delete
  2. sir,i have an idea to built an android app that control smart Tv by writing the channel number on the app.can you please provide any guidelines how to built the writing gestured app and to make connectivity to smart Tv's.?

    ReplyDelete
    Replies
    1. Hi, I have no experience with smart TVs. I think that they can be controlled over the network, do some more research on the net you should find something useful.

      Delete
  3. Hello, am in need of your assistance.. seems you might understand what help i need, am working on my finals project and its got me twisted with my head. am working on a mobile app that i would like to use as a remote device to connect to my computer and emulate the idea of mouse control to the system. As you have done with the stereo system... please i have limited assist on this and any help will be appreciated.

    https://www.youtube.com/watch?v=gsb3ZoaWSh8&t=145s

    the link above is the idea of the project i need to finish working on. Thank you

    ReplyDelete