DEV Community

Controlling a raspberry pi 3 from an android app

nikolaof on August 10, 2018

Hi. I'm planning to create an android app that will send commands and receive some results to/from a raspberry pi 3 module. For example one such co...
Collapse
 
michaelgv profile image
Mike

I'm going to assume you have Arduino setup on the board? Assuming this, you can build an API server in Python that exposes some methods (see mock below), and the Android application will call it. Alternatively, bluetooth can work, but you're limited by the surrounding area (be within X distance of bluetooth device).

Let's assume you have four tasks:

  1. Turn on fan for X minutes
  2. Turn off fan
  3. Turn on light attached to fan
  4. Turn off light attached to fan

I would personally sketch the UI with giant buttons for each (Turn on Fan w/ selector, turn off, etc...), next I would mock the API as follows:

GET ROUTES:
 '/api/{version}/uptime' controller: UptimeController
 '/api/{version}/getstatus/{module}.json', controller: StatusController
 '/api/{version}/authenticate', controller: AuthenticationController (optional only if you don't intend to expose this aside over Bluetooth)

POST ROUTES:
 '/api/{version}/set/{module}/{keyindex}', controller: 'Update{Module}StatusController'

Throughout the POST route, I would create an individual controller for each module, and have three action, for example:


python
class UpdateFanController
  def getStatus(handler):
    return handler(self.online)
  def setOn(handler, for_time = 6400):
    status = FanModule.turnOnStatus(true, for_time)
    return handler(status)
  def setOff(handler, for_time = 6400):
    status = FanModule.turnOffStatus(true, for_time)
    return handler(status)
``

(apologies for the rough draft, on mobile device)

Then you just invoke it accordingly. If you're using bluetooth I recommend [this module](https://github.com/pybluez/pybluez) for it in Python, and using Android's native methods to communicate with the Bluetooth. Otherwise, if you want to do HTTPS API, I'd say use Flask since it's lightweight enough for this task. If it gets bigger, consider Django, or some other framework.
Collapse
 
nikolaof profile image
nikolaof • Edited

I appreciate your answer but I strictly have to use Bluetooth protocol for the communication and not WiFi. Also, my problem and lack of knowledge is more in the android app side and not on how to design the python API.