Das ist mein Script:
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class ActivityLichtEG extends AppCompatActivity implements OnDataSendToActivity5 {
Button btn_Licht1eg;
Button btn_Licht2eg;
Button btn_Licht3eg;
Button btn_Licht4eg;
Button btn_Licht5eg;
Button btn_Licht6eg;
Button btn_Licht7eg;
Button btn_Licht8eg;
Button btn_EGmenueLicht;
Button btn_LichtEGallean;
Button btn_LichtEGalleaus;
Button btn_LANverbinden;
ImageView ImageView_Hintergrungeg; // Hintergrung
TextView txt_verbinugenLichteg; // IP Adresse
TextView txt_ipAdresseLichteg; // Stauts
TextView textView_2;
public EditText editText_ip;
// IP Adresse laden
public String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/RLS";
String url5 = "(meineip/)"; //Define your NodeMCU ESP-ACA3E6
//String url5 = editText_ip.getText().toString();
////String url5 = editText_ip.getText().toString();
// String url5 = txt_ipAdresseLichteg .getText().toString();
//String url5 = (txt_ipAdresseLichteg.toString());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_licht_eg);
btn_Licht1eg = (Button) findViewById(R.id.btnLicht1eg);
btn_Licht2eg = (Button) findViewById(R.id.btnLicht2eg);
btn_Licht3eg = (Button) findViewById(R.id.btnLicht3eg);
btn_Licht4eg = (Button) findViewById(R.id.btnLicht4eg);
btn_Licht5eg = (Button) findViewById(R.id.btnLicht5eg);
btn_Licht6eg = (Button) findViewById(R.id.btnLicht6eg);
btn_Licht7eg = (Button) findViewById(R.id.btnLicht7eg);
btn_Licht8eg = (Button) findViewById(R.id.btnLicht8eg);
btn_EGmenueLicht = (Button) findViewById(R.id.btnEGmenueLicht);
btn_LichtEGallean = (Button) findViewById(R.id.btnLichtEGallean);
btn_LichtEGalleaus = (Button) findViewById(R.id.btnLichtEGalleaus);
btn_LANverbinden = (Button) findViewById(R.id.btnLanspeichern);
ImageView_Hintergrungeg = (ImageView) findViewById(R.id.ImageViewHintergrungeg);
txt_verbinugenLichteg = (TextView) findViewById(R.id.txtverbinugenLichteg);
txt_ipAdresseLichteg = (TextView) findViewById(R.id.txtipAdresseLichteg);
editText_ip = (EditText) findViewById(R.id.editTextip);
//TextView t2 = (TextView) findViewById(R.id.textView2);
//t2.setMovementMethod(LinkMovementMethod.getInstance());
// IP Adresse laden
File dir = new File (path);
dir.mkdirs();
// Iptext laden
{
File file = new File(path + "/serverip3.txt");
String [] loadText = Load(file);
String finalString = "";
for (int i = 0; i < loadText.length; i++)
{
finalString += loadText[i] + System.getProperty("line.separator");
}
txt_ipAdresseLichteg.setText(finalString);
editText_ip.setText(finalString);
}
// IP Adresse laden
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (isNetworkAvailable5()) {
ImageView_Hintergrungeg.setImageResource(R.drawable.background_on);
txt_verbinugenLichteg.setText("Verbunden mit der IP");
} else {
ImageView_Hintergrungeg.setImageResource(R.drawable.background);
txt_verbinugenLichteg.setText("Konnte keine Verbindung zum Server herstellen");
}
updateStatus5();
handler.postDelayed(this, 2000);
}
}, 5000); //the time is in miliseconds
btn_Licht1eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//String url5_rl = url5 + "Rel1"; // ezt Tür
String url5_rl = url5 + "Rel1";
//String url5_rl = url5 + "Rel1";
// String url5_rl = (txt_ipAdresseLichteg + "Rel1") ;
// String url5_rl = (txt_ipAdresseLichteg + "Rel1");
//String url5_rl = txt_ipAdresseLichteg.toString() + "Rel1";
//String url5_rl = url5 + "Rel1";
//textView_2.setText(txt_ipAdresseLichteg + "Rel1");
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_Licht2eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 + "Rel2"; // ST1
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_Licht3eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 +"Rel3"; // ST1
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_Licht4eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 +"Rel4"; // ST1
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_Licht5eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 + "Rel5";
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_Licht6eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 + "Rel6";
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_Licht7eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 +"Rel7";
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_Licht8eg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 + "Rel8";
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_LichtEGallean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 + "allean";
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_LichtEGalleaus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url5_rl = url5 + "alleaus";
SelectTask5 task = new SelectTask5(url5_rl);
task.execute();
updateStatus5();
}
});
btn_LANverbinden.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ActivityLichtEG.this, ActivityLAN.class);
startActivity(intent);
finish();
}
}); // Menü
btn_EGmenueLicht.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ActivityLichtEG.this, ActivityScrolling.class);
startActivity(intent);
finish();
}
});
}
// IP Adresse laden
public static String[] Load(File file)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
}
catch (FileNotFoundException e){e.printStackTrace(); }
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String test;
int anzahl = 0;
try {
while ((test = br.readLine()) != null) {
anzahl++;
}
}
catch (IOException e) {e.printStackTrace();}
try
{
fis.getChannel().position(0);
}
catch (IOException e) {e.printStackTrace();}
String[] array = new String[anzahl];
String line;
int i = 0;
try
{
while ((line = br.readLine()) != null)
{
array[i] = line;
i++;
}
}
catch (IOException e) {e.printStackTrace();}
return array;
}
private boolean isNetworkAvailable5() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
public void sendData5(String str) {
updateButtonStatus5(str);
}
private void updateStatus5() {
String url5_rl = url5 + "status5";
StatusTask5 task = new StatusTask5(url5_rl, this);
task.execute();
}
//Funktion zum Aktualisieren des Tastenstatus
private void updateButtonStatus5(String jsonStrings) {
try {
JSONObject json = new JSONObject(jsonStrings);
String Rel1 = json.getString("Rel1"); // btn_20 Ausgang: D5
String Rel2 = json.getString("Rel2"); // btn_21 Ausgang: D6
String Rel3 = json.getString("Rel3"); // btn_22 Ausgang: D7
String Rel4 = json.getString("Rel4"); // btn_23 Ausgang: D8
String Rel5 = json.getString("Rel5"); // btn_20 Ausgang: D5
String Rel6 = json.getString("Rel6"); // btn_20 Ausgang: D5
String Rel7 = json.getString("Rel7"); // btn_20 Ausgang: D5
String Rel8 = json.getString("Rel8"); // btn_20 Ausgang: D5
String allean = json.getString("allean"); // btn_20 Ausgang: D5
String alleaus = json.getString("alleaus"); // btn_20 Ausgang: D5
if (Rel1.equals("1")) {
btn_Licht1eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht1eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (Rel2.equals("1")) {
btn_Licht2eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht2eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (Rel3.equals("1")) {
btn_Licht3eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht3eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (Rel4.equals("1")) {
btn_Licht4eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht4eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (Rel5.equals("1")) {
btn_Licht5eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht5eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (Rel6.equals("1")) {
btn_Licht6eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht6eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (Rel7.equals("1")) {
btn_Licht7eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht7eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (Rel8.equals("1")) {
btn_Licht8eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht8eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (allean.equals("1")) {
btn_Licht1eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht2eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht3eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht4eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht5eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht6eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht7eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht8eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht1eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht2eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht3eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht4eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht5eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht6eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht7eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht8eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
if (alleaus.equals("1")) {
btn_Licht1eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht2eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
btn_Licht3eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_on);
} else {
btn_Licht1eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht2eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
btn_Licht3eg.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.power_off);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}