Advertisement
Guest User

mqtt未修正

a guest
Aug 22nd, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.16 KB | None | 0 0
  1. package com.example.user.myapplication;
  2.  
  3. import android.os.Build;
  4. import android.graphics.Color; //色彩
  5. import android.os.Bundle; //手機資訊
  6. import android.os.Handler; //畫面更新
  7. import android.provider.Settings; //系統屬性
  8. import android.support.v7.app.AppCompatActivity; //專案
  9. import android.os.Bundle; //資料傳遞
  10. import android.text.StaticLayout;
  11. import android.util.Log; //查詢錯誤
  12. import android.view.View;
  13. import android.webkit.WebChromeClient;
  14. import android.widget.AdapterView;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.Button;
  17. import android.widget.CompoundButton;
  18. import android.widget.EditText;
  19. import android.widget.Spinner;
  20. import android.widget.Switch;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23.  
  24. import org.eclipse.paho.android.service.MqttAndroidClient;
  25. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  26. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  27. import org.eclipse.paho.client.mqttv3.IMqttToken;
  28. import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
  29. import org.eclipse.paho.client.mqttv3.MqttCallback;
  30. import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
  31. import org.eclipse.paho.client.mqttv3.MqttClient;
  32. import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
  33. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  34. import org.eclipse.paho.client.mqttv3.MqttException;
  35. import org.eclipse.paho.client.mqttv3.MqttMessage;
  36. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  37. import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
  38.  
  39. import android.support.design.widget.FloatingActionButton;
  40. import android.support.design.widget.Snackbar;
  41. import android.support.v7.widget.Toolbar;
  42. import android.view.Menu;
  43. import android.view.MenuItem;
  44. import android.webkit.WebView;
  45. import android.webkit.WebSettings;
  46. import android.webkit.WebViewClient;
  47.  
  48. public class MainActivity extends AppCompatActivity {
  49.  
  50. MqttAndroidClient client;
  51. MqttClient client1;
  52. MqttAsyncClient client2;
  53. TextView bugtext;
  54. Switch myswitch;
  55. MqttConnectOptions options;
  56. TextView ttv4, ttv5, ttv2, ttv6;
  57. WebView mWebView;
  58. Switch swstatus1, swstatus2, swstatus3;
  59. EditText edt;
  60. Button button;
  61. //public static float tempint;
  62. Float tempint, temedt;
  63. Integer ii;
  64.  
  65. @Override
  66. protected void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.activity_main);
  69. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  70. setSupportActionBar(toolbar);
  71. //ttv4 = (TextView) findViewById(R.id.textView4);
  72. //ttv5 = (TextView) findViewById(R.id.textView5);
  73. ttv2 = (TextView) findViewById(R.id.textView2);
  74. ttv6 = (TextView) findViewById(R.id.textView6);
  75. swstatus1 = (Switch) findViewById(R.id.switch1);
  76. swstatus1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  77. public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  78. if (buttonView.isChecked()) { //當開關開啟時傳送ON訊號,關閉時傳送OFF訊號
  79. ON2();
  80. }
  81. else {
  82. OFF2();
  83. }
  84. }
  85. });
  86. swstatus2 = (Switch) findViewById(R.id.switch2);
  87. swstatus2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  88. public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  89. if (buttonView.isChecked()) { //當開關開啟時傳送ON2訊號,關閉時傳送OFF2訊號
  90. ON();
  91. } else {
  92. OFF();
  93. }
  94. }
  95. });
  96. swstatus3 = (Switch) findViewById(R.id.switch3);
  97. swstatus3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  98. public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  99. if (buttonView.isChecked()) { //當開關開啟時傳送FAN_ON訊號,關閉時傳送FAN_OFF訊號
  100. FAN_ON(buttonView);
  101. } else {
  102. FAN_OFF(buttonView);
  103. }
  104. }
  105. });
  106. //edt = (EditText) findViewById(R.id.editText);
  107. button =(Button) findViewById(R.id.button);
  108. //Float tempint, temedt ;
  109. button.setOnClickListener(new View.OnClickListener() {
  110. @Override
  111. public void onClick(View v) {
  112. //temedt = Float.parseFloat(edt.getText().toString());
  113. /*float temedt ,tempfloat;
  114. temedt = Float.parseFloat(edt.getText().toString());
  115. tempfloat =Float.parseFloat(edt.getText().toString());
  116. if (tempint >= temedt){
  117. //ON2();
  118. //ttv5.setText(Integer.valueOf(temp));
  119. ii = 1;
  120. }else if(tempint < temedt){
  121. ii = 0;
  122. }*/
  123. }
  124. });
  125. /*Spinner spinner = (Spinner)findViewById(R.id.spinner);
  126. final ArrayAdapter<CharSequence> lunchList = ArrayAdapter.createFromResource(MainActivity.this,
  127. R.array.lunch,
  128. android.R.layout.simple_spinner_dropdown_item);
  129. spinner.setAdapter(lunchList);
  130. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
  131. @Override
  132. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  133. Toast.makeText(MainActivity.this, "你選的是" + lunchList.getItem(position),Toast.LENGTH_SHORT).show();
  134. }
  135. @Override
  136. public void onNothingSelected(AdapterView<?> parent) {
  137. }
  138. });*/
  139. //myswitch = (Switch)findViewById(R.id.switch1);
  140. //myswitch.setOnCheckedChangeListener(this);
  141. String clientId = MqttClient.generateClientId(); //定義MqttClient型態
  142. client = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.4.1:1883", clientId);
  143. // 設定MqttServer端ip
  144. options = new MqttConnectOptions(); //定義MqttClient設定型態
  145. options.setAutomaticReconnect(true); //設定開啟自動連接
  146. options.setCleanSession(true);
  147.  
  148.  
  149. MqttClientPersistence persistence = new MqttDefaultFilePersistence("/tmp");
  150. //MemoryPersistence dataStore = new MemoryPersistence();
  151.  
  152.  
  153. //client1 = new MqttClient("tcp://192.168.2.1:1883", clientid, new MemoryPersistence());
  154. //client2 = new MqttAsyncClient("tcp://192.168.2.1:1883",clientid2,dataStore);
  155. try {
  156. client.connect(); //連接MqttServer端
  157. IMqttToken token = client.connect();
  158. token.setActionCallback(new IMqttActionListener() {
  159. @Override
  160. public void onSuccess(IMqttToken asyncActionToken) {
  161. Toast.makeText(MainActivity.this, "成功登入", Toast.LENGTH_SHORT).show();
  162. try {
  163.  
  164. client.subscribe("esp8266", 0, null, new IMqttActionListener() {
  165. @Override
  166. public void onSuccess(IMqttToken iMqttToken) {
  167. Toast.makeText(MainActivity.this, "訂閱成功", Toast.LENGTH_SHORT).show();
  168. }
  169.  
  170. @Override
  171. public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
  172. Toast.makeText(MainActivity.this, "訂閱失敗", Toast.LENGTH_SHORT).show();
  173. }
  174. });
  175. } catch (MqttException e) {
  176. e.printStackTrace();
  177. }
  178. }
  179.  
  180. @Override
  181. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  182. Toast.makeText(MainActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
  183. }
  184. });
  185. } catch (MqttException e) {
  186. e.printStackTrace();
  187. }
  188. /*try {
  189. IMqttToken token1 = client.connect();
  190. token1.setActionCallback(new IMqttActionListener() {
  191. @Override
  192. public void onSuccess(IMqttToken iMqttToken) {
  193. Toast.makeText(MainActivity.this, "成功登入",Toast.LENGTH_LONG).show();
  194. }
  195.  
  196. @Override
  197. public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
  198. Toast.makeText(MainActivity.this, "成功登入",Toast.LENGTH_LONG).show();
  199. }
  200. });
  201. } catch (MqttException e){
  202. e.printStackTrace();
  203. bugtext.setText(e.getMessage());
  204. }*/
  205. // 取得XML中的WebView
  206. mWebView = (WebView) findViewById(R.id.web1);
  207. // WebView的設定選項
  208. WebSettings webSettings = mWebView.getSettings();
  209. // Enable Javascript
  210. webSettings.setJavaScriptEnabled(true);
  211. // Enable LocalStorage
  212. webSettings.setDomStorageEnabled(true);
  213. webSettings.setSupportZoom(true);
  214. webSettings.setBuiltInZoomControls(true);
  215. webSettings.setLoadWithOverviewMode(true);
  216. webSettings.setLoadsImagesAutomatically(true);
  217. WebChromeClient mwebchromeclient = new WebChromeClient();
  218. // 要加setWebViewClient以避免點連結時跳出APP用瀏覽器開啟
  219. mWebView.setWebViewClient(new WebViewClient());
  220.  
  221. // 要設定 WebChromeClient 才能支援 JS 的 Alert, Confirm
  222. mWebView.setWebChromeClient(new WebChromeClient());
  223.  
  224. // API 19 以上可使用硬體加速
  225. if (Build.VERSION.SDK_INT >= 19) {
  226. mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  227. } else {
  228. mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  229. }
  230. //mWebView.setBackgroundColor(Color.BLACK);
  231. mWebView.loadUrl("https://www.youtube.com/watch?v=UdTkLbqD598");
  232. //mWebView.loadUrl("http://www.google.com");
  233. //mWebView.loadUrl();
  234. }
  235. Handler myHandler = new Handler();
  236. public void ON() {
  237. String topic = "esp8266";
  238. String message = "5on";
  239. try {
  240. client.publish(topic, message.getBytes(),0, false);
  241. } catch (MqttException e) {
  242. e.printStackTrace();
  243. }
  244. }
  245. public void OFF() {
  246. String topic = "esp8266";
  247. String message = "5off";
  248. try {
  249. client.publish(topic, message.getBytes(), 0, false);
  250. } catch (MqttException e) {
  251. e.printStackTrace();
  252. }
  253. }
  254. public void ON2() {
  255. String topic = "esp8266"; //設定訂閱主題esp8266
  256. String message = "abc123"; //設定傳送的訊息 5on2
  257. try {
  258. client.publish(topic, message.getBytes(), 0, false); //訂閱主題
  259. } catch (MqttException e) {
  260. e.printStackTrace();
  261. }
  262.  
  263. }
  264. public void OFF2() {
  265. String topic = "esp8266"; //設定訂閱主題esp8266
  266. String message = "5off2"; //設定傳送的訊息 5on2
  267. try {
  268. client.publish(topic, message.getBytes(), 0, false); //訂閱主題
  269. } catch (MqttException e) {
  270. e.printStackTrace();
  271. }
  272. }
  273. public void RESTART(View v) {
  274. String topic = "esp8266";
  275. String message = "1restart";
  276. try {
  277. client.publish(topic, message.getBytes(), 0, false);
  278. } catch (MqttException e) {
  279. e.printStackTrace();
  280. }
  281. }
  282. public void UP(View v) {
  283. String topic = "esp8266";
  284. String message = "2on";
  285. try {
  286. client.publish(topic, message.getBytes(), 0, false);
  287. } catch (MqttException e) {
  288. e.printStackTrace();
  289. }
  290. new Handler().postDelayed(new Runnable() {
  291. public void run() {
  292. // do something
  293. String topic = "esp8266";
  294. String message = "2stop";
  295. try {
  296. client.publish(topic, message.getBytes(), 0, false);
  297. } catch (MqttException e) {
  298. e.printStackTrace();
  299. }
  300. }
  301. }, 7 * 100);
  302. }
  303. public void STOP(View v) {
  304. String topic = "esp8266";
  305. String message = "2stop";
  306. try {
  307. client.publish(topic, message.getBytes(), 0, false);
  308. } catch (MqttException e) {
  309. e.printStackTrace();
  310. }
  311. }
  312. public void DOWN(View v) {
  313. String topic = "esp8266";
  314. String message = "2off";
  315. try {
  316. client.publish(topic, message.getBytes(), 0, false);
  317. } catch (MqttException e) {
  318. e.printStackTrace();
  319. }
  320. new Handler().postDelayed(new Runnable() {
  321. public void run() {
  322. // do something
  323. String topic = "esp8266";
  324. String message = "2stop";
  325. try {
  326. client.publish(topic, message.getBytes(), 0, false);
  327. } catch (MqttException e) {
  328. e.printStackTrace();
  329. }
  330. }
  331. }, 7 * 100);
  332. }
  333. public void RESTART2(View v) {
  334. String topic = "esp8266";
  335. String message = "2restart";
  336. try {
  337. client.publish(topic, message.getBytes(), 0, false);
  338. } catch (MqttException e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. public void FAN_ON(View v) {
  343. String topic = "esp8266";
  344. String message = "3on";
  345. try {
  346. client.publish(topic, message.getBytes(), 0, false);
  347. } catch (MqttException e) {
  348. e.printStackTrace();
  349. }
  350. }
  351. public void FAN_OFF(View v) {
  352. String topic = "esp8266";
  353. String message = "3off";
  354. try {
  355. client.publish(topic, message.getBytes(), 0, false);
  356. } catch (MqttException e) {
  357. e.printStackTrace();
  358. }
  359. }
  360. public void RESTART3(View v) {
  361. String topic = "esp8266";
  362. String message = "3restart";
  363. try {
  364. client.publish(topic, message.getBytes(), 0, false);
  365. } catch (MqttException e) {
  366. e.printStackTrace();
  367. }
  368. }
  369. public void conn() {
  370. try {
  371. client.connect();
  372. IMqttToken token = client.connect(options);
  373. token.setActionCallback(new IMqttActionListener() {
  374.  
  375. public void onSuccess(IMqttToken asyncActionToken) {
  376. Toast.makeText(MainActivity.this, "成功登入", Toast.LENGTH_SHORT).show();
  377. }
  378.  
  379. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  380. Toast.makeText(MainActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
  381. }
  382. });
  383. } catch (MqttException e) {
  384. e.printStackTrace();
  385. bugtext.setText(e.getReasonCode());
  386. }
  387. boolean a = client.isConnected();
  388. //bugtext.setText("");
  389. }
  390. public void SETTEXT(Integer i) {
  391. if (i == 1) {
  392. ON2();
  393. } else if (i == 0) {
  394. OFF2();
  395. }
  396. }
  397. public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
  398. //Toast.makeText(MainActivity.this, mqttMessage.toString(),Toast.LENGTH_SHORT).show();
  399. //ttv5.setText(mqttMessage.toString());
  400. String op = "open";
  401. String cl = "close";
  402. String tem = mqttMessage.toString();
  403.  
  404. if (mqttMessage.toString().equals(op) == true) {
  405. ttv4.setText(mqttMessage.toString());
  406. ON();
  407. } else if (mqttMessage.toString().equals(cl) == true) {
  408. ttv4.setText(mqttMessage.toString());
  409. OFF();
  410. }
  411.  
  412.  
  413. String humi = mqttMessage.toString().substring(mqttMessage.toString().indexOf(",") + 1);
  414. String temp = mqttMessage.toString().substring(0, mqttMessage.toString().indexOf(","));
  415.  
  416. //ttv5.setText(tem);
  417. //Float temedt;
  418. //temedt = Float.parseFloat(edt.getText().toString());
  419. //ttv5.setText(temedt);
  420. ttv2.setText("溫度" + temp + "℃");
  421. ttv6.setText("濕度" + humi + "%");
  422. //Float tempint = Float.parseFloat(temp);
  423. tempint = Float.parseFloat(temp);
  424. ttv6.setText(tempint.toString());
  425. if (tempint >= temedt) {
  426. ON2();
  427. //swstatus2.toggle();
  428. //ttv5.setText(Integer.valueOf(temp));
  429. //ii = 1;
  430. } else if (tempint < temedt) {
  431. OFF2();
  432. //swstatus2.toggle();
  433. //ii = 0;
  434. }
  435. }
  436. public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
  437. }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement