Search code examples
javaandroidwifiandroid-wifiesp32

ESP32 Wifiserver and Android Appp


So i got my ESP32 and wanted to do some Porjects and I want to control them with an Android App, for example LED Stripes. I already did this with my Raspberry Pi where it runs perfectly. I already tried some codes and it could connect to the Wifi. And my PC and even Raspberry Pi could connect to it but when i tried with my Smartphone i just didn't worked. Here' my Android Code: mainActivity.jre

connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!IPAddress.getText().equals("") || IPAddress.getText() != null || !portNumber.getText().equals("") || portNumber.getText() != null){
                    return;
                }

                IPaddresse= IPAddress.getText().toString();
                port=Integer.parseInt(portNumber.getText().toString());

                try {
                    client = new Socket(IPaddresse,port);
                    pw = new PrintWriter(client.getOutputStream());
                    dataOutputStream= new DataOutputStream(client.getOutputStream());
                    msg.setText("Verbunden!");
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

Permissions:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

ESP32 Code:

#include <WiFi.h>

const char* ssid="Name";
const char* password="password";

WiFiServer server(80  );

void setup() {
  Serial.begin(115200);
  Serial.println("start");
  
  delay(1000);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
  Serial.println(WiFi.localIP());
 
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
 
  if (client) {
 
    while (client.connected()) {
 
      while (client.available()>0) {
        char c = client.read();
        client.write(c);
      }

      Serial.println(client.localIP());
      delay(10);
    }
  }
  
}

Sorry for my English. And my Code isn't the best im just beginning to Code Thanks


Solution

  • First on the ESP32 i suggest you add mDNS to your setup, this allows you to give your esp a name on the network like when you give your esp the name pietje then you can find it as pietje.local on your network.
    This seems not to work properly with the socket class, but maybe i did something work. It did not finding the ip of it. It was the first time working with sockets. So i learned something new.

    I tried your example and it did fault me to. With some short search i found this website: https://www.tutorialspoint.com/sending-and-receiving-data-with-sockets-in-android
    By adding you socket creation inside a thread it did work for me.

    I hope this helps you ahead.