I used a code that works when I want to send data from my mobile application(android studio) to the raspberry, but when I tried to update it so it can work for both connections, It doesn't work. How can I use tcpSerSock.send to send my data from my raspberry pi? or if there is another function I can use, what is it? and how to use it? Thank you :) .. here is my code for both:

This is the Android app Function:

    inner class Socket_AsyncTask : AsyncTask<Void?, Void?, Void?>() {
        var socket: Socket? = null
        protected override fun doInBackground(vararg params: Void): Void? {
            try {
                val inetAddress = InetAddress.getByName(wifiModuleIp)
                socket = Socket(inetAddress, wifiModulePort)
                val dataOutputStream = DataOutputStream(socket!!.getOutputStream())
            } catch (e: UnknownHostException) {
            } catch (e: IOException) {
            return null

This is the Python code on the Raspberry pi:

from socket import *
from time import ctime
import RPi.GPIO as GPIO

HOST = ''
PORT = 21567
BUFSIZE = 1024

tcpSerSock = socket(AF_INET, SOCK_STREAM)

while True:
    print('Waiting for connection')
    tcpCliSock,addr = tcpSerSock.accept()
    print('...connected from :', addr)
        while True:
            data = tcpCliSock.recv(BUFSIZE)
            if data == b'':
            if data == b'On':
            if data == b'Off':
    except KeyboardInterrupt:


It may just be me, but the python script you posted above is listening to the socket.

If I understand correctly, you have a sensor attached to your Pi and you wish to get the sensor data from the Pi to your android app.

There are several steps:

  1. Something on the Pi needs to read the sensor data or have the data pushed to it;
  2. The same something needs to push the data to a socket; THEN either: 3a. The socket pushes the data to the android app (Pi is the master); or 3b. The android app reads the data from the socket(Pi is the slave).

Your example app, above, opens a connection sends data and closes the connection.
But if you are reading pushed data (case 3a) you may need an app with a continuously open connection so that it can read data updates as they arrive. Will that work with your data tariff in your use case?

Alternatively if your android app is pulling the data, how will it be held on the pi until it is read?

First, I think you need to think about the logic of the data flow, decide whether the Pi is the master or slave of the data flow. then you can code a solution that works.

I hope that makes sense.

  • Thank you!The codes posted are the codes used to send from my android app to my raspberry and its working, My question is how could I do the opposite (send from raspberry to android app) ?
    – Emma
    Jun 3 at 13:29
  • I want to send sensor readings to the android app
    – Emma
    Jun 3 at 13:30
  • Updated answer to reflect comments
    – Falstone
    Jun 3 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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