0

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())
                dataOutputStream.writeBytes(CMD)
                dataOutputStream.close()
                socket!!.close()
            } catch (e: UnknownHostException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            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
ADDR = (HOST,PORT)


tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
    print('Waiting for connection')
    tcpCliSock,addr = tcpSerSock.accept()
    print('...connected from :', addr)
    try:
        while True:
            data = tcpCliSock.recv(BUFSIZE)
            if data == b'':
                break
            if data == b'On':
                print('ON')
            if data == b'Off':
                print('OFF')
    except KeyboardInterrupt:
        GPIO.cleanup()
tcpSerSock.close();

0

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.

3
  • 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.