How to interract android applications with a raspberry PI ...? - android

I can program my Raspberry PI to control its GPIOs using Python or Wiring-PI C library.
I can control it with web interfaces like webiopi or web2py.
What I intend to do is write my own android app (layouts created by me only) and want this app to remotely acces the PI and run the code stored for the GPIOs control.
I want my Java code to control my C code remotely or something like that.
I have no idea what kind of libraries I need for this kind of interaction and I would appreciate any ideas.

As you can control your Raspberry Pi with web interfaces like webiopi or web2py, why not use REST API from your Android application.
REST API Client Library for Android
Restful API service
If you are looking for more ideas you may consider Bluetooth client server application with an Android client (on device) and Java/Python server (running on the pi)
Initializing bluetooth connection android(client) to python(server) on pc

you'll need to setup a server on your raspeberry pi to receive GET or POST calls.
Then from your Android app use all the normal, standard Java to call this web service and get a suitable response, e.g. GPIO status.
for example android App use a GET request to http://192.168.0.10:8080/GPIO/Toggle/3 your raspberry Pi server, listening to port 8080, will toggle GPIO 3 and reply back to Android 00001000 (meaning that GPIO 3 is On).
But that's just one possible idea, you could use a library like Kryonet to communicate over traditional sockets.

Related

Communication between Raspberry pi and Android studio Application

I've created a mobile application using android studio to control some leds and sensors, so I'd want to connect my mobile application to my raspberry pi to send and receive data, is that possible? i.e. I want to write a code in the raspberry pi (ex:connect.py) and my mobile application that connects the two together.
Congrats on stepping into IoT!
The best way to get this done is by connecting your mobile application to a database and the same database to your raspberry pi as well.
The raspberry pi will be listening to the changes made on the database and trigger the functions like sending an email or moving a robo arm accordingly. The database is only responsible to act as a bridge between the app and the microcontroller.
And how do you connect to the database you may ask? You use APIs for that on both the client sides.
The mobile app can be used to send instructions like switch on the motor or receive data logs from the MCU. Again, the database is where each is connected.
You can check out one of my video demos on this at https://www.linkedin.com/feed/update/urn:li:activity:6731591459058839552/
Happy coding! :)

Communicate between two (and more) Flutter applications using flutter_bluetooth_serial

I'm developing Flutter app for Android, where I want to exchange some data between few instances of my app on different devices via Bluetooth. I'm using flutter_bluetooth_serial package (this is the only package I found which supports Bluetooth Classic, not Bluetooth Low Energy). I'm able to make connection
between device 1 and device 2 using BluetoothConnection.toAddress(device.address); (but even there was a problem, I had to modify the plugin and apply this workaround to make connection work).
Now I want to run my app on device 2 (which is connected with device 1) and send or receive some data, but I have no idea how I can do that using flutter_bluetooth_serial. In device 1 where I create connection I can use result of BluetoothConnection.toAddress(device.address) to send or listen for some data, but on device 2 I can't use it, because connection is already established by device 1 and I can't see another API for communication.
To simplify: let's say I want to achieve something more or less like Bluetooth chat functionality in this example application, but between multiple Flutter applications, not Flutter app and raspberry pi.
Is there any way of solving it with use of flutter_bluetooth_serial or any other package? Or I have to write some native Android code?

Interface between Raspberry Pi and Android

