DEV Community

nikolaof
nikolaof

Posted on

Controlling a raspberry pi 3 from an android app

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 command could be to enable a fan for 3 mins. For these three mins (while the fan is running) the raspberry pi will send the temperature data received from a temp sensor, in the app side, where a graph with these values will be provided.

The way I'm thinking of it:
The app is going to scan for available devices.
It will connect to the raspberry pi.
A python Bluetooth server will run on the raspberry pi site executing the commands that the android app sent to it and will send back to the app some of the data.

So far I found by a search on the internet, the Evothings framework but I would like to read your suggestions on how to build quite easily such app.

Thanks

Top comments (2)

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.