Skip to content

Commit 23fc424

Browse files
neel1998makoteq
authored andcommitted
interface for ESP wifi module added
1 parent a326364 commit 23fc424

File tree

10 files changed

+263
-8
lines changed

10 files changed

+263
-8
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ dependencies {
107107
implementation 'com.github.GoodieBag:ProtractorView:v1.2'
108108
implementation 'com.github.Triggertrap:SeekArc:v1.1'
109109

110+
implementation "com.squareup.okhttp3:okhttp:$rootProject.okHttpVersion"
111+
110112
implementation "com.jakewharton:butterknife:$rootProject.butterKnifeVersion"
111113
annotationProcessor "com.jakewharton:butterknife-compiler:$rootProject.butterKnifeVersion"
112114

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.support.v7.app.AppCompatActivity;
2525
import android.support.v7.widget.Toolbar;
2626
import android.util.Log;
27+
import android.view.LayoutInflater;
2728
import android.view.Menu;
2829
import android.view.MenuInflater;
2930
import android.view.MenuItem;
@@ -41,6 +42,7 @@
4142
import io.pslab.communication.CommunicationHandler;
4243
import io.pslab.fragment.AboutUsFragment;
4344
import io.pslab.fragment.BluetoothScanFragment;
45+
import io.pslab.fragment.ESPFragment;
4446
import io.pslab.fragment.FAQFragment;
4547
import io.pslab.fragment.HomeFragment;
4648
import io.pslab.fragment.InstrumentsFragment;
@@ -406,22 +408,51 @@ private void attemptToConnectPSLab() {
406408
initialisationDialog.dismiss();
407409
Toast.makeText(this, getString(R.string.device_not_found), Toast.LENGTH_SHORT).show();
408410
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
409-
.setMessage(getResources().getString(R.string.bluetooth_scan_dialog))
411+
.setMessage(getResources().getString(R.string.bluetooth_wifi_scan_dialog))
410412
.setPositiveButton(getResources().getString(R.string.bluetooth_scan_text), new DialogInterface.OnClickListener() {
411413
@Override
412414
public void onClick(DialogInterface dialog, int which) {
413-
BluetoothScanFragment bluetoothScanFragment = new BluetoothScanFragment();
414-
bluetoothScanFragment.show(getSupportFragmentManager(), "bluetooth");
415-
bluetoothScanFragment.setCancelable(true);
415+
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
416+
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
417+
View dialogView = inflater.inflate(R.layout.bluetooth_wifi_dialog_layout, null);
418+
dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
419+
@Override
420+
public void onClick(DialogInterface dialogInterface, int i) {
421+
dialog.cancel();
422+
}
423+
});
424+
425+
dialogView.findViewById(R.id.bluetooth_btn).setOnClickListener(new View.OnClickListener() {
426+
@Override
427+
public void onClick(View view) {
428+
BluetoothScanFragment bluetoothScanFragment = new BluetoothScanFragment();
429+
bluetoothScanFragment.show(getSupportFragmentManager(), "bluetooth");
430+
bluetoothScanFragment.setCancelable(true);
431+
dialog.cancel();
432+
}
433+
});
434+
435+
dialogView.findViewById(R.id.wifi_btn).setOnClickListener(new View.OnClickListener() {
436+
@Override
437+
public void onClick(View view) {
438+
ESPFragment espFragment = new ESPFragment();
439+
espFragment.show(getSupportFragmentManager(), "wifi");
440+
espFragment.setCancelable(true);
441+
dialog.cancel();
442+
}
443+
});
444+
445+
dialogBuilder.setView(dialogView)
446+
.create()
447+
.show();
416448
}
417449
})
418450
.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
419451
@Override
420452
public void onClick(DialogInterface dialog, int which) {
421453
dialog.cancel();
422454
}
423-
})
424-
.create();
455+
}).create();
425456
dialog.show();
426457
}
427458
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package io.pslab.fragment;
2+
3+
import android.os.AsyncTask;
4+
import android.os.Bundle;
5+
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
7+
import android.support.v4.app.DialogFragment;
8+
import android.util.Log;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.Button;
13+
import android.widget.EditText;
14+
import android.widget.ProgressBar;
15+
import android.widget.Toast;
16+
17+
import java.io.IOException;
18+
19+
import io.pslab.R;
20+
import okhttp3.OkHttpClient;
21+
import okhttp3.Request;
22+
import okhttp3.Response;
23+
24+
public class ESPFragment extends DialogFragment {
25+
private String espIPAddress = "";
26+
private ProgressBar espConnectProgressBar;
27+
private Button espConnectBtn;
28+
29+
@Override
30+
public void onCreate(@Nullable Bundle savedInstanceState) {
31+
super.onCreate(savedInstanceState);
32+
}
33+
34+
@Nullable
35+
@Override
36+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
37+
View rootView = inflater.inflate(R.layout.fragment_esp, container, false);
38+
EditText espIPEditText = rootView.findViewById(R.id.esp_ip_edit_text);
39+
espConnectBtn = rootView.findViewById(R.id.esp_connect_btn);
40+
espConnectProgressBar = rootView.findViewById(R.id.esp_connect_progressbar);
41+
espConnectBtn.setOnClickListener(new View.OnClickListener() {
42+
@Override
43+
public void onClick(View v) {
44+
espIPAddress = espIPEditText.getText().toString();
45+
if (espIPAddress.length() == 0) {
46+
Toast.makeText(getContext(), getResources().getString(R.string.incorrect_IP_address_message), Toast.LENGTH_SHORT).show();
47+
} else {
48+
new ESPTask().execute();
49+
}
50+
}
51+
});
52+
return rootView;
53+
}
54+
55+
@Override
56+
public void onResume() {
57+
super.onResume();
58+
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
59+
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
60+
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
61+
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
62+
}
63+
64+
private class ESPTask extends AsyncTask<Void, Void, String> {
65+
66+
@Override
67+
protected void onPreExecute() {
68+
espConnectBtn.setVisibility(View.GONE);
69+
espConnectProgressBar.setVisibility(View.VISIBLE);
70+
}
71+
72+
@Override
73+
protected String doInBackground(Void... voids) {
74+
String result = "";
75+
try {
76+
OkHttpClient client = new OkHttpClient();
77+
Request request = new Request.Builder()
78+
.url("http://" + espIPAddress)
79+
.build();
80+
Response response = client.newCall(request).execute();
81+
result = response.body().string();
82+
} catch (IOException e) {
83+
e.printStackTrace();
84+
}
85+
return result;
86+
}
87+
88+
@Override
89+
protected void onPostExecute(String result) {
90+
espConnectProgressBar.setVisibility(View.GONE);
91+
espConnectBtn.setVisibility(View.VISIBLE);
92+
if (result.length() == 0) {
93+
Toast.makeText(getContext(), getResources().getString(R.string.incorrect_IP_address_message), Toast.LENGTH_SHORT).show();
94+
} else {
95+
Log.v("Response", result);
96+
}
97+
}
98+
}
99+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24.0" android:viewportWidth="24.0"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#FF000000" android:pathData="M17.71,7.71L12,2h-1v7.59L6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 11,14.41L11,22h1l5.71,-5.71 -4.3,-4.29 4.3,-4.29zM13,5.83l1.88,1.88L13,9.59L13,5.83zM14.88,16.29L13,18.17v-3.76l1.88,1.88z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24.0" android:viewportWidth="24.0"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#FF000000" android:pathData="M1,9l2,2c4.97,-4.97 13.03,-4.97 18,0l2,-2C16.93,2.93 7.08,2.93 1,9zM9,17l3,3 3,-3c-1.65,-1.66 -4.34,-1.66 -6,0zM5,13l2,2c2.76,-2.76 7.24,-2.76 10,0l2,-2C15.14,9.14 8.87,9.14 5,13z"/>
5+
</vector>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:layout_gravity="center_horizontal"
6+
android:layout_margin="@dimen/dialog_space_width">
7+
8+
<io.pslab.items.SquareLinearLayout
9+
android:id="@+id/bluetooth_btn"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:layout_margin="@dimen/dialog_space_width"
13+
android:layout_weight="1"
14+
android:background="@drawable/btn_back_rounded"
15+
android:orientation="vertical"
16+
android:paddingTop="@dimen/btn_padding"
17+
android:stateListAnimator="@animator/selector_animator">
18+
19+
<TextView
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:gravity="center"
23+
android:maxLines="1"
24+
android:paddingLeft="@dimen/btn_title_side_padding"
25+
android:paddingRight="@dimen/btn_title_side_padding"
26+
android:text="@string/bluetooth"
27+
android:textColor="@color/white"
28+
android:textStyle="bold" />
29+
30+
<ImageView
31+
android:layout_width="@dimen/bluetooth_wifi_icon_dimen"
32+
android:layout_height="@dimen/bluetooth_wifi_icon_dimen"
33+
android:layout_gravity="center"
34+
android:layout_margin="@dimen/btn_margin"
35+
android:src="@drawable/ic_bluetooth" />
36+
37+
</io.pslab.items.SquareLinearLayout>
38+
39+
<View
40+
android:layout_width="@dimen/separator_width"
41+
android:layout_height="match_parent"
42+
android:layout_marginTop="@dimen/dialog_sep_margin_top"
43+
android:layout_marginBottom="@dimen/dialog_sep_margin_top"
44+
android:background="@color/grey_light" />
45+
46+
<io.pslab.items.SquareLinearLayout
47+
android:id="@+id/wifi_btn"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
android:layout_margin="@dimen/dialog_space_width"
51+
android:layout_weight="1"
52+
android:background="@drawable/btn_back_rounded"
53+
android:orientation="vertical"
54+
android:paddingTop="@dimen/btn_padding"
55+
android:stateListAnimator="@animator/selector_animator">
56+
57+
<TextView
58+
android:layout_width="match_parent"
59+
android:layout_height="wrap_content"
60+
android:gravity="center"
61+
android:maxLines="1"
62+
android:text="@string/wifi"
63+
android:textColor="@color/white"
64+
android:textStyle="bold" />
65+
66+
<ImageView
67+
android:layout_width="@dimen/bluetooth_wifi_icon_dimen"
68+
android:layout_height="@dimen/bluetooth_wifi_icon_dimen"
69+
android:layout_gravity="center"
70+
android:layout_margin="@dimen/btn_margin"
71+
android:src="@drawable/ic_wifi" />
72+
</io.pslab.items.SquareLinearLayout>
73+
74+
</LinearLayout>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:padding="@dimen/esp_fragment_padding">
6+
7+
<EditText
8+
android:id="@+id/esp_ip_edit_text"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:layout_centerHorizontal="true"
12+
android:hint="@string/enter_IP_hint" />
13+
14+
<Button
15+
android:id="@+id/esp_connect_btn"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:layout_below="@+id/esp_ip_edit_text"
19+
android:layout_centerHorizontal="true"
20+
android:background="@color/colorPrimary"
21+
android:text="@string/esp_connect_text"
22+
android:textColor="@color/white" />
23+
24+
<ProgressBar
25+
android:id="@+id/esp_connect_progressbar"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:layout_below="@+id/esp_ip_edit_text"
29+
android:layout_centerHorizontal="true"
30+
android:visibility="gone" />
31+
</RelativeLayout>