I am trying to do some wacky home automation which will require me to send a signal from my Android phone to my Pi 3 in order to execute a script to control a motor using the GPIO pins.
The only part I'm stumped on is the best way to connect the Android and Pi.
I've read so many different things and it's all overwhelming, the amount of differing answers I've seen.
So far I'm leaning towards using Jsch in my app to ssh into the Pi and execute the command, but I have been told this is silly.
Can anybody explain to me why this is a bad idea and explain to me a better one? Ideally the phone app would be able to connect over both local network and other networks.
Nothing is wrong with SSH, but people typically use web servers on the Pi plus HTTP requests on the Android side. Or you can run your own protocol via a raw socket connection.
SSH commands might be more secure if you use SSH keys.
Otherwise, you'd be exposing your commands to anyone snooping on your internet traffic, and random people will be controlling your devices
Either way, if you want access both internal and external to home, you can do more research to see if you can "port forward" your router
The advantage of using HTTP for this sort of thing is that it potentially creates its own user interface. What I mean by this is that, if the interface on the Pi is a Web server, then you have a way to provide an HTML/JavaScript interface to your Android device, and thus avoid the need to create an Android app at all -- the user just needs a browser. The whole user interface is managed from the Pi.
I've used this approach for motor control on the Pi a fair bit. On the Pi I use a C program that embeds the libmicrohttpd webserver engine. The program can serve out ordinary HTML pages to create the user interface on the browser, or respond to particular HTTP GET requests that result from the user clicking buttons or manipulating sliders or whatever. You can do some really sophisticated stuff by sending JavaScript functions that make their HTTP requests outside the normal HTTP request/response flow, so you can (for example) have a browser display that updates dynamically (e.g., display sensor values from the Pi).
Moreover, it's easy-ish to provide some kind of security using SSL and HTTP authentication. I prefer C, but there are webserver libraries for Python that work on the Pi as well.
To my way of thinking, the only time it's worth considering something more complex than this is when you need a user interface on Android that can't be implemented in HTML/JavaScript.
I'm sure there are many different ways to do what you want. I prefer to do most of the work on the Pi, because I find writing Android apps deeply unrewarding. On the other hand, if you like developing for Android and have plenty of experience doing so, the approach you suggested -- sending commands to the Pi over SSH -- could work perfectly well. It would just mean doing most of the work in Android.
Yes, ssh is silly solution. I suggest to develop rest api webservice, host it on your PI and invoke it from your Android app

Sending a command to Raspberry PI using an Android App

I'm currently working on a small project of simply controlling the lights in my apartment. I have a Raspberry Pi setup to control the lights with a 433 Mhz transmitter. I can send on/off commands to the lights using a "send code " command through the command line on the PI. I'm looking to create an Android App to do this for me, and I'm looking for a way to make this happen.
The Raspberry Pi is always connected to the internet, so I was thinking about hosting an Apache Webserver on it, and then somehow sending it commands from the app. Would anyone know how to go about doing this? I have little knowledge about Apache or webservers to begin with, but my basic google searches have pulled up ideas about using http posts.
Would figuring out how to use an HTTP post be the best course of action, or is there an easier way?
There are many options but probably some Python web framework running on your Raspberry PI will do the job (Twisted, Django or many other).
Python is preinstalled in the PI so it could be the easier choice.
Your Android app will then run HTTP requests (using HttpClient or something more elaborated) to the Python web server that will run in response the bash code.
The real problem comes when you will want to secure your end point (via login or whatever secret sent via Https) to be sure you are the only one switching on/off your lights :)
Personally I'd use an SSH library such as jsch to create a simple android app that sends the desired commands to your raspberry pi, on the press of a button. Of course, if you need to learn how to make an android app, have a look here first.

"Call" Android app from server using Ajax

I wish to create a Cordova/Android app that will be called/executed from my server. Both devices are on the same intranet and connect via WiFi, therefore no cloud messaging required .
For another scenario I have done the opposite. i.e. Android calls, via Ajax, an application on a Raspberry Pi for door opening purposes but this time I want to call an Android application from a Raspberry Pi. Where do I start? Ajax? Socket Server? What is the recommended connection method?
I think that websockets are the way to go. Implementing such a real time application allows you to 'push' data from the server to connected clients.
This can be initiated by clients (like in a chat application) but also by the server based on whatever events.
Have a look at Primus as a possible implementation. It abstracts the functionality from the websockets layer. This way it is possible to work with different websocket implementations.

Categories

Resources