From 8300425fade0b0533080ceb2b60f31d2a8eea8bd Mon Sep 17 00:00:00 2001 From: Anashuman Singh Date: Sat, 2 Nov 2024 22:18:07 +0530 Subject: [PATCH] feat: added support for wireless communication using ESP01 --- .../java/io/pslab/activity/MainActivity.java | 2 +- .../io/pslab/communication/PacketHandler.java | 48 ++++------- .../io/pslab/communication/ScienceLab.java | 23 +++++- .../io/pslab/communication/SocketClient.java | 81 +++++++++++++++++++ .../java/io/pslab/fragment/ESPFragment.java | 29 ++++--- .../io/pslab/others/ScienceLabCommon.java | 11 +++ 6 files changed, 142 insertions(+), 52 deletions(-) create mode 100644 app/src/main/java/io/pslab/communication/SocketClient.java diff --git a/app/src/main/java/io/pslab/activity/MainActivity.java b/app/src/main/java/io/pslab/activity/MainActivity.java index ad3965cc7..77d61422a 100644 --- a/app/src/main/java/io/pslab/activity/MainActivity.java +++ b/app/src/main/java/io/pslab/activity/MainActivity.java @@ -498,7 +498,7 @@ private void attemptToGetUSBPermission() { public boolean onPrepareOptionsMenu(Menu menu) { menu.getItem(0).setVisible(PSLabisConnected); menu.getItem(1).setVisible(!PSLabisConnected); - setPSLabVersionIDs(); + //setPSLabVersionIDs(); return true; } diff --git a/app/src/main/java/io/pslab/communication/PacketHandler.java b/app/src/main/java/io/pslab/communication/PacketHandler.java index 709b26659..4480df51c 100644 --- a/app/src/main/java/io/pslab/communication/PacketHandler.java +++ b/app/src/main/java/io/pslab/communication/PacketHandler.java @@ -34,7 +34,7 @@ public class PacketHandler { private int timeout = 500, VERSION_STRING_LENGTH = 8, FW_VERSION_LENGTH = 3; public static int PSLAB_FW_VERSION = 0; ByteBuffer burstBuffer = ByteBuffer.allocate(2000); - private HttpAsyncTask httpAsyncTask; + private SocketClient socketClient; public PacketHandler(int timeout, CommunicationHandler communicationHandler) { this.loadBurst = false; @@ -45,6 +45,13 @@ public PacketHandler(int timeout, CommunicationHandler communicationHandler) { connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected()); } + public PacketHandler() { + this.loadBurst = false; + this.connected = false; + this.mCommandsProto = new CommandsProto(); + connected = ScienceLabCommon.isWifiConnected(); + } + public boolean isConnected() { connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected()); return connected; @@ -247,48 +254,21 @@ public byte[] sendBurst() { public int commonRead(int bytesToRead) throws IOException { final int[] bytesRead = {0}; - if (mCommunicationHandler.isConnected()) { + if (mCommunicationHandler != null && mCommunicationHandler.isConnected()) { bytesRead[0] = mCommunicationHandler.read(buffer, bytesToRead, timeout); } else if (ScienceLabCommon.isWifiConnected()) { - httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback() { - @Override - public void success(JSONObject jsonObject) { - try { - //Server will send byte array - buffer = (byte[]) jsonObject.get("data"); - bytesRead[0] = buffer.length; - } catch (JSONException e) { - e.printStackTrace(); - } - } - - @Override - public void error(Exception e) { - Log.e(TAG, "Error reading data over ESP"); - } - }); - httpAsyncTask.execute(new byte[]{}); + socketClient = SocketClient.getInstance(); + bytesRead[0] = socketClient.read(buffer, bytesToRead); } return bytesRead[0]; } public void commonWrite(byte[] data) throws IOException { - if (mCommunicationHandler.isConnected()) { + if (mCommunicationHandler != null && mCommunicationHandler.isConnected()) { mCommunicationHandler.write(data, timeout); } else if (ScienceLabCommon.isWifiConnected()) { - httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback() { - @Override - public void success(JSONObject jsonObject) { - Log.v(TAG, "write response:" + jsonObject.toString()); - } - - @Override - public void error(Exception e) { - Log.e(TAG, "Error writing data over ESP"); - } - }); - - httpAsyncTask.execute(data); + socketClient = SocketClient.getInstance(); + socketClient.write(data); } } diff --git a/app/src/main/java/io/pslab/communication/ScienceLab.java b/app/src/main/java/io/pslab/communication/ScienceLab.java index 7b8430d1c..edebfec73 100644 --- a/app/src/main/java/io/pslab/communication/ScienceLab.java +++ b/app/src/main/java/io/pslab/communication/ScienceLab.java @@ -36,6 +36,7 @@ import io.pslab.communication.peripherals.I2C; import io.pslab.fragment.HomeFragment; import io.pslab.others.InitializationVariable; +import io.pslab.others.ScienceLabCommon; /** * Created by viveksb007 on 28/3/17. @@ -121,6 +122,24 @@ public void run() { } } + public ScienceLab() throws IOException { + mCommandsProto = new CommandsProto(); + mAnalogConstants = new AnalogConstants(); + try { + mPacketHandler = new PacketHandler(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + if (ScienceLabCommon.isWifiConnected()) { + initializeVariables(); + runInitSequence(); + if (HomeFragment.booleanVariable == null) { + HomeFragment.booleanVariable = new InitializationVariable(); + } + HomeFragment.booleanVariable.setVariable(true); + } + } + private void initializeVariables() { DDS_CLOCK = 0; timebase = 40; @@ -745,7 +764,7 @@ public boolean isDeviceFound() { * @return true is device is connected; false otherwise */ public boolean isConnected() { - return mCommunicationHandler.isConnected(); + return (ScienceLabCommon.isWifiConnected || mCommunicationHandler.isConnected()); } /* DIGITAL SECTION */ @@ -2157,7 +2176,7 @@ public void resetDevice() { * @throws IOException * @throws InterruptedException */ - public void enterBootloader() throws IOException, InterruptedException { + public void enterBootloader() throws Exception { mCommunicationHandler.close(); mCommunicationHandler.open(460800); mPacketHandler = new PacketHandler(50, mCommunicationHandler); diff --git a/app/src/main/java/io/pslab/communication/SocketClient.java b/app/src/main/java/io/pslab/communication/SocketClient.java new file mode 100644 index 000000000..d165e8013 --- /dev/null +++ b/app/src/main/java/io/pslab/communication/SocketClient.java @@ -0,0 +1,81 @@ +package io.pslab.communication; + +import android.util.Log; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +public class SocketClient { + + public static final String TAG = "SocketClient"; + private static SocketClient socketClient = null; + private Socket socket; + private OutputStream outputStream; + private InputStream inputStream; + private boolean isConnected = false; + + public static final int DEFAULT_READ_BUFFER_SIZE = 32 * 1024; + + private byte[] buffer = new byte[DEFAULT_READ_BUFFER_SIZE]; + + private SocketClient() { + } + + public void openConnection(String ip, int port) throws IOException { + socket = new Socket(ip, port); + outputStream = socket.getOutputStream(); + inputStream = socket.getInputStream(); + if (!socket.isConnected()) { + isConnected = false; + return; + } + isConnected = true; + } + + public static SocketClient getInstance() { + if (socketClient == null) { + socketClient = new SocketClient(); + } + return socketClient; + } + + public boolean isConnected() { + return isConnected; + } + + public void write(byte[] data) throws IOException { + if (isConnected && socketClient.isConnected && outputStream != null) { + outputStream.write(data); + } + } + + public int read(byte[] dest, int bytesToRead) throws IOException { + int bytesRead = 0; + if (isConnected && socketClient.isConnected && inputStream != null) { + bytesRead = inputStream.read(buffer, 0, bytesToRead); + + if (bytesRead > 0) { + System.arraycopy(buffer, 0, dest, 0, bytesRead); + } else { + Log.e(TAG, "Read Error: " + bytesToRead); + return bytesRead; + } + } + return bytesRead; + } + + public void closeConnection() { + try { + if (isConnected) { + inputStream.close(); + outputStream.close(); + socket.close(); + isConnected = false; + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/io/pslab/fragment/ESPFragment.java b/app/src/main/java/io/pslab/fragment/ESPFragment.java index 3c13b32ae..b0e2104fc 100644 --- a/app/src/main/java/io/pslab/fragment/ESPFragment.java +++ b/app/src/main/java/io/pslab/fragment/ESPFragment.java @@ -23,6 +23,8 @@ import java.io.IOException; import io.pslab.R; +import io.pslab.communication.CommunicationHandler; +import io.pslab.communication.SocketClient; import io.pslab.others.CustomSnackBar; import io.pslab.others.ScienceLabCommon; import okhttp3.OkHttpClient; @@ -84,7 +86,7 @@ public void onResume() { getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); } - private class ESPTask extends AsyncTask { + private class ESPTask extends AsyncTask { @Override protected void onPreExecute() { @@ -93,35 +95,32 @@ protected void onPreExecute() { } @Override - protected String doInBackground(Void... voids) { - String result = ""; + protected Boolean doInBackground(Void... voids) { try { - OkHttpClient client = new OkHttpClient(); - Request request = new Request.Builder() - .url("http://" + espIPAddress) - .build(); - Response response = client.newCall(request).execute(); - if (response.code() == 200) { + SocketClient socketClient = SocketClient.getInstance(); + socketClient.openConnection(espIPAddress, 80); + if (socketClient.isConnected()) { ScienceLabCommon.setIsWifiConnected(true); ScienceLabCommon.setEspBaseIP(espIPAddress); + ScienceLabCommon.getInstance().openDevice(); + return true; } - result = response.body().string(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } - return result; + return false; } @Override - protected void onPostExecute(String result) { + protected void onPostExecute(Boolean result) { espConnectProgressBar.setVisibility(View.GONE); espConnectBtn.setVisibility(View.VISIBLE); Activity activity; - if (result.isEmpty() && ((activity = getActivity()) != null)) { + if (!result && ((activity = getActivity()) != null)) { CustomSnackBar.showSnackBar(activity.findViewById(android.R.id.content), getString(R.string.incorrect_IP_address_message), null, null, Snackbar.LENGTH_SHORT); } else { - Log.v("Response", result); + Log.v("ESPFragment", "ESP Connection Successful"); } } } diff --git a/app/src/main/java/io/pslab/others/ScienceLabCommon.java b/app/src/main/java/io/pslab/others/ScienceLabCommon.java index 8bd20b962..130bf4a89 100644 --- a/app/src/main/java/io/pslab/others/ScienceLabCommon.java +++ b/app/src/main/java/io/pslab/others/ScienceLabCommon.java @@ -2,6 +2,8 @@ import android.util.Log; +import java.io.IOException; + import io.pslab.communication.CommunicationHandler; import io.pslab.communication.ScienceLab; @@ -31,6 +33,15 @@ public boolean openDevice(CommunicationHandler communicationHandler) { return true; } + public void openDevice() throws IOException { + scienceLab = new ScienceLab(); + if (!isWifiConnected()) { + Log.e(TAG, "Error in connection"); + return; + } + connected = true; + } + public static ScienceLabCommon getInstance() { if (scienceLabCommon == null) { scienceLabCommon = new ScienceLabCommon();