app/src/main/res/values/dimens.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@
328328
<dimen name="seekarc_padding">30dp</dimen>
329329
<dimen name="seekarc_edit_text">36sp</dimen>
330330

331+
<dimen name="esp_fragment_padding">30dp</dimen>
332+
<dimen name="bluetooth_wifi_icon_dimen">50dp</dimen>
333+
331334
<dimen name="create_config_margin1">10dp</dimen>
332335
<dimen name="create_config_margin2">20dp</dimen>
333336
<dimen name="create_config_margin3">22dp</dimen>

app/src/main/res/values/strings.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
<string name="pslab_pinlayout_front">Pin Layout Front</string>
1919
<string name="pslab_pinlayout_back">Pin Layout Back</string>
2020
<string name="bluetooth_connect_menu">Bluetooth Connection</string>
21-
<string name="bluetooth_scan_dialog">USB device not found. Do you want to scan for Bluetooth Connection?</string>
22-
21+
<string name="bluetooth_wifi_scan_dialog">USB device not found. Do you want to scan for Bluetooth or Wifi connection?</string>
22+
<string name="bluetooth">Bluetooth</string>
23+
<string name="wifi">WiFi</string>
2324
<string name="bluetooth_scan_text">Scan</string>
2425
<string name="bluetooth_stop_text">Stop</string>
2526
<string name="scanned_devices_list_title">Scanned Devices</string>
2627
<string name="bluetooth_not_supported">Your phone does not support bluetooth</string>
28+
<string name="incorrect_IP_address_message">Please enter valid IP address</string>
29+
<string name="enter_IP_hint">Enter IP address of ESP module</string>
30+
<string name="esp_connect_text">Connect</string>
2731

2832
<string name="oscilloscope">Oscilloscope</string>
2933
<string name="multimeter">Multimeter</string>

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ext {
5757
carouselPickerVersion = 'v1.0'
5858
speedView = '1.2.0'
5959
realmAdapter = '2.1.1'
60+
okHttpVersion = '4.0.1'
6061

6162
// Map related versions
6263
osmVersion = '5.1'

0 commit comments

Comments
 (0)