Skip to content

Commit

Permalink
feat: added support for wireless communication using ESP01
Browse files Browse the repository at this point in the history
  • Loading branch information
AsCress committed Nov 2, 2024
1 parent 161d8c2 commit 8300425
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 52 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/io/pslab/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
48 changes: 14 additions & 34 deletions app/src/main/java/io/pslab/communication/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<JSONObject>() {
@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<JSONObject>() {
@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);
}

}
Expand Down
23 changes: 21 additions & 2 deletions app/src/main/java/io/pslab/communication/ScienceLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
81 changes: 81 additions & 0 deletions app/src/main/java/io/pslab/communication/SocketClient.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
29 changes: 14 additions & 15 deletions app/src/main/java/io/pslab/fragment/ESPFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,7 +86,7 @@ public void onResume() {
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}

private class ESPTask extends AsyncTask<Void, Void, String> {
private class ESPTask extends AsyncTask<Void, Void, Boolean> {

@Override
protected void onPreExecute() {
Expand All @@ -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");
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/io/pslab/others/ScienceLabCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.util.Log;

import java.io.IOException;

import io.pslab.communication.CommunicationHandler;
import io.pslab.communication.ScienceLab;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8300425

Please sign in to comment.