User avatar
By leoneazevedo
#47969 Hello everyone!
I am trying to connect my ESP to a socket server using java.
But I'm having problems in comunication, the esp not complete the comunication and the server stay waiting for :( ...
Can someone help me? Already thanks!

This is the client side, the esp8266 code:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>

const char* ssid = "myssid";
const char* pass = "mypassword";
const char* host = "theIPwhereJavaSocketis";

void setup() {
  delay(500);
  Serial.begin(38400);
  Serial.println();
  Serial.print("conecting to: ");
  Serial.println(ssid);
  Serial.println("Try to connect to server: ");
  Serial.println(host);
 
  WiFi.mode(WIFI_STA);
  delay(1000);
  WiFi.begin(ssid, pass);
  //IPAddress subnet(255,255,255,0);
  //WiFi.config(IPAddress(192,168,1,150),IPAddress(192,168,1,10),subnet);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println();
  Serial.print("My IP: ");
  Serial.println(WiFi.localIP());
  long rssi = WiFi.RSSI();
  Serial.print("RSSI: ");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void loop() {

  WiFiClient client;
 
  if (!client.connect(host,3000)) { //The problem start here
    Serial.println(".");
    return;
  }
 
  Serial.println();
  Serial.print("Conected to IP: ");
  Serial.println(host);

  Serial.println("Sending string to server: ");
  client.println("OK");
  delay(100);
 
  String line = client.readStringUntil('\n');
  Serial.print(line);

  Serial.println();
  Serial.println("Closing connection");
  client.flush();
  client.stop();
  Serial.println("Connection Closed");
 
}


And the java code is:
Code: Select allimport java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ServerEcho {

    public static void main(String[] args) throws UnknownHostException, IOException {

        ServerSocket ss;
        Socket x;
        PrintWriter output;
        Scanner input;
        String msg1;

        ss = new ServerSocket(3000);
        x = ss.accept();
        System.out.println("Connection ok \n");
        input = new Scanner(x.getInputStream());
        output = new PrintWriter(x.getOutputStream(), true);

        do {

            msg1 = input.nextLine();
            System.out.println("Message received \n");
            System.out.println(msg1);
            //System.out.println("Sending back \n");
            //output.println(msg1);

        } while (!msg1.equals("bye"));
        x.close();
        ss.close();
    }
}
User avatar
By Subhajit Das
#47998 Your program kinda works.
Here is my update which works properly. Tested on ESP8266-12E.
SocketClient.ino

Java is same.
You do not have the required permissions to view the files attached to this post.
User avatar
By leoneazevedo
#48141
Subhajit Das wrote:Your program kinda works.
Here is my update which works properly. Tested on ESP8266-12E.
SocketClient.ino

Java is same.


Thanks for ask me!
I think my ESP has a problem.
I was trying comunicate with arduino and I had a lot of problems !
I will try other ESP.
Thanks