{"id":4716,"date":"2020-12-07T01:52:20","date_gmt":"2020-12-07T00:52:20","guid":{"rendered":"http:\/\/dronesonen.usn.no\/?p=4716"},"modified":"2020-12-07T01:52:20","modified_gmt":"2020-12-07T00:52:20","slug":"week-49-smart-aquarium","status":"publish","type":"post","link":"https:\/\/dronesonen.usn.no\/?p=4716","title":{"rendered":"Week 49: Smart Aquarium"},"content":{"rendered":"\n<p>Bluetooth functionality for the Smart Aquarium is done between an Android app and the Arduino Nano 33 BLE, meaning we need some Arduino code for sending information as well as a good amount of Android code to connect and receive this information.<br>The arduino code is almost complete (shown below), though the sensors inputs will need to be correctly calculated and possibly adjusted for. <br>Sending the sensor information through plaintext should not be an issue, so the arduino will do the calculation and send the numbers straight to the Android device.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const&nbsp;int&nbsp;mVolt&nbsp;=&nbsp;3300;\nconst&nbsp;int&nbsp;tmpPin&nbsp;=&nbsp;A0;\nconst&nbsp;int&nbsp;phPin&nbsp;=&nbsp;A1;\nconst&nbsp;int&nbsp;waterPin&nbsp;=&nbsp;A2;\nfloat&nbsp;tmpVal,&nbsp;phVal,&nbsp;waterVal&nbsp;=&nbsp;0;\n\nvoid&nbsp;setup()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;Serial.begin(9600);\n}\n\nvoid&nbsp;loop()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;vin&nbsp;=&nbsp;0;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/read&nbsp;input&nbsp;voltage&nbsp;from&nbsp;analog&nbsp;pin&nbsp;and&nbsp;calculate&nbsp;the&nbsp;value&nbsp;based&nbsp;on&nbsp;formula\n&nbsp;&nbsp;&nbsp;&nbsp;vin&nbsp;=&nbsp;analogRead(tmpPin);\n&nbsp;&nbsp;&nbsp;&nbsp;tmpVal=&nbsp;(vin*(mVolt\/1024)-500)\/10;\n\n&nbsp;&nbsp;&nbsp;&nbsp;vin&nbsp;=&nbsp;analogRead(phPin);\n&nbsp;&nbsp;&nbsp;&nbsp;phVal&nbsp;=&nbsp;vin;\n\n&nbsp;&nbsp;&nbsp;&nbsp;vin&nbsp;=&nbsp;analogRead(waterPin);\n&nbsp;&nbsp;&nbsp;&nbsp;waterVal&nbsp;=&nbsp;vin;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/write&nbsp;results&nbsp;to&nbsp;serial&nbsp;port&nbsp;(Nano&nbsp;33&nbsp;BLE&nbsp;serial&nbsp;is&nbsp;bluetooth)\n&nbsp;&nbsp;&nbsp;&nbsp;Serial.print(\"Example&nbsp;temperature&nbsp;reading:&nbsp;\");\n&nbsp;&nbsp;&nbsp;&nbsp;Serial.println(tmpVal,&nbsp;5);\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;Serial.print(\"Example&nbsp;PH&nbsp;reading:&nbsp;\");\n&nbsp;&nbsp;&nbsp;&nbsp;Serial.println(phVal,&nbsp;2);\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;Serial.print(\"Example&nbsp;flood&nbsp;reading:&nbsp;\");\n&nbsp;&nbsp;&nbsp;&nbsp;Serial.println(waterVal);\n} <\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"570\" src=\"http:\/\/dronesonen.usn.no\/wp-content\/uploads\/2020\/12\/Arduino-ABX00030-30150885-01.jpg\" alt=\"\" class=\"wp-image-4717\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2020\/12\/Arduino-ABX00030-30150885-01.jpg 1020w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2020\/12\/Arduino-ABX00030-30150885-01-300x168.jpg 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2020\/12\/Arduino-ABX00030-30150885-01-768x429.jpg 768w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/><figcaption>Arduino Nano 33 BLE<\/figcaption><\/figure>\n\n\n\n<p>On the receiving end, Bluetooth Low Energy code appears to be a bit complicated compared to normal Bluetooth, as BLE works a bit more like a messageboard that a device can read rather than a typical connection. <br>At the moment, Kasper is working on the Android code to make the Bluetooth stuff all work, the code below is a rough idea and is definitely not functional yet. All the code really needs to do is read the information broadcasted by the Nano. <br><br>Learning how to code BLE\/Android in between everything going on as well as other projects means we are a bit behind on this part.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private val bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().bluetoothLeScanner\n     private var mScanning = false\n     private val handler = Handler()\n\nprivate val SCAN_PERIOD: Long = 10000\n\nfun connectBLE() {\n    val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager\n    val bluetoothAdapter = bluetoothManager.adapter\n\n    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled) {\n        val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)\n        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)\n    }\n}\n\nprivate fun scanLeDevice() {\n    if (!mScanning) {\n        handler.postDelayed({\n            mScanning = false\n            bluetoothLeScanner.stopScan(leScanCallback)\n        }, SCAN_PERIOD)\n        mScanning = true\n        bluetoothLeScanner.startScan(leScanCallback)\n    } else {\n        mScanning = false\n        bluetoothLeScanner.stopScan(leScanCallback)\n    }\n}\n\nfun close() {\n    bluetoothGatt?.close()\n    bluetoothGatt = null\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Bluetooth functionality for the Smart Aquarium is done between an Android app and the Arduino Nano 33 BLE, meaning we need some Arduino code for sending information as well as a good amount of Android code to connect and receive this information.The arduino code is almost complete (shown below), though the sensors inputs will need [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[80,81],"class_list":["post-4716","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-group-3","tag-smart-aquarium"],"_links":{"self":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts\/4716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4716"}],"version-history":[{"count":2,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts\/4716\/revisions"}],"predecessor-version":[{"id":4719,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts\/4716\/revisions\/4719"}],"wp:attachment":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}