ESP8266 fails to connect to Java Socket
Posted: Fri Jan 01, 2016 11:35 am
Hello Guys!
I have a Socket on my vServer programmed in Java an I am flashing the code directly to the ESP.
My ESP connects to the Socket the first time without problems. If I reset the ESP one connection attempt fails before it suceeds. If I power cycle the ESP again it fails two times before it works. And so on...
The errors increase with every connection the ESP establishes.
This problem does not occur if I connect to the socket with my PC or my iPhone
The code is the WifiClient Example but slightly modified. What it does:
1. Connects to WiFi
2. Connects to my TCP Socket on specific port
3. Sends Message to Server
4. Closes Connection
Here is my code:
I have a Socket on my vServer programmed in Java an I am flashing the code directly to the ESP.
My ESP connects to the Socket the first time without problems. If I reset the ESP one connection attempt fails before it suceeds. If I power cycle the ESP again it fails two times before it works. And so on...
The errors increase with every connection the ESP establishes.
This problem does not occur if I connect to the socket with my PC or my iPhone
The code is the WifiClient Example but slightly modified. What it does:
1. Connects to WiFi
2. Connects to my TCP Socket on specific port
3. Sends Message to Server
4. Closes Connection
Here is my code:
Code: Select all
#include <ESP8266WiFi.h>
const char* ssid = "MySSID";
const char* password = "MySecretPassword";
const char* host = "myvserver.com";
void setup() {
Serial.begin(115200);
delay(800);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected with IP ");
Serial.println(WiFi.localIP());
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void loop() {
WiFiClient client;
if (!client.connect(host, 36443)) {
Serial.print("X");
return;
}
Serial.println();
Serial.print("Connected to ");
Serial.println(host);
Serial.println("Sending Message to server");
client.println("Hey I am ESP Boss");
delay(100);
String line = client.readStringUntil('\n');
Serial.print(line);
Serial.println();
Serial.println("closing connection");
client.flush();
client.stop();
Serial.println("connection closed");
Serial.println("Stopping Connection Attemtps");
}