Skip to content

Commit 8300425

Browse files
committed
feat: added support for wireless communication using ESP01
1 parent 161d8c2 commit 8300425

File tree

6 files changed

+142
-52
lines changed

6 files changed

+142
-52
lines changed

app/src/main/java/io/pslab/activity/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ private void attemptToGetUSBPermission() {
498498
public boolean onPrepareOptionsMenu(Menu menu) {
499499
menu.getItem(0).setVisible(PSLabisConnected);
500500
menu.getItem(1).setVisible(!PSLabisConnected);
501-
setPSLabVersionIDs();
501+
//setPSLabVersionIDs();
502502
return true;
503503
}
504504

app/src/main/java/io/pslab/communication/PacketHandler.java

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class PacketHandler {
3434
private int timeout = 500, VERSION_STRING_LENGTH = 8, FW_VERSION_LENGTH = 3;
3535
public static int PSLAB_FW_VERSION = 0;
3636
ByteBuffer burstBuffer = ByteBuffer.allocate(2000);
37-
private HttpAsyncTask httpAsyncTask;
37+
private SocketClient socketClient;
3838

3939
public PacketHandler(int timeout, CommunicationHandler communicationHandler) {
4040
this.loadBurst = false;
@@ -45,6 +45,13 @@ public PacketHandler(int timeout, CommunicationHandler communicationHandler) {
4545
connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected());
4646
}
4747

48+
public PacketHandler() {
49+
this.loadBurst = false;
50+
this.connected = false;
51+
this.mCommandsProto = new CommandsProto();
52+
connected = ScienceLabCommon.isWifiConnected();
53+
}
54+
4855
public boolean isConnected() {
4956
connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected());
5057
return connected;
@@ -247,48 +254,21 @@ public byte[] sendBurst() {
247254

248255
public int commonRead(int bytesToRead) throws IOException {
249256
final int[] bytesRead = {0};
250-
if (mCommunicationHandler.isConnected()) {
257+
if (mCommunicationHandler != null && mCommunicationHandler.isConnected()) {
251258
bytesRead[0] = mCommunicationHandler.read(buffer, bytesToRead, timeout);
252259
} else if (ScienceLabCommon.isWifiConnected()) {
253-
httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback<JSONObject>() {
254-
@Override
255-
public void success(JSONObject jsonObject) {
256-
try {
257-
//Server will send byte array
258-
buffer = (byte[]) jsonObject.get("data");
259-
bytesRead[0] = buffer.length;
260-
} catch (JSONException e) {
261-
e.printStackTrace();
262-
}
263-
}
264-
265-
@Override
266-
public void error(Exception e) {
267-
Log.e(TAG, "Error reading data over ESP");
268-
}
269-
});
270-
httpAsyncTask.execute(new byte[]{});
260+
socketClient = SocketClient.getInstance();
261+
bytesRead[0] = socketClient.read(buffer, bytesToRead);
271262
}
272263
return bytesRead[0];
273264
}
274265

275266
public void commonWrite(byte[] data) throws IOException {
276-
if (mCommunicationHandler.isConnected()) {
267+
if (mCommunicationHandler != null && mCommunicationHandler.isConnected()) {
277268
mCommunicationHandler.write(data, timeout);
278269
} else if (ScienceLabCommon.isWifiConnected()) {
279-
httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback<JSONObject>() {
280-
@Override
281-
public void success(JSONObject jsonObject) {
282-
Log.v(TAG, "write response:" + jsonObject.toString());
283-
}
284-
285-
@Override
286-
public void error(Exception e) {
287-
Log.e(TAG, "Error writing data over ESP");
288-
}
289-
});
290-
291-
httpAsyncTask.execute(data);
270+
socketClient = SocketClient.getInstance();
271+
socketClient.write(data);
292272
}
293273

294274
}

app/src/main/java/io/pslab/communication/ScienceLab.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.pslab.communication.peripherals.I2C;
3737
import io.pslab.fragment.HomeFragment;
3838
import io.pslab.others.InitializationVariable;
39+
import io.pslab.others.ScienceLabCommon;
3940

4041
/**
4142
* Created by viveksb007 on 28/3/17.
@@ -121,6 +122,24 @@ public void run() {
121122
}
122123
}
123124

125+
public ScienceLab() throws IOException {
126+
mCommandsProto = new CommandsProto();
127+
mAnalogConstants = new AnalogConstants();
128+
try {
129+
mPacketHandler = new PacketHandler();
130+
} catch (NullPointerException e) {
131+
e.printStackTrace();
132+
}
133+
if (ScienceLabCommon.isWifiConnected()) {
134+
initializeVariables();
135+
runInitSequence();
136+
if (HomeFragment.booleanVariable == null) {
137+
HomeFragment.booleanVariable = new InitializationVariable();
138+
}
139+
HomeFragment.booleanVariable.setVariable(true);
140+
}
141+
}
142+
124143
private void initializeVariables() {
125144
DDS_CLOCK = 0;
126145
timebase = 40;
@@ -745,7 +764,7 @@ public boolean isDeviceFound() {
745764
* @return true is device is connected; false otherwise
746765
*/
747766
public boolean isConnected() {
748-
return mCommunicationHandler.isConnected();
767+
return (ScienceLabCommon.isWifiConnected || mCommunicationHandler.isConnected());
749768
}
750769

751770
/* DIGITAL SECTION */
@@ -2157,7 +2176,7 @@ public void resetDevice() {
21572176
* @throws IOException
21582177
* @throws InterruptedException
21592178
*/
2160-
public void enterBootloader() throws IOException, InterruptedException {
2179+
public void enterBootloader() throws Exception {
21612180
mCommunicationHandler.close();
21622181
mCommunicationHandler.open(460800);
21632182
mPacketHandler = new PacketHandler(50, mCommunicationHandler);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.pslab.communication;
2+
3+
import android.util.Log;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.net.Socket;
9+
10+
public class SocketClient {
11+
12+
public static final String TAG = "SocketClient";
13+
private static SocketClient socketClient = null;
14+
private Socket socket;
15+
private OutputStream outputStream;
16+
private InputStream inputStream;
17+
private boolean isConnected = false;
18+
19+
public static final int DEFAULT_READ_BUFFER_SIZE = 32 * 1024;
20+
21+
private byte[] buffer = new byte[DEFAULT_READ_BUFFER_SIZE];
22+
23+
private SocketClient() {
24+
}
25+
26+
public void openConnection(String ip, int port) throws IOException {
27+
socket = new Socket(ip, port);
28+
outputStream = socket.getOutputStream();
29+
inputStream = socket.getInputStream();
30+
if (!socket.isConnected()) {
31+
isConnected = false;
32+
return;
33+
}
34+
isConnected = true;
35+
}
36+
37+
public static SocketClient getInstance() {
38+
if (socketClient == null) {
39+
socketClient = new SocketClient();
40+
}
41+
return socketClient;
42+
}
43+
44+
public boolean isConnected() {
45+
return isConnected;
46+
}
47+
48+
public void write(byte[] data) throws IOException {
49+
if (isConnected && socketClient.isConnected && outputStream != null) {
50+
outputStream.write(data);
51+
}
52+
}
53+
54+
public int read(byte[] dest, int bytesToRead) throws IOException {
55+
int bytesRead = 0;
56+
if (isConnected && socketClient.isConnected && inputStream != null) {
57+
bytesRead = inputStream.read(buffer, 0, bytesToRead);
58+
59+
if (bytesRead > 0) {
60+
System.arraycopy(buffer, 0, dest, 0, bytesRead);
61+
} else {
62+
Log.e(TAG, "Read Error: " + bytesToRead);
63+
return bytesRead;
64+
}
65+
}
66+
return bytesRead;
67+
}
68+
69+
public void closeConnection() {
70+
try {
71+
if (isConnected) {
72+
inputStream.close();
73+
outputStream.close();
74+
socket.close();
75+
isConnected = false;
76+
}
77+
} catch (Exception e) {
78+
e.printStackTrace();
79+
}
80+
}
81+
}

app/src/main/java/io/pslab/fragment/ESPFragment.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.IOException;
2424

2525
import io.pslab.R;
26+
import io.pslab.communication.CommunicationHandler;
27+
import io.pslab.communication.SocketClient;
2628
import io.pslab.others.CustomSnackBar;
2729
import io.pslab.others.ScienceLabCommon;
2830
import okhttp3.OkHttpClient;
@@ -84,7 +86,7 @@ public void onResume() {
8486
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
8587
}
8688

87-
private class ESPTask extends AsyncTask<Void, Void, String> {
89+
private class ESPTask extends AsyncTask<Void, Void, Boolean> {
8890

8991
@Override
9092
protected void onPreExecute() {
@@ -93,35 +95,32 @@ protected void onPreExecute() {
9395
}
9496

9597
@Override
96-
protected String doInBackground(Void... voids) {
97-
String result = "";
98+
protected Boolean doInBackground(Void... voids) {
9899
try {
99-
OkHttpClient client = new OkHttpClient();
100-
Request request = new Request.Builder()
101-
.url("http://" + espIPAddress)
102-
.build();
103-
Response response = client.newCall(request).execute();
104-
if (response.code() == 200) {
100+
SocketClient socketClient = SocketClient.getInstance();
101+
socketClient.openConnection(espIPAddress, 80);
102+
if (socketClient.isConnected()) {
105103
ScienceLabCommon.setIsWifiConnected(true);
106104
ScienceLabCommon.setEspBaseIP(espIPAddress);
105+
ScienceLabCommon.getInstance().openDevice();
106+
return true;
107107
}
108-
result = response.body().string();
109-
} catch (IOException e) {
108+
} catch (Exception e) {
110109
e.printStackTrace();
111110
}
112-
return result;
111+
return false;
113112
}
114113

115114
@Override
116-
protected void onPostExecute(String result) {
115+
protected void onPostExecute(Boolean result) {
117116
espConnectProgressBar.setVisibility(View.GONE);
118117
espConnectBtn.setVisibility(View.VISIBLE);
119118
Activity activity;
120-
if (result.isEmpty() && ((activity = getActivity()) != null)) {
119+
if (!result && ((activity = getActivity()) != null)) {
121120
CustomSnackBar.showSnackBar(activity.findViewById(android.R.id.content),
122121
getString(R.string.incorrect_IP_address_message), null, null, Snackbar.LENGTH_SHORT);
123122
} else {
124-
Log.v("Response", result);
123+
Log.v("ESPFragment", "ESP Connection Successful");
125124
}
126125
}
127126
}

app/src/main/java/io/pslab/others/ScienceLabCommon.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.util.Log;
44

5+
import java.io.IOException;
6+
57
import io.pslab.communication.CommunicationHandler;
68
import io.pslab.communication.ScienceLab;
79

@@ -31,6 +33,15 @@ public boolean openDevice(CommunicationHandler communicationHandler) {
3133
return true;
3234
}
3335

36+
public void openDevice() throws IOException {
37+
scienceLab = new ScienceLab();
38+
if (!isWifiConnected()) {
39+
Log.e(TAG, "Error in connection");
40+
return;
41+
}
42+
connected = true;
43+
}
44+
3445
public static ScienceLabCommon getInstance() {
3546
if (scienceLabCommon == null) {
3647
scienceLabCommon = new ScienceLabCommon();

0 commit comments

Comments
 (0)