-->
Page 1 of 3

Sending binary data from esp module to java socket

PostPosted: Sun Jul 17, 2016 2:48 pm
by debojitk
Hi,
I am planning to send raw data stream from esp8266 to a java pc application that is actually a serversocket.
The code for server is:
Code: Select allpackage com.test.socket;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;

public class GreetingServer extends Thread {
   private ServerSocket serverSocket;
   private SocketAddress remoteSocketAddress;

   public GreetingServer(ServerSocket serverSocket, String name) throws IOException {
      this.setName(name);
      this.serverSocket = serverSocket;
      //serverSocket.setSoTimeout(10000);
   }

   public void run() {
      System.out.println(getName()+"->Waiting for client on port " + serverSocket.getLocalPort() + "...");
      Socket server=null;
      byte buffer[]=new byte[1024];
      int readBytes=0;
      try {
         server = serverSocket.accept();
         remoteSocketAddress=server.getRemoteSocketAddress();
         System.out.println(getName()+"->Just connected to " + remoteSocketAddress);
      } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
         return;
      }
      while (true) {
         try {
            DataInputStream in = new DataInputStream(server.getInputStream());
            if(in.available()>0){
               readBytes=in.read(buffer, 0, buffer.length);
               System.out.println(getName()+"->"+remoteSocketAddress+"->"+new String(buffer,0, readBytes));
            }else{
               try {
                  Thread.sleep(100);
               } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
         } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
         } catch (IOException e) {
            e.printStackTrace();
            break;
         }
      }
      try {
         server.close();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      //int port = Integer.parseInt(args[0]);
      try {
         ServerSocket serverSocket=new ServerSocket(8086);
         Thread t = new GreetingServer(serverSocket,"T1");
         t.start();
         Thread t2 = new GreetingServer(serverSocket,"T2");
         t2.start();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}


I wrote a simple java socket client and I am able to send data asynchronously.
The client code:
Code: Select allpackage com.test.socket;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class GreetingClient {
   public static void main(String[] args) {
      String serverName = "localhost";
      int port = 8086;
      try {
         System.out.println("Connecting to " + serverName + " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("Just connected to " + client.getRemoteSocketAddress());
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out = new DataOutputStream(outToServer);
         BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
         while(true){
            String str=reader.readLine();
            if(str.equals("exit"))break;
            out.write(str.getBytes(), 0, str.length());
            out.flush();
         }
         out.writeUTF("Hello from " + client.getLocalSocketAddress());
         client.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}


Now I want to replace the java client with a esp client. That is I want to connect to a server socket that is run as a java server socket program.
I also found a thread about communication between a c socket server and a java socket client at http://stackoverflow.com/questions/19561941/socket-comunication-java-client-c-server.
So is that possible to create a esp sketch that would open a tcp/ip socket and send raw data. Here I am not using any application layer protocol like http or ftp however I am open to all type of suggestions that would cater my requirement to send raw data stream from esp to java program.

Thanks,
Debojit

Re: Sending binary data from esp module to java socket

PostPosted: Sun Jul 17, 2016 3:24 pm
by martinayotte
Of course, simply use plain TCP Client.
In ArduinoESP framework, there is an example in libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino.
Even if this example is doing a HTTP request in a Client.print() call, it doesn't need to be that request, it can be any thing that can emulate telnet client, even AT commands if you wish, all depends of the protocol used by your server's code.

Re: Sending binary data from esp module to java socket

PostPosted: Mon Jul 18, 2016 7:50 am
by debojitk
Hi,
Thanks a lot for your help.
Just wanted to know that what might be the data transfer rate for binary stream while sending from esp module. Actually i am planning to transfer pcm audio samples from esp to android and play there. I did a poc on audio sampling using arduino and found that I can sample at 38.5 khz (using ADC interrupt- http://www.instructables.com/id/Arduino-Audio-Input/), and the sampling result is very good. I know that esp also has onboard adc so I can probably implement the same audio sampling on esp.
Can you tell me that is it really possible to capture and send data @~40kBps from esp along with doing other things.

Thanks in advance.
Debojit

Re: Sending binary data from esp module to java socket

PostPosted: Mon Jul 18, 2016 8:12 am
by martinayotte
I can't tell you the real throughput. I guest you will have to experiment to figure out.
BTW, the on-board ESP ADC isn't good for audio, you will have to use an external ADC.