Step 1) add dependacy for Android Volley in app gradle file dependancy section
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.android.volley:volley:1.1.1'
}
Step 2) Add INTERNET Permission to android Manifes file: AndroidManifest.xml
Step 3) Add code for Volley
public class MainActivity extends AppCompatActivity {
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txtrs = findViewById(R.id.txtrs);
Button btnControl = findViewById(R.id.btnControl);
final RequestQueue qu =Volley.newRequestQueue(this);
final String url = "https://www.iocare.in/discovery";
pb = findViewById(R.id.progressBar);
pb.setVisibility(View.GONE);
btnControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pb.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
pb.setVisibility(View.GONE);
txtrs.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
txtrs.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
qu.add(stringRequest);
}
});
btnActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentActivity = new Intent(getApplicationContext(),ActivityData.class);
intentActivity.putExtra("in.iocare.sqlitetest.data","Welcome to Data");
startActivity(intentActivity);
}
});
btnDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "https://www.iocare.in";
Uri webAddress = Uri.parse(url);
Intent browserIntent = new Intent(Intent.ACTION_VIEW,webAddress);
if(browserIntent.resolveActivity(getPackageManager())!=null){
startActivity(browserIntent);
}
}
});
}
}
Step 4:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char *ssid = "iocare";
const char *password = "iocare2018";
ESP8266WebServer server(80);
const int led1 = 5;
const int led2 = 4;
void handleControl() {
int lightno = server.arg("lightno").toInt();
int state = server.arg("state").toInt();
if (lightno == 1) {
if (state == 1) {
digitalWrite(led1, HIGH);
server.send(200, "text/html", "LED1 ON");
} else {
digitalWrite(led1, LOW);
server.send(200, "text/html", "LED1 OFF");
}
}
if (lightno == 2) {
if (state == 1) {
digitalWrite(led2, HIGH);
server.send(200, "text/html", "LED2 ON");
} else {
digitalWrite(led2, LOW);
server.send(200, "text/html", "LED2 OFF");
}
}
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void) {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, 0);
digitalWrite(led2, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/control", handleControl);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}