This is my first post here so bear with me if I make any mistakes...
Okay so I'm simply trying to have a Java program running on my computer send information to an ESP8266 running the arduino software. First off, here's the code.
ESP8266(Arduino):
#include <ESP8266WiFi.h>
#define NAME "********"
#define PASS "********"
const char* host = "192.168.1.6";
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println();
WiFi.begin(NAME, PASS);
Serial.print("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
if (client.connect(host, 9090)) {
Serial.print("Connected to: ");
Serial.println(host);
String data = "No Message";
client.print("ESP8266 connected!");
if(client.available()) {
data = client.readStringUntil('\n');
}
Serial.print("Host message: ");
Serial.println(data);
client.stop();
} else {
client.stop();
}
}
void loop() {
}
Java Server:
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
try{
while(true){
Socket socket = listener.accept();
socket.setKeepAlive(true);
try{
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write("Hello from Java!\n");
} finally {
socket.close();
}
}
} finally {
listener.close();
}
}
}
The problem is that client.available() never returns true, even when information is sent by the Java server to the client. I have tried sending data from the Arduino to the server and that works just fine. I also made a quick Java program that could run as a client and ran this on another computer on the network. The Java server and client in this case communicated just fine. So the problem lies somewhere in the Arduino, but I'm at a loss as to what it is.