0

I am currently working on a project where I have to develop a Camera Security system and an android application. The camera system provides live streaming videos and also has the ability to detect motion. To activate the motion detection functionality, I have created an Android application that contains a button that, when clicked, should run the motion detection Python script on the Raspberry Pi. I am having problems figuring out how to run this python script present on the Raspberry Pi from the mobile application I have developed. I have been researching for a solution to this problem for about a week now, but I still haven't got any solutions. Any would be appreciated. Thanks in advance.

3
  • 1
    run a webserver/API on the Pi that will run the python script then call it from your Android app? Commented Feb 25, 2022 at 4:21
  • Write code to accept a socket connection to interpret commands. (This is basically what a web server does.)
    – Milliways
    Commented Feb 25, 2022 at 5:01
  • @SteveRobillard link is that what you meant? Commented Feb 25, 2022 at 6:29

2 Answers 2

1

There are several ways of doing this. You can use web api's (commonly called rest api's) and have the software on your pi receive HTTP requests (search webserver in x language). You can raw TCP or UDP connections (search listen on tcp/udp port in x language) and communicate over a socket. You can also use blutooth and communicate that way, however that may be more complicated then you might think.

Depending on your need and usecase, you may find projects like XMPP and matrix (https://matrix.org) useful as you don't have to run a server on your pi, allowing for easier communication across different networks. While matrix is primarily used as a chat system, it was designed to allow many types of communication to be used on top of it such as IoT systems. XMPP is more complex then matrix to use in my opinion, but may be slightly more flexible.

0

Updated 10/03/22 - see added comments at end of main body of text

I suggest you consider MQTT (mqtt.org) which is a lightweight publish subscribe message transfer protocol. I have used it successfully to transfer alerts from a Raspberry Pi to an android device. There are lots of different MQTT apps on android and I had to experiment to find one that worked for me. Hopefully there is one which works in the other direction. MQTT has standard linux apps on the Pi and should work fine if you can find a suitable android app do deliver a message to it.

I managed to connect from my android phone to my Raspberry Pi using MQTT. I installed the MQTT Broker mosquitto and the MQTT client library python3-paho-mqtt. sudo apt install mosquitto python3-paho-mqtt The Mosquitto broker is automatically started after reboot but doesn't allow connections from local network by default so I created a file in /etc/mosquitto/conf.d named network.conf and containing the two lines

allow_anonymous true
bind_address 0.0.0.0

After rebooting I was able to connect to the MQTT broker on the pi via my local network using various android MQTT apps such as 'MQTT Dash' and 'IOT MQTT Panel' and 'publish' some test information. I used the following program (found on another website which I can't find again) on the pi to subscribe to the MQTT service and receive a message from 'MQTT Dash' on topic 'android'. The program runs in a loop waiting for messages to be received. I am not sure how exactly these apps on android are intended to be used - the 'MQTT Dash' android client seems to need programs running in javascript to process received data but it also supplies capability to publish data in some forms.There must be a MQTT library in android see for example https://medium.com/swlh/android-and-mqtt-a-simple-guide-cb0cbba1931c which can be used to publish messages. However I know for certain that I was able to establish connection from android to Raspberry Pi via MQTT

I am not an Android developer, but my understanding is that Android apps are usually written in Kotlin, Java or C++ - not usually python though this may be possible. In any case to use MQTT on Android you have to use a MQTT client library. Search for 'MQTT Client Android'. Here is a link to one example. All the MQTT apps downloadable for android will of course be using such a library and perhaps Java is the most convenient language to use as it so well known and (pretty well) standard.

https://hivemq.github.io/hivemq-mqtt-client/docs/installation/android/

Good luck !

# python3.6

import random

from paho.mqtt import client as mqtt_client


broker = 'localhost'
port = 1883
topic = "android"
client_id = f'python-mqtt-{random.randint(0, 100)}'


def connect_mqtt() -> mqtt_client:
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    client.on_connect = on_connect
    client.connect(broker, port)
    return client


def subscribe(client: mqtt_client):
    def on_message(client, userdata, msg):
        print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")

    client.subscribe(topic)
    client.on_message = on_message


def run():
    client = connect_mqtt()
    subscribe(client)
    client.loop_forever()


if __name__ == '__main__':
    run()
1
  • Hi, Thanks for your reply and sorry for my late reply. I have done some researched about the MQTT server. But how do I make to connection from my own mobile application. The resources that I have found showed me to download an application from playstore. Commented Mar 10, 2022 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.