(Đồ án hcmute) điều khiển thiết bị điện báo nhiệt độ báo cháy

118 4 0
(Đồ án hcmute) điều khiển thiết bị điện báo nhiệt độ báo cháy

Đ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 KỸ THUẬT ĐIỆN - ĐIỆN TỬ ĐIỀU KHIỂN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY GVHD: ThS NGUYỄN ĐÌNH PHÚ SVTH: NGUYỄN THANH TUẤN MSSV: 07101152 SKL 0 Tp Hồ Chí Minh, tháng 07/2016 an 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-ĐIỆN TỬ ĐỀ TÀI: ĐIỀU KHIỂN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY GVHD: ThS.Nguyễn Đình Phú SVTH : Nguyễn Thanh Tuấn MSSV : 07101152 Tp Hồ Chí Minh – 7/2016 an 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 –ĐIỆN TỬ ĐỀ TÀI: ĐIỀU KHIỂN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY GVHD: ThS.Nguyễn Đình Phú SVTH: Nguyễn Thanh Tuấn MSSV: 07101152 Tp Hồ Chí Minh – 7/2016 an TRƯỜNG ĐH 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 CỘNG HỊA XÃ HỘI CHỦ NGHĨA VIỆT NAM ĐỘC LẬP - TỰ DO - HẠNH PHÚC Tp HCM, ngày tháng năm 2016 NHIỆM VỤ ĐỒ ÁN TỐT NGHIỆP Họ tên sinh viên: Chuyên ngành: Hệ đào tạo: Khóa: Nguyễn Thanh Tuấn Kỹ thuật Điện - Điện tử Đại học quy 2007 MSSV: 07101152 Mã ngành: 01 Mã hệ: Lớp: 071011A I TÊN ĐỀ TÀI: ĐIỀU KHIỂN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY II NHIỆM VỤ Các số liệu ban đầu:  Kit ArduinoMega 2560  Modul cảm biến : khí ga MQ2 , nhiệt độ độ ẩm DTH11  Modul thời gian thực , LCD, HC-05,4-relay  Chng ,bóng đèn 220v Nội dung thực hiện:  Thiết kế thi cơng mạch kit Arduino giao tiếp cảm biến khí ga,cảm biến đo nhiệt độ,LCD,thiết bị điện  Thiết kế thi cơng lập trình khối đo nhiệt độ ẩm  Thiết kế thi cơng lập trình khối thời gian thực  Thiết kế thi công lập trình khối giao diện kết nối Bluetooth  Thiết kế thi cơng lập trình khối nút nhấn  Lắp ráp khối điều khiển mơ hình  Chạy thử nghiệp hệ thống  Chỉnh sửa lỗi điều khiển, lỗi lập trình lỗi thiết bị  Viết sách luận văn  Báo cáo đề tài tốt nghiệp III NGÀY GIAO NHIỆM VỤ: 24/03/2016 IV NGÀY HOÀN THÀNH NHIỆM VỤ: 27/06/2016 V HỌ VÀ TÊN CÁN BỘ HƯỚNG DẪN: ThS Nguyễn Đình Phú CÁN BỘ HƯỚNG DẪN BM ĐIỆN TỬ CÔNG NGHIỆP an TRƯỜNG ĐH 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 CỘNG HỊA XÃ HỘI CHỦ NGHĨA VIỆT NAM ĐỘC LẬP - TỰ DO - HẠNH PHÚC Tp Hồ Chí Minh, 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 : Nguyễn Thanh Tuấn Lớp: 071011A MSSV: 07101152 Tên đề tài: ĐIỀU KHIỂN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY Xác nhận GVHD Nội dung Tuần/ngày 21/03/2016 26/03/2016 28/03/2016 02/04/2016 Trao đổi với giáo viên hướng dẫn nhận đề tài 04/04/2016 09/04/2016 Tìm hiểu điều khiển giao tiếp kit Arduino với cảm biến khí ga , hình LCD 11/04/2016 16/04/2016 Tìm hiểu giao tiếp kit Arduino với cảm biến đo nhiệt độ ,độ ẩm hiển thị LCD Báo cháy sử dụng cảm biến khí ga 18/04/2016 23/04/2016 Thiết kế giao diện điều khiển thiết bị điện phần mềm AndroidStudio điều khiển qua Bluetooth 25/04/2016 30/04/2016 02/05/2016 07/05/2016 09/05/2016 14/05/2016 16/05/2016 27/05/2016 30/05/2016 11/06/2016 13/06/2016 27/06/2016 Lập trình cho hệ thống GVHD giao cơng việc tìm hiểu mua linh kiện kit Arduino Mega2560 , cảm biến … Thi công phần cứng, kiểm tra, đo đạt thơng số tín hiệu Đang hồn thành chạy thử nghiệm Kiểm tra sai sót khắc phục lỗi Chỉnh sửa hệ thống viết báo cáo Hoàn thiện hệ thống báo cáo GV HƯỚNG DẪN (Ký ghi rõ họ tên) an LỜI CAM ĐOAN Tơi cam đoan cơng trình nghiên cứu Các số liệu, kết nêu luận văn trung thực chưa cơng bố cơng trình khác Tp Hồ Chí Minh, ngày 27 tháng 06 năm 2016 (Ký tên ghi rõ họ tên) an LỜI CẢM ƠN Xin gửi lời cảm ơn chân thành đến quý thầy cô khoa Điện – Điện tử giảng dạy em suốt bốn năm học tạo điều kiện thuận lợi để em thực tốt đề tài Cảm ơn gia đình, cha mẹ nguồn động viên to lớn vật chất tinh thần suốt thời gian học hành, để em có tương lai, theo đuổi ước mơ nghiệp Đặc biệt cảm ơn thầy ThS.Nguyễn Đình Phú tạo điều kiện hướng dẫn em cách học tập nghiên cứu để hoàn thành tốt đồ án tốt nghiệp Cuối xin chúc gia đình, bạn bè quý thầy, cô nhiều sức khỏe thành công công việc Xin chân thành cảm ơn! Người thực đề tài Nguyễn Thanh Tuấn an MỤC LỤC Trang bìa i Nhiệm vụ đồ án ii Lịch trình iii Nhận xét giáo viên hướng dẫn iv Cam đoan v Lời cảm ơn vi Mục lục vii Danh mục hình vẽ x Danh mục bảng xiii Tóm tắt xiv Chương TỔNG QUAN 1.1 TỔNG QUAN VỀ LĨNH VỰC NGHIÊN CỨU 1.2 MỤC TIÊU 1.3 NỘI DUNG NGHIÊN CỨU 1.4 GIỚI HẠN 1.5 BỐ CỤC Chương CƠ SỞ LÝ THUYẾT 2.1 Quy trình hoạt động thiết bị 2.1.1 Mơ tả quy trình 2.2 Giới thiệu phần cứng 2.2.1 Giới thiệu kit Arduino mega 2560 2.2.2 Màn hình LCD 12 a Giới thiệu 12 b Lựa chọn LCD 12 c Màn hình LCD 16x2 14 2.2.3 Cảm biến đo nhiệt độ, độ ẩm 17 a Giới thiệu 17 b Các loại cảm biến 17 an c Cảm biến độ ẩm nhiệt độ DHT11 21 2.2.4 Cảm biến khí gas 22 a Giới thiệu 22 b Cảm biến khí gas MQ2 22 c Giới thiệu 25 d Module đồng hồ thời gian thực RTC 25 2.2.5 Bộ truyền nhận Bluetooth 29 a Giới thiệu 29 b Module Bluetooth HC-05 30 2.2.6 Module relay 31 2.3 giới thiệu phương thức giao tiếp sử dụng 33 2.3.1 Giao tiếp I2C 33 a Giới thiệu 33 b Chế độ hoạt động (tốc độ truyền): 36 c Điều kiện START STOP (START and STOP conditions) 37 2.3.2 Truyền thông nối tiếp không đồng (UART) 39 a Giới thiệu 39 b Các thông số truyền nhận UART 40 2.3.3 Giao tiếp với cảm biến DHT11 41 a Giới thiệu 41 b Cách giao tiếp với cảm biến 41 Chương Tính tốn thiết kế 43 3.1 Giới thiệu 43 3.2 tính toán thiết kế hệ thống 44 3.2.1 Thiết kế sơ đồ khối 44 3.2.2 Tính tốn thiết kế mạch 45 a Tính tốn khối cảm biến 45 b Thiết kế khối hiển thị 46 c Khối tải 46 an d Khối nút nhấn 47 e Khối nguồn: 48 Chương Thi công hệ thống 50 4.1 Giới thiệu 50 4.1.1 Thi công khối nút nhấn 50 4.1.2 Thi công vỏ thiết bị 51 4.2 Lập trình hệ thống 52 4.2.1 Lưu đồ giải thuật 52 a Lưu đồ chương trình thiết bị 52 b Lưu đồ chương chình chỉnh thiết bị 53 c Lưu đồ chương trình chương trình điện thoại 54 4.2.2 Phần mềm lập trình cho điều khiển 54 a Giới thiệu phần mềm lập trình IDE 54 b Code chương trình 60 4.2.3 Giới thiệu phần mềm Android Studio 63 a Giới thiệu phần mềm Android Studio 63 4.3 chương trình điều khiển điện thoại 77 4.3.1 Hướng dẫn sử dụng phần mềm điều khiển 77 4.3.2 Quy trình thao tác 79 Chương Kết - Nhận xét – đánh giá 82 5.1 Kết đạt 82 5.2 Nhận xét 82 5.2.1 Về sản phẩm 82 Chương Kết luận hướng phát triển 84 6.1 KẾT LUẬN 84 6.2 HƯỚNG PHÁT TRIỂN 84 Tài liệu tham khảo …………………………………………………………… 86 Phụ lục………………………………………………………………………….87 an PHỤ LỤC if(min_set >= 60) min_set = 0; break; case 4: lcd.setCursor(0,0); lcd.print("Hour: "); printDigits(hr_set); quetphim_up(&hr_set); quetphim_down(&hr_set); if(hr_set >= 24) hr_set = 0; break; case 5: lcd.setCursor(0,0); lcd.print("WeekDay: "); printDigits(wd_set); quetphim_up(&wd_set); quetphim_down(&wd_set); if(wd_set >= 9) wd_set = 2; if(wd_set == 1) wd_set = 8; break; case 6: lcd.setCursor(0,0); lcd.print("Day: "); printDigits(day_set); quetphim_up(&day_set); quetphim_down(&day_set); if(day_set >= 32) day_set = 1; if(day_set == 0) day_set = 31; break; case 7: lcd.setCursor(0,0); lcd.print("Month: "); printDigits(mth_set); quetphim_up(&mth_set); quetphim_down(&mth_set); if(mth_set >= 13) mth_set = 1; if(mth_set == 0) mth_set = 12; break; case 8: lcd.setCursor(0,0); lcd.print("Year: "); printDigits(yr_set); quetphim_up(&yr_set); quetphim_down(&yr_set); if(yr_set >= 2100) yr_set = 2000; if(yr_set == 1999) yr_set = 2099; break; default: break; } setTime((byte)hr_set, (byte)min_set, (byte) sec_set, (byte)wd_set, (byte)day_set, (byte)mth_set, (byte)(yr_set - 2000)); } void quetphim_mode(){ if(!digitalRead(mode_pin)) delay(20); if(!digitalRead(mode_pin)) { mode++; if(mode == 9) {mode = 0;} lcd.clear(); while(!digitalRead(mode_pin)); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 89 PHỤ LỤC } } void quetphim_up(unsigned int *var_up){ if(!digitalRead(up_pin)) delay(20); if(!digitalRead(up_pin)) { (*var_up)++; while(!digitalRead(up_pin)); } } void quetphim_down(unsigned int *var_down){ if(!digitalRead(down_pin)) delay(20); if(!digitalRead(down_pin)) { (*var_down) ; while(!digitalRead(down_pin)); } } void quetphim_dv(int pin){ if(!digitalRead(pin)) delay(20); if(!digitalRead(pin)) { phathien = true; HC05_send("@dv"); switch (pin) { case 7: if(dv0) dv0 = false; else dv0 = true; if(dv0) HC05_send("01#"); else HC05_send("00#"); break; case 6: if(dv1) dv1 = false; else dv1 = true; if(dv1) HC05_send("11#"); else HC05_send("10#"); break; case 5: if(dv2) dv2 = false; else dv2 = true; if(dv2) HC05_send("21#"); else HC05_send("20#"); break; case 8: if(bell) bell = false; else bell = true; if(bell) HC05_send("31#"); else HC05_send("30#"); break; default: break; } while(!digitalRead(pin)); } } Chương trình điện thoại:  File DeviceList.java: BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 90 PHỤ LỤC package nguyenthanhtuan.myapplication; import java.util.Set; import import import import import import import import import import import import import import import import import import import android.annotation.SuppressLint; android.app.Activity; android.bluetooth.BluetoothAdapter; android.bluetooth.BluetoothDevice; android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.content.IntentFilter; android.os.Bundle; android.util.Log; android.view.View; android.view.Window; android.view.View.OnClickListener; android.widget.AdapterView; android.widget.ArrayAdapter; android.widget.Button; android.widget.ListView; android.widget.TextView; android.widget.AdapterView.OnItemClickListener; @SuppressLint("NewApi") public class DeviceList extends Activity { // Debugging private static final String TAG = "BluetoothSPP"; private static final boolean D = true; public static String EXTRA_ADDRESS = "device_address"; // Member fields private BluetoothAdapter mBtAdapter; private ArrayAdapter mPairedDevicesArrayAdapter; private Set pairedDevices; private Button scanButton; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Setup the window requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); int listId = getIntent().getIntExtra("layout_list", R.layout.device_layout_list); setContentView(listId); String strBluetoothDevices = getIntent().getStringExtra("bluetooth_devices"); if(strBluetoothDevices == null) strBluetoothDevices = "Bluetooth Devices"; setTitle(strBluetoothDevices); // Set result CANCELED in case the user backs out setResult(Activity.RESULT_CANCELED); // Initialize the button to perform device discovery scanButton = (Button) findViewById(R.id.button_scan); String strScanDevice = getIntent().getStringExtra("scan_for_devices"); if(strScanDevice == null) BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 91 PHỤ LỤC strScanDevice = "SCAN FOR DEVICES"; scanButton.setText(strScanDevice); scanButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { doDiscovery(); } }); // Initialize array adapters One for already paired devices // and one for newly discovered devices int layout_text = getIntent().getIntExtra("layout_text", R.layout.device_layout_text); mPairedDevicesArrayAdapter = new ArrayAdapter(this, layout_text); // Find and set up the ListView for paired devices ListView pairedListView = (ListView) findViewById(R.id.list_devices); pairedListView.setAdapter(mPairedDevicesArrayAdapter); pairedListView.setOnItemClickListener(mDeviceClickListener); // Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(mReceiver, filter); // Get the local Bluetooth adapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); // Get a set of currently paired devices pairedDevices = mBtAdapter.getBondedDevices(); // If there are paired devices, add each one to the ArrayAdapter if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } else { String noDevices = "No devices found"; mPairedDevicesArrayAdapter.add(noDevices); } } protected void onDestroy() { super.onDestroy(); // Make sure we're not doing discovery anymore if (mBtAdapter != null) { mBtAdapter.cancelDiscovery(); } // Unregister broadcast listeners this.unregisterReceiver(mReceiver); this.finish(); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 92 PHỤ LỤC } // Start device discover with the BluetoothAdapter private void doDiscovery() { if (D) Log.d(TAG, "doDiscovery()"); // Remove all element from the list mPairedDevicesArrayAdapter.clear(); // If there are paired devices, add each one to the ArrayAdapter if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } else { String strNoFound = getIntent().getStringExtra("no_devices_found"); if(strNoFound == null) strNoFound = "No devices found"; mPairedDevicesArrayAdapter.add(strNoFound); } // Indicate scanning in the title String strScanning = getIntent().getStringExtra("scanning"); if(strScanning == null) strScanning = "Scanning for devices "; setProgressBarIndeterminateVisibility(true); setTitle(strScanning); // Turn on sub-title for new devices // findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE); // If we're already discovering, stop it if (mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); } // Request discover from BluetoothAdapter mBtAdapter.startDiscovery(); } // The on-click listener for all devices in the ListViews private OnItemClickListener mDeviceClickListener = new OnItemClickListener() { public void onItemClick(AdapterView av, View v, int arg2, long arg3) { // Cancel discovery because it's costly and we're about to connect if(mBtAdapter.isDiscovering()) mBtAdapter.cancelDiscovery(); String strNoFound = getIntent().getStringExtra("no_devices_found"); if(strNoFound == null) strNoFound = "No devices found"; if(!((TextView) v).getText().toString().equals(strNoFound)) { BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 93 PHỤ LỤC // Get the device MAC address, which is the last 17 chars in the View String info = ((TextView) v).getText().toString(); String address = info.substring(info.length() - 17); // Create the result Intent and include the MAC address Intent intent = new Intent(); intent.putExtra("ok_address", address); // Set result and finish this Activity setResult(Activity.RESULT_OK, intent); finish(); } } }; // The BroadcastReceiver that listens for discovered devices and // changes the title when discovery is finished private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // If it's already paired, skip it, because it's been listed already if (device.getBondState() != BluetoothDevice.BOND_BONDED) { String strNoFound = getIntent().getStringExtra("no_devices_found"); if(strNoFound == null) strNoFound = "No devices found"; if(mPairedDevicesArrayAdapter.getItem(0).equals(strNoFound)) { mPairedDevicesArrayAdapter.remove(strNoFound); } mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } // When discovery is finished, change the Activity title } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { setProgressBarIndeterminateVisibility(false); String strSelectDevice = getIntent().getStringExtra("select_device"); if(strSelectDevice == null) strSelectDevice = "Select a device to connect"; setTitle(strSelectDevice); } } }; BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 94 PHỤ LỤC }  File AndroidMainfet.xml: BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 95 PHỤ LỤC  File MainActivity.java: package nguyenthanhtuan.myapplication; import import import import import import import import import import import import import import import import import import import import import import android.bluetooth.BluetoothAdapter; android.bluetooth.BluetoothDevice; android.bluetooth.BluetoothServerSocket; android.bluetooth.BluetoothSocket; android.content.Context; android.content.Intent; android.content.IntentFilter; android.media.AudioManager; android.media.ToneGenerator; android.os.Handler; android.os.Message; android.os.Vibrator; android.support.v7.app.AppCompatActivity; android.os.Bundle; android.util.Log; android.view.View; android.widget.Button; android.widget.ProgressBar; android.widget.Switch; android.widget.TextView; android.widget.Toast; java.io.IOException; import import import import import java.io.InputStream; java.io.OutputStream; java.io.Reader; java.util.Set; java.util.UUID; public class MainActivity extends AppCompatActivity { Handler mHandler; final int MESSAGE_READ = 0; final int handlerState = 0; private StringBuilder recDataString = new StringBuilder(); ConnectedThread mConnectedThread; BluetoothSocket socket = null; BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); private static String address = null; private static final int REQUEST_CONNECT_DEVICE = 2; private static final int REQUEST_ENABLE_BT = 3; private static final UUID MY_UUID = UUID.fromString("000011010000-1000-8000-00805F9B34FB"); Button connect_bluetooth; TextView tv_nhietdo, tv_doam, tv_chay; Switch sw1, sw2, sw3; ProgressBar wait_progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 96 PHỤ LỤC connect_bluetooth = (Button) findViewById(R.id.button_connect); tv_doam = (TextView)findViewById(R.id.tv_doam); tv_nhietdo = (TextView)findViewById(R.id.tv_nhietdo); tv_chay = (TextView)findViewById(R.id.tv_chay); sw1 = (Switch)findViewById(R.id.switch1); sw2 = (Switch)findViewById(R.id.switch2); sw3 = (Switch)findViewById(R.id.switch3); wait_progressBar = (ProgressBar)findViewById(R.id.progressBar); wait_progressBar.setMax(100); wait_progressBar.setVisibility(View.INVISIBLE); tv_doam.setText(" "); tv_nhietdo.setText(" "); tv_chay.setVisibility(View.INVISIBLE); sw1.setEnabled(false);sw2.setEnabled(false);sw3.setEnabled(false); sw1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (sw1.isChecked()) mConnectedThread.write("dv01"); else mConnectedThread.write("dv00"); sw1.setEnabled(false);sw2.setEnabled(false);sw3.setEnabled(false); wait_progressBar.setVisibility(View.VISIBLE); } }); sw2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(sw2.isChecked()) mConnectedThread.write("dv11"); else mConnectedThread.write("dv10"); sw1.setEnabled(false);sw2.setEnabled(false);sw3.setEnabled(false); wait_progressBar.setVisibility(View.VISIBLE); } }); sw3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(sw3.isChecked()) mConnectedThread.write("dv21"); else mConnectedThread.write("dv20"); sw1.setEnabled(false);sw2.setEnabled(false);sw3.setEnabled(false); wait_progressBar.setVisibility(View.VISIBLE); } }); connect_bluetooth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (socket != null) { try { if(mBluetoothAdapter.isEnabled()) socket.close(); socket = null; connect_bluetooth.setText("CONNECT"); tv_doam.setText(" "); tv_nhietdo.setText(" "); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 97 PHỤ LỤC sw1.setEnabled(false);sw2.setEnabled(false);sw3.setEnabled(false); Toast.makeText(getApplicationContext(), "Ngắt kết nối!", Toast.LENGTH_SHORT).show(); } catch (IOException closeError) { } } else { if (mBluetoothAdapter.isEnabled()) { Intent intent = new Intent(MainActivity.this, DeviceList.class); intent.putExtra("scan_for_devices", "Tìm"); startActivityForResult(intent, REQUEST_CONNECT_DEVICE); } else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } } }); mHandler = new Handler() { public void handleMessage(Message msg) { if (msg.what == MESSAGE_READ) { String readMessage = (String) msg.obj; // msg.arg1 = bytes from connect thread recDataString.append(readMessage); //keep appending to string until int beginOfLineIndex = recDataString.indexOf("@"); int endOfLineIndex = recDataString.indexOf("#"); // determine the end-of-line int dv=0, dv_stt=0, chay=0; if (endOfLineIndex > 0) { // make sure there data before ~ String dataInPrint = recDataString.substring(beginOfLineIndex + 1, endOfLineIndex); // extract string int dataLength = dataInPrint.length(); //get length of data received if(dataLength == 13) { Log.d("ok_log", "String Length = " + String.valueOf(dataLength)); Log.d("ok_log", "Độ ẩm: " + dataInPrint.substring(4, 6)); tv_doam.setText(dataInPrint.substring(4, 6)); Log.d("ok_log", "Nhiệt độ: " + dataInPrint.substring(11, 13)); tv_nhietdo.setText(dataInPrint.substring(11, 13)); Log.d("ok_log", "Cháy: " + dataInPrint.substring(3, 4)); if(dataInPrint.substring(3, 4).equals("1")){ tv_chay.setVisibility(View.VISIBLE); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 98 PHỤ LỤC Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds v.vibrate(500); ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100); toneGen1.startTone(ToneGenerator.TONE_DTMF_0, 500); } else {tv_chay.setVisibility(View.INVISIBLE);} } else if(dataLength == 4){ Log.d("ok_log", "String Length = " + String.valueOf(dataLength)); Log.d("ok_log", "Data Received = " + dataInPrint); dv = Integer.parseInt(dataInPrint.substring(2, 3)); dv_stt = Integer.parseInt(dataInPrint.substring(3,4)); switch (dv){ case 0: if(dv_stt == 1) sw1.setChecked(true); else sw1.setChecked(false); sw1.setEnabled(true);sw2.setEnabled(true);sw3.setEnabled(true); break; case 1: if(dv_stt == 1) sw2.setChecked(true); else sw2.setChecked(false); sw1.setEnabled(true);sw2.setEnabled(true);sw3.setEnabled(true); break; case 2: if(dv_stt == 1) sw3.setChecked(true); else sw3.setChecked(false); sw1.setEnabled(true);sw2.setEnabled(true);sw3.setEnabled(true); break; case 3: if(dv_stt == 1) Log.d("ok_log", "Cháy"); else Log.d("ok_log", "Cháy1"); break; default: break; } } recDataString.delete(0, recDataString.length()); //clear all string data wait_progressBar.setVisibility(View.INVISIBLE); } } } }; BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 99 PHỤ LỤC } public void onActivityResult(int requestCode, int resultCode, Intent data) { boolean ConnectSuccess = true; if (requestCode == REQUEST_CONNECT_DEVICE) { if (resultCode == RESULT_OK) { address = data.getStringExtra("ok_address"); BluetoothDevice device_ok = mBluetoothAdapter.getRemoteDevice(address); try { mBluetoothAdapter.cancelDiscovery(); socket = device_ok.createInsecureRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { socket.connect(); } catch (IOException connectException) { ConnectSuccess = false; // Unable to connect; close the socket and get out try { socket.close(); } catch (IOException closeException) { } } if (ConnectSuccess) { Toast.makeText(getApplicationContext(), "Kết nối thành công!", Toast.LENGTH_SHORT).show(); connect_bluetooth.setText("DISCONNECT"); sw1.setEnabled(true);sw2.setEnabled(true);sw3.setEnabled(true); mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); } else{ Toast.makeText(getApplicationContext(), "Kết nối thất bại!", Toast.LENGTH_SHORT).show(); sw1.setEnabled(false);sw2.setEnabled(false);sw3.setEnabled(false); } } else if (resultCode == RESULT_CANCELED) Toast.makeText(getApplicationContext(), "Không chọn thiết bị", Toast.LENGTH_SHORT).show(); } else if (requestCode == REQUEST_ENABLE_BT) { if (resultCode == RESULT_OK) { Toast.makeText(getApplicationContext(), "Bật Bluetooth thành công!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Bật Bluetooth thất bại!", Toast.LENGTH_SHORT).show(); } } BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 100 PHỤ LỤC } private class ConnectedThread extends Thread { private final InputStream mmInStream; private final OutputStream mmOutStream; //creation of the connect thread public ConnectedThread(BluetoothSocket sockett) { InputStream tmpIn = null; OutputStream tmpOut = null; try { //Create I/O streams for connection tmpIn = sockett.getInputStream(); tmpOut = sockett.getOutputStream(); } catch (IOException e) { } mmInStream = tmpIn; mmOutStream = tmpOut; } public void run() { byte[] buffer = new byte[256]; int bytes; // Keep looping to listen for received messages while (true) { try { bytes = mmInStream.read(buffer); //read bytes from input buffer String readMessage = new String(buffer, 0, bytes); // Send the obtained bytes to the UI Activity via handler mHandler.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget(); } catch (IOException e) { break; } } } //write method public void write(String input) { byte[] msgBuffer = input.getBytes(); //converts entered String into bytes try { mmOutStream.write(msgBuffer); //write bytes over BT connection via outstream mmOutStream.flush(); } catch (IOException e) { //if you cannot write, close the application Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show(); try { socket.close(); } catch (IOException s) { } BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 101 PHỤ LỤC } } public void cancel() { try { socket.close(); } catch (IOException e) { } } } @Override public void onPause() { super.onPause(); Log.d("ok_log", " In onPause() "); if(socket != null) try { socket.close(); socket = null; connect_bluetooth.setText("CONNECT"); } catch (IOException e2) { } } } BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an 102 S an K L 0 ... 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-ĐIỆN TỬ ĐỀ TÀI: ĐIỀU KHIỂN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY GVHD: ThS.Nguyễn... 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 –ĐIỆN TỬ ĐỀ TÀI: ĐIỀU KHIỂN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY GVHD: ThS.Nguyễn... ĐIỀU KHIỂN BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP an CHƯƠNG TỔNG QUAN THIẾT BỊ ĐIỆN BÁO NHIỆT ĐỘ BÁO CHÁY sử dụng kit Arduino mega2560 1.2 MỤC TIÊU  Thiết kế cảnh báo nhiệt độ, độ ẩm hiển thị LCD  Thiết

Ngày đăng: 02/02/2023, 09:07

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

Tài liệu liên quan