Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for wireless communication using ESP01 #2561

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions app/src/main/java/io/pslab/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.pslab.activity;

import static io.pslab.others.ScienceLabCommon.isWifiConnected;
import static io.pslab.others.ScienceLabCommon.scienceLab;

import android.app.PendingIntent;
Expand All @@ -10,6 +11,7 @@
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
Expand Down Expand Up @@ -55,6 +57,7 @@
import io.pslab.others.InitializationVariable;
import io.pslab.others.ScienceLabCommon;
import io.pslab.receivers.USBDetachReceiver;
import io.pslab.receivers.WifiDisconnectReceiver;

public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -136,6 +139,11 @@ protected void onCreate(Bundle savedInstanceState) {
usbDetachReceiver = new USBDetachReceiver(this);
registerReceiver(usbDetachReceiver, usbDetachFilter);

IntentFilter wifiDisconnectFilter = new IntentFilter();
wifiDisconnectFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
WifiDisconnectReceiver wifiDisconnectReceiver = new WifiDisconnectReceiver(this);
registerReceiver(wifiDisconnectReceiver, wifiDisconnectFilter);

setSupportActionBar(toolbar);
mHandler = new Handler();

Expand Down Expand Up @@ -414,7 +422,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_pslab_connected:
case R.id.menu_wifi_connected, R.id.menu_pslab_connected:
CustomSnackBar.showSnackBar(findViewById(android.R.id.content),
getString(R.string.device_connected_successfully), null, null, Snackbar.LENGTH_SHORT);
break;
Expand Down Expand Up @@ -496,8 +504,9 @@ private void attemptToGetUSBPermission() {

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.getItem(0).setVisible(PSLabisConnected);
menu.getItem(1).setVisible(!PSLabisConnected);
menu.getItem(0).setVisible(isWifiConnected);
menu.getItem(1).setVisible(PSLabisConnected);
menu.getItem(2).setVisible(!PSLabisConnected && !isWifiConnected);
setPSLabVersionIDs();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public void open(int baudRate) throws IOException {
}
if (mUsbDevice.getProductId() == PSLAB_PRODUCT_ID_V6 && mUsbDevice.getVendorId() == PSLAB_VENDOR_ID_V6) {
PSLAB_VERSION = 6;
}
else {
} else {
PSLAB_VERSION = 5;
}
connected = true;
Expand Down Expand Up @@ -102,7 +101,7 @@ public int read(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOExce
}
int numBytesRead = 0;
int readNow;
Log.v(TAG, "TO read : " + bytesToBeRead);
Log.v(TAG, "To read : " + bytesToBeRead);
int bytesToBeReadTemp = bytesToBeRead;
while (numBytesRead < bytesToBeRead) {
readNow = port.read(mReadBuffer, bytesToBeReadTemp, timeoutMillis);
Expand All @@ -123,7 +122,7 @@ public int read(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOExce
public int readCdcAcm(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOException {
int numBytesRead = 0;
int readNow;
Log.v(TAG, "TO read : " + bytesToBeRead);
Log.v(TAG, "To read : " + bytesToBeRead);
int bytesToBeReadTemp = bytesToBeRead;
while (numBytesRead < bytesToBeRead) {
readNow = mConnection.bulkTransfer(mUsbDevice.getInterface(1).getEndpoint(1), mReadBuffer, bytesToBeReadTemp, timeoutMillis);
Expand Down
43 changes: 0 additions & 43 deletions app/src/main/java/io/pslab/communication/HttpAsyncTask.java

This file was deleted.

87 changes: 0 additions & 87 deletions app/src/main/java/io/pslab/communication/HttpHandler.java

This file was deleted.

82 changes: 47 additions & 35 deletions app/src/main/java/io/pslab/communication/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -13,8 +10,10 @@
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import io.pslab.interfaces.HttpCallback;
import io.pslab.others.ScienceLabCommon;

/**
Expand All @@ -34,15 +33,19 @@ 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;
ExecutorService executor = Executors.newSingleThreadExecutor();
AsCress marked this conversation as resolved.
Show resolved Hide resolved

public PacketHandler(int timeout, CommunicationHandler communicationHandler) {
this.loadBurst = false;
this.connected = false;
this.timeout = timeout;
this.mCommandsProto = new CommandsProto();
this.mCommunicationHandler = communicationHandler;
connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected());
socketClient = SocketClient.getInstance();
if (communicationHandler != null) {
this.mCommunicationHandler = communicationHandler;
}
connected = (ScienceLabCommon.isWifiConnected() || mCommunicationHandler.isConnected());
}

public boolean isConnected() {
Expand Down Expand Up @@ -247,49 +250,58 @@ 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) {
Future<Void> future = executor.submit(() -> {
try {
socketClient.read(bytesToRead);
System.arraycopy(socketClient.getReceivedData(), 0, buffer, 0, bytesToRead);
bytesRead[0] = bytesToRead;
} catch (Exception e) {
Log.e(TAG, "Error reading data over ESP");
AsCress marked this conversation as resolved.
Show resolved Hide resolved
}
return null;
});
httpAsyncTask.execute(new byte[]{});
try {
future.get();
} catch (Exception e) {
throw new IOException("Error reading data", e);
}
}
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) {
Future<Void> future = executor.submit(() -> {
try {
socketClient.write(data);
} catch (Exception e) {
Log.e(TAG, "Error writing data over ESP");
}
return null;
});

httpAsyncTask.execute(data);
try {
future.get();
} catch (Exception e) {
throw new IOException("Error writing data", e);
}
}
}

public void close() {
try {
if (mCommunicationHandler != null) {
mCommunicationHandler.close();
} else {
socketClient.closeConnection();
}
executor.shutdown();
} catch (Exception e) {
Log.e(TAG, "Error closing connection");
}
}
}
}
Loading
Loading