(Luận văn thạc sĩ hcmute) thiết kế thiết bị giám sát cảm biến từ xa qua smartphone

152 2 0
(Luận văn thạc sĩ hcmute) thiết kế thiết bị giám sát cảm biến từ xa qua smartphone

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

BỘ GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP.HCM KHOA ĐIỆN - ĐIỆN TỬ ĐỒ ÁN TỐT NGHIỆP NGÀNH CÔNG NGHỆ KỸ THUẬT ĐIỆN TỬ - TRUYỀN THÔNG THIẾT KẾ THIẾT BỊ GIÁM SÁT CẢM BIẾN TỪ XA QUA SMARTPHONE GVHD: ThS NGUYỄN VĂN HIỆP SVTH: NGUYỄN GIA BẢO MSSV: 12141013 SVTH: HÀ THANH ĐẠT MSSV: 12141045 SKL 0 Tp Hồ Chí Minh, tháng 07/2016 Luan van BỘ GIÁO DỤC & ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP HỒ CHÍ MINH KHOA ĐIỆN - ĐIỆN TỬ BỘ MÔN ĐIỆN TỬ - CÔNG NGHIỆP - ĐỒ ÁN TỐT NGHIỆP NGÀNH KỸ THUẬT ĐIỆN TỬ TRUYỀN THÔNG ĐỀ TÀI: THIẾT KẾ THIẾT BỊ GIÁM SÁT CẢM BIẾN TỪ XA QUA SMARTPHONE (3G, WIFI) GVHD: ThS Nguyễn Văn Hiệp SVTH: Nguyễn Gia Bảo 12141013 Hà Thanh Đạt 12141045 Tp Hồ Chí Minh - 07/2016 Luan van BỘ GIÁO DỤC & ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP HỒ CHÍ MINH KHOA ĐIỆN - ĐIỆN TỬ BỘ MÔN ĐIỆN TỬ - CÔNG NGHIỆP - ĐỒ ÁN TỐT NGHIỆP NGÀNH KỸ THUẬT ĐIỆN TỬ TRUYỀN THÔNG ĐỀ TÀI: THIẾT KẾ THIẾT BỊ GIÁM SÁT CẢM BIẾN TỪ XA QUA SMARTPHONE (3G, WIFI) GVHD: ThS Nguyễn Văn Hiệp SVTH: Nguyễn Gia Bảo 12141013 Hà Thanh Đạt 12141045 Tp Hồ Chí Minh - 07/2016 i Luan van TRƯỜNG ĐH SƯ PHẠM KỸ THUẬT CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM TP.HỒ CHÍ MINH Độc lập - Tự - Hạnh phúc KHOA ĐIỆN - ĐIỆN TỬ Tp HCM, ngày tháng năm 2016 NHIỆM VỤ ĐỒ ÁN TỐT NGHIỆP Họ tên sinh viên: Nguyễn Gia Bảo MSSV: 12141013 Hà Thanh Đạt MSSV: 12141045 Chuyên ngành: CNKT Điện Tử - Truyền Thông Mã ngành: 141 Hệ đào tạo: Đại học quy Mã hệ: Khóa: 2012 Lớp: 12141DT2B I TÊN ĐỀ TÀI: Thiết kế thiết bị giám sát cảm biến từ xa qua Smartphone (3G, WIFI) II NHIỆM VỤ Các số liệu ban đầu: Tài liệu liên quan đến đề tài datasheet, trang web module liên quan, phần mềm lập trình Nội dung thực hiện:          Nghiên cứu kit Arduino, Module SIM800L, Module wifi ESP8266 Lập trình cho Arduino Module SIM800L cho mơi trường 3G Lập trình cho Arduino Module ESP8266 cho mơi trường wifi Lập trình ứng dụng Android Thiết kế phần cứng Thi công cân chỉnh mạch Chạy thử nghiệm thiết bị Viết báo cáo Hoàn thiện sản phẩm III NGÀY GIAO NHIỆM VỤ: 01/03/2016 IV NGÀY HOÀN THÀNH NHIỆM VỤ: 07/07/2016 V HỌ VÀ TÊN CÁN BỘ HƯỚNG DẪN: CÁN BỘ HƯỚNG DẪN ThS NGUYỄN VĂN HIỆP BM ĐIỆN TỬ CÔNG NGHIỆP GV.THS NGUYỄN VĂN HIỆP GV.TS NGUYỄN THANH HẢI ii Luan van TRƯỜNG ĐH SƯ PHẠM KỸ THUẬT CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM TP.HỒ CHÍ MINH Độc Lập-Tự Do-Hạnh Phúc KHOA ĐIỆN-ĐIỆN TỬ Tp HCM, ngày tháng năm 2016 LỊCH TRÌNH THỰC HIỆN ĐỒ ÁN TỐT NGHIỆP Họ tên sinh viên 1: Ngyễn Gia Bảo Lớp: 12141DT2B Họ tên sinh viên 2: Hà Thanh Đạt Lớp: 12141DT2B MSSV: 12141013 MSSV: 12141045 Tên đề tài: Thiết kế thiết bị giám sát cảm biến từ xa qua Smartphone (3G, WIFI) Tuần Tuầ n Nội dung Tìm hiể u tính năng, cách lập trình cho Arduino, module wifi ESP8266, module SIM800L Tuầ n Nghiên cứu upload giá tri ̣ cảm biế n lên Google Spreadsheets qua mạng internet dùng module sdfsfhfhsjSSpreSpreadsheet wifi ESP8266 Nghiên cứu upload giá tri ̣ cảm biế n lên Google Spreadsheets qua mạng 3G dùng module SIM800L dựa phương thức tương tự module wifi Nghiên cứu ̣ điều hành Android Viế t ứng du ̣ng quản lý, load liê ̣u từ Google Spreadsheets Viết ứng dụng điều khiển thiết bị qua internet, 3G Viết ứng du ̣ng biểu diễn liệu load từ Google Spreadsheets qua loại biểu đồ Xử lí, xuất file EXCEL Tính tốn, thiế t kế mạch ngun lý Thi công, lắp ráp phầ n cứng Chạy thử nghiệm thiết bị, vá lỗi Đóng hộp thiết bị Viế t báo cáo, hoàn thiện sản phẩm Hoàn thành đề tài Tuầ n Tuầ n 4+5 Tuầ n 6+7 Tuầ n Tuầ n 9+10 Tuầ n 11+12 Xác nhận GVHD GVHD GV HƯỚNG DẪN (Ký ghi rõ họ tên) ThS Nguyễn Văn Hiệp iii Luan van LỜI CAM ĐOAN Đề tài tự thực dựa vào số tài liệu trước khơng chép từ tài liệu hay cơng trình có trước Nhóm thực đề tài Nguyễn Gia Bảo - Hà Thanh Đạt iv Luan van LỜI CẢM ƠN  Sau thời gian nghiên cứu làm việc nghiêm túc, đến đồ án tốt nghiệp với đề tài: “Thiết kế thiết bị giám sát cảm biến từ xa qua Smartphone (3G, WIFI)” hoàn thành đạt kết theo mục tiêu đề Trong q trình thực hiện, nhóm thực đề tài gặp khơng khó khăn, vướng mắc thơng qua học hỏi thêm nhiều kinh nghiệm bổ ích tìm hướng giải vấn đề tốt Nhờ giúp đỡ tận tình q Thầy Cơ bạn tạo điều kiện để nhóm thực đồ án cách tốt Nhóm thực đề tài xin gửi lời cảm ơn chân thành đến thầy Nguyễn Văn Hiệp tận tình giúp đỡ, gợi ý hướng giải khắc phục vướng mắc, cố q trình nghiên cứu Bên cạnh đó, nhóm thực xin gửi lời cảm ơn đến thầy TS Nguyễn Thanh Hải, Trưởng Bộ môn Điện tử công nghiệp, quý thầy/cô khoa Điện – Điện tử trường Đại học Sư Phạm Kỹ Thuật TP.HCM bạn bè giúp đỡ, chia sẻ kinh nghiệm để nhóm hồn thành đề tài tiến độ Cảm ơn đến cha mẹ, gia đình động viên tạo điều kiện để hồn thành đề tài Do kinh nghiệm cịn hạn chế thời gian nghiên cứu có hạn nên nhóm thực đề tài khơng tránh khỏi thiếu sót, mong quý thầy cô, bạn thông cảm đóng góp ý kiến để đề tài ngày hồn thiện Xin chân thành cảm ơn! Nhóm thực đề tài Nguyễn Gia Bảo – Hà Thanh Đạt v Luan van MỤC LỤC NỘI DUNG TRANG Trang bìa i Nhiệm vụ đồ án ii Lịch trình iii Phiếu đánh giá iv Cam đoan v Lời cảm ơn vi Mục lục vii Liệt kê hình vẽ ix Liệt kê bảng xii Tóm tắt xiii Chương TỔNG QUAN Error! Bookmark not defined 1.1 ĐẶT VẤN ĐỀ Error! Bookmark not defined 1.2 MỤC TIÊU Error! Bookmark not defined 1.3 NỘI DUNG NGHIÊN CỨU Error! Bookmark not defined 1.4 GIỚI HẠN Error! Bookmark not defined 1.5 BỐ CỤC Error! Bookmark not defined Chương CƠ SỞ LÝ THUYẾT Error! Bookmark not defined 2.1 LỰA CHỌN GIẢI PHÁP Error! Bookmark not defined 2.1.1 Giải pháp giám sát cảm biến Error! Bookmark not defined 2.1.2 Giải pháp điều khiển thiết bị Error! Bookmark not defined 2.2 GIỚI THIỆU VỀ ANDROID Error! Bookmark not defined 2.3 GIỚI THIỆU VỀ MẠNG WIFI VÀ 3G Error! Bookmark not defined 2.4 CÁC CHUẨN GIAO TIẾP Error! Bookmark not defined 2.4.1 UART Error! Bookmark not defined 2.4.2 I2C Error! Bookmark not defined 2.4.3 ONE WIRE Error! Bookmark not defined 2.5 Công cụ ThingSpeak Error! Bookmark not defined 2.5.1 Giới thiệu Error! Bookmark not defined 2.5.2 Tính Error! Bookmark not defined 2.5.3 Các thuộc tính kênh Error! Bookmark not defined vi Luan van 2.5.4 2.6 Google Spreadsheets Error! Bookmark not defined Chương 3.1 ThingHTTP App Error! Bookmark not defined TÍNH TỐN VÀ THIẾT KẾ Error! Bookmark not defined BÀI TOÁN THIẾT KẾ Error! Bookmark not defined 3.1.1 Sơ đồ mô tả thiết bị Error! Bookmark not defined 3.1.2 Hoạt động thiết bị Error! Bookmark not defined 3.2 TÍNH TỐN VÀ THIẾT KẾ THIẾT BỊ Error! Bookmark not defined 3.2.1 Thiết kế phần cứng Error! Bookmark not defined 3.2.1 Thiết kế ứng dụng Error! Bookmark not defined Chương 4.1 THI CÔNG HỆ THỐNG Error! Bookmark not defined THI CÔNG PHẦN CỨNG - THIẾT BỊ Error! Bookmark not defined 4.1.1 Thi công thiết bị Error! Bookmark not defined 4.1.2 Lắp ráp kiểm tra Error! Bookmark not defined 4.1.3 Lập trình cho vi điều khiển Error! Bookmark not defined 4.2 THI CÔNG ỨNG DỤNG TRÊN ĐIỆN THOẠI Error! Bookmark not defined 4.2.1 Giao diện bắt đầu ứng dụng Error! Bookmark not defined 4.2.2 Load liệu từ GoogleSpreadsheets Error! Bookmark not defined 4.2.3 Điều khiển thiết bị Error! Bookmark not defined 4.2.4 Thiết lập thiết bị Error! Bookmark not defined 4.2.5 Biểu đồ Error! Bookmark not defined 4.3 GIỚI THIỆU PHẦN MỀM LẬP TRÌNHError! Bookmark not defined 4.3.1 Arduino IDE Error! Bookmark not defined 4.3.2 Phần mềm lập trình ứng dụng cho điện thoại: Android Studio Error! Bookmark not defined 4.4 KẾT QUẢ THỐNG KÊ Error! Bookmark not defined 4.4.1 Quá trình điều khiển thiết bị Error! Bookmark not defined 4.4.2 Kết giám sát cảm biến Error! Bookmark not defined 4.5 TÀI LIỆU HƯỚNG DẪN SỬ DỤNG - THAO TÁC TRÊN THIẾT BỊError! Bookmark not defined 4.5.1 Hướng dẫn sử dụng Error! Bookmark not defined 4.5.2 Quy trình thao tác Error! Bookmark not defined Chương KẾT QUẢ - NHẬN XÉT - ĐÁNH GIÁError! Bookmark not defined 5.1 CẢM BIẾN Error! Bookmark not defined 5.2 BỘ VI ĐIỀU KHIỂN Error! Bookmark not defined vii Luan van 5.3 MODULE KẾT NỐI MẠNG Error! Bookmark not defined 5.4 PHẦN MỀM LẬP TRÌNH Error! Bookmark not defined 5.5 PHẦN MỀM VẼ MẠCH Error! Bookmark not defined 5.6 NHẬN XÉT - ĐÁNH GIÁ Error! Bookmark not defined Chương KẾT LUẬN - HƯỚNG PHÁT TRIỂNError! Bookmark not defined 6.1 KẾT LUẬN Error! Bookmark not defined 6.2 HƯỚNG PHÁT TRIỂN Error! Bookmark not defined TÀI LIỆU THAM KHẢO PHU LỤC viii Luan van PHỤ LỤC public void switch_check() { if (switch1.isChecked()){field1_device1="2";} else field1_device1="1"; if (switch2.isChecked()){field1_device2="2";} else field1_device2="1"; } private class LoadThingSpeak extends AsyncTask { boolean isConnected=true; long SLEEPTIME=3000;//3s long OPERATIONPERIOD=10000;//10s @Override protected void onPreExecute() { Log.d("TAG", "Bat dau LoadThingSpeak"); super.onPreExecute(); } @Override protected String doInBackground(String args) { Log.d("TAG", "doInBackground"); while((!isCancelled())&&(networkInfo.isConnected())){ SystemClock.sleep(SLEEPTIME); String status_field1=null; status_field1=mReq.sendGet("http://api.thingspeak.com/channels/77282/fields/2/last") ; //Log.i(myTag, "get=" + status_field1); publishProgress(status_field1); } return null; } @Override protected void onProgressUpdate(String values) { super.onProgressUpdate(values); Log.d(myTag,values[0]); if(values[0] !=null){ if (values[0].equals("11")){if (status_resum){switch1.setChecked(false); switch2.setChecked(false);} icon_device1.setImageResource(R.drawable.device1); icon_device2.setImageResource(R.drawable.device1);} if (values[0].equals("12")){if (status_resum){switch1.setChecked(false); switch2.setChecked(true);} icon_device1.setImageResource(R.drawable.device1); icon_device2.setImageResource(R.drawable.device2);} if (values[0].equals("21")){if (status_resum){switch1.setChecked(true); switch2.setChecked(false);} Luan van PHỤ LỤC icon_device1.setImageResource(R.drawable.device2); icon_device2.setImageResource(R.drawable.device1);} if (values[0].equals("22")){if (status_resum){switch1.setChecked(true); switch2.setChecked(true);} icon_device1.setImageResource(R.drawable.device2); icon_device2.setImageResource(R.drawable.device2);} } } } protected void onPause() { super.onPause(); Log.d("TAG", "onPause Activity_sensor"); if(!mLoadThingSpeak.isCancelled()) { mLoadThingSpeak.cancel(true); } } @Override protected void onResume() { super.onResume();Log.d("TAG", "onResume Activity_sensor"); mLoadThingSpeak= new LoadThingSpeak(); mLoadThingSpeak.execute(); status_resum=true; } public void ToastMessage(String message, int x, int y) { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setTextSize(17); text.setText(message); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, x, y); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); } } Setting_Activity.java://class thiết lập ngưỡng vẽ biểu đồ package com.example.ngb.iotsensor; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; Luan van PHỤ LỤC import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Setting_Activity extends AppCompatActivity { EditText Hthap, Hondinh, Hcao, Tthap, Tondinh,Tcao; Button btnOk; Intent myintent; public static final String MY_PREFS_NAME = "MyPrefsFile"; int VTthap=0, VTondinh=0, VTcao=0, VTvuot=0,VHthap=0, VHondinh=0, VHcao=0, VHvuot=0; @Override protected void onCreate(Bundle savedInstanceState) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); myintent=new Intent(Setting_Activity.this, ListViewMultiChartActivity.class); Hthap=(EditText)findViewById(R.id.editText_thap); Hondinh=(EditText)findViewById(R.id.editText_ondinh); Hcao=(EditText)findViewById(R.id.editText_cao); Tthap=(EditText)findViewById(R.id.editTextthap); Tondinh=(EditText)findViewById(R.id.editTextondinh); Tcao=(EditText)findViewById(R.id.editTextcao); btnOk=(Button)this.findViewById(R.id.buttonok); recieveData(); btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((!Hthap.getText().toString().equals("")) && (!Hondinh.getText().toString().equals("")) && (!Hcao.getText().toString().equals("")) && (!Tthap.getText().toString().equals("")) && (!Tondinh.getText().toString().equals("")) && (!Tcao.getText().toString().equals(""))) { VTthap = Integer.parseInt(Tthap.getText().toString()); VTondinh = Integer.parseInt(Tondinh.getText().toString()); VTcao = Integer.parseInt(Tcao.getText().toString()); VHthap = Integer.parseInt(Hthap.getText().toString()); VHondinh = Integer.parseInt(Hondinh.getText().toString()); VHcao = Integer.parseInt(Hcao.getText().toString()); saveData(); Bundle bundle = new Bundle(); bundle.putInt("Tthap", VTthap); bundle.putInt("Tondinh", VTondinh); bundle.putInt("Tcao", VTcao); bundle.putInt("Hthap", VHthap); Luan van PHỤ LỤC bundle.putInt("Hondinh", VHondinh); bundle.putInt("Hcao", VHcao); myintent.putExtra("MyPackage", bundle); startActivity(myintent); finish(); } else { Toast.makeText(Setting_Activity.this, "nhập đủ số liệu vào", Toast.LENGTH_LONG).show(); } } }); } public void saveData() { SharedPreferences pref = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putString("tthap", ""+VTthap); editor.putString("tondinh", "" + VTondinh); editor.putString("tcao", "" + VTcao); editor.putString("hthap", "" + VHthap); editor.putString("hondinh", "" + VHondinh); editor.putString("hcao", "" + VHcao); editor.apply(); } public void recieveData() { SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE); VTthap = Integer.parseInt(prefs.getString("tthap", "30")); VTondinh = Integer.parseInt(prefs.getString("tondinh", "60")); VTcao = Integer.parseInt(prefs.getString("tcao", "100")); VHthap = Integer.parseInt(prefs.getString("hthap", "20")); VHondinh = Integer.parseInt(prefs.getString("hondinh", "40")); VHcao = Integer.parseInt(prefs.getString("hcao", "60")); Tthap.setText(""+VTthap); Tondinh.setText(""+VTondinh); Tcao.setText(""+VTcao); Hthap.setText(""+VHthap); Hondinh.setText(""+VHondinh); Hcao.setText(""+VHcao); } } DowmloadWebpageTask.java://class truy cập mạng để tải xuống liệu package com.example.ngb.iotsensor; Luan van PHỤ LỤC import android.os.AsyncTask; import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class DownloadWebpageTask extends AsyncTask { AsyncResult callback; public DownloadWebpageTask(AsyncResult callback) { this.callback = callback; } @Override protected String doInBackground(String urls) { // params comes from the execute() call: params[0] is the url try { return downloadUrl("https://spreadsheets.google.com/tq?key=1TEIopdrPAXIbelUVB20a4e Ra0SNyyjyvQOvHFzjGMV4"); } catch (IOException e) { e.printStackTrace(); } return null; } // onPostExecute displays the results of the AsyncTask @Override protected void onPostExecute(String result) { // remove the unnecessary parts from the response and construct a JSON Log.d("esp",result); int start = -1; int end = -1; start = result.indexOf("{", result.indexOf("{") + 1); end = result.lastIndexOf("}"); if ((start !=-1)&&(end != -1)) { String jsonResponse = result.substring(start, end); try { JSONObject table = new JSONObject(jsonResponse); callback.onResult(table); } catch (JSONException e) { Luan van PHỤ LỤC e.printStackTrace(); } } } private String downloadUrl(String urlString) throws IOException { InputStream is = null; String contentAsString=null; try { { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000 /* milliseconds */); conn.setConnectTimeout(5000 /* milliseconds*/); conn.setRequestMethod("GET"); conn.setDoInput(true); // Starts the query conn.connect(); int responseCode = conn.getResponseCode(); is = conn.getInputStream(); contentAsString = convertStreamToString(is); Log.d("esp_", contentAsString); }while (contentAsString.contains("HTTP/1.1 302 Moved Temporarily")); return contentAsString; } finally { if (is != null) { is.close(); contentAsString=null; } } } private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } Luan van PHỤ LỤC } return sb.toString(); } } DemoBase.java://class vẽ biểu đồ đường package com.example.ngb.iotsensor; import android.support.v4.app.FragmentActivity; //import com.xxmassdeveloper.mpchartexample.R; /** * Baseclass of all Activities of the Demo Application * * @author Philipp Jahoda */ public abstract class DemoBase extends FragmentActivity { protected String[] mMonths = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec" }; protected String[] mParties = new String[] { "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H", "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P", "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X", "Party Y", "Party Z" }; @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity); } } TeamAdapter.java://class truy gán id cho đối tượng package com.example.ngb.iotsensor; import android.content.Context; import android.view.LayoutInflater; Luan van PHỤ LỤC import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.ArrayList; public class TeamsAdapter extends ArrayAdapter { Context context; private ArrayList teams; public TeamsAdapter(Sheet_Activity context, int textViewResourceId, ArrayList items) { super(context, textViewResourceId, items); this.context = context; this.teams = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.team, null); } Team o = teams.get(position); if (o != null) { /*TextView pos = (TextView) v.findViewById(R.id.position); TextView name = (TextView) v.findViewById(R.id.name); TextView wins = (TextView) v.findViewById(R.id.wins); TextView draws = (TextView) v.findViewById(R.id.draws); TextView losses = (TextView) v.findViewById(R.id.losses); TextView points = (TextView) v.findViewById(R.id.points); pos.setText(String.valueOf(o.getPosition())); name.setText(String.valueOf(o.getName())); wins.setText(String.valueOf(o.getWins())); draws.setText(String.valueOf(o.getDraws())); losses.setText(String.valueOf(o.getLosses())); points.setText(String.valueOf(o.getPoints()));*/ TextView txt_Time = (TextView) v.findViewById(R.id.textView_time); TextView txt_cb1 = (TextView) v.findViewById(R.id.textView_cb1); TextView txt_cb2 = (TextView) v.findViewById(R.id.textView_cb2); txt_Time.setText(String.valueOf(o.getTime())); txt_cb1.setText(String.valueOf(o.getCb1())); txt_cb2.setText(String.valueOf(o.getCb2())); } Luan van PHỤ LỤC return v; } } ChartItem.java: //class truy gán lại biểu đồ package com.example.ngb.iotsensor; import android.content.Context; import android.view.View; import com.github.mikephil.charting.data.ChartData; /** * baseclass of the chart-listview items * @author philipp * */ public abstract class ChartItem { protected static final int TYPE_BARCHART = 0; protected static final int TYPE_LINECHART = 1; protected static final int TYPE_PIECHART = 2; protected ChartData mChartData; public ChartItem(ChartData cd) { this.mChartData = cd; } public abstract int getItemType(); public abstract View getView(int position, View convertView, Context c); } ListViewMultiChartActivity.java://class vẽ loại biểu đồ package com.example.ngb.iotsensor; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.ArrayAdapter; Luan van PHỤ LỤC import android.widget.ListView; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import com.github.mikephil.charting.utils.ColorTemplate; import java.util.ArrayList; import java.util.List; //import com.xxmassdeveloper.mpchartexample.listviewitems.BarChartItem; //import com.xxmassdeveloper.mpchartexample.listviewitems.ChartItem; //import com.xxmassdeveloper.mpchartexample.listviewitems.LineChartItem; //import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; public class ListViewMultiChartActivity extends DemoBase { DatabaseHandler db; String id,pw; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_listview_chart); //mDBSensor=new DBSensor(this); db= new DatabaseHandler(this); lv = (ListView) findViewById(R.id.listView1); Intent callerIntent=getIntent(); Bundle packageFromCaller= callerIntent.getBundleExtra("MyPackage"); int Tthap=packageFromCaller.getInt("Tthap"); int Tondinh=packageFromCaller.getInt("Tondinh"); int Tcao=packageFromCaller.getInt("Tcao"); int Hthap=packageFromCaller.getInt("Hthap"); int Hondinh=packageFromCaller.getInt("Hondinh"); int Hcao=packageFromCaller.getInt("Hcao"); ArrayList list = new ArrayList(); list.add(new LineChartItem(generateDataLineBeatTem(), getApplicationContext())); list.add(new PieChartItem(generateDataPieTemperature(Tthap,Tondinh,Tcao), getApplicationContext())); Luan van PHỤ LỤC list.add(new PieChartItem(generateDataPieHumidity(Hthap,Hondinh,Hcao), getApplicationContext())); ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list); lv.setAdapter(cda); } @Override protected void onResume(){ super.onResume(); //Display(); } /* public void Display(){ ArrayList list = new ArrayList(); list.add(new LineChartItem(generateDataLineBeatTem(), getApplicationContext())); list.add(new PieChartItem(generateDataPieTemperature(), getApplicationContext())); list.add(new PieChartItem(generateDataPieHumidity(), getApplicationContext())); ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list); lv.setAdapter(cda); }*/ /** adapter that supports different item types */ private class ChartDataAdapter extends ArrayAdapter { public ChartDataAdapter(Context context, List objects) { super(context, 0, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { return getItem(position).getView(position, convertView, getContext()); } @Override public int getItemViewType(int position) { // return the views type return getItem(position).getItemType(); } @Override public int getViewTypeCount() { return 3; // we have different item-types } } Luan van PHỤ LỤC private LineData generateDataLineBeatTem() { ArrayList mYValue1 = new ArrayList(); ArrayList mYValue2 = new ArrayList(); ArrayList mXLable=new ArrayList(); List data = db.getAllSensorData(); int dem=0; for (SensorTable cn : data) { mYValue1.add(new Entry(cn.getCb1(),dem)); mYValue2.add(new Entry(cn.getCb2(),dem)); mXLable.add(cn.getTime()); dem++; } LineDataSet d1 = new LineDataSet(mYValue1, "CB1 "); d1.setLineWidth(2.5f); d1.setCircleRadius(3.5f); d1.setHighLightColor(Color.rgb(244, 117, 117)); d1.setDrawValues(true); LineDataSet d2 = new LineDataSet(mYValue2, "CB2 "); d2.setLineWidth(2.5f); d2.setCircleRadius(3.5f); d2.setHighLightColor(Color.rgb(244, 117, 117)); d2.setColor(ColorTemplate.VORDIPLOM_COLORS[0]); d2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]); d2.setDrawValues(true); ArrayList sets = new ArrayList(); sets.add(d1); sets.add(d2); return new LineData(mXLable, sets); } private PieData generateDataPieTemperature(int value1, int value2, int value3) { ArrayList entries = new ArrayList(); List data = db.getAllSensorData(); int count = (int) db.getProfilesCount(); int dem=0; float data1, data2, data3, data4; int Cdata1=0, Cdata2=0, Cdata3=0, Cdata4=0; for(SensorTable cn: data) { if (cn.getCb1()

Ngày đăng: 02/02/2023, 10:03

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan