So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By pleslie
#72860 I am creating a android client app to communicate with an ESp8266 connected to an Arduino. I can't seem to get the socket to pass data to the ESP. I am using Android studio to write the client app and when debugging it shows as the socket being connected but when I send data to the socketserver(ESP8266-01) I get no response and there is nothing being printed out on the serial monitor connected to the ESP. Attached is the ESP code which I am programming through the arduino IDE.
Code: Select all#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Hash.h>





//ESP8266WiFiMulti WiFiMulti;

ESP8266WebServer server = ESP8266WebServer(80);
WebSocketsServer webSocket = WebSocketsServer(81);

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

    switch(type) {
        case WStype_DISCONNECTED:
            Serial.printf("[%u] Disconnected!\n", num);
            break;
        case WStype_CONNECTED: {
            IPAddress ip = webSocket.remoteIP(num);
            Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);

            // send message to client
            webSocket.sendTXT(num, "Connected");
        }
            break;
        case WStype_TEXT:
         IPAddress ip = webSocket.remoteIP(num);
            Serial.printf("$%s?\n", payload);
               webSocket.sendTXT(num, payload,sizeof(payload),false);
            break;
    }

}

void setup() {
    //Serial.begin(921600);
    Serial.begin(115200);

    //Serial.setDebugOutput(true);

    Serial.println();
    Serial.println();
    Serial.println();

    for(uint8_t t = 4; t > 0; t--) {
        Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
        Serial.flush();
        delay(1000);
    }

   
    Serial.println("Starting AP");
    WiFi.mode(WIFI_AP);
    WiFi.softAP("Alpine_SIM_CNTRL","12345678");
    Serial.println("AP Started");

    //while(WiFiMulti.run() != WL_CONNECTED) {
        //delay(100);
        //Serial.println("Delayed");
    //}
    Serial.println("Starting Socket");

    // start webSocket server
    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
    Serial.println("Socket Started");

    if(MDNS.begin("simcontrol")) {
        Serial.println("MDNS responder started");
    }
    Serial.println("Starting Server");
    //handle index
    server.on(
      "/", []() {
        server.send(200,"text/plain","You are connected");
    });

    server.begin();
    Serial.println("Server Started");

    //Add service to MDNS
    MDNS.addService("http", "tcp", 80);
    MDNS.addService("ws", "tcp", 81);

   

}

void loop() {
    webSocket.loop();
    server.handleClient();
}

And below is the ASync task from the Android code:
Code: Select allpublic class WriteToServer extends AsyncTask<MyTaskParams, Void, String> {


    Context context;
    Socket socket;



    public WriteToServer(MyTaskParams Params) {
    }


    @Override
    protected String doInBackground(MyTaskParams... params) {
        socket = null;
        DataInputStream in = null;
        DataOutputStream out = null;


        context = params[0].context;
        String command = params[0].cmd;
        String response = null;


        try {
            socket = new Socket("192.168.4.1",81);
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
            out.writeUTF(command);
            Log.i("Command","Command sent: " + command);
            while(in.available() > 0) {
                response = in.readUTF();
            }

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Error","There was an error writing to the socket. Thrown IO exception");
        }finally {
            if(socket != null){
                try{
                    Log.i("INFO","closing the socket");
                    socket.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            if(in != null){
                try{
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(out != null){
                try{
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response;
    }]



I have been banging my head for a couple of days on this and can't figure out why the socket is not sending data to the ESP8266 or vice versa. Just a side note I connected my PC to the ESP and typed in 192.168.4.1:81/**** and I got a response back "This is only a Socket Server". So I know the Socket server is up and running. As I am a noob to both ESP and Android any help would be greatly appreciated.
User avatar
By pleslie
#72983 Fixed the issue. Raw Sockets will not work with arduinoWebSockets library. Two different technologies. I ended up using org.java.websockets and was able to eventually get it working.

As a note when creating the client socket on android device using org.java.websockets make sure you create the socket using the correct Draft version. By default if you only create the websocket using just the URI it will default to websocket version 8. To get the socket to work correctly with arduinoWebSockets library you must create the socket using Draft 17 which is websocket version 13. You do this by creating the socket like this:
Code: Select all mWebSocketClient = new WebSocketClient(uri, new Draft_17())
User avatar
By Aliaksei
#95961
pleslie wrote:Fixed the issue. Raw Sockets will not work with arduinoWebSockets library. Two different technologies. I ended up using org.java.websockets and was able to eventually get it working.

As a note when creating the client socket on android device using org.java.websockets make sure you create the socket using the correct Draft version. By default if you only create the websocket using just the URI it will default to websocket version 8. To get the socket to work correctly with arduinoWebSockets library you must create the socket using Draft 17 which is websocket version 13. You do this by creating the socket like this:
Code: Select all mWebSocketClient = new WebSocketClient(uri, new Draft_17())

Hello. Thank you very much, everything worked) :D