Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.user.myapplication;
- import android.os.Build;
- import android.graphics.Color; //色彩
- import android.os.Bundle; //手機資訊
- import android.os.Handler; //畫面更新
- import android.provider.Settings; //系統屬性
- import android.support.v7.app.AppCompatActivity; //專案
- import android.os.Bundle; //資料傳遞
- import android.text.StaticLayout;
- import android.util.Log; //查詢錯誤
- import android.view.View;
- import android.webkit.WebChromeClient;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.Switch;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.eclipse.paho.android.service.MqttAndroidClient;
- import org.eclipse.paho.client.mqttv3.IMqttActionListener;
- import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
- import org.eclipse.paho.client.mqttv3.IMqttToken;
- import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
- import org.eclipse.paho.client.mqttv3.MqttCallback;
- import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
- import org.eclipse.paho.client.mqttv3.MqttClient;
- import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
- import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
- import org.eclipse.paho.client.mqttv3.MqttException;
- import org.eclipse.paho.client.mqttv3.MqttMessage;
- import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
- import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.widget.Toolbar;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.webkit.WebView;
- import android.webkit.WebSettings;
- import android.webkit.WebViewClient;
- public class MainActivity extends AppCompatActivity {
- MqttAndroidClient client;
- MqttClient client1;
- MqttAsyncClient client2;
- TextView bugtext;
- Switch myswitch;
- MqttConnectOptions options;
- TextView ttv4, ttv5, ttv2, ttv6;
- WebView mWebView;
- Switch swstatus1, swstatus2, swstatus3;
- EditText edt;
- Button button;
- //public static float tempint;
- Float tempint, temedt;
- Integer ii;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- //ttv4 = (TextView) findViewById(R.id.textView4);
- //ttv5 = (TextView) findViewById(R.id.textView5);
- ttv2 = (TextView) findViewById(R.id.textView2);
- ttv6 = (TextView) findViewById(R.id.textView6);
- swstatus1 = (Switch) findViewById(R.id.switch1);
- swstatus1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- if (buttonView.isChecked()) { //當開關開啟時傳送ON訊號,關閉時傳送OFF訊號
- ON2();
- }
- else {
- OFF2();
- }
- }
- });
- swstatus2 = (Switch) findViewById(R.id.switch2);
- swstatus2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- if (buttonView.isChecked()) { //當開關開啟時傳送ON2訊號,關閉時傳送OFF2訊號
- ON();
- } else {
- OFF();
- }
- }
- });
- swstatus3 = (Switch) findViewById(R.id.switch3);
- swstatus3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- if (buttonView.isChecked()) { //當開關開啟時傳送FAN_ON訊號,關閉時傳送FAN_OFF訊號
- FAN_ON(buttonView);
- } else {
- FAN_OFF(buttonView);
- }
- }
- });
- //edt = (EditText) findViewById(R.id.editText);
- button =(Button) findViewById(R.id.button);
- //Float tempint, temedt ;
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //temedt = Float.parseFloat(edt.getText().toString());
- /*float temedt ,tempfloat;
- temedt = Float.parseFloat(edt.getText().toString());
- tempfloat =Float.parseFloat(edt.getText().toString());
- if (tempint >= temedt){
- //ON2();
- //ttv5.setText(Integer.valueOf(temp));
- ii = 1;
- }else if(tempint < temedt){
- ii = 0;
- }*/
- }
- });
- /*Spinner spinner = (Spinner)findViewById(R.id.spinner);
- final ArrayAdapter<CharSequence> lunchList = ArrayAdapter.createFromResource(MainActivity.this,
- R.array.lunch,
- android.R.layout.simple_spinner_dropdown_item);
- spinner.setAdapter(lunchList);
- spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- Toast.makeText(MainActivity.this, "你選的是" + lunchList.getItem(position),Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });*/
- //myswitch = (Switch)findViewById(R.id.switch1);
- //myswitch.setOnCheckedChangeListener(this);
- String clientId = MqttClient.generateClientId(); //定義MqttClient型態
- client = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.4.1:1883", clientId);
- // 設定MqttServer端ip
- options = new MqttConnectOptions(); //定義MqttClient設定型態
- options.setAutomaticReconnect(true); //設定開啟自動連接
- options.setCleanSession(true);
- MqttClientPersistence persistence = new MqttDefaultFilePersistence("/tmp");
- //MemoryPersistence dataStore = new MemoryPersistence();
- //client1 = new MqttClient("tcp://192.168.2.1:1883", clientid, new MemoryPersistence());
- //client2 = new MqttAsyncClient("tcp://192.168.2.1:1883",clientid2,dataStore);
- try {
- client.connect(); //連接MqttServer端
- IMqttToken token = client.connect();
- token.setActionCallback(new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- Toast.makeText(MainActivity.this, "成功登入", Toast.LENGTH_SHORT).show();
- try {
- client.subscribe("esp8266", 0, null, new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken iMqttToken) {
- Toast.makeText(MainActivity.this, "訂閱成功", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
- Toast.makeText(MainActivity.this, "訂閱失敗", Toast.LENGTH_SHORT).show();
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- Toast.makeText(MainActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- }
- /*try {
- IMqttToken token1 = client.connect();
- token1.setActionCallback(new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken iMqttToken) {
- Toast.makeText(MainActivity.this, "成功登入",Toast.LENGTH_LONG).show();
- }
- @Override
- public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
- Toast.makeText(MainActivity.this, "成功登入",Toast.LENGTH_LONG).show();
- }
- });
- } catch (MqttException e){
- e.printStackTrace();
- bugtext.setText(e.getMessage());
- }*/
- // 取得XML中的WebView
- mWebView = (WebView) findViewById(R.id.web1);
- // WebView的設定選項
- WebSettings webSettings = mWebView.getSettings();
- // Enable Javascript
- webSettings.setJavaScriptEnabled(true);
- // Enable LocalStorage
- webSettings.setDomStorageEnabled(true);
- webSettings.setSupportZoom(true);
- webSettings.setBuiltInZoomControls(true);
- webSettings.setLoadWithOverviewMode(true);
- webSettings.setLoadsImagesAutomatically(true);
- WebChromeClient mwebchromeclient = new WebChromeClient();
- // 要加setWebViewClient以避免點連結時跳出APP用瀏覽器開啟
- mWebView.setWebViewClient(new WebViewClient());
- // 要設定 WebChromeClient 才能支援 JS 的 Alert, Confirm
- mWebView.setWebChromeClient(new WebChromeClient());
- // API 19 以上可使用硬體加速
- if (Build.VERSION.SDK_INT >= 19) {
- mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
- } else {
- mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
- }
- //mWebView.setBackgroundColor(Color.BLACK);
- mWebView.loadUrl("https://www.youtube.com/watch?v=UdTkLbqD598");
- //mWebView.loadUrl("http://www.google.com");
- //mWebView.loadUrl();
- }
- Handler myHandler = new Handler();
- public void ON() {
- String topic = "esp8266";
- String message = "5on";
- try {
- client.publish(topic, message.getBytes(),0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void OFF() {
- String topic = "esp8266";
- String message = "5off";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void ON2() {
- String topic = "esp8266"; //設定訂閱主題esp8266
- String message = "abc123"; //設定傳送的訊息 5on2
- try {
- client.publish(topic, message.getBytes(), 0, false); //訂閱主題
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void OFF2() {
- String topic = "esp8266"; //設定訂閱主題esp8266
- String message = "5off2"; //設定傳送的訊息 5on2
- try {
- client.publish(topic, message.getBytes(), 0, false); //訂閱主題
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void RESTART(View v) {
- String topic = "esp8266";
- String message = "1restart";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void UP(View v) {
- String topic = "esp8266";
- String message = "2on";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- new Handler().postDelayed(new Runnable() {
- public void run() {
- // do something
- String topic = "esp8266";
- String message = "2stop";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- }, 7 * 100);
- }
- public void STOP(View v) {
- String topic = "esp8266";
- String message = "2stop";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void DOWN(View v) {
- String topic = "esp8266";
- String message = "2off";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- new Handler().postDelayed(new Runnable() {
- public void run() {
- // do something
- String topic = "esp8266";
- String message = "2stop";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- }, 7 * 100);
- }
- public void RESTART2(View v) {
- String topic = "esp8266";
- String message = "2restart";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void FAN_ON(View v) {
- String topic = "esp8266";
- String message = "3on";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void FAN_OFF(View v) {
- String topic = "esp8266";
- String message = "3off";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void RESTART3(View v) {
- String topic = "esp8266";
- String message = "3restart";
- try {
- client.publish(topic, message.getBytes(), 0, false);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void conn() {
- try {
- client.connect();
- IMqttToken token = client.connect(options);
- token.setActionCallback(new IMqttActionListener() {
- public void onSuccess(IMqttToken asyncActionToken) {
- Toast.makeText(MainActivity.this, "成功登入", Toast.LENGTH_SHORT).show();
- }
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- Toast.makeText(MainActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- bugtext.setText(e.getReasonCode());
- }
- boolean a = client.isConnected();
- //bugtext.setText("");
- }
- public void SETTEXT(Integer i) {
- if (i == 1) {
- ON2();
- } else if (i == 0) {
- OFF2();
- }
- }
- public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
- //Toast.makeText(MainActivity.this, mqttMessage.toString(),Toast.LENGTH_SHORT).show();
- //ttv5.setText(mqttMessage.toString());
- String op = "open";
- String cl = "close";
- String tem = mqttMessage.toString();
- if (mqttMessage.toString().equals(op) == true) {
- ttv4.setText(mqttMessage.toString());
- ON();
- } else if (mqttMessage.toString().equals(cl) == true) {
- ttv4.setText(mqttMessage.toString());
- OFF();
- }
- String humi = mqttMessage.toString().substring(mqttMessage.toString().indexOf(",") + 1);
- String temp = mqttMessage.toString().substring(0, mqttMessage.toString().indexOf(","));
- //ttv5.setText(tem);
- //Float temedt;
- //temedt = Float.parseFloat(edt.getText().toString());
- //ttv5.setText(temedt);
- ttv2.setText("溫度" + temp + "℃");
- ttv6.setText("濕度" + humi + "%");
- //Float tempint = Float.parseFloat(temp);
- tempint = Float.parseFloat(temp);
- ttv6.setText(tempint.toString());
- if (tempint >= temedt) {
- ON2();
- //swstatus2.toggle();
- //ttv5.setText(Integer.valueOf(temp));
- //ii = 1;
- } else if (tempint < temedt) {
- OFF2();
- //swstatus2.toggle();
- //ii = 0;
- }
- }
- public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement