├── Android studio
├── Connectivity.java
├── AndroidManifest.xml
├── build.gradle
├── activity_1.java
├── activity_1.xml
├── activity_2.java
└── activity_2.xml
├── README.md
└── Esp32_from_and_to_android_studio.ino
/Android studio/Connectivity.java:
--------------------------------------------------------------------------------
1 | package com.example.flowcalibration2;
2 |
3 | import java.io.IOException;
4 |
5 | import okhttp3.OkHttpClient;
6 | import okhttp3.Request;
7 | import okhttp3.Response;
8 |
9 | public class Connectivity {
10 | public static String geturl (String url_esp32){
11 |
12 | OkHttpClient client = new OkHttpClient();
13 |
14 | Request request = new Request.Builder()
15 | .url(url_esp32)
16 | .build();
17 |
18 | try {
19 | Response response = client.newCall(request).execute();
20 | return response.body().string();
21 |
22 | } catch (IOException error) {
23 | return error.toString();
24 | }
25 |
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ESP32-Android-Studio
2 | ESP32 Android Studio
3 |
4 | This project explain how to receive data from ESP32 and show the value on Android App
5 |
6 | and send data to ESP32 from Android App which we build for turning on LED or something
7 |
8 | Just for infomartion, ESP32 and ESP8266 have same principle work also programming.
9 | But, if u try to use this program and compiling to ESP8266 change library " #include wifi.h" into "#include esp8266wifi.h"
10 |
11 | i already share how to build this project on my YouTube channel. U guys can support me to subs and like the video.
12 |
13 | I separate the video into 3 part :
14 |
15 | part 1 : Send data From Web to ESP32 (https://youtu.be/8UWpnN-A0do)
16 |
17 | part 2 : Receive data on Web from ESP32 (https://youtu.be/PTqr-8-afkc)
18 |
19 | part 3 : Receive and send data ESP32 android app (https://youtu.be/xBh8qh2zZ0k)
20 |
21 |
--------------------------------------------------------------------------------
/Android studio/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/Android studio/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | }
4 |
5 | android {
6 | compileSdkVersion 30
7 | buildToolsVersion "30.0.3"
8 |
9 | defaultConfig {
10 | applicationId "com.example.flowcalibration2"
11 | minSdkVersion 23
12 | targetSdkVersion 30
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_1_8
27 | targetCompatibility JavaVersion.VERSION_1_8
28 | }
29 | }
30 |
31 | dependencies {
32 |
33 | implementation 'androidx.appcompat:appcompat:1.2.0'
34 | implementation 'com.google.android.material:material:1.3.0'
35 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
36 | testImplementation 'junit:junit:4.+'
37 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
38 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
39 | implementation 'com.squareup.okhttp3:okhttp:4.9.0'
40 | }
41 |
--------------------------------------------------------------------------------
/Android studio/activity_1.java:
--------------------------------------------------------------------------------
1 | // Little project from wiwidnadw
2 | // program use java language
3 | //u can reach me on https://github.com/wiwidnadw
4 | //https://www.youtube.com/channel/UClxwaaJ-or0SJtlWMi3pHpA
5 | // line : wiwidnadw
6 | //gmail : nurahmaddw@gmail.com
7 |
8 | package com.example.flowcalibration2;
9 |
10 | import android.content.Intent;
11 | import android.os.Bundle;
12 | import android.view.View;
13 | import android.widget.Button;
14 | import android.widget.EditText;
15 |
16 | import androidx.appcompat.app.AppCompatActivity;
17 |
18 | public class activity_1 extends AppCompatActivity {
19 | private EditText ip;
20 | private Button enter_ip;
21 | public static String ip_address;
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_1);
27 |
28 | ip=(EditText) findViewById(R.id.edit_ip_address);
29 | enter_ip=(Button) findViewById(R.id.button_ip);
30 |
31 | enter_ip.setOnClickListener(new View.OnClickListener() {
32 | @Override
33 | public void onClick(View v) {
34 | ip_address = ip.getText().toString();
35 | Intent intent = new Intent(activity_1.this, activity_2.class);
36 | startActivity(intent);
37 |
38 |
39 | }
40 |
41 | });
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Android studio/activity_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
19 |
20 |
29 |
30 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/Esp32_from_and_to_android_studio.ino:
--------------------------------------------------------------------------------
1 | //This project tell how to receive and send data between ESP32 and Android app.
2 | //also u can see on web by type the ip address
3 | //github : https://github.com/wiwidnadw
4 | //YouTube: Digitalneering (https://www.youtube.com/channel/UClxwaaJ-or0SJtlWMi3pHpA)
5 | //Line : wiwidnadw
6 |
7 | #include
8 |
9 | IPAddress ip(192, 168, 43, 150);
10 | IPAddress gateway(192, 168, 43, 1);
11 | IPAddress subnet(255, 255, 255, 0);
12 | IPAddress dns(192, 168, 43, 1);
13 |
14 | #define potentio 34
15 | #define led1 26
16 | #define led2 27
17 | #define led3 25
18 |
19 |
20 | const char* ssid = "ur network";
21 | const char* password = "ur password";
22 |
23 | WiFiServer server(80);
24 |
25 | void setup()
26 | {
27 | Serial.begin(115200);
28 | pinMode(led1, OUTPUT);
29 | pinMode(led2, OUTPUT);
30 | pinMode(led3, OUTPUT);
31 | pinMode(potentio, INPUT);
32 |
33 | delay(10);
34 |
35 | // We start by connecting to a WiFi network
36 |
37 | Serial.println();
38 | Serial.println();
39 | Serial.print("Connecting to ");
40 | Serial.println(ssid);
41 |
42 | WiFi.config(ip,dns,gateway,subnet);
43 | WiFi.begin(ssid, password);
44 |
45 | while (WiFi.status() != WL_CONNECTED) {
46 | delay(500);
47 | Serial.print(".");
48 | }
49 |
50 | Serial.println("");
51 | Serial.println("WiFi connected.");
52 | Serial.println("IP address: ");
53 | Serial.println(WiFi.localIP());
54 |
55 | server.begin();
56 |
57 | }
58 |
59 | int value = 0;
60 |
61 | void loop(){
62 | WiFiClient client = server.available();
63 | if (!client) {
64 | return;
65 | }
66 | Serial.println("New Client.");
67 | while(!client.available()) {
68 | delay(1);
69 | }
70 |
71 | String request = client.readStringUntil('\r');
72 | Serial.print(request);
73 |
74 |
75 | if(request.indexOf("led1") != -1 ) {
76 | digitalWrite(led1, !digitalRead(led1));
77 | }
78 | if(request.indexOf("led2") != -1 ) {
79 | digitalWrite(led2, !digitalRead(led2));
80 | }
81 | if(request.indexOf("led3") != -1 ) {
82 | digitalWrite(led3, !digitalRead(led3));
83 | }
84 |
85 | int sensorvalue = analogRead(potentio);
86 | Serial.print(sensorvalue);
87 |
88 |
89 | client.println("HTTP/1.1 200 OK");
90 | client.println("Content-type:text/html");
91 | client.println("");
92 |
93 |
94 | client.print(",");
95 | client.print(sensorvalue);
96 | delay(1);
97 |
98 |
99 | }
100 |
101 |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/Android studio/activity_2.java:
--------------------------------------------------------------------------------
1 | // Little project from wiwidnadw
2 | // program use java language
3 | //u can reach me on https://github.com/wiwidnadw
4 | //https://www.youtube.com/channel/UClxwaaJ-or0SJtlWMi3pHpA
5 | // line : wiwidnadw
6 | //gmail : nurahmaddw@gmail.com
7 |
8 | package com.example.flowcalibration2;
9 |
10 | import android.content.Context;
11 | import android.net.ConnectivityManager;
12 | import android.net.NetworkInfo;
13 | import android.os.AsyncTask;
14 | import android.os.Bundle;
15 | import android.os.Handler;
16 |
17 | import android.util.Log;
18 | import android.view.View;
19 | import android.widget.ArrayAdapter;
20 | import android.widget.Button;
21 | import android.widget.Spinner;
22 | import android.widget.TextView;
23 | import android.widget.Toast;
24 |
25 | import androidx.appcompat.app.AppCompatActivity;
26 |
27 |
28 |
29 | import static com.example.flowcalibration2.activity_1.ip_address;
30 |
31 |
32 | public class activity_2 extends AppCompatActivity {
33 | private static Button valve1on, valve1off, valve2on, valve2off, valve3on, valve3off, save_flow;
34 | private Spinner spflowunit;
35 | private String[] flowunit = {
36 | "L/h",
37 | "t/h", "m3/h",
38 | };
39 |
40 | TextView txvalue;
41 | Handler handler = new Handler();
42 | boolean statusdevice = true;
43 |
44 | @Override
45 | protected void onCreate(Bundle savedInstanceState) {
46 | super.onCreate(savedInstanceState);
47 | setContentView(R.layout.activity_2);
48 |
49 | spflowunit = (Spinner) findViewById(R.id.spinner);
50 | // inisialiasi Array Adapter dengan memasukkan string array di atas
51 | final ArrayAdapter adapter = new ArrayAdapter<>(this,
52 | android.R.layout.simple_spinner_item, flowunit);
53 |
54 | // mengeset Array Adapter tersebut ke Spinner
55 | spflowunit.setAdapter(adapter);
56 | save_flow = (Button) findViewById(R.id.save_flow);
57 |
58 | save_flow.setOnClickListener(new View.OnClickListener() {
59 | @Override
60 | public void onClick(View v) {
61 | TextView rangeunit = findViewById(R.id.range_unit);
62 | TextView txunit = findViewById(R.id.tx_unit);
63 |
64 | String setflowunit = String.valueOf(spflowunit.getSelectedItem());
65 | rangeunit.setText(setflowunit);
66 | txunit.setText(setflowunit);
67 |
68 | }
69 | });
70 |
71 |
72 | valve1on = (Button) findViewById(R.id.valve1_on);
73 | valve1off = (Button) findViewById(R.id.valve1_off);
74 | valve2on = (Button) findViewById(R.id.valve2_on);
75 | valve2off = (Button) findViewById(R.id.valve2_off);
76 | valve3on = (Button) findViewById(R.id.valve3_on);
77 | valve3off = (Button) findViewById(R.id.valve3_off);
78 |
79 | txvalue = (TextView ) findViewById(R.id.tx_value);
80 |
81 | handler.postDelayed(status_data,0);
82 |
83 | valve1on.setOnClickListener(new View.OnClickListener() {
84 | @Override
85 | public void onClick(View v) {
86 | {
87 | request_to_url("led1");
88 | }
89 |
90 | }
91 | });
92 | valve1off.setOnClickListener(new View.OnClickListener() {
93 | @Override
94 | public void onClick(View v) {
95 |
96 | }
97 | });
98 | valve2on.setOnClickListener(new View.OnClickListener() {
99 | @Override
100 | public void onClick(View v) {
101 |
102 |
103 | }
104 | });
105 | valve2off.setOnClickListener(new View.OnClickListener() {
106 | @Override
107 | public void onClick(View v) {
108 |
109 | }
110 | });
111 | valve3on.setOnClickListener(new View.OnClickListener() {
112 | @Override
113 | public void onClick(View v) {
114 |
115 |
116 | }
117 | });
118 | valve3off.setOnClickListener(new View.OnClickListener() {
119 | @Override
120 | public void onClick(View v) {
121 |
122 | }
123 | });
124 | }
125 |
126 | private Runnable status_data = new Runnable() {
127 | @Override
128 | public void run() {
129 | if (statusdevice) {
130 | request_to_url("");
131 | handler.postDelayed(this, 2000);
132 | Log.d("Status", "Connectivity_esp32");
133 | }else {
134 | handler.removeCallbacks(status_data);
135 | Log.d("Status","Finalizado");
136 | }
137 | }
138 | };
139 |
140 | @Override
141 | protected void onDestroy() {
142 | super.onDestroy();
143 | statusdevice = false;
144 | }
145 |
146 | public void request_to_url (String command) {
147 | ConnectivityManager connMgr = (ConnectivityManager)
148 | getSystemService(Context.CONNECTIVITY_SERVICE);
149 | NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
150 |
151 | if(networkInfo != null && networkInfo.isConnected()) {
152 |
153 | new request_data().execute("http://" + ip_address + "/" + command);
154 |
155 | }else {
156 | Toast.makeText(activity_2.this, "Not connected ", Toast.LENGTH_LONG).show();
157 |
158 | }
159 | }
160 |
161 | private class request_data extends AsyncTask {
162 |
163 | @Override
164 | protected String doInBackground(String... url) {
165 |
166 | return Connectivity.geturl(url[0]);
167 |
168 | }
169 |
170 | @Override
171 | protected void onPostExecute(String result_data) {
172 | if(result_data !=null) {
173 | txvalue.setText(result_data);
174 |
175 | }else{
176 |
177 | Toast.makeText(activity_2.this, "Null data", Toast.LENGTH_LONG).show();
178 | }
179 | }
180 | }
181 |
182 |
183 | }
184 |
185 |
186 |
--------------------------------------------------------------------------------
/Android studio/activity_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
18 |
19 |
29 |
30 |
40 |
41 |
51 |
52 |
62 |
63 |
76 |
77 |
86 |
87 |
96 |
97 |
106 |
107 |
116 |
117 |
126 |
127 |
140 |
141 |
151 |
152 |
162 |
163 |
173 |
174 |
183 |
184 |
185 |
195 |
196 |
206 |
207 | />
216 |
217 |
218 |
--------------------------------------------------------------------------------