0

I am creating an app that will run a program on a raspberry pi. Right now, the program outputs everything onto the console and I want to capture that information and display it on the android app.

So far, I have a TCP connection between the android app and the raspberry pi (python server) and can send a command, run the command and receive some text back (decided by the python code).

What I am looking for is:

  1. how can I get the python server to send the console output back to the app?
  2. how can I display that on the android app?

For example, the console output will say

 ENSEMBLE 6
 Average accuracy on test data: 96 +/- 1.8%
 Average accuracy on training data: 95.8 +/- 2.1%

 ENSEMBLE 7
 Average accuracy on test data: 98 +/- 1.8%
 Average accuracy on training data: 97.8 +/- 2.1

 ...

I want to display the accuracy numbers in different line plots on the android app. How can I do this? What techniques will I need to learn in order to implement this on the app?

The python code:

#coding=utf-8
import socket
import time
import sys
import os



HOST_IP = "192.168.1.1"    #My Raspberry Pi as the IP address of the AP hotspot
HOST_PORT = 7654            #The port number

print("Starting socket: TCP...")
socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    #Create socket

print("TCP server listen @ %s:%d!" %(HOST_IP, HOST_PORT) )
host_addr = (HOST_IP, HOST_PORT)
socket_tcp.bind(host_addr)    #Binding my Raspberry Pi's IP address and port number
socket_tcp.listen(1)   
# The parameter of the #listen function is the number of listening clients. Only one is monitored here, that is, only one client is allowed to establish a connection.

while True:
        print ('waiting for connection...')
        socket_con, (client_ip, client_port) = socket_tcp.accept()    #Receive client's request
        print("Connection accepted from %s." %client_ip)

        socket_con.send("Welcome to RPi TCP server!")    #send data

        while True:
                data=socket_con.recv(1024)    #Receive data

                if data:    #If the data is not empty, print the data and forward the data to the client
                        
            print("Command executed: " + data)
            os.system(data)
            socket_con.send(data)

socket_tcp.close()
  • I think this covers capturing output from python subprocesses: https://stackoverflow.com/a/4760517/10761353 Also, consider *not* passing the request directly to `os.system(cmd)` (what if someone saw this code and sent your Pi the input `sudo rm -rf /`??) – Adam Smooch Aug 04 '21 at 15:01

0 Answers0